1 #define _XOPEN_SOURCE 700
9 #define DISABLE_SLOW_TESTS
11 #define TEST(r, f, x, m) ( \
12 ((r) = (f)) == (x) || \
13 (error("%s failed (" m ")\n", #f, r, x), 0) )
15 #define TEST_S(s, x, m) ( \
17 (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
24 /* width, precision, alignment */
25 { "%04d", 12, "0012" },
26 { "%.3d", 12, "012" },
28 { "%-3d", 12, "12 " },
29 { "%+3d", 12, "+12" },
30 { "%+-5d", 12, "+12 " },
31 { "%+- 5d", 12, "+12 " },
32 { "%- 5d", 12, " 12 " },
34 { "%0-5d", 12, "12 " },
35 { "%-05d", 12, "12 " },
37 /* ...explicit precision of 0 shall be no characters. */
44 /* ...but it still has to honor width and flags. */
52 /* hex: test alt form and case */
54 { "%#x", 63, "0x3f" },
57 /* octal: test alt form */
69 /* basic form, handling of exponent/precision for 0 */
70 { "%e", 0.0, "0.000000e+00" },
71 { "%f", 0.0, "0.000000" },
73 { "%#g", 0.0, "0.00000" },
76 { "%f", 1.1, "1.100000" },
77 { "%f", 1.2, "1.200000" },
78 { "%f", 1.3, "1.300000" },
79 { "%f", 1.4, "1.400000" },
80 { "%f", 1.5, "1.500000" },
81 { "%.4f", 1.06125, "1.0612" },
82 { "%.2f", 1.375, "1.38" },
83 { "%.1f", 1.375, "1.4" },
84 { "%.15f", 1.1, "1.100000000000000" },
85 { "%.16f", 1.1, "1.1000000000000001" },
86 { "%.17f", 1.1, "1.10000000000000009" },
87 { "%.2e", 1500001.0, "1.50e+06" },
88 { "%.2e", 1505000.0, "1.50e+06" },
89 { "%.2e", 1505000.00000095367431640625, "1.51e+06" },
90 { "%.2e", 1505001.0, "1.51e+06" },
91 { "%.2e", 1506000.0, "1.51e+06" },
93 /* correctness in DBL_DIG places */
94 { "%.15g", 1.23456789012345, "1.23456789012345" },
96 /* correct choice of notation for %g */
97 { "%g", 0.0001, "0.0001" },
98 { "%g", 0.00001, "1e-05" },
99 { "%g", 123456, "123456" },
100 { "%g", 1234567, "1.23457e+06" },
101 { "%.7g", 1234567, "1234567" },
102 { "%.7g", 12345678, "1.234568e+07" },
103 { "%.8g", 0.1, "0.1" },
104 { "%.9g", 0.1, "0.1" },
105 { "%.10g", 0.1, "0.1" },
106 { "%.11g", 0.1, "0.1" },
108 /* pi in double precision, printed to a few extra places */
109 { "%.15f", M_PI, "3.141592653589793" },
110 { "%.18f", M_PI, "3.141592653589793116" },
112 /* exact conversion of large integers */
113 { "%.0f", 340282366920938463463374607431768211456.0,
114 "340282366920938463463374607431768211456" },
119 void test_snprintf(void)
124 TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d");
125 TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d");
126 TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d");
128 strcpy(b, "xxxxxxxx");
129 TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d");
130 TEST_S(b, "123", "incorrect output");
131 TEST(i, b[5], 'x', "buffer overrun");
133 /* Perform ascii arithmetic to test printing tiny doubles */
134 TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d");
136 for (i=0; i<1021; i++) {
137 for (k=0, j=1023; j>0; j--) {
138 if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0;
139 else b[j]+=b[j]-'0'-10+k, k=1;
142 TEST(i, b[1], '1', "'%c' != '%c'");
143 for (j=2; b[j]=='0'; j++);
144 TEST(i, j, 1024, "%d != %d");
147 #ifndef DISABLE_SLOW_TESTS
149 TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
150 TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
151 TEST(i, errno, EOVERFLOW, "after overflow: %d != %d");
153 for (j=0; int_tests[j].fmt; j++) {
154 TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d");
155 TEST_S(b, int_tests[j].expect, "bad integer conversion");
158 for (j=0; fp_tests[j].fmt; j++) {
159 TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d");
160 TEST_S(b, fp_tests[j].expect, "bad floating point conversion");
163 TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d");