3 /* Be afraid. Be very afraid.
4 Virtually everything in the printf() / scanf() test drivers is heavily
5 depending on the platform, i.e. the width of the integer values. Since
6 we need widths, string representations in hex, octal and decimal and
7 whatnot, there is some heavy macro abuse coming up...
11 #define sym2s( x ) sym2v( x )
13 #if INT_MAX >> 15 == 1
17 #define INT_DIG_LESS1 "4"
18 #define INT_DIG_PLUS1 "6"
19 #define INT_DIG_PLUS2 "7"
20 #define INT_HEXDIG "FFF"
21 #define INT_hexdig "fff"
22 #define INT_OCTDIG "177777"
23 #define INT_MAX_DEZ_STR "32767"
24 #define INT_MIN_DEZ_STR "32768"
25 #define UINT_MAX_DEZ_STR "65535"
26 #define INT_MAX_OCT_STR
27 #define INT_MIN_OCT_STR
28 #define UINT_MAX_OCT_STR
29 #define INT_MAX_HEX_STR
30 #define INT_MIN_HEX_STR
31 #define UINT_MAX_HEX_STR
33 #elif UINT_MAX >> 31 == 1
37 #define INT_DIG_LESS1 "9"
38 #define INT_DIG_PLUS1 "11"
39 #define INT_DIG_PLUS2 "12"
40 #define INT_HEXDIG "FFFFFFF"
41 #define INT_hexdig "fffffff"
42 #define INT_OCTDIG "37777777777"
43 #define INT_MAX_DEZ_STR "2147483647"
44 #define INT_MIN_DEZ_STR "2147483648"
45 #define UINT_MAX_DEZ_STR "4294967295"
46 #define INT_MAX_OCT_STR
47 #define INT_MIN_OCT_STR
48 #define UINT_MAX_OCT_STR
49 #define INT_MAX_HEX_STR
50 #define INT_MIN_HEX_STR
51 #define UINT_MAX_HEX_STR
53 #elif UINT_MAX >> 63 == 1
57 #define INT_DIG_LESS1 "18"
58 #define INT_DIG_PLUS1 "20"
59 #define INT_DIG_PLUS2 "21"
60 #define INT_HEXDIG "FFFFFFFFFFFFFFF"
61 #define INT_hexdig "fffffffffffffff"
62 #define INT_OCTDIG "1777777777777777777777"
63 #define INT_MAX_DEZ_STR "9223372036854775807"
64 #define INT_MIN_DEZ_STR "9223372036854775808"
65 #define UINT_MAX_DEZ_STR "18446744073709551615"
66 #define INT_MAX_OCT_STR
67 #define INT_MIN_OCT_STR
68 #define UINT_MAX_OCT_STR
69 #define INT_MAX_HEX_STR
70 #define INT_MIN_HEX_STR
71 #define UINT_MAX_HEX_STR
75 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
80 #if ULONG_MAX >> 31 == 1
84 #define LONG_MAX_DEZ_STR "2147483647"
85 #define LONG_MIN_DEZ_STR "2147483648"
86 #define ULONG_MAX_DEZ_STR "4294967295"
87 #define LONG_MAX_OCT_STR
88 #define LONG_MIN_OCT_STR
89 #define ULONG_MAX_OCT_STR
90 #define LONG_MAX_HEX_STR
91 #define LONG_MIN_HEX_STR
92 #define ULONG_MAX_HEX_STR
94 #elif ULONG_MAX >> 63 == 1
98 #define LONG_MAX_DEZ_STR "9223372036854775807"
99 #define LONG_MIN_DEZ_STR "9223372036854775808"
100 #define ULONG_MAX_DEZ_STR "18446744073709551615"
101 #define LONG_MAX_OCT_STR
102 #define LONG_MIN_OCT_STR
103 #define ULONG_MAX_OCT_STR
104 #define LONG_MAX_HEX_STR
105 #define LONG_MIN_HEX_STR
106 #define ULONG_MAX_HEX_STR
110 #error Unsupported width of 'long' (neither 32 nor 64 bit).
115 #if ULLONG_MAX >> 63 == 1
117 #define ULLONG_DIG 20
119 #define LLONG_MAX_DEZ_STR "9223372036854775807"
120 #define LLONG_MIN_DEZ_STR "9223372036854775808"
121 #define ULLONG_MAX_DEZ_STR "18446744073709551615"
122 #define LLONG_MAX_OCT_STR
123 #define LLONG_MIN_OCT_STR
124 #define ULLONG_MAX_OCT_STR
125 #define LLONG_MAX_HEX_STR
126 #define LLONG_MIN_HEX_STR
127 #define ULLONG_MAX_HEX_STR
129 #elif ULLONG_MAX >> 127 == 1
131 #define ULLONG_DIG 38
133 #define LLONG_MAX_DEZ_STR "170141183460469231731687303715884105727"
134 #define LLONG_MIN_DEZ_STR "170141183460469231731687303715884105728"
135 #define ULLONG_MAX_DEZ_STR "340282366920938463463374607431768211455"
136 #define LLONG_MAX_OCT_STR
137 #define LLONG_MIN_OCT_STR
138 #define ULLONG_MAX_OCT_STR
139 #define LLONG_MAX_HEX_STR
140 #define LLONG_MIN_HEX_STR
141 #define ULLONG_MAX_HEX_STR
145 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
150 TESTCASE( testprintf( target, "%hhd", CHAR_MIN ) == 4 );
151 TESTCASE_SPRINTF( "-128" );
152 TESTCASE( testprintf( target, "%hhd", CHAR_MAX ) == 3 );
153 TESTCASE_SPRINTF( "127" );
154 TESTCASE( testprintf( target, "%hhd", 0 ) == 1 );
155 TESTCASE_SPRINTF( "0" );
156 TESTCASE( testprintf( target, "%hd", SHRT_MIN ) == 6 );
157 TESTCASE_SPRINTF( "-32768" );
158 TESTCASE( testprintf( target, "%hd", SHRT_MAX ) == 5 );
159 TESTCASE_SPRINTF( "32767" );
160 TESTCASE( testprintf( target, "%hd", 0 ) == 1 );
161 TESTCASE_SPRINTF( "0" );
162 TESTCASE( testprintf( target, "%d", INT_MIN ) == INT_DIG + 1 );
163 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
164 TESTCASE( testprintf( target, "%d", INT_MAX ) == INT_DIG );
165 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
166 TESTCASE( testprintf( target, "%d", 0 ) == 1 );
167 TESTCASE_SPRINTF( "0" );
168 TESTCASE( testprintf( target, "%ld", LONG_MIN ) == LONG_DIG + 1 );
169 TESTCASE_SPRINTF( "-" LONG_MIN_DEZ_STR );
170 TESTCASE( testprintf( target, "%ld", LONG_MAX ) == LONG_DIG );
171 TESTCASE_SPRINTF( LONG_MAX_DEZ_STR );
172 TESTCASE( testprintf( target, "%ld", 0l ) == 1 );
173 TESTCASE_SPRINTF( "0" );
174 TESTCASE( testprintf( target, "%lld", LLONG_MIN ) == LLONG_DIG + 1 );
175 TESTCASE_SPRINTF( "-" LLONG_MIN_DEZ_STR );
176 TESTCASE( testprintf( target, "%lld", LLONG_MAX ) == LLONG_DIG );
177 TESTCASE_SPRINTF( LLONG_MAX_DEZ_STR );
178 TESTCASE( testprintf( target, "%lld", 0ll ) );
179 TESTCASE_SPRINTF( "0" );
180 TESTCASE( testprintf( target, "%hhu", UCHAR_MAX ) == 3 );
181 TESTCASE_SPRINTF( "255" );
182 TESTCASE( testprintf( target, "%hhu", (unsigned char)-1 ) == 3 );
183 TESTCASE_SPRINTF( "255" );
184 TESTCASE( testprintf( target, "%hu", USHRT_MAX ) == 5 );
185 TESTCASE_SPRINTF( "65535" );
186 TESTCASE( testprintf( target, "%hu", (unsigned short)-1 ) == 5 );
187 TESTCASE_SPRINTF( "65535" );
188 TESTCASE( testprintf( target, "%u", UINT_MAX ) == UINT_DIG );
189 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
190 TESTCASE( testprintf( target, "%u", -1u ) == UINT_DIG );
191 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
192 TESTCASE( testprintf( target, "%lu", ULONG_MAX ) == ULONG_DIG );
193 TESTCASE_SPRINTF( ULONG_MAX_DEZ_STR );
194 TESTCASE( testprintf( target, "%lu", -1ul ) == ULONG_DIG );
195 TESTCASE_SPRINTF( ULONG_MAX_DEZ_STR );
196 TESTCASE( testprintf( target, "%llu", ULLONG_MAX ) == ULLONG_DIG );
197 TESTCASE_SPRINTF( ULLONG_MAX_DEZ_STR );
198 TESTCASE( testprintf( target, "%llu", -1ull ) == ULLONG_DIG );
199 TESTCASE_SPRINTF( ULLONG_MAX_DEZ_STR );
200 TESTCASE( testprintf( target, "%X", UINT_MAX ) == (int)strlen( INT_HEXDIG ) + 1 );
201 TESTCASE_SPRINTF( "F" INT_HEXDIG );
202 TESTCASE( testprintf( target, "%#X", -1u ) == (int)strlen( INT_HEXDIG ) + 3 );
203 TESTCASE_SPRINTF( "0XF" INT_HEXDIG );
204 TESTCASE( testprintf( target, "%x", UINT_MAX ) == (int)strlen( INT_HEXDIG ) + 1 );
205 TESTCASE_SPRINTF( "f" INT_hexdig );
206 TESTCASE( testprintf( target, "%#x", -1u ) == (int)strlen( INT_HEXDIG ) + 3 );
207 TESTCASE_SPRINTF( "0xf" INT_hexdig );
208 TESTCASE( testprintf( target, "%o", UINT_MAX ) == (int)strlen( INT_OCTDIG ) );
209 TESTCASE_SPRINTF( INT_OCTDIG );
210 TESTCASE( testprintf( target, "%#o", -1u ) == (int)strlen( INT_OCTDIG ) + 1 );
211 TESTCASE_SPRINTF( "0" INT_OCTDIG );
213 /* TODO: This test case is broken, doesn't test what it was intended to. */
214 TESTCASE( testprintf( target, "%.0#o", 0 ) == 5 );
215 TESTCASE_SPRINTF( "%.0#o" );
217 TESTCASE( testprintf( target, "%+d", INT_MIN ) == INT_DIG + 1 );
218 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
219 TESTCASE( testprintf( target, "%+d", INT_MAX ) == INT_DIG + 1 );
220 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
221 TESTCASE( testprintf( target, "%+d", 0 ) == 2 );
222 TESTCASE_SPRINTF( "+0" );
223 TESTCASE( testprintf( target, "%+u", UINT_MAX ) == UINT_DIG );
224 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
225 TESTCASE( testprintf( target, "%+u", -1u ) == UINT_DIG );
226 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
227 TESTCASE( testprintf( target, "% d", INT_MIN ) == INT_DIG + 1 );
228 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
229 TESTCASE( testprintf( target, "% d", INT_MAX ) == INT_DIG + 1 );
230 TESTCASE_SPRINTF( " " INT_MAX_DEZ_STR );
231 TESTCASE( testprintf( target, "% d", 0 ) == 2 );
232 TESTCASE_SPRINTF( " 0" );
233 TESTCASE( testprintf( target, "% u", UINT_MAX ) == UINT_DIG );
234 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
235 TESTCASE( testprintf( target, "% u", -1u ) == UINT_DIG );
236 TESTCASE_SPRINTF( UINT_MAX_DEZ_STR );
237 TESTCASE( testprintf( target, "%" INT_DIG_LESS1 "d", INT_MIN ) == INT_DIG + 1 );
238 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
239 TESTCASE( testprintf( target, "%" INT_DIG_LESS1 "d", INT_MAX ) == INT_DIG );
240 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
241 TESTCASE( testprintf( target, "%" sym2s(INT_DIG) "d", INT_MIN ) == INT_DIG + 1 );
242 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
243 TESTCASE( testprintf( target, "%" sym2s(INT_DIG) "d", INT_MAX ) == INT_DIG );
244 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
245 TESTCASE( testprintf( target, "%" INT_DIG_PLUS1 "d", INT_MIN ) == INT_DIG + 1 );
246 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
247 TESTCASE( testprintf( target, "%" INT_DIG_PLUS1 "d", INT_MAX ) == INT_DIG + 1 );
248 TESTCASE_SPRINTF( " " INT_MAX_DEZ_STR );
249 TESTCASE( testprintf( target, "%" INT_DIG_PLUS2 "d", INT_MIN ) == INT_DIG + 2 );
250 TESTCASE_SPRINTF( " -" INT_MIN_DEZ_STR );
251 TESTCASE( testprintf( target, "%" INT_DIG_PLUS2 "d", INT_MAX ) == INT_DIG + 2 );
252 TESTCASE_SPRINTF( " " INT_MAX_DEZ_STR );
253 TESTCASE( testprintf( target, "%-" INT_DIG_LESS1 "d", INT_MIN ) == INT_DIG + 1 );
254 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
255 TESTCASE( testprintf( target, "%-" INT_DIG_LESS1 "d", INT_MAX ) == INT_DIG );
256 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
257 TESTCASE( testprintf( target, "%-" sym2s(INT_DIG) "d", INT_MIN ) == INT_DIG + 1 );
258 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
259 TESTCASE( testprintf( target, "%-" sym2s(INT_DIG) "d", INT_MAX ) == INT_DIG );
260 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
261 TESTCASE( testprintf( target, "%-" INT_DIG_PLUS1 "d", INT_MIN ) == INT_DIG + 1 );
262 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
263 TESTCASE( testprintf( target, "%-" INT_DIG_PLUS1 "d", INT_MAX ) == INT_DIG + 1 );
264 TESTCASE_SPRINTF( INT_MAX_DEZ_STR " " );
265 TESTCASE( testprintf( target, "%-" INT_DIG_PLUS2 "d", INT_MIN ) == INT_DIG + 2 );
266 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR " " );
267 TESTCASE( testprintf( target, "%-" INT_DIG_PLUS2 "d", INT_MAX ) == INT_DIG + 2 );
268 TESTCASE_SPRINTF( INT_MAX_DEZ_STR " " );
269 TESTCASE( testprintf( target, "%0" INT_DIG_LESS1 "d", INT_MIN ) == INT_DIG + 1 );
270 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
271 TESTCASE( testprintf( target, "%0" INT_DIG_LESS1 "d", INT_MAX ) == INT_DIG );
272 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
273 TESTCASE( testprintf( target, "%0" sym2s(INT_DIG) "d", INT_MIN ) == INT_DIG + 1 );
274 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
275 TESTCASE( testprintf( target, "%0" sym2s(INT_DIG) "d", INT_MAX ) == INT_DIG );
276 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
277 TESTCASE( testprintf( target, "%0" INT_DIG_PLUS1 "d", INT_MIN ) == INT_DIG + 1 );
278 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
279 TESTCASE( testprintf( target, "%0" INT_DIG_PLUS1 "d", INT_MAX ) == INT_DIG + 1 );
280 TESTCASE_SPRINTF( "0" INT_MAX_DEZ_STR );
281 TESTCASE( testprintf( target, "%0" INT_DIG_PLUS2 "d", INT_MIN ) == INT_DIG + 2 );
282 TESTCASE_SPRINTF( "-0" INT_MIN_DEZ_STR );
283 TESTCASE( testprintf( target, "%0" INT_DIG_PLUS2 "d", INT_MAX ) == INT_DIG + 2 );
284 TESTCASE_SPRINTF( "00" INT_MAX_DEZ_STR );
285 TESTCASE( testprintf( target, "%-0" INT_DIG_LESS1 "d", INT_MIN ) == INT_DIG + 1 );
286 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
287 TESTCASE( testprintf( target, "%-0" INT_DIG_LESS1 "d", INT_MAX ) == INT_DIG );
288 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
289 TESTCASE( testprintf( target, "%-0" sym2s(INT_DIG) "d", INT_MIN ) == INT_DIG + 1 );
290 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
291 TESTCASE( testprintf( target, "%-0" sym2s(INT_DIG) "d", INT_MAX ) == INT_DIG );
292 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
293 TESTCASE( testprintf( target, "%-0" INT_DIG_PLUS1 "d", INT_MIN ) == INT_DIG + 1 );
294 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
295 TESTCASE( testprintf( target, "%-0" INT_DIG_PLUS1 "d", INT_MAX ) == INT_DIG + 1 );
296 TESTCASE_SPRINTF( INT_MAX_DEZ_STR " " );
297 TESTCASE( testprintf( target, "%-0" INT_DIG_PLUS2 "d", INT_MIN ) == INT_DIG + 2 );
298 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR " " );
299 TESTCASE( testprintf( target, "%-0" INT_DIG_PLUS2 "d", INT_MAX ) == INT_DIG + 2 );
300 TESTCASE_SPRINTF( INT_MAX_DEZ_STR " " );
301 /* FIXME: This test not yet 32/64 bit agnostic */
302 TESTCASE( testprintf( target, "%030.20d", INT_MAX ) == 30 );
303 TESTCASE_SPRINTF( " 00000000002147483647" );
304 TESTCASE( testprintf( target, "%.6x", UINT_MAX ) == (int)strlen( INT_HEXDIG ) + 1 );
305 TESTCASE_SPRINTF( "f" INT_hexdig );
306 TESTCASE( testprintf( target, "%#6.3x", UINT_MAX ) == (int)strlen( INT_HEXDIG ) + 3 );
307 TESTCASE_SPRINTF( "0xf" INT_hexdig );
308 TESTCASE( testprintf( target, "%#3.6x", UINT_MAX ) == (int)strlen( INT_HEXDIG ) + 3 );
309 TESTCASE_SPRINTF( "0xf" INT_hexdig );
310 TESTCASE( testprintf( target, "%.6d", INT_MIN ) == INT_DIG + 1 );
311 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
312 TESTCASE( testprintf( target, "%6.3d", INT_MIN ) == INT_DIG + 1 );
313 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
314 TESTCASE( testprintf( target, "%3.6d", INT_MIN ) == INT_DIG + 1 );
315 TESTCASE_SPRINTF( "-" INT_MIN_DEZ_STR );
316 TESTCASE( testprintf( target, "%#0.6x", UINT_MAX ) == UINT_DIG );
317 TESTCASE_SPRINTF( "0xf" INT_hexdig );
318 TESTCASE( testprintf( target, "%#06.3x", UINT_MAX ) == UINT_DIG );
319 TESTCASE_SPRINTF( "0xf" INT_hexdig );
320 TESTCASE( testprintf( target, "%#03.6x", UINT_MAX ) == UINT_DIG );
321 TESTCASE_SPRINTF( "0xf" INT_hexdig );
322 TESTCASE( testprintf( target, "%#0.6d", INT_MAX ) == INT_DIG );
323 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
324 TESTCASE( testprintf( target, "%#06.3d", INT_MAX ) == INT_DIG );
325 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
326 TESTCASE( testprintf( target, "%#03.6d", INT_MAX ) == INT_DIG );
327 TESTCASE_SPRINTF( INT_MAX_DEZ_STR );
328 TESTCASE( testprintf( target, "%#+.6d", INT_MAX ) == INT_DIG + 1 );
329 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
330 TESTCASE( testprintf( target, "%#+6.3d", INT_MAX ) == INT_DIG + 1 );
331 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
332 TESTCASE( testprintf( target, "%#+3.6d", INT_MAX ) == INT_DIG + 1 );
333 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
334 TESTCASE( testprintf( target, "%+0.6d", INT_MAX ) == INT_DIG + 1 );
335 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
336 TESTCASE( testprintf( target, "%+06.3d", INT_MAX ) == INT_DIG + 1 );
337 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
338 TESTCASE( testprintf( target, "%+03.6d", INT_MAX ) == INT_DIG + 1 );
339 TESTCASE_SPRINTF( "+" INT_MAX_DEZ_STR );
340 #ifndef TEST_CONVERSION_ONLY
341 TESTCASE( testprintf( target, "- %d", INT_MAX ) == INT_DIG + 2 );
342 TESTCASE_SPRINTF( "- " INT_MAX_DEZ_STR );
343 TESTCASE( testprintf( target, "- %d %% %d", INT_MAX, INT_MIN ) == INT_DIG * 2 + 6 );
344 TESTCASE_SPRINTF( "- " INT_MAX_DEZ_STR " % -" INT_MIN_DEZ_STR );
346 TESTCASE( testprintf( target, "%c", 'x' ) == 1 );
347 TESTCASE_SPRINTF( "x" );
348 TESTCASE( testprintf( target, "%s", "abcdef" ) == 6 );
349 TESTCASE_SPRINTF( "abcdef" );
350 /* FIXME: This test not yet 32/64 bit agnostic */
351 TESTCASE( testprintf( target, "%p", (void *)0xdeadbeef ) == 10 );
352 TESTCASE_SPRINTF( "0xdeadbeef" );
353 TESTCASE( testprintf( target, "%#6x", 1 ) == 6 );
354 TESTCASE_SPRINTF( " 0x1" );
355 #ifndef TEST_CONVERSION_ONLY
358 TESTCASE( testprintf( target, "123456%n789%n", &val1, &val2 ) == 9 );
359 TESTCASE_SPRINTF( "123456789" ); /* FIXME */
360 TESTCASE( val1 == 6 );
361 TESTCASE( val2 == 9 );