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" } */
8
9 #ifndef LINE
10 # define LINE 0
11 #endif
12
13 #if __STDC_VERSION__ < 199901L
14 # define __func__ __FUNCTION__
15 #endif
16
17 typedef __SIZE_TYPE__ size_t;
18
19 unsigned ntests;
20 unsigned nfails;
21
22 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)23 checkv (const char *func, int line, int res, int min, int max,
24 char *dst, const char *fmt, __builtin_va_list va)
25 {
26 int n = __builtin_vsprintf (dst, fmt, va);
27 int len = __builtin_strlen (dst);
28
29 ++ntests;
30
31 int fail = 0;
32 if (n != res)
33 {
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);
41
42 fail = 1;
43 }
44 else
45 {
46 if (len < min || max < len)
47 {
48 __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" "
49 "doesn't match output length: ",
50 func, line, fmt, dst);
51
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);
56
57 fail = 1;
58 }
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);
65 }
66
67 if (fail)
68 ++nfails;
69 }
70
71 void __attribute__ ((noclone, noinline))
check(const char * func,int line,int res,int min,int max,char * dst,const char * fmt,...)72 check (const char *func, int line, int res, int min, int max,
73 char *dst, const char *fmt, ...)
74 {
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);
79 }
80
81 char buffer[4100];
82 char* volatile dst = buffer;
83 char* ptr = buffer;
84
85 #define concat(a, b) a ## b
86 #define CAT(a, b) concat (a, b)
87
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
108
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) \
116 { \
117 TEST_FAILURE (__LINE__, expect, expect, result); \
118 } \
119 check (__func__, __LINE__, result, expect, expect, dst, fmt, \
120 __VA_ARGS__); \
121 } while (0)
122
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) \
130 { \
131 TEST_FAILURE (__LINE__, min, max, result); \
132 } \
133 check (__func__, __LINE__, result, min, max, dst, fmt, \
134 __VA_ARGS__); \
135 } while (0)
136
137 static void __attribute__ ((noinline, noclone))
test_c(char c)138 test_c (char c)
139 {
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);
152
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');
159
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');
166
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);
170 }
171
172 /* Generate a pseudo-random unsigned value. */
173
174 unsigned __attribute__ ((noclone, noinline))
unsigned_value(void)175 unsigned_value (void)
176 {
177 extern int rand ();
178 return rand ();
179 }
180
181 /* Generate a pseudo-random signed value. */
182
183 int __attribute__ ((noclone, noinline))
int_value(void)184 int_value (void)
185 {
186 extern int rand ();
187 return rand ();
188 }
189
190 /* Generate an unsigned char value in the specified range. */
191
192 static unsigned char
uchar_range(unsigned min,unsigned max)193 uchar_range (unsigned min, unsigned max)
194 {
195 unsigned x = unsigned_value ();
196 if (x < min || max < x)
197 x = min;
198 return x;
199 }
200
201 /* Generate a signed int value in the specified range. */
202
203 static int
int_range(int min,int max)204 int_range (int min, int max)
205 {
206 int val = int_value ();
207 if (val < min || max < val)
208 val = min;
209 return val;
210 }
211
212 #define IR(min, max) int_range (min, max)
213
214 static void __attribute__ ((noinline, noclone))
test_d_i(int i,long li)215 test_d_i (int i, long li)
216 {
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);
231
232 EQL ( 1, 2, "%i", uchar_range (0, 9));
233 EQL ( 1, -1, "%i", uchar_range (0, 9));
234
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));
241 */
242
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
261
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
278
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. */
282
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);
292
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));
296
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));
304
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));
309
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));
313
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));
317
318 #if __SIZEOF_SHORT__ == 2
319 RNG ( 1, 6, 7, "%hi", i);
320 RNG ( 1, 5, 6, "%hu", i);
321 RNG ( 1, 6, 7, "%.1hi", i);
322 RNG ( 2, 6, 7, "%.2hi", i);
323 RNG ( 3, 6, 7, "%.3hi", i);
324 RNG ( 4, 6, 7, "%.4hi", i);
325 RNG ( 5, 6, 7, "%.5hi", i);
326 RNG ( 6, 7, 8, "%.6hi", i);
327 RNG ( 7, 8, 9, "%.7hi", i);
328 #elif __SIZEOF_SHORT__ == 4
329 RNG ( 1, 11, 12, "%hi", i);
330 RNG ( 1, 10, 11, "%hu", i);
331
332 RNG ( 1, 11, 12, "%.1hi", i);
333 RNG ( 2, 11, 12, "%.2hi", i);
334 RNG ( 3, 11, 12, "%.3hi", i);
335 RNG ( 4, 11, 12, "%.4hi", i);
336 RNG ( 5, 11, 12, "%.5hi", i);
337 RNG ( 6, 11, 12, "%.6hi", i);
338 RNG ( 7, 11, 12, "%.7hi", i);
339 RNG ( 8, 11, 12, "%.8hi", i);
340 RNG ( 9, 11, 12, "%.9hi", i);
341 RNG (10, 11, 12, "%.10hi", i);
342 RNG (11, 12, 13, "%.11hi", i);
343 RNG (12, 13, 14, "%.12hi", i);
344 RNG (13, 14, 15, "%.13hi", i);
345 #endif
346
347 #if __SIZEOF_INT__ == 2
348 RNG ( 1, 6, 7, "%i", i);
349 RNG ( 1, 5, 6, "%u", i);
350
351 RNG ( 1, 6, 7, "%.1i", i);
352 RNG ( 2, 6, 7, "%.2i", i);
353 RNG ( 3, 6, 7, "%.3i", i);
354 RNG ( 4, 6, 7, "%.4i", i);
355 RNG ( 5, 6, 7, "%.5i", i);
356 RNG ( 6, 7, 8, "%.6i", i);
357 RNG ( 7, 8, 9, "%.7i", i);
358 #elif __SIZEOF_INT__ == 4
359 RNG ( 1, 11, 12, "%i", i);
360 RNG ( 1, 10, 11, "%u", i);
361
362 RNG ( 1, 11, 12, "%.1i", i);
363 RNG ( 2, 11, 12, "%.2i", i);
364 RNG ( 3, 11, 12, "%.3i", i);
365 RNG ( 4, 11, 12, "%.4i", i);
366 RNG ( 5, 11, 12, "%.5i", i);
367 RNG ( 6, 11, 12, "%.6i", i);
368 RNG ( 7, 11, 12, "%.7i", i);
369 RNG ( 8, 11, 12, "%.8i", i);
370 RNG ( 9, 11, 12, "%.9i", i);
371 RNG (10, 11, 12, "%.10i", i);
372 RNG (11, 12, 13, "%.11i", i);
373 RNG (12, 13, 14, "%.12i", i);
374 RNG (13, 14, 15, "%.13i", i);
375 #elif __SIZEOF_INT__ == 8
376 RNG ( 1, 20, 21, "%i", i);
377 RNG ( 1, 19, 20, "%u", i);
378 #endif
379
380 #if __SIZEOF_LONG__ == 4
381 RNG ( 1, 11, 12, "%li", li);
382 RNG ( 1, 10, 11, "%lu", li);
383
384 RNG ( 1, 11, 12, "%.1li", li);
385 RNG ( 2, 11, 12, "%.2li", li);
386 RNG ( 3, 11, 12, "%.3li", li);
387 RNG ( 4, 11, 12, "%.4li", li);
388 RNG ( 5, 11, 12, "%.5li", li);
389 RNG ( 6, 11, 12, "%.6li", li);
390 RNG ( 7, 11, 12, "%.7li", li);
391 RNG ( 8, 11, 12, "%.8li", li);
392 RNG ( 9, 11, 12, "%.9li", li);
393 RNG (10, 11, 12, "%.10li", li);
394 RNG (11, 12, 13, "%.11li", li);
395 RNG (12, 13, 14, "%.12li", li);
396 RNG (13, 14, 15, "%.13li", li);
397 #elif __SIZEOF_LONG__ == 8
398 RNG ( 1, 20, 21, "%li", li);
399 RNG ( 1, 19, 20, "%lu", li);
400 #endif
401 }
402
403 static void __attribute__ ((noinline, noclone))
test_x(unsigned char uc,unsigned short us,unsigned ui)404 test_x (unsigned char uc, unsigned short us, unsigned ui)
405 {
406 EQL ( 1, 2, "%hhx", 0);
407 EQL ( 2, 3, "%2hhx", 0);
408 EQL ( 2, 3, "%02hhx", 0);
409 EQL ( 2, 3, "%#02hhx", 0);
410
411 EQL ( 1, 2, "%hhx", 1);
412 EQL ( 2, 3, "%2hhx", 1);
413 EQL ( 2, 3, "%02hhx", 1);
414 EQL ( 3, 4, "%#02hhx", 1);
415
416 EQL ( 2, 3, "%2hhx", uc);
417 EQL ( 2, 3, "%02hhx", uc);
418 EQL ( 5, 6, "%#05hhx", uc);
419
420 EQL ( 2, 3, "%2hhx", us);
421 EQL ( 2, 3, "%02hhx", us);
422 EQL ( 5, 6, "%#05hhx", us);
423
424 EQL ( 2, 3, "%2hhx", ui);
425 EQL ( 2, 3, "%02hhx", ui);
426 EQL ( 5, 6, "%#05hhx", ui);
427
428 EQL ( 1, 2, "%x", 0);
429 EQL ( 1, 2, "%#x", 0);
430 EQL ( 1, 2, "%#0x", 0);
431 EQL ( 1, 2, "%x", 1);
432 EQL ( 1, 2, "%x", 0xf);
433 EQL ( 2, 3, "%x", 0x10);
434 EQL ( 2, 3, "%x", 0xff);
435 EQL ( 3, 4, "%x", 0x100);
436
437 EQL (11, 12, "%02x:%02x:%02x:%02x", 0xde, 0xad, 0xbe, 0xef);
438
439 /* The following would be optimized if the range information of
440 the variable's type was made available. Alas, it's lost due
441 to the promotion of the actual argument (unsined char) to
442 the type of the "formal" argument (int in the case of the
443 ellipsis).
444 EQL (11, 12, "%02x:%02x:%02x:%02x", uc, uc, uc, uc);
445 */
446 EQL (11, 12, "%02hhx:%02hhx:%02hhx:%02hhx", uc, uc, uc, uc);
447
448 #if __SIZEOF_SHORT__ == 2
449 EQL ( 4, 5, "%04hx", us);
450 EQL ( 9, 10, "%04hx:%04hx", us, us);
451 EQL (14, 15, "%04hx:%04hx:%04hx", us, us, us);
452 EQL (19, 20, "%04hx:%04hx:%04hx:%04hx", us, us, us, us);
453 #endif
454
455 #if __SIZEOF_INT__ == 2
456 EQL ( 4, 5, "%04x", ui);
457 EQL ( 6, 7, "%#06x", ui);
458 #elif __SIZEOF_INT__ == 4
459 EQL ( 8, 9, "%08x", ui);
460 EQL (10, 10 + 1, "%#010x", ui);
461 #elif __SIZEOF_INT__ == 8
462 EQL (16, 17, "%016x", ui);
463 EQL (18, 19, "%#018x", ui);
464 #endif
465 }
466
467 static void __attribute__ ((noinline, noclone))
test_a_double(double d)468 test_a_double (double d)
469 {
470 EQL ( 6, 7, "%.0a", 0.0); /* 0x0p+0 */
471 EQL ( 6, 7, "%.0a", 1.0); /* 0x8p-3 */
472 EQL ( 6, 7, "%.0a", 2.0); /* 0x8p-2 */
473
474 /* The decimal point may be up to MB_LEN_MAX long. */
475 RNG ( 8, 13, 14, "%.1a", 3.0); /* 0xc.0p-2 */
476 RNG ( 9, 14, 15, "%.2a", 4.0); /* 0x8.00p-1 */
477 RNG (10, 15, 16, "%.3a", 5.0); /* 0xa.000p-1 */
478
479 RNG (11, 16, 17, "%.*a", 4, 6.0); /* 0xc.0000p-1 */
480 RNG (12, 17, 18, "%.*a", 5, 7.0); /* 0xe.00000p-1 */
481 /* d is in [ 0, -DBL_MAX ] */
482 RNG ( 3, 10, 11, "%.0a", d); /* inf/nan or 0x0p+0 ... -0x2p+1023 */
483 /* %a is poorly specified and allows for implementations divergence:
484 some (such as Glibc) trim redundant trailing zeros after decimal
485 point and others (e.g., Solaris) don't. */
486 RNG ( 3, 30, 31, "%.1a", d); /* inf or 0x0.0p+0 ... -0x2.0...0p+1023 */
487 RNG ( 3, 30, 31, "%.2a", d); /* inf or 0x0.00p+0 ... -0x2.00...0p+1023 */
488 }
489
490 static void __attribute__ ((noinline, noclone))
test_a_long_double(void)491 test_a_long_double (void)
492 {
493 EQL ( 6, 7, "%.0La", 0.0L); /* 0x0p+0 */
494 EQL ( 6, 7, "%.0La", 1.0L); /* 0x8p-3 */
495 EQL ( 6, 7, "%.0La", 2.0L); /* 0x8p-2 */
496
497 RNG ( 8, 13, 14, "%.1La", 3.0L); /* 0xc.0p-2 */
498 RNG ( 9, 14, 15, "%.2La", 4.0L); /* 0xa.00p-1 */
499 }
500
501 static void __attribute__ ((noinline, noclone))
test_e_double(double d)502 test_e_double (double d)
503 {
504 RNG (12, 17, 18, "%e", 1.0e0);
505 RNG (13, 18, 19, "%e", -1.0e0);
506 RNG (12, 17, 18, "%e", 1.0e+1);
507 RNG (13, 18, 19, "%e", -1.0e+1);
508 RNG (12, 17, 18, "%e", 1.0e+12);
509 RNG (13, 18, 19, "%e", -1.0e+12);
510 RNG (13, 18, 19, "%e", 1.0e+123);
511 RNG (14, 19, 20, "%e", -1.0e+123);
512
513 RNG (12, 17, 18, "%e", 9.999e+99);
514 RNG (12, 17, 18, "%e", 9.9999e+99);
515 RNG (12, 17, 18, "%e", 9.99999e+99);
516
517 /* The actual output of the following directive depends on the rounding
518 mode. */
519 /* RNG (12, "%e", 9.9999994e+99); */
520
521 RNG (12, 17, 18, "%e", 1.0e-1);
522 RNG (12, 17, 18, "%e", 1.0e-12);
523 RNG (13, 18, 19, "%e", 1.0e-123);
524
525 RNG ( 3, 19, 20, "%e", d);
526 RNG ( 3, 11, 12, "%.e", d);
527 RNG ( 3, 12, 13, "%.0e", d);
528 RNG ( 3, 14, 15, "%.1e", d);
529 RNG ( 3, 15, 16, "%.2e", d);
530 RNG ( 3, 16, 17, "%.3e", d);
531 RNG ( 3, 17, 18, "%.4e", d);
532 RNG ( 3, 18, 19, "%.5e", d);
533 RNG ( 3, 19, 20, "%.6e", d);
534 RNG ( 3, 20, 21, "%.7e", d);
535
536 RNG ( 3, 4013, 4014, "%.4000e", d);
537
538 RNG ( 3, 7, 8, "%.*e", 0, d);
539 RNG ( 3, 14, 15, "%.*e", 1, d);
540 RNG ( 3, 15, 16, "%.*e", 2, d);
541 RNG ( 3, 16, 17, "%.*e", 3, d);
542 RNG ( 3, 17, 18, "%.*e", 4, d);
543 RNG ( 3, 18, 19, "%.*e", 5, d);
544 RNG ( 3, 19, 20, "%.*e", 6, d);
545 RNG ( 3, 20, 21, "%.*e", 7, d);
546
547 RNG ( 3, 4013, 4014, "%.*e", 4000, d);
548 RNG ( 4, 4013, 4014, "%+.*e", 4000, d);
549 RNG ( 4, 4013, 4014, "% .*e", 4000, d);
550 RNG ( 3, 4013, 4014, "%#.*e", 4000, d);
551 }
552
553 static void __attribute__ ((noinline, noclone))
test_e_long_double(long double d)554 test_e_long_double (long double d)
555 {
556 RNG (12, 17, 18, "%Le", 1.0e0L);
557 RNG (13, 18, 19, "%Le", -1.0e0L);
558 RNG (12, 17, 18, "%Le", 1.0e+1L);
559 RNG (13, 18, 19, "%Le", -1.0e+1L);
560 RNG (12, 18, 19, "%Le", 1.0e+12L);
561 RNG (13, 19, 20, "%Le", -1.0e+12L);
562 RNG (13, 19, 20, "%Le", 1.0e+123L);
563 RNG (14, 20, 21, "%Le", -1.0e+123L);
564
565 RNG (12, 18, 19, "%Le", 9.999e+99L);
566 RNG (12, 18, 19, "%Le", 9.9999e+99L);
567 RNG (12, 18, 19, "%Le", 9.99999e+99L);
568
569 #if __DBL_DIG__ < __LDBL_DIG__
570 RNG (12, 17, 18, "%Le", 9.999999e+99L);
571 #else
572 RNG (12, 18, 19, "%Le", 9.999999e+99L);
573 #endif
574
575 /* The actual output of the following directive depends on the rounding
576 mode. */
577 /* RNG (12, "%Le", 9.9999994e+99L); */
578
579 RNG (12, 17, 18, "%Le", 1.0e-1L);
580 RNG (12, 17, 18, "%Le", 1.0e-12L);
581 RNG (13, 18, 19, "%Le", 1.0e-123L);
582
583 EQL ( 6, 7, "%.0Le", 1.0e-111L);
584
585 RNG ( 8, 13, 14, "%.1Le", 1.0e-111L);
586 RNG (19, 25, 25, "%.12Le", 1.0e-112L);
587 RNG (20, 26, 27, "%.13Le", 1.0e-113L);
588
589 /* The following correspond to the double results plus 1 for the upper
590 bound accounting for the four-digit exponent. The lower bound is
591 for inf/nan. */
592 RNG ( 3, 20, 21, "%Le", d); /* inf or 0.000000e+00 ... -1.189732e+4932 */
593 RNG ( 3, 8, 9, "%.Le", d);
594 RNG ( 3, 9, 10, "%.0Le", d);
595 RNG ( 3, 15, 16, "%.1Le", d); /* inf or 0.0e+00 ... -1.2e+4932 */
596 RNG ( 3, 16, 17, "%.2Le", d); /* inf or 0.00e+00 ... -1.19e+4932 */
597 RNG ( 3, 17, 18, "%.3Le", d);
598 RNG ( 3, 18, 19, "%.4Le", d);
599 RNG ( 3, 19, 20, "%.5Le", d);
600 RNG ( 3, 20, 21, "%.6Le", d); /* same as plain "%Le" */
601 RNG ( 3, 21, 22, "%.7Le", d); /* inf or 0.0000000e+00 ... -1.1897315e+4932 */
602
603 RNG ( 3, 9, 10, "%.*Le", 0, d);
604 RNG ( 3, 15, 16, "%.*Le", 1, d);
605 RNG ( 3, 16, 17, "%.*Le", 2, d);
606 RNG ( 3, 17, 18, "%.*Le", 3, d);
607 RNG ( 3, 18, 19, "%.*Le", 4, d);
608 RNG ( 3, 19, 20, "%.*Le", 5, d);
609 RNG ( 3, 20, 21, "%.*Le", 6, d);
610 RNG ( 3, 21, 22, "%.*Le", 7, d);
611 }
612
613 static void __attribute__ ((noinline, noclone))
test_f_double(double d)614 test_f_double (double d)
615 {
616 RNG ( 8, 13, 14, "%f", 0.0e0);
617 RNG ( 8, 13, 14, "%f", 0.1e0);
618 RNG ( 8, 13, 14, "%f", 0.12e0);
619 RNG ( 8, 13, 14, "%f", 0.123e0);
620 RNG ( 8, 13, 14, "%f", 0.1234e0);
621 RNG ( 8, 13, 14, "%f", 0.12345e0);
622 RNG ( 8, 13, 14, "%f", 0.123456e0);
623 RNG ( 8, 13, 14, "%f", 1.234567e0);
624
625 RNG ( 9, 14, 15, "%f", 1.0e+1);
626 RNG ( 20, 26, 27, "%f", 1.0e+12);
627 RNG (130, 136, 137, "%f", 1.0e+123);
628
629 RNG ( 8, 13, 14, "%f", 1.0e-1);
630 RNG ( 8, 13, 14, "%f", 1.0e-12);
631 RNG ( 8, 13, 14, "%f", 1.0e-123);
632
633 RNG ( 3, 322, 323, "%f", d);
634 RNG ( 4, 322, 323, "%+f", d);
635 RNG ( 4, 322, 323, "% f", d);
636 RNG ( 3, 322, 323, "%#f", d);
637 }
638
639 static void __attribute__ ((noinline, noclone))
test_f_long_double(void)640 test_f_long_double (void)
641 {
642 RNG ( 8, 15, 16, "%Lf", 0.0e0L);
643 RNG ( 8, 14, 15, "%Lf", 0.1e0L);
644 RNG ( 8, 14, 15, "%Lf", 0.12e0L);
645 RNG ( 8, 14, 15, "%Lf", 0.123e0L);
646 RNG ( 8, 14, 15, "%Lf", 0.1234e0L);
647 RNG ( 8, 14, 15, "%Lf", 0.12345e0L);
648 RNG ( 8, 14, 15, "%Lf", 0.123456e0L);
649 RNG ( 8, 14, 15, "%Lf", 1.234567e0L);
650
651 RNG ( 9, 15, 16, "%Lf", 1.0e+1L);
652 RNG ( 20, 26, 27, "%Lf", 1.0e+12L);
653 RNG (130, 136, 137, "%Lf", 1.0e+123L);
654
655 RNG ( 8, 14, 15, "%Lf", 1.0e-1L);
656 RNG ( 8, 14, 15, "%Lf", 1.0e-12L);
657 RNG ( 8, 14, 15, "%Lf", 1.0e-123L);
658 }
659
660 static void __attribute__ ((noinline, noclone))
test_g_double(double d)661 test_g_double (double d)
662 {
663 /* Numbers exactly representable in binary floating point. */
664 EQL ( 1, 2, "%g", 0.0);
665
666 RNG ( 3, 8, 9, "%g", 1.0 / 2);
667 RNG ( 4, 9, 10, "%g", 1.0 / 4);
668 RNG ( 5, 10, 11, "%g", 1.0 / 8);
669 RNG ( 6, 11, 12, "%g", 1.0 / 16);
670 RNG ( 7, 12, 13, "%g", 1.0 / 32);
671 RNG ( 8, 13, 14, "%g", 1.0 / 64);
672 RNG ( 9, 14, 15, "%g", 1.0 / 128);
673 RNG ( 10, 15, 16, "%g", 1.0 / 256);
674 RNG ( 10, 16, 17, "%g", 1.0 / 512);
675
676 /* Numbers that are not exactly representable. */
677 RNG ( 3, 13, 14, "%g", 0.1);
678 RNG ( 4, 13, 14, "%g", 0.12);
679 RNG ( 5, 13, 14, "%g", 0.123);
680 RNG ( 6, 13, 14, "%g", 0.1234);
681 RNG ( 7, 13, 14, "%g", 0.12345);
682 RNG ( 8, 13, 14, "%g", 0.123456);
683
684 RNG ( 4, 17, 18, "%g", 0.123e+1);
685 RNG ( 8, 18, 19, "%g", 0.123e+12);
686 RNG ( 9, 19, 20, "%g", 0.123e+134);
687
688 RNG ( 1, 18, 19, "%g", d);
689 RNG ( 1, 12, 13, "%.g", d);
690 RNG ( 1, 12, 13, "%.0g", d);
691 RNG ( 1, 12, 13, "%.1g", d);
692 RNG ( 1, 14, 15, "%.2g", d);
693 RNG ( 1, 15, 16, "%.3g", d);
694 RNG ( 1, 16, 17, "%.4g", d);
695 RNG ( 1, 17, 18, "%.5g", d);
696 RNG ( 1, 18, 19, "%.6g", d);
697 RNG ( 1, 19, 20, "%.7g", d);
698 RNG ( 1, 20, 21, "%.8g", d);
699
700 RNG ( 1, 315, 316, "%.9999g", d);
701
702 RNG ( 1, 12, 13, "%.*g", 0, d);
703 RNG ( 1, 12, 13, "%.*g", 1, d);
704 RNG ( 1, 14, 15, "%.*g", 2, d);
705 RNG ( 1, 15, 16, "%.*g", 3, d);
706 RNG ( 1, 16, 17, "%.*g", 4, d);
707 RNG ( 1, 17, 18, "%.*g", 5, d);
708 RNG ( 1, 18, 19, "%.*g", 6, d);
709 RNG ( 1, 19, 20, "%.*g", 7, d);
710 RNG ( 1, 20, 21, "%.*g", 8, d);
711
712 RNG ( 1, 315, 316, "%.*g", 9999, d);
713 }
714
715 static void __attribute__ ((noinline, noclone))
test_g_long_double(void)716 test_g_long_double (void)
717 {
718 /* Numbers exactly representable in binary floating point. */
719 EQL ( 1, 2, "%Lg", 0.0L);
720 RNG ( 3, 8, 9, "%Lg", 1.0L / 2);
721 RNG ( 4, 9, 10, "%Lg", 1.0L / 4);
722 RNG ( 5, 10, 11, "%Lg", 1.0L / 8);
723 RNG ( 6, 11, 12, "%Lg", 1.0L / 16);
724 RNG ( 7, 12, 13, "%Lg", 1.0L / 32);
725 RNG ( 8, 13, 14, "%Lg", 1.0L / 64);
726 RNG ( 9, 14, 15, "%Lg", 1.0L / 128);
727 RNG ( 10, 15, 16, "%Lg", 1.0L / 256);
728 RNG ( 10, 15, 16, "%Lg", 1.0L / 512);
729
730 /* Numbers that are not exactly representable. */
731
732 /* The following test case results in up to 14 bytes on powerpc*-*-*
733 but only in 13 bytes on x86_64 (see PR testsuite/79293). Test just
734 for the former for simplicity. */
735 RNG ( 3, 14, 15, "%Lg", 0.1L);
736
737 RNG ( 4, 13, 14, "%Lg", 0.12L);
738 RNG ( 5, 13, 14, "%Lg", 0.123L);
739 RNG ( 6, 13, 14, "%Lg", 0.1234L);
740 RNG ( 7, 13, 14, "%Lg", 0.12345L);
741 RNG ( 8, 13, 14, "%Lg", 0.123456L);
742
743 RNG ( 4, 12, 13, "%Lg", 0.123e+1L);
744 RNG ( 8, 13, 14, "%Lg", 0.123e+12L);
745 RNG ( 9, 17, 18, "%Lg", 0.123e+134L);
746 }
747
748 static void __attribute__ ((noinline, noclone))
test_s(int i)749 test_s (int i)
750 {
751 EQL ( 0, 1, "%s", "");
752 EQL ( 0, 1, "%s", "\0");
753 EQL ( 1, 2, "%1s", "");
754 EQL ( 1, 2, "%s", "1");
755 EQL ( 2, 3, "%2s", "");
756 EQL ( 2, 3, "%s", "12");
757 EQL ( 2, 3, "%s%s", "12", "");
758 EQL ( 2, 3, "%s%s", "", "12");
759 EQL ( 2, 3, "%s%s", "1", "2");
760 EQL ( 3, 4, "%3s", "");
761 EQL ( 3, 4, "%3s", "1");
762 EQL ( 3, 4, "%3s", "12");
763 EQL ( 3, 4, "%3s", "123");
764 EQL ( 3, 4, "%3.3s", "1");
765 EQL ( 3, 4, "%3.3s", "12");
766 EQL ( 3, 4, "%3.3s", "123");
767 EQL ( 3, 4, "%3.3s", "1234");
768 EQL ( 3, 4, "%3.3s", "12345");
769 EQL ( 3, 4, "%s %s", "1", "2");
770 EQL ( 4, 5, "%s %s", "12", "3");
771 EQL ( 5, 6, "%s %s", "12", "34");
772 EQL ( 5, 6, "[%s %s]", "1", "2");
773 EQL ( 6, 7, "[%s %s]", "12", "3");
774 EQL ( 7, 8, "[%s %s]", "12", "34");
775
776 /* Verify the result of a conditional expression involving string
777 literals is in the expected range of their lengths. */
778 RNG ( 0, 3, 4, "%-s", i ? "" : "123");
779 RNG ( 1, 4, 5, "%-s", i ? "1" : "1234");
780 RNG ( 2, 5, 6, "%-s", i ? "12" : "12345");
781 RNG ( 3, 6, 7, "%-s", i ? "123" : "123456");
782 }
783
784 static void __attribute__ ((noinline, noclone))
test_n(void)785 test_n (void)
786 {
787 int n;
788 EQL ( 0, 1, "%n", &n);
789 EQL ( 1, 2, "1%n", &n);
790 EQL ( 2, 3, "12%n", &n);
791 EQL ( 3, 4, "12%n3", &n);
792 EQL ( 4, 5, "12%n34", &n);
793 EQL ( 4, 5, "12%n34%n", &n, &n);
794 EQL ( 5, 6, "12%n34%n5", &n, &n);
795 EQL ( 6, 7, "12%n34%n56", &n, &n);
796 EQL ( 6, 7, "%s%n%s%n%s", "12", &n, "34", &n, "56");
797 }
798
799 static void __attribute__ ((noinline, noclone))
test_percent(void)800 test_percent (void)
801 {
802 /* Provide extra arguments siunce the EQL macro needs at least one. */
803 EQL ( 1, 2, "%%", 0); /* { dg-warning "too many arguments" } */
804 EQL ( 2, 3, "%%%%", 0); /* { dg-warning "too many arguments" } */
805 EQL ( 3, 4, "%%%%%%", 0); /* { dg-warning "too many arguments" } */
806 EQL ( 3, 4, "%%%%%%%s", "");
807 EQL ( 3, 4, "%%%%%s%%", "");
808 EQL ( 3, 4, "%%%s%%%%", "");
809 EQL ( 3, 4, "%s%%%%%%", "");
810 }
811
main(void)812 int main (void)
813 {
814 test_c ('?');
815 test_d_i (0xdeadbeef, 0xdeadbeefL);
816 test_x ('?', 0xdead, 0xdeadbeef);
817
818 test_a_double (0.0);
819 test_e_double (0.0);
820 test_f_double (0.0);
821 test_g_double (0.0);
822
823 test_a_long_double ();
824 test_e_long_double (0.0);
825 test_f_long_double ();
826 test_g_long_double ();
827
828 test_s (0);
829
830 test_n ();
831
832 test_percent ();
833
834 if (nfails)
835 {
836 __builtin_printf ("%u out of %u tests failed\n", nfails, ntests);
837 __builtin_abort ();
838 }
839
840 return 0;
841 }
842