2 #define _XOPEN_SOURCE 700
11 #define DISABLE_SLOW_TESTS
13 #define TEST(r, f, x, m) ( \
14 ((r) = (f)) == (x) || \
15 (error("%s failed (" m ")\n", #f, r, x), 0) )
17 #define TEST_S(s, x, m) ( \
19 (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
26 /* width, precision, alignment */
27 { "%04d", 12, "0012" },
28 { "%.3d", 12, "012" },
30 { "%-3d", 12, "12 " },
31 { "%+3d", 12, "+12" },
32 { "%+-5d", 12, "+12 " },
33 { "%+- 5d", 12, "+12 " },
34 { "%- 5d", 12, " 12 " },
36 { "%0-5d", 12, "12 " },
37 { "%-05d", 12, "12 " },
39 /* ...explicit precision of 0 shall be no characters. */
46 /* ...but it still has to honor width and flags. */
54 /* hex: test alt form and case */
56 { "%#x", 63, "0x3f" },
59 /* octal: test alt form */
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" },
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" },
81 { "%#lg", 0.0, "0.00000" },
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.0612" },
90 { "%.2f", 1.375, "1.38" },
91 { "%.1f", 1.375, "1.4" },
92 { "%.1lf", 1.375, "1.4" },
93 { "%.15f", 1.1, "1.100000000000000" },
94 { "%.16f", 1.1, "1.1000000000000001" },
95 { "%.17f", 1.1, "1.10000000000000009" },
96 { "%.2e", 1500001.0, "1.50e+06" },
97 { "%.2e", 1505000.0, "1.50e+06" },
98 { "%.2e", 1505000.00000095367431640625, "1.51e+06" },
99 { "%.2e", 1505001.0, "1.51e+06" },
100 { "%.2e", 1506000.0, "1.51e+06" },
102 /* correctness in DBL_DIG places */
103 { "%.15g", 1.23456789012345, "1.23456789012345" },
105 /* correct choice of notation for %g */
106 { "%g", 0.0001, "0.0001" },
107 { "%g", 0.00001, "1e-05" },
108 { "%g", 123456, "123456" },
109 { "%g", 1234567, "1.23457e+06" },
110 { "%.7g", 1234567, "1234567" },
111 { "%.7g", 12345678, "1.234568e+07" },
112 { "%.8g", 0.1, "0.1" },
113 { "%.9g", 0.1, "0.1" },
114 { "%.10g", 0.1, "0.1" },
115 { "%.11g", 0.1, "0.1" },
117 /* pi in double precision, printed to a few extra places */
118 { "%.15f", M_PI, "3.141592653589793" },
119 { "%.18f", M_PI, "3.141592653589793116" },
121 /* exact conversion of large integers */
122 { "%.0f", 340282366920938463463374607431768211456.0,
123 "340282366920938463463374607431768211456" },
133 TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d");
134 TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d");
135 TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d");
137 strcpy(b, "xxxxxxxx");
138 TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d");
139 TEST_S(b, "123", "incorrect output");
140 TEST(i, b[5], 'x', "buffer overrun");
142 /* Perform ascii arithmetic to test printing tiny doubles */
143 TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d");
145 for (i=0; i<1021; i++) {
146 for (k=0, j=1023; j>0; j--) {
147 if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0;
148 else b[j]+=b[j]-'0'-10+k, k=1;
151 TEST(i, b[1], '1', "'%c' != '%c'");
152 for (j=2; b[j]=='0'; j++);
153 TEST(i, j, 1024, "%d != %d");
156 #ifndef DISABLE_SLOW_TESTS
158 TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
159 TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
160 TEST(i, errno, EOVERFLOW, "after overflow: %d != %d");
162 for (j=0; int_tests[j].fmt; j++) {
163 TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d");
164 TEST_S(b, int_tests[j].expect, "bad integer conversion");
167 for (j=0; fp_tests[j].fmt; j++) {
168 TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d");
169 TEST_S(b, fp_tests[j].expect, "bad floating point conversion");
172 TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d");