Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6433 | siemargl | 1 | #ifndef _XOPEN_SOURCE |
2 | #define _XOPEN_SOURCE 700 |
||
3 | #endif |
||
4 | #include |
||
5 | #include |
||
6 | //#include |
||
7 | //#include |
||
8 | #include |
||
9 | #include "test.h" |
||
10 | |||
11 | #define DISABLE_SLOW_TESTS |
||
12 | |||
13 | #define TEST(r, f, x, m) ( \ |
||
14 | ((r) = (f)) == (x) || \ |
||
15 | (t_error("%s failed (" m ")\n", #f, r, x), 0) ) |
||
16 | |||
17 | #define TEST_S(s, x, m) ( \ |
||
18 | !strcmp((s),(x)) || \ |
||
19 | (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) |
||
20 | |||
21 | static const struct { |
||
22 | const char *fmt; |
||
23 | int i; |
||
24 | const char *expect; |
||
25 | } int_tests[] = { |
||
26 | /* width, precision, alignment */ |
||
27 | { "%04d", 12, "0012" }, |
||
28 | { "%.3d", 12, "012" }, |
||
29 | { "%3d", 12, " 12" }, |
||
30 | { "%-3d", 12, "12 " }, |
||
31 | { "%+3d", 12, "+12" }, |
||
32 | { "%+-5d", 12, "+12 " }, |
||
33 | { "%+- 5d", 12, "+12 " }, |
||
34 | { "%- 5d", 12, " 12 " }, |
||
35 | { "% d", 12, " 12" }, |
||
36 | { "%0-5d", 12, "12 " }, |
||
37 | { "%-05d", 12, "12 " }, |
||
38 | |||
39 | /* ...explicit precision of 0 shall be no characters. */ |
||
40 | { "%.0d", 0, "" }, |
||
41 | { "%.0o", 0, "" }, |
||
42 | { "%#.0d", 0, "" }, |
||
43 | { "%#.0o", 0, "" }, |
||
44 | { "%#.0x", 0, "" }, |
||
45 | |||
46 | /* ...but it still has to honor width and flags. */ |
||
47 | { "%2.0u", 0, " " }, |
||
48 | { "%02.0u", 0, " " }, |
||
49 | { "%2.0d", 0, " " }, |
||
50 | { "%02.0d", 0, " " }, |
||
51 | { "% .0d", 0, " " }, |
||
52 | { "%+.0d", 0, "+" }, |
||
53 | |||
54 | /* hex: test alt form and case */ |
||
55 | { "%x", 63, "3f" }, |
||
56 | { "%#x", 63, "0x3f" }, |
||
57 | { "%X", 63, "3F" }, |
||
58 | |||
59 | /* octal: test alt form */ |
||
60 | { "%o", 15, "17" }, |
||
61 | { "%#o", 15, "017" }, |
||
62 | |||
63 | { NULL, 0.0, NULL } |
||
64 | }; |
||
65 | |||
66 | static const struct { |
||
67 | const char *fmt; |
||
68 | double f; |
||
69 | const char *expect; |
||
70 | } fp_tests[] = { |
||
71 | /* basic form, handling of exponent/precision for 0 */ |
||
72 | { "%a", 0.0, "0x0p+0" }, |
||
73 | { "%e", 0.0, "0.000000e+00" }, |
||
74 | { "%f", 0.0, "0.000000" }, |
||
75 | { "%g", 0.0, "0" }, |
||
76 | { "%#g", 0.0, "0.00000" }, |
||
77 | { "%la", 0.0, "0x0p+0" }, |
||
78 | { "%le", 0.0, "0.000000e+00" }, |
||
79 | { "%lf", 0.0, "0.000000" }, |
||
80 | { "%lg", 0.0, "0" }, |
||
81 | { "%#lg", 0.0, "0.00000" }, |
||
82 | |||
83 | /* rounding */ |
||
84 | { "%f", 1.1, "1.100000" }, |
||
85 | { "%f", 1.2, "1.200000" }, |
||
86 | { "%f", 1.3, "1.300000" }, |
||
87 | { "%f", 1.4, "1.400000" }, |
||
88 | { "%f", 1.5, "1.500000" }, |
||
89 | { "%.4f", 1.06125, "1.0613" }, /* input is not representible exactly as double */ |
||
90 | { "%.4f", 1.03125, "1.0312" }, /* 0x1.08p0 */ |
||
91 | { "%.2f", 1.375, "1.38" }, |
||
92 | { "%.1f", 1.375, "1.4" }, |
||
93 | { "%.1lf", 1.375, "1.4" }, |
||
94 | { "%.15f", 1.1, "1.100000000000000" }, |
||
95 | { "%.16f", 1.1, "1.1000000000000001" }, |
||
96 | { "%.17f", 1.1, "1.10000000000000009" }, |
||
97 | { "%.2e", 1500001.0, "1.50e+06" }, |
||
98 | { "%.2e", 1505000.0, "1.50e+06" }, |
||
99 | { "%.2e", 1505000.00000095367431640625, "1.51e+06" }, |
||
100 | { "%.2e", 1505001.0, "1.51e+06" }, |
||
101 | { "%.2e", 1506000.0, "1.51e+06" }, |
||
102 | |||
103 | /* correctness in DBL_DIG places */ |
||
104 | { "%.15g", 1.23456789012345, "1.23456789012345" }, |
||
105 | |||
106 | /* correct choice of notation for %g */ |
||
107 | { "%g", 0.0001, "0.0001" }, |
||
108 | { "%g", 0.00001, "1e-05" }, |
||
109 | { "%g", 123456, "123456" }, |
||
110 | { "%g", 1234567, "1.23457e+06" }, |
||
111 | { "%.7g", 1234567, "1234567" }, |
||
112 | { "%.7g", 12345678, "1.234568e+07" }, |
||
113 | { "%.8g", 0.1, "0.1" }, |
||
114 | { "%.9g", 0.1, "0.1" }, |
||
115 | { "%.10g", 0.1, "0.1" }, |
||
116 | { "%.11g", 0.1, "0.1" }, |
||
117 | |||
118 | /* pi in double precision, printed to a few extra places */ |
||
119 | { "%.15f", M_PI, "3.141592653589793" }, |
||
120 | { "%.18f", M_PI, "3.141592653589793116" }, |
||
121 | |||
122 | /* exact conversion of large integers */ |
||
123 | { "%.0f", 340282366920938463463374607431768211456.0, |
||
124 | "340282366920938463463374607431768211456" }, |
||
125 | |||
126 | { NULL, 0.0, NULL } |
||
127 | }; |
||
128 | |||
129 | int main(void) |
||
130 | { |
||
131 | int i, j, k; |
||
132 | char b[2000]; |
||
133 | |||
134 | TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d"); |
||
135 | TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d"); |
||
136 | TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d"); |
||
137 | |||
138 | strcpy(b, "xxxxxxxx"); |
||
139 | TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d"); |
||
140 | TEST_S(b, "123", "incorrect output"); |
||
141 | TEST(i, b[5], 'x', "buffer overrun"); |
||
142 | |||
143 | /* Perform ascii arithmetic to test printing tiny doubles */ |
||
144 | TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d"); |
||
145 | b[1] = '0'; |
||
146 | for (i=0; i<1021; i++) { |
||
147 | for (k=0, j=1023; j>0; j--) { |
||
148 | if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0; |
||
149 | else b[j]+=b[j]-'0'-10+k, k=1; |
||
150 | } |
||
151 | } |
||
152 | TEST(i, b[1], '1', "'%c' != '%c'"); |
||
153 | for (j=2; b[j]=='0'; j++); |
||
154 | TEST(i, j, 1024, "%d != %d"); |
||
155 | |||
156 | |||
157 | #ifndef DISABLE_SLOW_TESTS |
||
158 | errno = 0; |
||
159 | TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d"); |
||
160 | TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d"); |
||
161 | TEST(i, errno, EOVERFLOW, "after overflow: %d != %d"); |
||
162 | #endif |
||
163 | for (j=0; int_tests[j].fmt; j++) { |
||
164 | i = snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i); |
||
165 | if (i != strlen(int_tests[j].expect)) { |
||
166 | t_error("snprintf(b, sizeof b, \"%s\", %d) returned %d wanted %d\n", |
||
167 | int_tests[j].fmt, int_tests[j].i, i, strlen(int_tests[j].expect)); |
||
168 | } |
||
169 | if (strcmp(b, int_tests[j].expect) != 0) |
||
170 | t_error("bad integer conversion fmt[%s]: got \"%s\", want \"%s\"\n", int_tests[j].fmt, b, int_tests[j].expect); |
||
171 | } |
||
172 | |||
173 | for (j=0; fp_tests[j].fmt; j++) { |
||
174 | i = snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f); |
||
175 | if (i != strlen(fp_tests[j].expect)) { |
||
176 | t_error("snprintf(b, sizeof b, \"%s\", %f) returned %d wanted %d\n", |
||
177 | fp_tests[j].fmt, fp_tests[j].f, i, strlen(fp_tests[j].expect)); |
||
178 | } |
||
179 | if (strcmp(b, fp_tests[j].expect) != 0) |
||
180 | t_error("bad floating-point conversion: got \"%s\", want \"%s\"\n", b, fp_tests[j].expect); |
||
181 | } |
||
182 | |||
183 | TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d"); |
||
184 | |||
185 | printf("%s finished\n", __FILE__); |
||
186 | |||
187 | return t_status; |
||
188 | }'5')>1021;> |