Debian Codegraph @

gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf.c

raw
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 { target c99_runtime } } */
7 /* { dg-do link { target { ! c99_runtime } } } */
8 /* { dg-skip-if "not IEEE float layout" { "pdp11-*-*" } } */
9 /* { dg-additional-options "-O2 -Wall -Wno-pedantic -fprintf-return-value" } */
10
11 #ifndef LINE
12 # define LINE 0
13 #endif
14
15 #if __STDC_VERSION__ < 199901L
16 # define __func__ __FUNCTION__
17 #endif
18
19 typedef __SIZE_TYPE__ size_t;
20
21 unsigned ntests;
22 unsigned nfails;
23
24 void __attribute__ ((noclone, noinline))
25 checkv (const char *func, int line, int res, int min, int max,
26 char *dst, const char *fmt, __builtin_va_list va)
27 {
28 int n = __builtin_vsprintf (dst, fmt, va);
29 int len = __builtin_strlen (dst);
30
31 ++ntests;
32
33 int fail = 0;
34 if (n != res)
35 {
36 __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" "
37 "doesn't match function call return value: ",
38 func, line, fmt, dst);
39 if (min == max)
40 __builtin_printf ("%i != %i\n", n, min);
41 else
42 __builtin_printf ("%i not in [%i, %i]\n", n, min, max);
43
44 fail = 1;
45 }
46 else
47 {
48 if (len < min || max < len)
49 {
50 __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" "
51 "doesn't match output length: ",
52 func, line, fmt, dst);
53
54 if (min == max)
55 __builtin_printf ("%i != %i\n", len, min);
56 else
57 __builtin_printf ("%i not in [%i, %i]\n", len, min, max);
58
59 fail = 1;
60 }
61 else if (min == max)
62 __builtin_printf ("PASS: %s:%i: \"%s\" result %i: \"%s\"\n",
63 func, line, fmt, n, dst);
64 else
65 __builtin_printf ("PASS: %s:%i: \"%s\" result %i in [%i, %i]: \"%s\"\n",
66 func, line, fmt, n, min, max, dst);
67 }
68
69 if (fail)
70 ++nfails;
71 }
72
73 void __attribute__ ((noclone, noinline))
74 check (const char *func, int line, int res, int min, int max,
75 char *dst, const char *fmt, ...)
76 {
77 __builtin_va_list va;
78 __builtin_va_start (va, fmt);
79 checkv (func, line, res, min, max, dst, fmt, va);
80 __builtin_va_end (va);
81 }
82
83 char buffer[4100];
84 char* volatile dst = buffer;
85 char* ptr = buffer;
86
87 #define concat(a, b) a ## b
88 #define CAT(a, b) concat (a, b)
89
90 #if __OPTIMIZE__
91 /* With optimization references to the following undefined symbol which
92 is unique for each test case are expected to be eliminated. */
93 # define TEST_FAILURE(line, ignore1, ignore2, ignore3) \
94 do { \
95 extern void CAT (failure_on_line_, line)(void); \
96 CAT (failure_on_line_, line)(); \
97 } while (0)
98 #else
99 /* The test is run by DejaGnu with optimization enabled. When it's run
100 with it disabled (i.e., at -O0) each test case is verified at runtime
101 and the test aborts just before exiting if any of them failed. */
102 # define TEST_FAILURE(line, result, min, max) \
103 if (min == max) \
104 __builtin_printf ("FAIL: %s:%i: expected %i, got %i\n", \
105 __func__, line, min, result); \
106 else \
107 __builtin_printf ("FAIL: %s:%i: expected range [%i, %i], got %i\n", \
108 __func__, line, min, max, result);
109 #endif
110
111 /* Verify that the result is exactly equal to RES. */
112 #define EQL(expect, size, fmt, ...) \
113 if (!LINE || LINE == __LINE__) \
114 do { \
115 char *buf = (size) < 0 ? ptr : buffer + sizeof buffer - (size); \
116 int result = __builtin_sprintf (buf, fmt, __VA_ARGS__); \
117 if (result != expect) \
118 { \
119 TEST_FAILURE (__LINE__, expect, expect, result); \
120 } \
121 check (__func__, __LINE__, result, expect, expect, dst, fmt, \
122 __VA_ARGS__); \
123 } while (0)
124
125 /* Verify that the result is in the range [MIN, MAX]. */
126 #define RNG(min, max, size, fmt, ...) \
127 if (!LINE || LINE == __LINE__) \
128 do { \
129 char *buf = (size) < 0 ? ptr : buffer + sizeof buffer - (size); \
130 int result = __builtin_sprintf (buf, fmt, __VA_ARGS__); \
131 if (result < min || max < result) \
132 { \
133 TEST_FAILURE (__LINE__, min, max, result); \
134 } \
135 check (__func__, __LINE__, result, min, max, dst, fmt, \
136 __VA_ARGS__); \
137 } while (0)
138
139 static void __attribute__ ((noinline, noclone))
140 test_c (char c)
141 {
142 EQL (1, 2, "%c", c);
143 EQL (1, -1, "%c", c);
144 EQL (1, 2, "%1c", c);
145 EQL (1, -1, "%1c", c);
146 EQL (1, 2, "%*c", 1, c);
147 EQL (1, -1, "%*c", 1, c);
148 EQL (2, 3, "%c%c", '1', '2');
149 EQL (2, -1, "%c%c", '1', '2');
150 EQL (3, 4, "%3c", c);
151 EQL (3, -1, "%3c", c);
152 EQL (3, 4, "%*c", 3, c);
153 EQL (3, -1, "%*c", 3, c);
154
155 EQL (3, 4, "%*c%*c", 2, c, 1, c);
156 EQL (3, 4, "%*c%*c", 1, c, 2, c);
157 EQL (3, 4, "%c%c%c", '1', '2', '3');
158 EQL (3, 4, "%*c%c%c", 1, '1', '2', '3');
159 EQL (3, 4, "%*c%*c%c", 1, '1', 1, '2', '3');
160 EQL (3, 4, "%*c%*c%*c", 1, '1', 1, '2', 1, '3');
161
162 EQL (3, -1, "%*c%*c", 2, c, 1, c);
163 EQL (3, -1, "%*c%*c", 1, c, 2, c);
164 EQL (3, -1, "%c%c%c", '1', '2', '3');
165 EQL (3, -1, "%*c%c%c", 1, '1', '2', '3');
166 EQL (3, -1, "%*c%*c%c", 1, '1', 1, '2', '3');
167 EQL (3, -1, "%*c%*c%*c", 1, '1', 1, '2', 1, '3');
168
169 EQL (4, 5, "%c%c %c", '1', '2', '3');
170 EQL (5, 6, "%c %c %c", '1', '2', '3');
171 EQL (5, 6, "%c %c %c", c, c, c);
172 }
173
174 /* Generate a pseudo-random unsigned value. */
175
176 unsigned __attribute__ ((noclone, noinline))
177 unsigned_value (void)
178 {
179 extern int rand ();
180 return rand ();
181 }
182
183 /* Generate a pseudo-random signed value. */
184
185 int __attribute__ ((noclone, noinline))
186 int_value (void)
187 {
188 extern int rand ();
189 return rand ();
190 }
191
192 /* Generate an unsigned char value in the specified range. */
193
194 static unsigned char
195 uchar_range (unsigned min, unsigned max)
196 {
197 unsigned x = unsigned_value ();
198 if (x < min || max < x)
199 x = min;
200 return x;
201 }
202
203 /* Generate a signed int value in the specified range. */
204
205 static int
206 int_range (int min, int max)
207 {
208 int val = int_value ();
209 if (val < min || max < val)
210 val = min;
211 return val;
212 }
213
214 #define IR(min, max) int_range (min, max)
215
216 static void __attribute__ ((noinline, noclone))
217 test_d_i (int i, long li)
218 {
219 /* +-------------------------- expected return value */
220 /* | +---------------------- destination size */
221 /* | | +------------------- format string */
222 /* | | | +-- variable argument(s) */
223 /* | | | | */
224 /* V V V V */
225 EQL ( 1, 2, "%d", 0);
226 EQL ( 2, 3, "%d%d", 0, 1);
227 EQL ( 3, 4, "%d%d", 9, 10);
228 EQL ( 4, 5, "%d%d", 11, 12);
229 EQL ( 5, 6, "%d:%d", 12, 34);
230 EQL ( 5, 6, "%d", 12345);
231 EQL ( 6, 7, "%d", -12345);
232 EQL (15, 16, "%d:%d:%d:%d", 123, 124, 125, 126);
233
234 EQL ( 1, 2, "%i", uchar_range (0, 9));
235 EQL ( 1, -1, "%i", uchar_range (0, 9));
236
237 /* The range information available to passes other than the Value
238 Range Propoagation pass itself is so bad that the following two
239 tests fail (the range seen in the test below is [0, 99] rather
240 than [10, 99].
241 EQL ( 2, 3, "%i", uchar_range (10, 99));
242 EQL ( 3, 4, "%i", uchar_range (100, 199));
243 */
244
245 /* Verify that the width allows the return value in the following
246 calls can be folded despite the unknown value of the argument. */
247 #if __SIZEOF_INT__ == 2
248 EQL ( 6, 7, "%6d", i);
249 EQL ( 6, 7, "%+6d", i);
250 EQL ( 6, 7, "%-6d", i);
251 EQL ( 6, 7, "%06d", i);
252 #elif __SIZEOF_INT__ == 4
253 EQL (11, 12, "%11d", i);
254 EQL (11, 12, "%+11d", i);
255 EQL (11, 12, "%-11d", i);
256 EQL (11, 12, "%011d", i);
257 #elif __SIZEOF_INT__ == 8
258 EQL (20, 21, "%20d", i);
259 EQL (20, 21, "%+20d", i);
260 EQL (20, 21, "%-29d", i);
261 EQL (20, 21, "%020d", i);
262 #endif
263
264 #if __SIZEOF_LONG__ == 2
265 EQL ( 6, 7, "%6ld", li);
266 EQL ( 6, 7, "%+6ld", li);
267 EQL ( 6, 7, "%-6ld", li);
268 EQL ( 6, 7, "%06ld", li);
269 #elif __SIZEOF_LONG__ == 4
270 EQL (11, 12, "%11ld", li);
271 EQL (11, 12, "%+11ld", li);
272 EQL (11, 12, "%-11ld", li);
273 EQL (11, 12, "%011ld", li);
274 #elif __SIZEOF_LONG__ == 8
275 EQL (20, 21, "%20ld", li);
276 EQL (20, 21, "%+20ld", li);
277 EQL (20, 21, "%-20ld", li);
278 EQL (20, 21, "%020ld", li);
279 #endif
280
281 /* Verify that the output of a directive with an unknown argument
282 is correctly determined at compile time to be in the expected
283 range. */
284
285 /* +---------------------------- expected minimum return value */
286 /* | +------------------------ expected maximum return value */
287 /* | | +-------------------- destination size */
288 /* | | | +----------------- format string */
289 /* | | | | +----- variable argument(s) */
290 /* | | | | | */
291 /* V V V V V */
292 RNG ( 1, 4, 5, "%hhi", i);
293 RNG ( 1, 3, 4, "%hhu", i);
294
295 RNG ( 3, 4, 5, "%hhi", IR (-128, -10));
296 RNG ( 2, 4, 5, "%hhi", IR (-128, -1));
297 RNG ( 1, 4, 5, "%hhi", IR (-128, 0));
298
299 RNG ( 1, 4, 5, "%1hhi", IR (-128, 0));
300 RNG ( 1, 4, 5, "%2hhi", IR (-128, 0));
301 RNG ( 1, 4, 5, "%3hhi", IR (-128, 0));
302 RNG ( 1, 4, 5, "%4hhi", IR (-128, 0));
303 RNG ( 1, 5, 6, "%5hhi", IR (-128, 0));
304 RNG ( 1, 6, 7, "%6hhi", IR (-128, 0));
305 RNG ( 2, 6, 7, "%6hhi", IR (-128, 10));
306
307 RNG ( 0, 1, 2, "%.hhi", IR ( 0, 1));
308 RNG ( 0, 1, 2, "%.0hhi", IR ( 0, 1));
309 RNG ( 0, 1, 2, "%0.0hhi", IR ( 0, 1)); /* { dg-warning ".0. flag ignored with precision" } */
310 RNG ( 0, 1, 2, "%*.0hhi", 0, IR ( 0, 1));
311
312 RNG ( 1, 2, 3, "%hhi", IR (1024, 1034));
313 RNG ( 1, 4, 5, "%hhi", IR (1024, 2048));
314 RNG ( 2, 3, 4, "%hhi", IR (1034, 1151));
315
316 RNG ( 1, 2, 3, "%hhu", IR (1024, 1034));
317 RNG ( 1, 3, 4, "%hhu", IR (1024, 2048));
318 RNG ( 2, 3, 4, "%hhu", IR (1034, 1151));
319
320 #if __SIZEOF_SHORT__ == 2
321 RNG ( 1, 6, 7, "%hi", i);
322 RNG ( 1, 5, 6, "%hu", i);
323 RNG ( 1, 6, 7, "%.1hi", i);
324 RNG ( 2, 6, 7, "%.2hi", i);
325 RNG ( 3, 6, 7, "%.3hi", i);
326 RNG ( 4, 6, 7, "%.4hi", i);
327 RNG ( 5, 6, 7, "%.5hi", i);
328 RNG ( 6, 7, 8, "%.6hi", i);
329 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);
333
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
348
349 #if __SIZEOF_INT__ == 2
350 RNG ( 1, 6, 7, "%i", i);
351 RNG ( 1, 5, 6, "%u", i);
352
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);
363
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
381
382 #if __SIZEOF_LONG__ == 4
383 RNG ( 1, 11, 12, "%li", li);
384 RNG ( 1, 10, 11, "%lu", li);
385
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
403 }
404
405 static void __attribute__ ((noinline, noclone))
406 test_x (unsigned char uc, unsigned short us, unsigned ui)
407 {
408 EQL ( 1, 2, "%hhx", 0);
409 EQL ( 2, 3, "%2hhx", 0);
410 EQL ( 2, 3, "%02hhx", 0);
411 EQL ( 2, 3, "%#02hhx", 0);
412
413 EQL ( 1, 2, "%hhx", 1);
414 EQL ( 2, 3, "%2hhx", 1);
415 EQL ( 2, 3, "%02hhx", 1);
416 EQL ( 3, 4, "%#02hhx", 1);
417
418 EQL ( 2, 3, "%2hhx", uc);
419 EQL ( 2, 3, "%02hhx", uc);
420 EQL ( 5, 6, "%#05hhx", uc);
421
422 EQL ( 2, 3, "%2hhx", us);
423 EQL ( 2, 3, "%02hhx", us);
424 EQL ( 5, 6, "%#05hhx", us);
425
426 EQL ( 2, 3, "%2hhx", ui);
427 EQL ( 2, 3, "%02hhx", ui);
428 EQL ( 5, 6, "%#05hhx", ui);
429
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);
438
439 EQL (11, 12, "%02x:%02x:%02x:%02x", 0xde, 0xad, 0xbe, 0xef);
440
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);
447 */
448 EQL (11, 12, "%02hhx:%02hhx:%02hhx:%02hhx", uc, uc, uc, uc);
449
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
456
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
467 }
468
469 static void __attribute__ ((noinline, noclone))
470 test_a_double (double d)
471 {
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
476 /* The decimal point may be up to MB_LEN_MAX long. */
477 RNG ( 8, 13, 14, "%.1a", 3.0); /* 0xc.0p-2 */
478 RNG ( 9, 14, 15, "%.2a", 4.0); /* 0x8.00p-1 */
479 RNG (10, 15, 16, "%.3a", 5.0); /* 0xa.000p-1 */
480
481 RNG (11, 16, 17, "%.*a", 4, 6.0); /* 0xc.0000p-1 */
482 RNG (12, 17, 18, "%.*a", 5, 7.0); /* 0xe.00000p-1 */
483 /* d is in [ 0, -DBL_MAX ] */
484 RNG ( 3, 10, 11, "%.0a", d); /* inf/nan or 0x0p+0 ... -0x2p+1023 */
485 /* %a is poorly specified and allows for implementations divergence:
486 some (such as Glibc) trim redundant trailing zeros after decimal
487 point and others (e.g., Solaris) don't. */
488 RNG ( 3, 30, 31, "%.1a", d); /* inf or 0x0.0p+0 ... -0x2.0...0p+1023 */
489 RNG ( 3, 30, 31, "%.2a", d); /* inf or 0x0.00p+0 ... -0x2.00...0p+1023 */
490 }
491
492 static void __attribute__ ((noinline, noclone))
493 test_a_long_double (void)
494 {
495 EQL ( 6, 7, "%.0La", 0.0L); /* 0x0p+0 */
496 EQL ( 6, 7, "%.0La", 1.0L); /* 0x8p-3 */
497 EQL ( 6, 7, "%.0La", 2.0L); /* 0x8p-2 */
498
499 RNG ( 8, 13, 14, "%.1La", 3.0L); /* 0xc.0p-2 */
500 RNG ( 9, 14, 15, "%.2La", 4.0L); /* 0xa.00p-1 */
501 }
502
503 static void __attribute__ ((noinline, noclone))
504 test_e_double (double d)
505 {
506 RNG (12, 17, 18, "%e", 1.0e0);
507 RNG (13, 18, 19, "%e", -1.0e0);
508 RNG (12, 17, 18, "%e", 1.0e+1);
509 RNG (13, 18, 19, "%e", -1.0e+1);
510 RNG (12, 17, 18, "%e", 1.0e+12);
511 RNG (13, 18, 19, "%e", -1.0e+12);
512 RNG (13, 18, 19, "%e", 1.0e+123);
513 RNG (14, 19, 20, "%e", -1.0e+123);
514
515 RNG (12, 17, 18, "%e", 9.999e+99);
516 RNG (12, 17, 18, "%e", 9.9999e+99);
517 RNG (12, 17, 18, "%e", 9.99999e+99);
518
519 /* The actual output of the following directive depends on the rounding
520 mode. */
521 /* RNG (12, "%e", 9.9999994e+99); */
522
523 RNG (12, 17, 18, "%e", 1.0e-1);
524 RNG (12, 17, 18, "%e", 1.0e-12);
525 RNG (13, 18, 19, "%e", 1.0e-123);
526
527 RNG ( 3, 19, 20, "%e", d);
528 RNG ( 3, 11, 12, "%.e", d);
529 RNG ( 3, 12, 13, "%.0e", d);
530 RNG ( 3, 14, 15, "%.1e", d);
531 RNG ( 3, 15, 16, "%.2e", d);
532 RNG ( 3, 16, 17, "%.3e", d);
533 RNG ( 3, 17, 18, "%.4e", d);
534 RNG ( 3, 18, 19, "%.5e", d);
535 RNG ( 3, 19, 20, "%.6e", d);
536 RNG ( 3, 20, 21, "%.7e", d);
537
538 RNG ( 3, 4013, 4014, "%.4000e", d);
539
540 RNG ( 3, 7, 8, "%.*e", 0, d);
541 RNG ( 3, 14, 15, "%.*e", 1, d);
542 RNG ( 3, 15, 16, "%.*e", 2, d);
543 RNG ( 3, 16, 17, "%.*e", 3, d);
544 RNG ( 3, 17, 18, "%.*e", 4, d);
545 RNG ( 3, 18, 19, "%.*e", 5, d);
546 RNG ( 3, 19, 20, "%.*e", 6, d);
547 RNG ( 3, 20, 21, "%.*e", 7, d);
548
549 RNG ( 3, 4013, 4014, "%.*e", 4000, d);
550 RNG ( 4, 4013, 4014, "%+.*e", 4000, d);
551 RNG ( 4, 4013, 4014, "% .*e", 4000, d);
552 RNG ( 3, 4013, 4014, "%#.*e", 4000, d);
553 }
554
555 static void __attribute__ ((noinline, noclone))
556 test_e_long_double (long double d)
557 {
558 RNG (12, 17, 18, "%Le", 1.0e0L);
559 RNG (13, 18, 19, "%Le", -1.0e0L);
560 RNG (12, 17, 18, "%Le", 1.0e+1L);
561 RNG (13, 18, 19, "%Le", -1.0e+1L);
562 RNG (12, 18, 19, "%Le", 1.0e+12L);
563 RNG (13, 19, 20, "%Le", -1.0e+12L);
564 RNG (13, 19, 20, "%Le", 1.0e+123L);
565 RNG (14, 20, 21, "%Le", -1.0e+123L);
566
567 RNG (12, 18, 19, "%Le", 9.999e+99L);
568 RNG (12, 18, 19, "%Le", 9.9999e+99L);
569 RNG (12, 18, 19, "%Le", 9.99999e+99L);
570
571 #if __DBL_DIG__ < __LDBL_DIG__
572 RNG (12, 17, 18, "%Le", 9.999999e+99L);
573 #else
574 RNG (12, 18, 19, "%Le", 9.999999e+99L);
575 #endif
576
577 /* The actual output of the following directive depends on the rounding
578 mode. */
579 /* RNG (12, "%Le", 9.9999994e+99L); */
580
581 RNG (12, 17, 18, "%Le", 1.0e-1L);
582 RNG (12, 17, 18, "%Le", 1.0e-12L);
583 RNG (13, 18, 19, "%Le", 1.0e-123L);
584
585 EQL ( 6, 7, "%.0Le", 1.0e-111L);
586
587 RNG ( 8, 13, 14, "%.1Le", 1.0e-111L);
588 RNG (19, 25, 25, "%.12Le", 1.0e-112L);
589 RNG (20, 26, 27, "%.13Le", 1.0e-113L);
590
591 /* The following correspond to the double results plus 1 for the upper
592 bound accounting for the four-digit exponent. The lower bound is
593 for inf/nan. */
594 RNG ( 3, 20, 21, "%Le", d); /* inf or 0.000000e+00 ... -1.189732e+4932 */
595 RNG ( 3, 8, 9, "%.Le", d);
596 RNG ( 3, 9, 10, "%.0Le", d);
597 RNG ( 3, 15, 16, "%.1Le", d); /* inf or 0.0e+00 ... -1.2e+4932 */
598 RNG ( 3, 16, 17, "%.2Le", d); /* inf or 0.00e+00 ... -1.19e+4932 */
599 RNG ( 3, 17, 18, "%.3Le", d);
600 RNG ( 3, 18, 19, "%.4Le", d);
601 RNG ( 3, 19, 20, "%.5Le", d);
602 RNG ( 3, 20, 21, "%.6Le", d); /* same as plain "%Le" */
603 RNG ( 3, 21, 22, "%.7Le", d); /* inf or 0.0000000e+00 ... -1.1897315e+4932 */
604
605 RNG ( 3, 9, 10, "%.*Le", 0, d);
606 RNG ( 3, 15, 16, "%.*Le", 1, d);
607 RNG ( 3, 16, 17, "%.*Le", 2, d);
608 RNG ( 3, 17, 18, "%.*Le", 3, d);
609 RNG ( 3, 18, 19, "%.*Le", 4, d);
610 RNG ( 3, 19, 20, "%.*Le", 5, d);
611 RNG ( 3, 20, 21, "%.*Le", 6, d);
612 RNG ( 3, 21, 22, "%.*Le", 7, d);
613 }
614
615 static void __attribute__ ((noinline, noclone))
616 test_f_double (double d)
617 {
618 RNG ( 8, 13, 14, "%f", 0.0e0);
619 RNG ( 8, 13, 14, "%f", 0.1e0);
620 RNG ( 8, 13, 14, "%f", 0.12e0);
621 RNG ( 8, 13, 14, "%f", 0.123e0);
622 RNG ( 8, 13, 14, "%f", 0.1234e0);
623 RNG ( 8, 13, 14, "%f", 0.12345e0);
624 RNG ( 8, 13, 14, "%f", 0.123456e0);
625 RNG ( 8, 13, 14, "%f", 1.234567e0);
626
627 RNG ( 9, 14, 15, "%f", 1.0e+1);
628 RNG ( 20, 26, 27, "%f", 1.0e+12);
629 RNG (130, 136, 137, "%f", 1.0e+123);
630
631 RNG ( 8, 13, 14, "%f", 1.0e-1);
632 RNG ( 8, 13, 14, "%f", 1.0e-12);
633 RNG ( 8, 13, 14, "%f", 1.0e-123);
634
635 RNG ( 3, 322, 323, "%f", d);
636 RNG ( 4, 322, 323, "%+f", d);
637 RNG ( 4, 322, 323, "% f", d);
638 RNG ( 3, 322, 323, "%#f", d);
639 }
640
641 static void __attribute__ ((noinline, noclone))
642 test_f_long_double (void)
643 {
644 RNG ( 8, 15, 16, "%Lf", 0.0e0L);
645 RNG ( 8, 14, 15, "%Lf", 0.1e0L);
646 RNG ( 8, 14, 15, "%Lf", 0.12e0L);
647 RNG ( 8, 14, 15, "%Lf", 0.123e0L);
648 RNG ( 8, 14, 15, "%Lf", 0.1234e0L);
649 RNG ( 8, 14, 15, "%Lf", 0.12345e0L);
650 RNG ( 8, 14, 15, "%Lf", 0.123456e0L);
651 RNG ( 8, 14, 15, "%Lf", 1.234567e0L);
652
653 RNG ( 9, 15, 16, "%Lf", 1.0e+1L);
654 RNG ( 20, 26, 27, "%Lf", 1.0e+12L);
655 RNG (130, 136, 137, "%Lf", 1.0e+123L);
656
657 RNG ( 8, 14, 15, "%Lf", 1.0e-1L);
658 RNG ( 8, 14, 15, "%Lf", 1.0e-12L);
659 RNG ( 8, 14, 15, "%Lf", 1.0e-123L);
660 }
661
662 static void __attribute__ ((noinline, noclone))
663 test_g_double (double d)
664 {
665 /* Numbers exactly representable in binary floating point. */
666 EQL ( 1, 2, "%g", 0.0);
667
668 RNG ( 3, 8, 9, "%g", 1.0 / 2);
669 RNG ( 4, 9, 10, "%g", 1.0 / 4);
670 RNG ( 5, 10, 11, "%g", 1.0 / 8);
671 RNG ( 6, 11, 12, "%g", 1.0 / 16);
672 RNG ( 7, 12, 13, "%g", 1.0 / 32);
673 RNG ( 8, 13, 14, "%g", 1.0 / 64);
674 RNG ( 9, 14, 15, "%g", 1.0 / 128);
675 RNG ( 10, 15, 16, "%g", 1.0 / 256);
676 RNG ( 10, 16, 17, "%g", 1.0 / 512);
677
678 /* Numbers that are not exactly representable. */
679 RNG ( 3, 13, 14, "%g", 0.1);
680 RNG ( 4, 13, 14, "%g", 0.12);
681 RNG ( 5, 13, 14, "%g", 0.123);
682 RNG ( 6, 13, 14, "%g", 0.1234);
683 RNG ( 7, 13, 14, "%g", 0.12345);
684 RNG ( 8, 13, 14, "%g", 0.123456);
685
686 RNG ( 4, 17, 18, "%g", 0.123e+1);
687 RNG ( 8, 18, 19, "%g", 0.123e+12);
688 RNG ( 9, 19, 20, "%g", 0.123e+134);
689
690 RNG ( 1, 18, 19, "%g", d);
691 RNG ( 1, 12, 13, "%.g", d);
692 RNG ( 1, 12, 13, "%.0g", d);
693 RNG ( 1, 12, 13, "%.1g", d);
694 RNG ( 1, 14, 15, "%.2g", d);
695 RNG ( 1, 15, 16, "%.3g", d);
696 RNG ( 1, 16, 17, "%.4g", d);
697 RNG ( 1, 17, 18, "%.5g", d);
698 RNG ( 1, 18, 19, "%.6g", d);
699 RNG ( 1, 19, 20, "%.7g", d);
700 RNG ( 1, 20, 21, "%.8g", d);
701
702 RNG ( 1, 315, 316, "%.9999g", d);
703
704 RNG ( 1, 12, 13, "%.*g", 0, d);
705 RNG ( 1, 12, 13, "%.*g", 1, d);
706 RNG ( 1, 14, 15, "%.*g", 2, d);
707 RNG ( 1, 15, 16, "%.*g", 3, d);
708 RNG ( 1, 16, 17, "%.*g", 4, d);
709 RNG ( 1, 17, 18, "%.*g", 5, d);
710 RNG ( 1, 18, 19, "%.*g", 6, d);
711 RNG ( 1, 19, 20, "%.*g", 7, d);
712 RNG ( 1, 20, 21, "%.*g", 8, d);
713
714 RNG ( 1, 315, 316, "%.*g", 9999, d);
715 }
716
717 static void __attribute__ ((noinline, noclone))
718 test_g_long_double (void)
719 {
720 /* Numbers exactly representable in binary floating point. */
721 EQL ( 1, 2, "%Lg", 0.0L);
722 RNG ( 3, 8, 9, "%Lg", 1.0L / 2);
723 RNG ( 4, 9, 10, "%Lg", 1.0L / 4);
724 RNG ( 5, 10, 11, "%Lg", 1.0L / 8);
725 RNG ( 6, 11, 12, "%Lg", 1.0L / 16);
726 RNG ( 7, 12, 13, "%Lg", 1.0L / 32);
727 RNG ( 8, 13, 14, "%Lg", 1.0L / 64);
728 RNG ( 9, 14, 15, "%Lg", 1.0L / 128);
729 RNG ( 10, 15, 16, "%Lg", 1.0L / 256);
730 RNG ( 10, 15, 16, "%Lg", 1.0L / 512);
731
732 /* Numbers that are not exactly representable. */
733
734 /* The following test case results in up to 14 bytes on powerpc*-*-*
735 but only in 13 bytes on x86_64 (see PR testsuite/79293). Test just
736 for the former for simplicity. */
737 RNG ( 3, 14, 15, "%Lg", 0.1L);
738
739 RNG ( 4, 13, 14, "%Lg", 0.12L);
740 RNG ( 5, 13, 14, "%Lg", 0.123L);
741 RNG ( 6, 13, 14, "%Lg", 0.1234L);
742 RNG ( 7, 13, 14, "%Lg", 0.12345L);
743 RNG ( 8, 13, 14, "%Lg", 0.123456L);
744
745 RNG ( 4, 12, 13, "%Lg", 0.123e+1L);
746 RNG ( 8, 13, 14, "%Lg", 0.123e+12L);
747 RNG ( 9, 17, 18, "%Lg", 0.123e+134L);
748 }
749
750 static void __attribute__ ((noinline, noclone))
751 test_s (int i)
752 {
753 EQL ( 0, 1, "%s", "");
754 EQL ( 0, 1, "%s", "\0");
755 EQL ( 1, 2, "%1s", "");
756 EQL ( 1, 2, "%s", "1");
757 EQL ( 2, 3, "%2s", "");
758 EQL ( 2, 3, "%s", "12");
759 EQL ( 2, 3, "%s%s", "12", "");
760 EQL ( 2, 3, "%s%s", "", "12");
761 EQL ( 2, 3, "%s%s", "1", "2");
762 EQL ( 3, 4, "%3s", "");
763 EQL ( 3, 4, "%3s", "1");
764 EQL ( 3, 4, "%3s", "12");
765 EQL ( 3, 4, "%3s", "123");
766 EQL ( 3, 4, "%3.3s", "1");
767 EQL ( 3, 4, "%3.3s", "12");
768 EQL ( 3, 4, "%3.3s", "123");
769 EQL ( 3, 4, "%3.3s", "1234");
770 EQL ( 3, 4, "%3.3s", "12345");
771 EQL ( 3, 4, "%s %s", "1", "2");
772 EQL ( 4, 5, "%s %s", "12", "3");
773 EQL ( 5, 6, "%s %s", "12", "34");
774 EQL ( 5, 6, "[%s %s]", "1", "2");
775 EQL ( 6, 7, "[%s %s]", "12", "3");
776 EQL ( 7, 8, "[%s %s]", "12", "34");
777
778 /* Verify the result of a conditional expression involving string
779 literals is in the expected range of their lengths. */
780 RNG ( 0, 3, 4, "%-s", i ? "" : "123");
781 RNG ( 1, 4, 5, "%-s", i ? "1" : "1234");
782 RNG ( 2, 5, 6, "%-s", i ? "12" : "12345");
783 RNG ( 3, 6, 7, "%-s", i ? "123" : "123456");
784 }
785
786 static void __attribute__ ((noinline, noclone))
787 test_n (void)
788 {
789 int n;
790 EQL ( 0, 1, "%n", &n);
791 EQL ( 1, 2, "1%n", &n);
792 EQL ( 2, 3, "12%n", &n);
793 EQL ( 3, 4, "12%n3", &n);
794 EQL ( 4, 5, "12%n34", &n);
795 EQL ( 4, 5, "12%n34%n", &n, &n);
796 EQL ( 5, 6, "12%n34%n5", &n, &n);
797 EQL ( 6, 7, "12%n34%n56", &n, &n);
798 EQL ( 6, 7, "%s%n%s%n%s", "12", &n, "34", &n, "56");
799 }
800
801 static void __attribute__ ((noinline, noclone))
802 test_percent (void)
803 {
804 /* Provide extra arguments siunce the EQL macro needs at least one. */
805 EQL ( 1, 2, "%%", 0); /* { dg-warning "too many arguments" } */
806 EQL ( 2, 3, "%%%%", 0); /* { dg-warning "too many arguments" } */
807 EQL ( 3, 4, "%%%%%%", 0); /* { dg-warning "too many arguments" } */
808 EQL ( 3, 4, "%%%%%%%s", "");
809 EQL ( 3, 4, "%%%%%s%%", "");
810 EQL ( 3, 4, "%%%s%%%%", "");
811 EQL ( 3, 4, "%s%%%%%%", "");
812 }
813
814 int main (void)
815 {
816 test_c ('?');
817 test_d_i (0xdeadbeef, 0xdeadbeefL);
818 test_x ('?', 0xdead, 0xdeadbeef);
819
820 test_a_double (0.0);
821 test_e_double (0.0);
822 test_f_double (0.0);
823 test_g_double (0.0);
824
825 test_a_long_double ();
826 test_e_long_double (0.0);
827 test_f_long_double ();
828 test_g_long_double ();
829
830 test_s (0);
831
832 test_n ();
833
834 test_percent ();
835
836 if (nfails)
837 {
838 __builtin_printf ("%u out of %u tests failed\n", nfails, ntests);
839 __builtin_abort ();
840 }
841
842 return 0;
843 }