1 /* Copyright (C) 1991-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include </usr/include/stdio.h>
20 #define EXIT_SUCCESS 0
29 #include <libc-diag.h>
31 /* This whole file is picayune tests of corner cases of printf format strings.
32 The compiler warnings are not useful here. */
33 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
35 #if __GNUC_PREREQ (7, 0)
36 /* Compiler warnings about snprintf output truncation should also be
38 DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
41 static void rfg1 (void);
42 static void rfg2 (void);
43 static void rfg3 (void);
47 fmtchk (const char *fmt
)
49 (void) fputs(fmt
, stdout
);
50 (void) printf(":\t`");
51 (void) printf(fmt
, 0x12);
56 fmtst1chk (const char *fmt
)
58 (void) fputs(fmt
, stdout
);
59 (void) printf(":\t`");
60 (void) printf(fmt
, 4, 0x12);
65 fmtst2chk (const char *fmt
)
67 (void) fputs(fmt
, stdout
);
68 (void) printf(":\t`");
69 (void) printf(fmt
, 4, 4, 0x12);
76 static char shortstr
[] = "Hi, Z.";
77 static char longstr
[] = "Good morning, Doctor Chandra. This is Hal. \
78 I am ready for my first lesson today.";
94 printf("bad format:\t\"%b\"\n");
95 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL
);
98 printf("decimal negative:\t\"%d\"\n", -2345);
99 printf("octal negative:\t\"%o\"\n", -2345);
100 printf("hex negative:\t\"%x\"\n", -2345);
101 printf("long decimal number:\t\"%ld\"\n", -123456L);
102 printf("long octal negative:\t\"%lo\"\n", -2345L);
103 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
104 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
105 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
106 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
107 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
109 printf("zero-padded string:\t\"%010s\"\n", shortstr
);
110 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr
);
111 printf("space-padded string:\t\"%10s\"\n", shortstr
);
112 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr
);
113 /* GCC 9 warns about the NULL format argument; this is deliberately
115 DIAG_PUSH_NEEDS_COMMENT
;
116 #if __GNUC_PREREQ (7, 0)
117 DIAG_IGNORE_NEEDS_COMMENT (9, "-Wformat-overflow=");
119 printf("null string:\t\"%s\"\n", (char *)NULL
);
120 DIAG_POP_NEEDS_COMMENT
;
121 printf("limited string:\t\"%.22s\"\n", longstr
);
123 printf("a-style max:\t\"%a\"\n", DBL_MAX
);
124 printf("a-style -max:\t\"%a\"\n", -DBL_MAX
);
125 printf("e-style >= 1:\t\"%e\"\n", 12.34);
126 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
127 printf("e-style < .1:\t\"%e\"\n", 0.001234);
128 printf("e-style big:\t\"%.60e\"\n", 1e20
);
129 printf ("e-style == .1:\t\"%e\"\n", 0.1);
130 printf("f-style == 0:\t\"%f\"\n", 0.0);
131 printf("f-style >= 1:\t\"%f\"\n", 12.34);
132 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
133 printf("f-style < .1:\t\"%f\"\n", 0.001234);
134 printf("g-style == 0:\t\"%g\"\n", 0.0);
135 printf("g-style >= 1:\t\"%g\"\n", 12.34);
136 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
137 printf("g-style < .1:\t\"%g\"\n", 0.001234);
138 printf("g-style big:\t\"%.60g\"\n", 1e20
);
140 printf("Lf-style == 0:\t\"%Lf\"\n", (long double) 0.0);
141 printf("Lf-style >= 1:\t\"%Lf\"\n", (long double) 12.34);
142 printf("Lf-style >= .1:\t\"%Lf\"\n", (long double) 0.1234);
143 printf("Lf-style < .1:\t\"%Lf\"\n", (long double) 0.001234);
144 printf("Lg-style == 0:\t\"%Lg\"\n", (long double) 0.0);
145 printf("Lg-style >= 1:\t\"%Lg\"\n", (long double) 12.34);
146 printf("Lg-style >= .1:\t\"%Lg\"\n", (long double) 0.1234);
147 printf("Lg-style < .1:\t\"%Lg\"\n", (long double) 0.001234);
148 printf("Lg-style big:\t\"%.60Lg\"\n", (long double) 1e20
);
150 printf (" %6.5f\n", .099999999860301614);
151 printf (" %6.5f\n", .1);
152 printf ("x%5.4fx\n", .5);
154 printf (" %6.5Lf\n", (long double) .099999999860301614);
155 printf (" %6.5Lf\n", (long double) .1);
156 printf ("x%5.4Lfx\n", (long double) .5);
158 printf ("%#03x\n", 1);
160 printf ("something really insane: %.10000f\n", 1.0);
161 printf ("something really insane (long double): %.10000Lf\n",
169 printf ("%.17e\n", d
/ 2);
173 printf ("%15.5e\n", 4.9406564584124654e-324);
175 #define FORMAT "|%12.4f|%12.4e|%12.4g|%12.4Lf|%12.4Lg|\n"
176 printf (FORMAT
, 0.0, 0.0, 0.0,
177 (long double) 0.0, (long double) 0.0);
178 printf (FORMAT
, 1.0, 1.0, 1.0,
179 (long double) 1.0, (long double) 1.0);
180 printf (FORMAT
, -1.0, -1.0, -1.0,
181 (long double) -1.0, (long double) -1.0);
182 printf (FORMAT
, 100.0, 100.0, 100.0,
183 (long double) 100.0, (long double) 100.0);
184 printf (FORMAT
, 1000.0, 1000.0, 1000.0,
185 (long double) 1000.0, (long double) 1000.0);
186 printf (FORMAT
, 10000.0, 10000.0, 10000.0,
187 (long double) 10000.0, (long double) 10000.0);
188 printf (FORMAT
, 12345.0, 12345.0, 12345.0,
189 (long double) 12345.0, (long double) 12345.0);
190 printf (FORMAT
, 100000.0, 100000.0, 100000.0,
191 (long double) 100000.0, (long double) 100000.0);
192 printf (FORMAT
, 123456.0, 123456.0, 123456.0,
193 (long double) 123456.0, (long double) 123456.0);
199 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
200 snprintf (buf
, sizeof (buf
), "%30s", "foo"), (int) sizeof (buf
),
202 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
203 snprintf (buf2
, sizeof (buf2
), "%.999999u", 10));
206 printf("%.8f\n", DBL_MAX
);
207 printf("%.8f\n", -DBL_MAX
);
208 printf ("%e should be 1.234568e+06\n", 1234567.8);
209 printf ("%f should be 1234567.800000\n", 1234567.8);
210 printf ("%g should be 1.23457e+06\n", 1234567.8);
211 printf ("%g should be 123.456\n", 123.456);
212 printf ("%g should be 1e+06\n", 1000000.0);
213 printf ("%g should be 10\n", 10.0);
214 printf ("%g should be 0.02\n", 0.02);
217 /* This test rather checks the way the compiler handles constant
218 folding. gcc behavior wrt to this changed in 3.2 so it is not a
222 printf("%.17f\n",(1.0/x
/10.0+1.0)*x
-x
);
229 sprintf(buf
,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
231 result
|= strcmp (buf
,
234 puts (result
!= 0 ? "Test failed!" : "Test ok.");
240 sprintf (buf
, "%07Lo", 040000000000ll);
241 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf
);
243 if (strcmp (buf
, "40000000000") != 0)
246 fputs ("\tFAILED", stdout
);
251 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
252 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX
+ 2, USHRT_MAX
+ 2);
253 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
254 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX
+ 2, USHRT_MAX
+ 2);
256 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
257 UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
258 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX
+ 2, USHRT_MAX
+ 2);
259 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
260 UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
261 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX
+ 2, USHRT_MAX
+ 2);
263 puts ("--- Should be no further output. ---");
272 memset (bytes
, '\xff', sizeof bytes
);
273 sprintf (buf
, "foo%hhn\n", &bytes
[3]);
274 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
275 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
277 puts ("%hhn overwrite more bytes");
282 puts ("%hhn wrote incorrect value");
295 sprintf (buf
, "%5.s", "xyz");
296 if (strcmp (buf
, " ") != 0)
297 printf ("got: '%s', expected: '%s'\n", buf
, " ");
298 sprintf (buf
, "%5.f", 33.3);
299 if (strcmp (buf
, " 33") != 0)
300 printf ("got: '%s', expected: '%s'\n", buf
, " 33");
301 sprintf (buf
, "%5.Lf", (long double) 33.3);
302 if (strcmp (buf
, " 33") != 0)
303 printf ("got: '%s', expected: '%s'\n", buf
, " 33");
304 sprintf (buf
, "%8.e", 33.3e7
);
305 if (strcmp (buf
, " 3e+08") != 0)
306 printf ("got: '%s', expected: '%s'\n", buf
, " 3e+08");
307 sprintf (buf
, "%8.E", 33.3e7
);
308 if (strcmp (buf
, " 3E+08") != 0)
309 printf ("got: '%s', expected: '%s'\n", buf
, " 3E+08");
310 sprintf (buf
, "%.g", 33.3);
311 if (strcmp (buf
, "3e+01") != 0)
312 printf ("got: '%s', expected: '%s'\n", buf
, "3e+01");
313 sprintf (buf
, "%.Lg", (long double) 33.3);
314 if (strcmp (buf
, "3e+01") != 0)
315 printf ("got: '%s', expected: '%s'\n", buf
, "3e+01");
316 sprintf (buf
, "%.G", 33.3);
317 if (strcmp (buf
, "3E+01") != 0)
318 printf ("got: '%s', expected: '%s'\n", buf
, "3E+01");
328 sprintf (buf
, "%.*g", prec
, 3.3);
329 if (strcmp (buf
, "3") != 0)
330 printf ("got: '%s', expected: '%s'\n", buf
, "3");
332 sprintf (buf
, "%.*G", prec
, 3.3);
333 if (strcmp (buf
, "3") != 0)
334 printf ("got: '%s', expected: '%s'\n", buf
, "3");
336 sprintf (buf
, "%7.*G", prec
, 3.33);
337 if (strcmp (buf
, " 3") != 0)
338 printf ("got: '%s', expected: '%s'\n", buf
, " 3");
340 sprintf (buf
, "%.*Lg", prec
, (long double) 3.3);
341 if (strcmp (buf
, "3") != 0)
342 printf ("got: '%s', expected: '%s'\n", buf
, "3");
344 sprintf (buf
, "%.*LG", prec
, (long double) 3.3);
345 if (strcmp (buf
, "3") != 0)
346 printf ("got: '%s', expected: '%s'\n", buf
, "3");
348 sprintf (buf
, "%7.*LG", prec
, (long double) 3.33);
349 if (strcmp (buf
, " 3") != 0)
350 printf ("got: '%s', expected: '%s'\n", buf
, " 3");
352 sprintf (buf
, "%04.*o", prec
, 33);
353 if (strcmp (buf
, " 041") != 0)
354 printf ("got: '%s', expected: '%s'\n", buf
, " 041");
356 sprintf (buf
, "%09.*u", prec
, 33);
357 if (strcmp (buf
, " 0000033") != 0)
358 printf ("got: '%s', expected: '%s'\n", buf
, " 0000033");
360 sprintf (buf
, "%04.*x", prec
, 33);
361 if (strcmp (buf
, " 021") != 0)
362 printf ("got: '%s', expected: '%s'\n", buf
, " 021");
364 sprintf (buf
, "%04.*X", prec
, 33);
365 if (strcmp (buf
, " 021") != 0)
366 printf ("got: '%s', expected: '%s'\n", buf
, " 021");
373 double g
= 5.0000001;
374 unsigned long l
= 1234567890;
375 double d
= 321.7654321;
376 const char s
[] = "test-string";
381 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
382 i
, h
, l
, d
, 8, 5, 14, 14, d
, g
, s
, 14, 3, 14);
384 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
385 printf ("got: '%s', expected: '%s'\n", buf
,
386 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");
389 #define TEST_FUNCTION do_test ()
390 #include "../test-skeleton.c"