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