PR tree-optimization/78910 - Wrong print-return-value for a negative number
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / builtin-sprintf.c
blob275cb284ff260393581118f15fdad2bb7059e7c8
1 /* Test to verify that the return value of calls to __builtin_sprintf
2 that produce a known number of bytes on output is available for
3 constant folding. With optimization enabled the test will fail to
4 link if any of the assertions fails. Without optimization the test
5 aborts at runtime if any of the assertions fails. */
6 /* { dg-do run } */
7 /* { dg-additional-options "-O2 -Wall -Wno-pedantic -fprintf-return-value" } */
9 #ifndef LINE
10 # define LINE 0
11 #endif
13 #if __STDC_VERSION__ < 199901L
14 # define __func__ __FUNCTION__
15 #endif
17 typedef __SIZE_TYPE__ size_t;
19 unsigned ntests;
20 unsigned nfails;
22 void __attribute__ ((noclone, noinline))
23 checkv (const char *func, int line, int res, int min, int max,
24 char *dst, const char *fmt, __builtin_va_list va)
26 int n = __builtin_vsprintf (dst, fmt, va);
27 int len = __builtin_strlen (dst);
29 ++ntests;
31 int fail = 0;
32 if (n != res)
34 __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" "
35 "doesn't match function call return value: ",
36 func, line, fmt, dst);
37 if (min == max)
38 __builtin_printf ("%i != %i\n", n, min);
39 else
40 __builtin_printf ("%i not in [%i, %i]\n", n, min, max);
42 fail = 1;
44 else
46 if (len < min || max < len)
48 __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" "
49 "doesn't match output length: ",
50 func, line, fmt, dst);
52 if (min == max)
53 __builtin_printf ("%i != %i\n", len, min);
54 else
55 __builtin_printf ("%i not in [%i, %i]\n", len, min, max);
57 fail = 1;
59 else if (min == max)
60 __builtin_printf ("PASS: %s:%i: \"%s\" result %i: \"%s\"\n",
61 func, line, fmt, n, dst);
62 else
63 __builtin_printf ("PASS: %s:%i: \"%s\" result %i in [%i, %i]: \"%s\"\n",
64 func, line, fmt, n, min, max, dst);
67 if (fail)
68 ++nfails;
71 void __attribute__ ((noclone, noinline))
72 check (const char *func, int line, int res, int min, int max,
73 char *dst, const char *fmt, ...)
75 __builtin_va_list va;
76 __builtin_va_start (va, fmt);
77 checkv (func, line, res, min, max, dst, fmt, va);
78 __builtin_va_end (va);
81 char buffer[4100];
82 char* volatile dst = buffer;
83 char* ptr = buffer;
85 #define concat(a, b) a ## b
86 #define CAT(a, b) concat (a, b)
88 #if __OPTIMIZE__
89 /* With optimization references to the following undefined symbol which
90 is unique for each test case are expected to be eliminated. */
91 # define TEST_FAILURE(line, ignore1, ignore2, ignore3) \
92 do { \
93 extern void CAT (failure_on_line_, line)(void); \
94 CAT (failure_on_line_, line)(); \
95 } while (0)
96 #else
97 /* The test is run by DejaGnu with optimization enabled. When it's run
98 with it disabled (i.e., at -O0) each test case is verified at runtime
99 and the test aborts just before exiting if any of them failed. */
100 # define TEST_FAILURE(line, result, min, max) \
101 if (min == max) \
102 __builtin_printf ("FAIL: %s:%i: expected %i, got %i\n", \
103 __func__, line, min, result); \
104 else \
105 __builtin_printf ("FAIL: %s:%i: expected range [%i, %i], got %i\n", \
106 __func__, line, min, max, result);
107 #endif
109 /* Verify that the result is exactly equal to RES. */
110 #define EQL(expect, size, fmt, ...) \
111 if (!LINE || LINE == __LINE__) \
112 do { \
113 char *buf = (size) < 0 ? ptr : buffer + sizeof buffer - (size); \
114 int result = __builtin_sprintf (buf, fmt, __VA_ARGS__); \
115 if (result != expect) \
117 TEST_FAILURE (__LINE__, expect, expect, result); \
119 check (__func__, __LINE__, result, expect, expect, dst, fmt, \
120 __VA_ARGS__); \
121 } while (0)
123 /* Verify that the result is in the range [MIN, MAX]. */
124 #define RNG(min, max, size, fmt, ...) \
125 if (!LINE || LINE == __LINE__) \
126 do { \
127 char *buf = (size) < 0 ? ptr : buffer + sizeof buffer - (size); \
128 int result = __builtin_sprintf (buf, fmt, __VA_ARGS__); \
129 if (result < min || max < result) \
131 TEST_FAILURE (__LINE__, min, max, result); \
133 check (__func__, __LINE__, result, min, max, dst, fmt, \
134 __VA_ARGS__); \
135 } while (0)
137 static void __attribute__ ((noinline, noclone))
138 test_c (char c)
140 EQL (1, 2, "%c", c);
141 EQL (1, -1, "%c", c);
142 EQL (1, 2, "%1c", c);
143 EQL (1, -1, "%1c", c);
144 EQL (1, 2, "%*c", 1, c);
145 EQL (1, -1, "%*c", 1, c);
146 EQL (2, 3, "%c%c", '1', '2');
147 EQL (2, -1, "%c%c", '1', '2');
148 EQL (3, 4, "%3c", c);
149 EQL (3, -1, "%3c", c);
150 EQL (3, 4, "%*c", 3, c);
151 EQL (3, -1, "%*c", 3, c);
153 EQL (3, 4, "%*c%*c", 2, c, 1, c);
154 EQL (3, 4, "%*c%*c", 1, c, 2, c);
155 EQL (3, 4, "%c%c%c", '1', '2', '3');
156 EQL (3, 4, "%*c%c%c", 1, '1', '2', '3');
157 EQL (3, 4, "%*c%*c%c", 1, '1', 1, '2', '3');
158 EQL (3, 4, "%*c%*c%*c", 1, '1', 1, '2', 1, '3');
160 EQL (3, -1, "%*c%*c", 2, c, 1, c);
161 EQL (3, -1, "%*c%*c", 1, c, 2, c);
162 EQL (3, -1, "%c%c%c", '1', '2', '3');
163 EQL (3, -1, "%*c%c%c", 1, '1', '2', '3');
164 EQL (3, -1, "%*c%*c%c", 1, '1', 1, '2', '3');
165 EQL (3, -1, "%*c%*c%*c", 1, '1', 1, '2', 1, '3');
167 EQL (4, 5, "%c%c %c", '1', '2', '3');
168 EQL (5, 6, "%c %c %c", '1', '2', '3');
169 EQL (5, 6, "%c %c %c", c, c, c);
172 /* Generate a pseudo-random unsigned value. */
174 unsigned __attribute__ ((noclone, noinline))
175 unsigned_value (void)
177 extern int rand ();
178 return rand ();
181 /* Generate a pseudo-random signed value. */
183 int __attribute__ ((noclone, noinline))
184 int_value (void)
186 extern int rand ();
187 return rand ();
190 /* Generate an unsigned char value in the specified range. */
192 static unsigned char
193 uchar_range (unsigned min, unsigned max)
195 unsigned x = unsigned_value ();
196 if (x < min || max < x)
197 x = min;
198 return x;
201 /* Generate a signed int value in the specified range. */
203 static int
204 int_range (int min, int max)
206 int val = int_value ();
207 if (val < min || max < val)
208 val = min;
209 return val;
212 #define IR(min, max) int_range (min, max)
214 static void __attribute__ ((noinline, noclone))
215 test_d_i (int i, long li)
217 /* +-------------------------- expected return value */
218 /* | +---------------------- destination size */
219 /* | | +------------------- format string */
220 /* | | | +-- variable argument(s) */
221 /* | | | | */
222 /* V V V V */
223 EQL ( 1, 2, "%d", 0);
224 EQL ( 2, 3, "%d%d", 0, 1);
225 EQL ( 3, 4, "%d%d", 9, 10);
226 EQL ( 4, 5, "%d%d", 11, 12);
227 EQL ( 5, 6, "%d:%d", 12, 34);
228 EQL ( 5, 6, "%d", 12345);
229 EQL ( 6, 7, "%d", -12345);
230 EQL (15, 16, "%d:%d:%d:%d", 123, 124, 125, 126);
232 EQL ( 1, 2, "%i", uchar_range (0, 9));
233 EQL ( 1, -1, "%i", uchar_range (0, 9));
235 /* The range information available to passes other than the Value
236 Range Propoagation pass itself is so bad that the following two
237 tests fail (the range seen in the test below is [0, 99] rather
238 than [10, 99].
239 EQL ( 2, 3, "%i", uchar_range (10, 99));
240 EQL ( 3, 4, "%i", uchar_range (100, 199));
243 /* Verify that the width allows the return value in the following
244 calls can be folded despite the unknown value of the argument. */
245 #if __SIZEOF_INT__ == 2
246 EQL ( 6, 7, "%6d", i);
247 EQL ( 6, 7, "%+6d", i);
248 EQL ( 6, 7, "%-6d", i);
249 EQL ( 6, 7, "%06d", i);
250 #elif __SIZEOF_INT__ == 4
251 EQL (11, 12, "%11d", i);
252 EQL (11, 12, "%+11d", i);
253 EQL (11, 12, "%-11d", i);
254 EQL (11, 12, "%011d", i);
255 #elif __SIZEOF_INT__ == 8
256 EQL (20, 21, "%20d", i);
257 EQL (20, 21, "%+20d", i);
258 EQL (20, 21, "%-29d", i);
259 EQL (20, 21, "%020d", i);
260 #endif
262 #if __SIZEOF_LONG__ == 2
263 EQL ( 6, 7, "%6ld", li);
264 EQL ( 6, 7, "%+6ld", li);
265 EQL ( 6, 7, "%-6ld", li);
266 EQL ( 6, 7, "%06ld", li);
267 #elif __SIZEOF_LONG__ == 4
268 EQL (11, 12, "%11ld", li);
269 EQL (11, 12, "%+11ld", li);
270 EQL (11, 12, "%-11ld", li);
271 EQL (11, 12, "%011ld", li);
272 #elif __SIZEOF_LONG__ == 8
273 EQL (20, 21, "%20ld", li);
274 EQL (20, 21, "%+20ld", li);
275 EQL (20, 21, "%-20ld", li);
276 EQL (20, 21, "%020ld", li);
277 #endif
279 /* Verify that the output of a directive with an unknown argument
280 is correctly determined at compile time to be in the expected
281 range. */
283 /* +---------------------------- expected minimum return value */
284 /* | +------------------------ expected maximum return value */
285 /* | | +-------------------- destination size */
286 /* | | | +----------------- format string */
287 /* | | | | +----- variable argument(s) */
288 /* | | | | | */
289 /* V V V V V */
290 RNG ( 1, 4, 5, "%hhi", i);
291 RNG ( 1, 3, 4, "%hhu", i);
293 RNG ( 3, 4, 5, "%hhi", IR (-128, -10));
294 RNG ( 2, 4, 5, "%hhi", IR (-128, -1));
295 RNG ( 1, 4, 5, "%hhi", IR (-128, 0));
297 RNG ( 1, 4, 5, "%1hhi", IR (-128, 0));
298 RNG ( 1, 4, 5, "%2hhi", IR (-128, 0));
299 RNG ( 1, 4, 5, "%3hhi", IR (-128, 0));
300 RNG ( 1, 4, 5, "%4hhi", IR (-128, 0));
301 RNG ( 1, 5, 6, "%5hhi", IR (-128, 0));
302 RNG ( 1, 6, 7, "%6hhi", IR (-128, 0));
303 RNG ( 2, 6, 7, "%6hhi", IR (-128, 10));
305 RNG ( 0, 1, 2, "%.hhi", IR ( 0, 1));
306 RNG ( 0, 1, 2, "%.0hhi", IR ( 0, 1));
307 RNG ( 0, 1, 2, "%0.0hhi", IR ( 0, 1)); /* { dg-warning ".0. flag ignored with precision" } */
308 RNG ( 0, 1, 2, "%*.0hhi", 0, IR ( 0, 1));
310 RNG ( 1, 2, 3, "%hhi", IR (1024, 1034));
311 RNG ( 1, 4, 5, "%hhi", IR (1024, 2048));
312 RNG ( 2, 3, 4, "%hhi", IR (1034, 1151));
314 RNG ( 1, 2, 3, "%hhu", IR (1024, 1034));
315 RNG ( 1, 3, 4, "%hhu", IR (1024, 2048));
316 RNG ( 2, 3, 4, "%hhu", IR (1034, 1151));
318 #if __SIZEOF_SHORT__ == 2
319 RNG ( 1, 6, 7, "%hi", i);
320 RNG ( 1, 5, 6, "%hu", i);
322 RNG ( 1, 6, 7, "%.1hi", i);
323 RNG ( 2, 6, 7, "%.2hi", i);
324 RNG ( 3, 6, 7, "%.3hi", i);
325 RNG ( 4, 6, 7, "%.4hi", i);
326 RNG ( 5, 6, 7, "%.5hi", i);
327 RNG ( 6, 7, 8, "%.6hi", i);
328 RNG ( 7, 8, 9, "%.7hi", i);
330 #elif __SIZEOF_SHORT__ == 4
331 RNG ( 1, 11, 12, "%hi", i);
332 RNG ( 1, 10, 11, "%hu", i);
334 RNG ( 1, 11, 12, "%.1hi", i);
335 RNG ( 2, 11, 12, "%.2hi", i);
336 RNG ( 3, 11, 12, "%.3hi", i);
337 RNG ( 4, 11, 12, "%.4hi", i);
338 RNG ( 5, 11, 12, "%.5hi", i);
339 RNG ( 6, 11, 12, "%.6hi", i);
340 RNG ( 7, 11, 12, "%.7hi", i);
341 RNG ( 8, 11, 12, "%.8hi", i);
342 RNG ( 9, 11, 12, "%.9hi", i);
343 RNG (10, 11, 12, "%.10hi", i);
344 RNG (11, 12, 13, "%.11hi", i);
345 RNG (12, 13, 14, "%.12hi", i);
346 RNG (13, 14, 15, "%.13hi", i);
347 #endif
349 #if __SIZEOF_INT__ == 2
350 RNG ( 1, 6, 7, "%i", i);
351 RNG ( 1, 5, 6, "%u", i);
353 RNG ( 1, 6, 7, "%.1i", i);
354 RNG ( 2, 6, 7, "%.2i", i);
355 RNG ( 3, 6, 7, "%.3i", i);
356 RNG ( 4, 6, 7, "%.4i", i);
357 RNG ( 5, 6, 7, "%.5i", i);
358 RNG ( 6, 7, 8, "%.6i", i);
359 RNG ( 7, 8, 9, "%.7i", i);
360 #elif __SIZEOF_INT__ == 4
361 RNG ( 1, 11, 12, "%i", i);
362 RNG ( 1, 10, 11, "%u", i);
364 RNG ( 1, 11, 12, "%.1i", i);
365 RNG ( 2, 11, 12, "%.2i", i);
366 RNG ( 3, 11, 12, "%.3i", i);
367 RNG ( 4, 11, 12, "%.4i", i);
368 RNG ( 5, 11, 12, "%.5i", i);
369 RNG ( 6, 11, 12, "%.6i", i);
370 RNG ( 7, 11, 12, "%.7i", i);
371 RNG ( 8, 11, 12, "%.8i", i);
372 RNG ( 9, 11, 12, "%.9i", i);
373 RNG (10, 11, 12, "%.10i", i);
374 RNG (11, 12, 13, "%.11i", i);
375 RNG (12, 13, 14, "%.12i", i);
376 RNG (13, 14, 15, "%.13i", i);
377 #elif __SIZEOF_INT__ == 8
378 RNG ( 1, 20, 21, "%i", i);
379 RNG ( 1, 19, 20, "%u", i);
380 #endif
382 #if __SIZEOF_LONG__ == 4
383 RNG ( 1, 11, 12, "%li", li);
384 RNG ( 1, 10, 11, "%lu", li);
386 RNG ( 1, 11, 12, "%.1li", li);
387 RNG ( 2, 11, 12, "%.2li", li);
388 RNG ( 3, 11, 12, "%.3li", li);
389 RNG ( 4, 11, 12, "%.4li", li);
390 RNG ( 5, 11, 12, "%.5li", li);
391 RNG ( 6, 11, 12, "%.6li", li);
392 RNG ( 7, 11, 12, "%.7li", li);
393 RNG ( 8, 11, 12, "%.8li", li);
394 RNG ( 9, 11, 12, "%.9li", li);
395 RNG (10, 11, 12, "%.10li", li);
396 RNG (11, 12, 13, "%.11li", li);
397 RNG (12, 13, 14, "%.12li", li);
398 RNG (13, 14, 15, "%.13li", li);
399 #elif __SIZEOF_LONG__ == 8
400 RNG ( 1, 20, 21, "%li", li);
401 RNG ( 1, 19, 20, "%lu", li);
402 #endif
405 static void __attribute__ ((noinline, noclone))
406 test_x (unsigned char uc, unsigned short us, unsigned ui)
408 EQL ( 1, 2, "%hhx", 0);
409 EQL ( 2, 3, "%2hhx", 0);
410 EQL ( 2, 3, "%02hhx", 0);
411 EQL ( 2, 3, "%#02hhx", 0);
413 EQL ( 1, 2, "%hhx", 1);
414 EQL ( 2, 3, "%2hhx", 1);
415 EQL ( 2, 3, "%02hhx", 1);
416 EQL ( 3, 4, "%#02hhx", 1);
418 EQL ( 2, 3, "%2hhx", uc);
419 EQL ( 2, 3, "%02hhx", uc);
420 EQL ( 5, 6, "%#05hhx", uc);
422 EQL ( 2, 3, "%2hhx", us);
423 EQL ( 2, 3, "%02hhx", us);
424 EQL ( 5, 6, "%#05hhx", us);
426 EQL ( 2, 3, "%2hhx", ui);
427 EQL ( 2, 3, "%02hhx", ui);
428 EQL ( 5, 6, "%#05hhx", ui);
430 EQL ( 1, 2, "%x", 0);
431 EQL ( 1, 2, "%#x", 0);
432 EQL ( 1, 2, "%#0x", 0);
433 EQL ( 1, 2, "%x", 1);
434 EQL ( 1, 2, "%x", 0xf);
435 EQL ( 2, 3, "%x", 0x10);
436 EQL ( 2, 3, "%x", 0xff);
437 EQL ( 3, 4, "%x", 0x100);
439 EQL (11, 12, "%02x:%02x:%02x:%02x", 0xde, 0xad, 0xbe, 0xef);
441 /* The following would be optimized if the range information of
442 the variable's type was made available. Alas, it's lost due
443 to the promotion of the actual argument (unsined char) to
444 the type of the "formal" argument (int in the case of the
445 ellipsis).
446 EQL (11, 12, "%02x:%02x:%02x:%02x", uc, uc, uc, uc);
448 EQL (11, 12, "%02hhx:%02hhx:%02hhx:%02hhx", uc, uc, uc, uc);
450 #if __SIZEOF_SHORT__ == 2
451 EQL ( 4, 5, "%04hx", us);
452 EQL ( 9, 10, "%04hx:%04hx", us, us);
453 EQL (14, 15, "%04hx:%04hx:%04hx", us, us, us);
454 EQL (19, 20, "%04hx:%04hx:%04hx:%04hx", us, us, us, us);
455 #endif
457 #if __SIZEOF_INT__ == 2
458 EQL ( 4, 5, "%04x", ui);
459 EQL ( 6, 7, "%#06x", ui);
460 #elif __SIZEOF_INT__ == 4
461 EQL ( 8, 9, "%08x", ui);
462 EQL (10, 10 + 1, "%#010x", ui);
463 #elif __SIZEOF_INT__ == 8
464 EQL (16, 17, "%016x", ui);
465 EQL (18, 19, "%#018x", ui);
466 #endif
469 static void __attribute__ ((noinline, noclone))
470 test_a_double (double d)
472 EQL ( 6, 7, "%.0a", 0.0); /* 0x0p+0 */
473 EQL ( 6, 7, "%.0a", 1.0); /* 0x8p-3 */
474 EQL ( 6, 7, "%.0a", 2.0); /* 0x8p-2 */
475 EQL ( 8, 9, "%.1a", 3.0); /* 0xc.0p-2 */
476 EQL ( 9, 10, "%.2a", 4.0); /* 0x8.00p-1 */
477 EQL (10, 11, "%.3a", 5.0); /* 0xa.000p-1 */
479 EQL (11, 12, "%.*a", 4, 6.0); /* 0xc.0000p-1 */
480 EQL (12, 13, "%.*a", 5, 7.0); /* 0xe.00000p-1 */
481 /* d is in [ 0, -DBL_MAX ] */
482 RNG ( 6, 10, 11, "%.0a", d); /* 0x0p+0 ... -0x2p+1023 */
483 RNG ( 6, 12, 13, "%.1a", d); /* 0x0p+0 ... -0x2.0p+1023 */
484 RNG ( 6, 13, 14, "%.2a", d); /* 0x0p+0 ... -0x2.00p+1023 */
487 static void __attribute__ ((noinline, noclone))
488 test_a_long_double (void)
490 EQL ( 6, 7, "%.0La", 0.0L); /* 0x0p+0 */
491 EQL ( 6, 7, "%.0La", 1.0L); /* 0x8p-3 */
492 EQL ( 6, 7, "%.0La", 2.0L); /* 0x8p-2 */
493 EQL ( 8, 9, "%.1La", 3.0L); /* 0xc.0p-2 */
494 EQL ( 9, 10, "%.2La", 4.0L); /* 0xa.00p-1 */
497 static void __attribute__ ((noinline, noclone))
498 test_e_double (double d)
500 EQL (12, 13, "%e", 1.0e0);
501 EQL (13, 14, "%e", -1.0e0);
502 EQL (12, 13, "%e", 1.0e+1);
503 EQL (13, 14, "%e", -1.0e+1);
504 EQL (12, 13, "%e", 1.0e+12);
505 EQL (13, 14, "%e", -1.0e+12);
506 EQL (13, 14, "%e", 1.0e+123);
507 EQL (14, 15, "%e", -1.0e+123);
509 EQL (12, 13, "%e", 9.999e+99);
510 EQL (12, 13, "%e", 9.9999e+99);
511 EQL (12, 13, "%e", 9.99999e+99);
513 /* The actual output of the following directive depends on the rounding
514 mode. */
515 /* EQL (12, "%e", 9.9999994e+99); */
517 EQL (12, 13, "%e", 1.0e-1);
518 EQL (12, 13, "%e", 1.0e-12);
519 EQL (13, 14, "%e", 1.0e-123);
521 RNG (12, 14, 15, "%e", d);
522 RNG ( 5, 7, 8, "%.e", d);
523 RNG ( 5, 7, 8, "%.0e", d);
524 RNG ( 7, 9, 10, "%.1e", d);
525 RNG ( 8, 10, 11, "%.2e", d);
526 RNG ( 9, 11, 12, "%.3e", d);
527 RNG (10, 12, 13, "%.4e", d);
528 RNG (11, 13, 14, "%.5e", d);
529 RNG (12, 14, 15, "%.6e", d);
530 RNG (13, 15, 16, "%.7e", d);
532 RNG (4006, 4008, 4009, "%.4000e", d);
534 RNG ( 5, 7, 8, "%.*e", 0, d);
535 RNG ( 7, 9, 10, "%.*e", 1, d);
536 RNG ( 8, 10, 11, "%.*e", 2, d);
537 RNG ( 9, 11, 12, "%.*e", 3, d);
538 RNG (10, 12, 13, "%.*e", 4, d);
539 RNG (11, 13, 14, "%.*e", 5, d);
540 RNG (12, 14, 15, "%.*e", 6, d);
541 RNG (13, 15, 16, "%.*e", 7, d);
543 RNG (4006, 4008, 4009, "%.*e", 4000, d);
546 static void __attribute__ ((noinline, noclone))
547 test_e_long_double (long double d)
549 EQL (12, 13, "%Le", 1.0e0L);
550 EQL (13, 14, "%Le", -1.0e0L);
551 EQL (12, 13, "%Le", 1.0e+1L);
552 EQL (13, 14, "%Le", -1.0e+1L);
553 EQL (12, 13, "%Le", 1.0e+12L);
554 EQL (13, 14, "%Le", -1.0e+12L);
555 EQL (13, 14, "%Le", 1.0e+123L);
556 EQL (14, 15, "%Le", -1.0e+123L);
558 EQL (12, 13, "%Le", 9.999e+99L);
559 EQL (12, 13, "%Le", 9.9999e+99L);
560 EQL (12, 13, "%Le", 9.99999e+99L);
562 #if __DBL_DIG__ < __LDBL_DIG__
563 EQL (12, 13, "%Le", 9.999999e+99L);
564 #else
565 RNG (12, 13, 14, "%Le", 9.999999e+99L);
566 #endif
568 /* The actual output of the following directive depends on the rounding
569 mode. */
570 /* EQL (12, "%Le", 9.9999994e+99L); */
572 EQL (12, 13, "%Le", 1.0e-1L);
573 EQL (12, 13, "%Le", 1.0e-12L);
574 EQL (13, 14, "%Le", 1.0e-123L);
576 EQL ( 6, 7, "%.0Le", 1.0e-111L);
577 EQL ( 8, 9, "%.1Le", 1.0e-111L);
578 EQL (19, 20, "%.12Le", 1.0e-112L);
579 EQL (20, 21, "%.13Le", 1.0e-113L);
581 /* The following correspond to the double results plus 1 for the upper
582 bound accounting for the four-digit exponent. */
583 RNG (12, 15, 16, "%Le", d); /* 0.000000e+00 ... -1.189732e+4932 */
584 RNG ( 5, 8, 9, "%.Le", d);
585 RNG ( 5, 9, 10, "%.0Le", d);
586 RNG ( 7, 10, 11, "%.1Le", d); /* 0.0e+00 ... -1.2e+4932 */
587 RNG ( 8, 11, 12, "%.2Le", d); /* 0.00e+00 ... -1.19e+4932 */
588 RNG ( 9, 12, 13, "%.3Le", d);
589 RNG (10, 13, 14, "%.4Le", d);
590 RNG (11, 14, 15, "%.5Le", d);
591 RNG (12, 15, 16, "%.6Le", d); /* same as plain "%Le" */
592 RNG (13, 16, 17, "%.7Le", d); /* 0.0000000e+00 ... -1.1897315e+4932 */
594 RNG ( 5, 9, 10, "%.*Le", 0, d);
595 RNG ( 7, 10, 11, "%.*Le", 1, d);
596 RNG ( 8, 11, 12, "%.*Le", 2, d);
597 RNG ( 9, 12, 13, "%.*Le", 3, d);
598 RNG (10, 13, 14, "%.*Le", 4, d);
599 RNG (11, 14, 15, "%.*Le", 5, d);
600 RNG (12, 15, 16, "%.*Le", 6, d);
601 RNG (13, 16, 17, "%.*Le", 7, d);
604 static void __attribute__ ((noinline, noclone))
605 test_f_double (double d)
607 EQL ( 8, 9, "%f", 0.0e0);
608 EQL ( 8, 9, "%f", 0.1e0);
609 EQL ( 8, 9, "%f", 0.12e0);
610 EQL ( 8, 9, "%f", 0.123e0);
611 EQL ( 8, 9, "%f", 0.1234e0);
612 EQL ( 8, 9, "%f", 0.12345e0);
613 EQL ( 8, 9, "%f", 0.123456e0);
614 EQL ( 8, 9, "%f", 1.234567e0);
616 EQL ( 9, 10, "%f", 1.0e+1);
617 EQL ( 20, 21, "%f", 1.0e+12);
618 EQL (130, 131, "%f", 1.0e+123);
620 EQL ( 8, 9, "%f", 1.0e-1);
621 EQL ( 8, 9, "%f", 1.0e-12);
622 EQL ( 8, 9, "%f", 1.0e-123);
624 RNG ( 8, 317, 318, "%f", d);
627 static void __attribute__ ((noinline, noclone))
628 test_f_long_double (void)
630 EQL ( 8, 9, "%Lf", 0.0e0L);
631 EQL ( 8, 9, "%Lf", 0.1e0L);
632 EQL ( 8, 9, "%Lf", 0.12e0L);
633 EQL ( 8, 9, "%Lf", 0.123e0L);
634 EQL ( 8, 9, "%Lf", 0.1234e0L);
635 EQL ( 8, 9, "%Lf", 0.12345e0L);
636 EQL ( 8, 9, "%Lf", 0.123456e0L);
637 EQL ( 8, 9, "%Lf", 1.234567e0L);
639 EQL ( 9, 10, "%Lf", 1.0e+1L);
640 EQL ( 20, 21, "%Lf", 1.0e+12L);
641 EQL (130, 131, "%Lf", 1.0e+123L);
643 EQL ( 8, 9, "%Lf", 1.0e-1L);
644 EQL ( 8, 9, "%Lf", 1.0e-12L);
645 EQL ( 8, 9, "%Lf", 1.0e-123L);
648 static void __attribute__ ((noinline, noclone))
649 test_g_double (double d)
651 /* Numbers exactly representable in binary floating point. */
652 EQL ( 1, 2, "%g", 0.0);
653 EQL ( 3, 4, "%g", 1.0 / 2);
654 EQL ( 4, 5, "%g", 1.0 / 4);
655 EQL ( 5, 6, "%g", 1.0 / 8);
656 EQL ( 6, 7, "%g", 1.0 / 16);
657 EQL ( 7, 8, "%g", 1.0 / 32);
658 EQL ( 8, 9, "%g", 1.0 / 64);
659 EQL ( 9, 10, "%g", 1.0 / 128);
660 EQL ( 10, 11, "%g", 1.0 / 256);
661 EQL ( 10, 11, "%g", 1.0 / 512);
663 /* Numbers that are not exactly representable. */
664 RNG ( 3, 8, 9, "%g", 0.1);
665 RNG ( 4, 8, 9, "%g", 0.12);
666 RNG ( 5, 8, 9, "%g", 0.123);
667 RNG ( 6, 8, 9, "%g", 0.1234);
668 RNG ( 7, 8, 9, "%g", 0.12345);
669 RNG ( 8, 8, 9, "%g", 0.123456);
671 RNG ( 4, 7, 8, "%g", 0.123e+1);
672 EQL ( 8, 9, "%g", 0.123e+12);
673 RNG ( 9, 12, 13, "%g", 0.123e+134);
675 RNG ( 1, 13, 14, "%g", d);
676 RNG ( 1, 7, 8, "%.g", d);
677 RNG ( 1, 7, 8, "%.0g", d);
678 RNG ( 1, 7, 8, "%.1g", d);
679 RNG ( 1, 9, 10, "%.2g", d);
680 RNG ( 1, 10, 11, "%.3g", d);
681 RNG ( 1, 11, 12, "%.4g", d);
682 RNG ( 1, 12, 13, "%.5g", d);
683 RNG ( 1, 13, 14, "%.6g", d);
684 RNG ( 1, 14, 15, "%.7g", d);
685 RNG ( 1, 15, 16, "%.8g", d);
687 RNG ( 1,310,311, "%.9999g", d);
689 RNG ( 1, 7, 8, "%.*g", 0, d);
690 RNG ( 1, 7, 8, "%.*g", 1, d);
691 RNG ( 1, 9, 10, "%.*g", 2, d);
692 RNG ( 1, 10, 11, "%.*g", 3, d);
693 RNG ( 1, 11, 12, "%.*g", 4, d);
694 RNG ( 1, 12, 13, "%.*g", 5, d);
695 RNG ( 1, 13, 14, "%.*g", 6, d);
696 RNG ( 1, 14, 15, "%.*g", 7, d);
697 RNG ( 1, 15, 16, "%.*g", 8, d);
698 RNG ( 1,310,311, "%.*g", 9999, d);
701 static void __attribute__ ((noinline, noclone))
702 test_g_long_double (void)
704 /* Numbers exactly representable in binary floating point. */
705 EQL ( 1, 2, "%Lg", 0.0L);
706 EQL ( 3, 4, "%Lg", 1.0L / 2);
707 EQL ( 4, 5, "%Lg", 1.0L / 4);
708 EQL ( 5, 6, "%Lg", 1.0L / 8);
709 EQL ( 6, 7, "%Lg", 1.0L / 16);
710 EQL ( 7, 8, "%Lg", 1.0L / 32);
711 EQL ( 8, 9, "%Lg", 1.0L / 64);
712 EQL ( 9, 10, "%Lg", 1.0L / 128);
713 EQL ( 10, 11, "%Lg", 1.0L / 256);
714 EQL ( 10, 11, "%Lg", 1.0L / 512);
716 /* Numbers that are not exactly representable. */
717 RNG ( 3, 8, 9, "%Lg", 0.1L);
718 RNG ( 4, 8, 9, "%Lg", 0.12L);
719 RNG ( 5, 8, 9, "%Lg", 0.123L);
720 RNG ( 6, 8, 9, "%Lg", 0.1234L);
721 RNG ( 7, 8, 9, "%Lg", 0.12345L);
722 RNG ( 8, 8, 9, "%Lg", 0.123456L);
724 RNG ( 4, 7, 8, "%Lg", 0.123e+1L);
725 EQL ( 8, 9, "%Lg", 0.123e+12L);
726 RNG ( 9, 12, 13, "%Lg", 0.123e+134L);
729 static void __attribute__ ((noinline, noclone))
730 test_s (int i)
732 EQL ( 0, 1, "%s", "");
733 EQL ( 0, 1, "%s", "\0");
734 EQL ( 1, 2, "%1s", "");
735 EQL ( 1, 2, "%s", "1");
736 EQL ( 2, 3, "%2s", "");
737 EQL ( 2, 3, "%s", "12");
738 EQL ( 2, 3, "%s%s", "12", "");
739 EQL ( 2, 3, "%s%s", "", "12");
740 EQL ( 2, 3, "%s%s", "1", "2");
741 EQL ( 3, 4, "%3s", "");
742 EQL ( 3, 4, "%3s", "1");
743 EQL ( 3, 4, "%3s", "12");
744 EQL ( 3, 4, "%3s", "123");
745 EQL ( 3, 4, "%3.3s", "1");
746 EQL ( 3, 4, "%3.3s", "12");
747 EQL ( 3, 4, "%3.3s", "123");
748 EQL ( 3, 4, "%3.3s", "1234");
749 EQL ( 3, 4, "%3.3s", "12345");
750 EQL ( 3, 4, "%s %s", "1", "2");
751 EQL ( 4, 5, "%s %s", "12", "3");
752 EQL ( 5, 6, "%s %s", "12", "34");
753 EQL ( 5, 6, "[%s %s]", "1", "2");
754 EQL ( 6, 7, "[%s %s]", "12", "3");
755 EQL ( 7, 8, "[%s %s]", "12", "34");
757 /* Verify the result of a conditional expression involving string
758 literals is in the expected range of their lengths. */
759 RNG ( 0, 3, 4, "%-s", i ? "" : "123");
760 RNG ( 1, 4, 5, "%-s", i ? "1" : "1234");
761 RNG ( 2, 5, 6, "%-s", i ? "12" : "12345");
762 RNG ( 3, 6, 7, "%-s", i ? "123" : "123456");
765 int main (void)
767 test_c ('?');
768 test_d_i (0xdeadbeef, 0xdeadbeefL);
769 test_x ('?', 0xdead, 0xdeadbeef);
771 test_a_double (0.0);
772 test_e_double (0.0);
773 test_f_double (0.0);
774 test_g_double (0.0);
776 test_a_long_double ();
777 test_e_long_double (0.0);
778 test_f_long_double ();
779 test_g_long_double ();
781 test_s (0);
783 if (nfails)
785 __builtin_printf ("%u out of %u tests failed\n", nfails, ntests);
786 __builtin_abort ();
789 return 0;