1570af302Sopenharmony_ci#ifndef _XOPEN_SOURCE
2570af302Sopenharmony_ci#define _XOPEN_SOURCE 700
3570af302Sopenharmony_ci#endif
4570af302Sopenharmony_ci#include <stdio.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include <errno.h>
7570af302Sopenharmony_ci#include <limits.h>
8570af302Sopenharmony_ci#include <math.h>
9570af302Sopenharmony_ci#include <wchar.h>
10570af302Sopenharmony_ci#include <locale.h>
11570af302Sopenharmony_ci#include <langinfo.h>
12570af302Sopenharmony_ci#include "test.h"
13570af302Sopenharmony_ci
14570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \
15570af302Sopenharmony_ci	((r) = (f)) == (x) || \
16570af302Sopenharmony_ci	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
17570af302Sopenharmony_ci
18570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \
19570af302Sopenharmony_ci	!wcscmp((s),(x)) || \
20570af302Sopenharmony_ci	(t_error("[%ls] != [%ls] (%s)\n", s, x, m), 0) )
21570af302Sopenharmony_ci
22570af302Sopenharmony_cistatic const struct {
23570af302Sopenharmony_ci	const wchar_t *fmt;
24570af302Sopenharmony_ci	int i;
25570af302Sopenharmony_ci	const wchar_t *expect;
26570af302Sopenharmony_ci} int_tests[] = {
27570af302Sopenharmony_ci	/* width, precision, alignment */
28570af302Sopenharmony_ci	{ L"%04d", 12, L"0012" },
29570af302Sopenharmony_ci	{ L"%.3d", 12, L"012" },
30570af302Sopenharmony_ci	{ L"%3d", 12, L" 12" },
31570af302Sopenharmony_ci	{ L"%-3d", 12, L"12 " },
32570af302Sopenharmony_ci	{ L"%+3d", 12, L"+12" },
33570af302Sopenharmony_ci	{ L"%+-5d", 12, L"+12  " },
34570af302Sopenharmony_ci	{ L"%+- 5d", 12, L"+12  " },
35570af302Sopenharmony_ci	{ L"%- 5d", 12, L" 12  " },
36570af302Sopenharmony_ci	{ L"% d", 12, L" 12" },
37570af302Sopenharmony_ci	{ L"%0-5d", 12, L"12   " },
38570af302Sopenharmony_ci	{ L"%-05d", 12, L"12   " },
39570af302Sopenharmony_ci
40570af302Sopenharmony_ci	/* ...explicit precision of 0 shall be no characters except for alt-octal. */
41570af302Sopenharmony_ci	{ L"%.0d", 0, L"" },
42570af302Sopenharmony_ci	{ L"%.0o", 0, L"" },
43570af302Sopenharmony_ci	{ L"%#.0d", 0, L"" },
44570af302Sopenharmony_ci	{ L"%#.0o", 0, L"0" },
45570af302Sopenharmony_ci	{ L"%#.0x", 0, L"" },
46570af302Sopenharmony_ci
47570af302Sopenharmony_ci	/* hex: test alt form and case */
48570af302Sopenharmony_ci	{ L"%x", 63, L"3f" },
49570af302Sopenharmony_ci	{ L"%#x", 63, L"0x3f" },
50570af302Sopenharmony_ci	{ L"%X", 63, L"3F" },
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci	/* octal: test alt form */
53570af302Sopenharmony_ci	{ L"%o", 15, L"17" },
54570af302Sopenharmony_ci	{ L"%#o", 15, L"017" },
55570af302Sopenharmony_ci
56570af302Sopenharmony_ci	{ NULL, 0.0, NULL }
57570af302Sopenharmony_ci};
58570af302Sopenharmony_ci
59570af302Sopenharmony_cistatic const struct {
60570af302Sopenharmony_ci	const wchar_t *fmt;
61570af302Sopenharmony_ci	double f;
62570af302Sopenharmony_ci	const wchar_t *expect;
63570af302Sopenharmony_ci} fp_tests[] = {
64570af302Sopenharmony_ci	/* basic form, handling of exponent/precision for 0 */
65570af302Sopenharmony_ci	{ L"%e", 0.0, L"0.000000e+00" },
66570af302Sopenharmony_ci	{ L"%f", 0.0, L"0.000000" },
67570af302Sopenharmony_ci	{ L"%g", 0.0, L"0" },
68570af302Sopenharmony_ci	{ L"%#g", 0.0, L"0.00000" },
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci	/* rounding */
71570af302Sopenharmony_ci	{ L"%f", 1.1, L"1.100000" },
72570af302Sopenharmony_ci	{ L"%f", 1.2, L"1.200000" },
73570af302Sopenharmony_ci	{ L"%f", 1.3, L"1.300000" },
74570af302Sopenharmony_ci	{ L"%f", 1.4, L"1.400000" },
75570af302Sopenharmony_ci	{ L"%f", 1.5, L"1.500000" },
76570af302Sopenharmony_ci
77570af302Sopenharmony_ci	/* correctness in DBL_DIG places */
78570af302Sopenharmony_ci	{ L"%.15g", 1.23456789012345, L"1.23456789012345" },
79570af302Sopenharmony_ci
80570af302Sopenharmony_ci	/* correct choice of notation for %g */
81570af302Sopenharmony_ci	{ L"%g", 0.0001, L"0.0001" },
82570af302Sopenharmony_ci	{ L"%g", 0.00001, L"1e-05" },
83570af302Sopenharmony_ci	{ L"%g", 123456, L"123456" },
84570af302Sopenharmony_ci	{ L"%g", 1234567, L"1.23457e+06" },
85570af302Sopenharmony_ci	{ L"%.7g", 1234567, L"1234567" },
86570af302Sopenharmony_ci	{ L"%.7g", 12345678, L"1.234568e+07" },
87570af302Sopenharmony_ci
88570af302Sopenharmony_ci	/* pi in double precision, printed to a few extra places */
89570af302Sopenharmony_ci	{ L"%.15f", M_PI, L"3.141592653589793" },
90570af302Sopenharmony_ci	{ L"%.18f", M_PI, L"3.141592653589793116" },
91570af302Sopenharmony_ci
92570af302Sopenharmony_ci	/* exact conversion of large integers */
93570af302Sopenharmony_ci	{ L"%.0f", 340282366920938463463374607431768211456.0,
94570af302Sopenharmony_ci	         L"340282366920938463463374607431768211456" },
95570af302Sopenharmony_ci
96570af302Sopenharmony_ci	{ NULL, 0.0, NULL }
97570af302Sopenharmony_ci};
98570af302Sopenharmony_ci
99570af302Sopenharmony_ciint main(void)
100570af302Sopenharmony_ci{
101570af302Sopenharmony_ci	int i, j;
102570af302Sopenharmony_ci	wchar_t b[500];
103570af302Sopenharmony_ci
104570af302Sopenharmony_ci	(void)(
105570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en_US.UTF-8") ||
106570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en_GB.UTF-8") ||
107570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en.UTF-8") ||
108570af302Sopenharmony_ci	setlocale(LC_CTYPE, "POSIX.UTF-8") ||
109570af302Sopenharmony_ci	setlocale(LC_CTYPE, "C.UTF-8") ||
110570af302Sopenharmony_ci	setlocale(LC_CTYPE, "UTF-8") ||
111570af302Sopenharmony_ci	setlocale(LC_CTYPE, "") );
112570af302Sopenharmony_ci
113570af302Sopenharmony_ci	TEST(i, strcmp(nl_langinfo(CODESET), "UTF-8"), 0, "no UTF-8 locale; tests might fail");
114570af302Sopenharmony_ci
115570af302Sopenharmony_ci	TEST(i, swprintf(0, 0, L"%d", 123456)<0, 1, "%d != %d");
116570af302Sopenharmony_ci
117570af302Sopenharmony_ci	TEST(i, swprintf(b, 2, L"%lc", 0xc0), 1, "%d != %d");
118570af302Sopenharmony_ci	TEST(i, b[0], 0xc0, "wrong character %x != %x");
119570af302Sopenharmony_ci	TEST(i, swprintf(b, 2, L"%lc", 0x20ac), 1, "%d != %d");
120570af302Sopenharmony_ci	TEST(i, b[0], 0x20ac, "wrong character %x != %x");
121570af302Sopenharmony_ci	TEST(i, swprintf(b, 3, L"%s", "\xc3\x80!"), 2, "%d != %d");
122570af302Sopenharmony_ci	TEST(i, b[0], 0xc0, "wrong character %x != %x");
123570af302Sopenharmony_ci	TEST(i, swprintf(b, 2, L"%.1s", "\xc3\x80!"), 1, "%d != %d");
124570af302Sopenharmony_ci	TEST(i, b[0], 0xc0, "wrong character %x != %x");
125570af302Sopenharmony_ci
126570af302Sopenharmony_ci	wcscpy(b, L"xxxxxxxx");
127570af302Sopenharmony_ci	TEST(i, swprintf(b, 4, L"%d", 123456)<0, 1, "%d != %d");
128570af302Sopenharmony_ci	TEST_S(b, L"123", "incorrect output");
129570af302Sopenharmony_ci	TEST(i, b[5], 'x', "buffer overrun");
130570af302Sopenharmony_ci
131570af302Sopenharmony_ci	for (j=0; int_tests[j].fmt; j++) {
132570af302Sopenharmony_ci		i = swprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i);
133570af302Sopenharmony_ci		if (i != wcslen(int_tests[j].expect)) {
134570af302Sopenharmony_ci			t_error("swprintf(b, sizeof b, \"%ls\", %d) returned %d wanted %d\n",
135570af302Sopenharmony_ci				int_tests[j].fmt, int_tests[j].i, i, wcslen(int_tests[j].expect));
136570af302Sopenharmony_ci		}
137570af302Sopenharmony_ci		if (wcscmp(b, int_tests[j].expect) != 0)
138570af302Sopenharmony_ci			t_error("bad integer conversion: got \"%ls\", want \"%ls\"\n", b, int_tests[j].expect);
139570af302Sopenharmony_ci	}
140570af302Sopenharmony_ci
141570af302Sopenharmony_ci	for (j=0; fp_tests[j].fmt; j++) {
142570af302Sopenharmony_ci		i = swprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f);
143570af302Sopenharmony_ci		if (i != wcslen(fp_tests[j].expect)) {
144570af302Sopenharmony_ci			t_error("swprintf(b, sizeof b, \"%ls\", %f) returned %d wanted %d\n",
145570af302Sopenharmony_ci				fp_tests[j].fmt, fp_tests[j].f, i, wcslen(fp_tests[j].expect));
146570af302Sopenharmony_ci		}
147570af302Sopenharmony_ci		if (wcscmp(b, fp_tests[j].expect) != 0)
148570af302Sopenharmony_ci			t_error("bad floating-point conversion: got \"%ls\", want \"%ls\"\n", b, fp_tests[j].expect);
149570af302Sopenharmony_ci	}
150570af302Sopenharmony_ci	return t_status;
151570af302Sopenharmony_ci}
152