2 #define _XOPEN_SOURCE 700
11 #define DISABLE_SLOW_TESTS
13 #define TEST(r, f, x, m) ( \
14 ((r) = (f)) == (x) || \
15 (t_error("%s failed (" m ")\n", #f, r, x), 0) )
17 #define TEST_S(s, x, m) ( \
19 (t_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 except for alt-octal. */
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 */
63 /* octal: corner cases */
70 { "%#04o", 1, "0001" },
71 { "%#04.0o", 1, " 01" },
72 { "%#04.1o", 1, " 01" },
73 { "%04o", 1, "0001" },
74 { "%04.0o", 1, " 1" },
75 { "%04.1o", 1, " 1" },
85 /* basic form, handling of exponent/precision for 0 */
86 { "%a", 0.0, "0x0p+0" },
87 { "%e", 0.0, "0.000000e+00" },
88 { "%f", 0.0, "0.000000" },
90 { "%#g", 0.0, "0.00000" },
91 { "%la", 0.0, "0x0p+0" },
92 { "%le", 0.0, "0.000000e+00" },
93 { "%lf", 0.0, "0.000000" },
95 { "%#lg", 0.0, "0.00000" },
98 { "%f", 1.1, "1.100000" },
99 { "%f", 1.2, "1.200000" },
100 { "%f", 1.3, "1.300000" },
101 { "%f", 1.4, "1.400000" },
102 { "%f", 1.5, "1.500000" },
103 { "%.4f", 1.06125, "1.0613" }, /* input is not representible exactly as double */
104 { "%.4f", 1.03125, "1.0312" }, /* 0x1.08p0 */
105 { "%.2f", 1.375, "1.38" },
106 { "%.1f", 1.375, "1.4" },
107 { "%.1lf", 1.375, "1.4" },
108 { "%.15f", 1.1, "1.100000000000000" },
109 { "%.16f", 1.1, "1.1000000000000001" },
110 { "%.17f", 1.1, "1.10000000000000009" },
111 { "%.2e", 1500001.0, "1.50e+06" },
112 { "%.2e", 1505000.0, "1.50e+06" },
113 { "%.2e", 1505000.00000095367431640625, "1.51e+06" },
114 { "%.2e", 1505001.0, "1.51e+06" },
115 { "%.2e", 1506000.0, "1.51e+06" },
117 /* correctness in DBL_DIG places */
118 { "%.15g", 1.23456789012345, "1.23456789012345" },
120 /* correct choice of notation for %g */
121 { "%g", 0.0001, "0.0001" },
122 { "%g", 0.00001, "1e-05" },
123 { "%g", 123456, "123456" },
124 { "%g", 1234567, "1.23457e+06" },
125 { "%.7g", 1234567, "1234567" },
126 { "%.7g", 12345678, "1.234568e+07" },
127 { "%.8g", 0.1, "0.1" },
128 { "%.9g", 0.1, "0.1" },
129 { "%.10g", 0.1, "0.1" },
130 { "%.11g", 0.1, "0.1" },
132 /* pi in double precision, printed to a few extra places */
133 { "%.15f", M_PI
, "3.141592653589793" },
134 { "%.18f", M_PI
, "3.141592653589793116" },
136 /* exact conversion of large integers */
137 { "%.0f", 340282366920938463463374607431768211456.0,
138 "340282366920938463463374607431768211456" },
148 TEST(i
, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d");
149 TEST(i
, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d");
150 TEST(i
, snprintf(b
, 0, "%.0s", "goodbye"), 0, "length returned %d != %d");
152 strcpy(b
, "xxxxxxxx");
153 TEST(i
, snprintf(b
, 4, "%d", 123456), 6, "length returned %d != %d");
154 TEST_S(b
, "123", "incorrect output");
155 TEST(i
, b
[5], 'x', "buffer overrun");
157 /* Perform ascii arithmetic to test printing tiny doubles */
158 TEST(i
, snprintf(b
, sizeof b
, "%.1022f", 0x1p
-1021), 1024, "%d != %d");
160 for (i
=0; i
<1021; i
++) {
161 for (k
=0, j
=1023; j
>0; j
--) {
162 if (b
[j
]<'5') b
[j
]+=b
[j
]-'0'+k
, k
=0;
163 else b
[j
]+=b
[j
]-'0'-10+k
, k
=1;
166 TEST(i
, b
[1], '1', "'%c' != '%c'");
167 for (j
=2; b
[j
]=='0'; j
++);
168 TEST(i
, j
, 1024, "%d != %d");
171 #ifndef DISABLE_SLOW_TESTS
173 TEST(i
, snprintf(NULL
, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
174 TEST(i
, snprintf(NULL
, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
175 TEST(i
, errno
, EOVERFLOW
, "after overflow: %d != %d");
177 for (j
=0; int_tests
[j
].fmt
; j
++) {
178 i
= snprintf(b
, sizeof b
, int_tests
[j
].fmt
, int_tests
[j
].i
);
179 if (i
!= strlen(int_tests
[j
].expect
)) {
180 t_error("snprintf(b, sizeof b, \"%s\", %d) returned %d wanted %d\n",
181 int_tests
[j
].fmt
, int_tests
[j
].i
, i
, strlen(int_tests
[j
].expect
));
183 if (strcmp(b
, int_tests
[j
].expect
) != 0)
184 t_error("bad integer conversion: got \"%s\", want \"%s\"\n", b
, int_tests
[j
].expect
);
187 for (j
=0; fp_tests
[j
].fmt
; j
++) {
188 i
= snprintf(b
, sizeof b
, fp_tests
[j
].fmt
, fp_tests
[j
].f
);
189 if (i
!= strlen(fp_tests
[j
].expect
)) {
190 t_error("snprintf(b, sizeof b, \"%s\", %f) returned %d wanted %d\n",
191 fp_tests
[j
].fmt
, fp_tests
[j
].f
, i
, strlen(fp_tests
[j
].expect
));
193 if (strcmp(b
, fp_tests
[j
].expect
) != 0)
194 t_error("bad floating-point conversion: got \"%s\", want \"%s\"\n", b
, fp_tests
[j
].expect
);
197 TEST(i
, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d");