NEWS: fix spelling mistake
[glibc.git] / stdio-common / tst-printf.c
blobd73f0cc34eb6ab49f67d81ba0e12af07923e907c
1 /* Copyright (C) 1991-2018 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 <http://www.gnu.org/licenses/>. */
18 #ifdef BSD
19 #include </usr/include/stdio.h>
20 #define EXIT_SUCCESS 0
21 #else
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #endif
28 #include <float.h>
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
37 ignored. */
38 DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
39 #endif
41 static void rfg1 (void);
42 static void rfg2 (void);
43 static void rfg3 (void);
46 static void
47 fmtchk (const char *fmt)
49 (void) fputs(fmt, stdout);
50 (void) printf(":\t`");
51 (void) printf(fmt, 0x12);
52 (void) printf("'\n");
55 static void
56 fmtst1chk (const char *fmt)
58 (void) fputs(fmt, stdout);
59 (void) printf(":\t`");
60 (void) printf(fmt, 4, 0x12);
61 (void) printf("'\n");
64 static void
65 fmtst2chk (const char *fmt)
67 (void) fputs(fmt, stdout);
68 (void) printf(":\t`");
69 (void) printf(fmt, 4, 4, 0x12);
70 (void) printf("'\n");
73 /* This page is covered by the following copyright: */
75 /* (C) Copyright C E Chew
77 * Feel free to copy, use and distribute this software provided:
79 * 1. you do not pretend that you wrote it
80 * 2. you leave this copyright notice intact.
84 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
87 #define DEC -123
88 #define INT 255
89 #define UNS (~0)
91 /* Formatted Output Test
93 * This exercises the output formatting code.
96 static void
97 fp_test (void)
99 int i, j, k, l;
100 char buf[7];
101 char *prefix = buf;
102 char tp[20];
104 puts("\nFormatted output test");
105 printf("prefix 6d 6o 6x 6X 6u\n");
106 strcpy(prefix, "%");
107 for (i = 0; i < 2; i++) {
108 for (j = 0; j < 2; j++) {
109 for (k = 0; k < 2; k++) {
110 for (l = 0; l < 2; l++) {
111 strcpy(prefix, "%");
112 if (i == 0) strcat(prefix, "-");
113 if (j == 0) strcat(prefix, "+");
114 if (k == 0) strcat(prefix, "#");
115 if (l == 0) strcat(prefix, "0");
116 printf("%5s |", prefix);
117 strcpy(tp, prefix);
118 strcat(tp, "6d |");
119 printf(tp, DEC);
120 strcpy(tp, prefix);
121 strcat(tp, "6o |");
122 printf(tp, INT);
123 strcpy(tp, prefix);
124 strcat(tp, "6x |");
125 printf(tp, INT);
126 strcpy(tp, prefix);
127 strcat(tp, "6X |");
128 printf(tp, INT);
129 strcpy(tp, prefix);
130 strcat(tp, "6u |");
131 printf(tp, UNS);
132 printf("\n");
137 printf("%10s\n", (char *) NULL);
138 printf("%-10s\n", (char *) NULL);
139 printf("%.8f\n", DBL_MAX);
140 printf("%.8f\n", -DBL_MAX);
143 static int
144 do_test (void)
146 static char shortstr[] = "Hi, Z.";
147 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
148 I am ready for my first lesson today.";
149 int result = 0;
151 fmtchk("%.4x");
152 fmtchk("%04x");
153 fmtchk("%4.4x");
154 fmtchk("%04.4x");
155 fmtchk("%4.3x");
156 fmtchk("%04.3x");
158 fmtst1chk("%.*x");
159 fmtst1chk("%0*x");
160 fmtst2chk("%*.*x");
161 fmtst2chk("%0*.*x");
163 #ifndef BSD
164 printf("bad format:\t\"%b\"\n");
165 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
166 #endif
168 printf("decimal negative:\t\"%d\"\n", -2345);
169 printf("octal negative:\t\"%o\"\n", -2345);
170 printf("hex negative:\t\"%x\"\n", -2345);
171 printf("long decimal number:\t\"%ld\"\n", -123456L);
172 printf("long octal negative:\t\"%lo\"\n", -2345L);
173 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
174 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
175 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
176 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
177 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
179 printf("zero-padded string:\t\"%010s\"\n", shortstr);
180 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
181 printf("space-padded string:\t\"%10s\"\n", shortstr);
182 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
183 printf("null string:\t\"%s\"\n", (char *)NULL);
184 printf("limited string:\t\"%.22s\"\n", longstr);
186 printf("a-style max:\t\"%a\"\n", DBL_MAX);
187 printf("a-style -max:\t\"%a\"\n", -DBL_MAX);
188 printf("e-style >= 1:\t\"%e\"\n", 12.34);
189 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
190 printf("e-style < .1:\t\"%e\"\n", 0.001234);
191 printf("e-style big:\t\"%.60e\"\n", 1e20);
192 printf ("e-style == .1:\t\"%e\"\n", 0.1);
193 printf("f-style >= 1:\t\"%f\"\n", 12.34);
194 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
195 printf("f-style < .1:\t\"%f\"\n", 0.001234);
196 printf("g-style >= 1:\t\"%g\"\n", 12.34);
197 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
198 printf("g-style < .1:\t\"%g\"\n", 0.001234);
199 printf("g-style big:\t\"%.60g\"\n", 1e20);
201 printf (" %6.5f\n", .099999999860301614);
202 printf (" %6.5f\n", .1);
203 printf ("x%5.4fx\n", .5);
205 printf ("%#03x\n", 1);
207 printf ("something really insane: %.10000f\n", 1.0);
210 double d = FLT_MIN;
211 int niter = 17;
213 while (niter-- != 0)
214 printf ("%.17e\n", d / 2);
215 fflush (stdout);
218 printf ("%15.5e\n", 4.9406564584124654e-324);
220 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
221 printf (FORMAT, 0.0, 0.0, 0.0);
222 printf (FORMAT, 1.0, 1.0, 1.0);
223 printf (FORMAT, -1.0, -1.0, -1.0);
224 printf (FORMAT, 100.0, 100.0, 100.0);
225 printf (FORMAT, 1000.0, 1000.0, 1000.0);
226 printf (FORMAT, 10000.0, 10000.0, 10000.0);
227 printf (FORMAT, 12345.0, 12345.0, 12345.0);
228 printf (FORMAT, 100000.0, 100000.0, 100000.0);
229 printf (FORMAT, 123456.0, 123456.0, 123456.0);
230 #undef FORMAT
233 char buf[20];
234 char buf2[512];
235 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
236 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
237 buf);
238 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
239 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
242 fp_test ();
244 printf ("%e should be 1.234568e+06\n", 1234567.8);
245 printf ("%f should be 1234567.800000\n", 1234567.8);
246 printf ("%g should be 1.23457e+06\n", 1234567.8);
247 printf ("%g should be 123.456\n", 123.456);
248 printf ("%g should be 1e+06\n", 1000000.0);
249 printf ("%g should be 10\n", 10.0);
250 printf ("%g should be 0.02\n", 0.02);
252 #if 0
253 /* This test rather checks the way the compiler handles constant
254 folding. gcc behavior wrt to this changed in 3.2 so it is not a
255 portable test. */
257 double x=1.0;
258 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
260 #endif
263 char buf[200];
265 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
267 result |= strcmp (buf,
268 "onetwo three ");
270 puts (result != 0 ? "Test failed!" : "Test ok.");
274 char buf[200];
276 sprintf (buf, "%07Lo", 040000000000ll);
277 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
279 if (strcmp (buf, "40000000000") != 0)
281 result = 1;
282 fputs ("\tFAILED", stdout);
284 puts ("");
287 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
288 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
289 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
290 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
292 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
293 UCHAR_MAX + 2, UCHAR_MAX + 2);
294 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
295 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
296 UCHAR_MAX + 2, UCHAR_MAX + 2);
297 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
299 puts ("--- Should be no further output. ---");
300 rfg1 ();
301 rfg2 ();
302 rfg3 ();
305 char bytes[7];
306 char buf[20];
308 memset (bytes, '\xff', sizeof bytes);
309 sprintf (buf, "foo%hhn\n", &bytes[3]);
310 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
311 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
313 puts ("%hhn overwrite more bytes");
314 result = 1;
316 if (bytes[3] != 3)
318 puts ("%hhn wrote incorrect value");
319 result = 1;
323 return result != 0;
326 static void
327 rfg1 (void)
329 char buf[100];
331 sprintf (buf, "%5.s", "xyz");
332 if (strcmp (buf, " ") != 0)
333 printf ("got: '%s', expected: '%s'\n", buf, " ");
334 sprintf (buf, "%5.f", 33.3);
335 if (strcmp (buf, " 33") != 0)
336 printf ("got: '%s', expected: '%s'\n", buf, " 33");
337 sprintf (buf, "%8.e", 33.3e7);
338 if (strcmp (buf, " 3e+08") != 0)
339 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
340 sprintf (buf, "%8.E", 33.3e7);
341 if (strcmp (buf, " 3E+08") != 0)
342 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
343 sprintf (buf, "%.g", 33.3);
344 if (strcmp (buf, "3e+01") != 0)
345 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
346 sprintf (buf, "%.G", 33.3);
347 if (strcmp (buf, "3E+01") != 0)
348 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
351 static void
352 rfg2 (void)
354 int prec;
355 char buf[100];
357 prec = 0;
358 sprintf (buf, "%.*g", prec, 3.3);
359 if (strcmp (buf, "3") != 0)
360 printf ("got: '%s', expected: '%s'\n", buf, "3");
361 prec = 0;
362 sprintf (buf, "%.*G", prec, 3.3);
363 if (strcmp (buf, "3") != 0)
364 printf ("got: '%s', expected: '%s'\n", buf, "3");
365 prec = 0;
366 sprintf (buf, "%7.*G", prec, 3.33);
367 if (strcmp (buf, " 3") != 0)
368 printf ("got: '%s', expected: '%s'\n", buf, " 3");
369 prec = 3;
370 sprintf (buf, "%04.*o", prec, 33);
371 if (strcmp (buf, " 041") != 0)
372 printf ("got: '%s', expected: '%s'\n", buf, " 041");
373 prec = 7;
374 sprintf (buf, "%09.*u", prec, 33);
375 if (strcmp (buf, " 0000033") != 0)
376 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
377 prec = 3;
378 sprintf (buf, "%04.*x", prec, 33);
379 if (strcmp (buf, " 021") != 0)
380 printf ("got: '%s', expected: '%s'\n", buf, " 021");
381 prec = 3;
382 sprintf (buf, "%04.*X", prec, 33);
383 if (strcmp (buf, " 021") != 0)
384 printf ("got: '%s', expected: '%s'\n", buf, " 021");
387 static void
388 rfg3 (void)
390 char buf[100];
391 double g = 5.0000001;
392 unsigned long l = 1234567890;
393 double d = 321.7654321;
394 const char s[] = "test-string";
395 int i = 12345;
396 int h = 1234;
398 sprintf (buf,
399 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
400 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
401 if (strcmp (buf,
402 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
403 printf ("got: '%s', expected: '%s'\n", buf,
404 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");
407 #define TEST_FUNCTION do_test ()
408 #include "../test-skeleton.c"