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