1#ifndef _XOPEN_SOURCE
2#define _XOPEN_SOURCE 700
3#endif
4#include <stdio.h>
5#include <string.h>
6#include <errno.h>
7#include <limits.h>
8#include <math.h>
9#include <wchar.h>
10#include <locale.h>
11#include <langinfo.h>
12#include "test.h"
13
14#define TEST(r, f, x, m) ( \
15	((r) = (f)) == (x) || \
16	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
17
18#define TEST_S(s, x, m) ( \
19	!wcscmp((s),(x)) || \
20	(t_error("[%ls] != [%ls] (%s)\n", s, x, m), 0) )
21
22static const struct {
23	const wchar_t *fmt;
24	int i;
25	const wchar_t *expect;
26} int_tests[] = {
27	/* width, precision, alignment */
28	{ L"%04d", 12, L"0012" },
29	{ L"%.3d", 12, L"012" },
30	{ L"%3d", 12, L" 12" },
31	{ L"%-3d", 12, L"12 " },
32	{ L"%+3d", 12, L"+12" },
33	{ L"%+-5d", 12, L"+12  " },
34	{ L"%+- 5d", 12, L"+12  " },
35	{ L"%- 5d", 12, L" 12  " },
36	{ L"% d", 12, L" 12" },
37	{ L"%0-5d", 12, L"12   " },
38	{ L"%-05d", 12, L"12   " },
39
40	/* ...explicit precision of 0 shall be no characters except for alt-octal. */
41	{ L"%.0d", 0, L"" },
42	{ L"%.0o", 0, L"" },
43	{ L"%#.0d", 0, L"" },
44	{ L"%#.0o", 0, L"0" },
45	{ L"%#.0x", 0, L"" },
46
47	/* hex: test alt form and case */
48	{ L"%x", 63, L"3f" },
49	{ L"%#x", 63, L"0x3f" },
50	{ L"%X", 63, L"3F" },
51
52	/* octal: test alt form */
53	{ L"%o", 15, L"17" },
54	{ L"%#o", 15, L"017" },
55
56	{ NULL, 0.0, NULL }
57};
58
59static const struct {
60	const wchar_t *fmt;
61	double f;
62	const wchar_t *expect;
63} fp_tests[] = {
64	/* basic form, handling of exponent/precision for 0 */
65	{ L"%e", 0.0, L"0.000000e+00" },
66	{ L"%f", 0.0, L"0.000000" },
67	{ L"%g", 0.0, L"0" },
68	{ L"%#g", 0.0, L"0.00000" },
69
70	/* rounding */
71	{ L"%f", 1.1, L"1.100000" },
72	{ L"%f", 1.2, L"1.200000" },
73	{ L"%f", 1.3, L"1.300000" },
74	{ L"%f", 1.4, L"1.400000" },
75	{ L"%f", 1.5, L"1.500000" },
76
77	/* correctness in DBL_DIG places */
78	{ L"%.15g", 1.23456789012345, L"1.23456789012345" },
79
80	/* correct choice of notation for %g */
81	{ L"%g", 0.0001, L"0.0001" },
82	{ L"%g", 0.00001, L"1e-05" },
83	{ L"%g", 123456, L"123456" },
84	{ L"%g", 1234567, L"1.23457e+06" },
85	{ L"%.7g", 1234567, L"1234567" },
86	{ L"%.7g", 12345678, L"1.234568e+07" },
87
88	/* pi in double precision, printed to a few extra places */
89	{ L"%.15f", M_PI, L"3.141592653589793" },
90	{ L"%.18f", M_PI, L"3.141592653589793116" },
91
92	/* exact conversion of large integers */
93	{ L"%.0f", 340282366920938463463374607431768211456.0,
94	         L"340282366920938463463374607431768211456" },
95
96	{ NULL, 0.0, NULL }
97};
98
99int main(void)
100{
101	int i, j;
102	wchar_t b[500];
103
104	(void)(
105	setlocale(LC_CTYPE, "en_US.UTF-8") ||
106	setlocale(LC_CTYPE, "en_GB.UTF-8") ||
107	setlocale(LC_CTYPE, "en.UTF-8") ||
108	setlocale(LC_CTYPE, "POSIX.UTF-8") ||
109	setlocale(LC_CTYPE, "C.UTF-8") ||
110	setlocale(LC_CTYPE, "UTF-8") ||
111	setlocale(LC_CTYPE, "") );
112
113	TEST(i, strcmp(nl_langinfo(CODESET), "UTF-8"), 0, "no UTF-8 locale; tests might fail");
114
115	TEST(i, swprintf(0, 0, L"%d", 123456)<0, 1, "%d != %d");
116
117	TEST(i, swprintf(b, 2, L"%lc", 0xc0), 1, "%d != %d");
118	TEST(i, b[0], 0xc0, "wrong character %x != %x");
119	TEST(i, swprintf(b, 2, L"%lc", 0x20ac), 1, "%d != %d");
120	TEST(i, b[0], 0x20ac, "wrong character %x != %x");
121	TEST(i, swprintf(b, 3, L"%s", "\xc3\x80!"), 2, "%d != %d");
122	TEST(i, b[0], 0xc0, "wrong character %x != %x");
123	TEST(i, swprintf(b, 2, L"%.1s", "\xc3\x80!"), 1, "%d != %d");
124	TEST(i, b[0], 0xc0, "wrong character %x != %x");
125
126	wcscpy(b, L"xxxxxxxx");
127	TEST(i, swprintf(b, 4, L"%d", 123456)<0, 1, "%d != %d");
128	TEST_S(b, L"123", "incorrect output");
129	TEST(i, b[5], 'x', "buffer overrun");
130
131	for (j=0; int_tests[j].fmt; j++) {
132		i = swprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i);
133		if (i != wcslen(int_tests[j].expect)) {
134			t_error("swprintf(b, sizeof b, \"%ls\", %d) returned %d wanted %d\n",
135				int_tests[j].fmt, int_tests[j].i, i, wcslen(int_tests[j].expect));
136		}
137		if (wcscmp(b, int_tests[j].expect) != 0)
138			t_error("bad integer conversion: got \"%ls\", want \"%ls\"\n", b, int_tests[j].expect);
139	}
140
141	for (j=0; fp_tests[j].fmt; j++) {
142		i = swprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f);
143		if (i != wcslen(fp_tests[j].expect)) {
144			t_error("swprintf(b, sizeof b, \"%ls\", %f) returned %d wanted %d\n",
145				fp_tests[j].fmt, fp_tests[j].f, i, wcslen(fp_tests[j].expect));
146		}
147		if (wcscmp(b, fp_tests[j].expect) != 0)
148			t_error("bad floating-point conversion: got \"%ls\", want \"%ls\"\n", b, fp_tests[j].expect);
149	}
150	return t_status;
151}
152