Update copyright notices with scripts/update-copyrights
[glibc.git] / stdio-common / tst-printf.c
blob10673ce77d16be8e7815c4ace29e222a56fced9e
1 /* Copyright (C) 1991-2014 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>
30 static void rfg1 (void);
31 static void rfg2 (void);
32 static void rfg3 (void);
35 static void
36 fmtchk (const char *fmt)
38 (void) fputs(fmt, stdout);
39 (void) printf(":\t`");
40 (void) printf(fmt, 0x12);
41 (void) printf("'\n");
44 static void
45 fmtst1chk (const char *fmt)
47 (void) fputs(fmt, stdout);
48 (void) printf(":\t`");
49 (void) printf(fmt, 4, 0x12);
50 (void) printf("'\n");
53 static void
54 fmtst2chk (const char *fmt)
56 (void) fputs(fmt, stdout);
57 (void) printf(":\t`");
58 (void) printf(fmt, 4, 4, 0x12);
59 (void) printf("'\n");
62 /* This page is covered by the following copyright: */
64 /* (C) Copyright C E Chew
66 * Feel free to copy, use and distribute this software provided:
68 * 1. you do not pretend that you wrote it
69 * 2. you leave this copyright notice intact.
73 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
76 #define DEC -123
77 #define INT 255
78 #define UNS (~0)
80 /* Formatted Output Test
82 * This exercises the output formatting code.
85 static void
86 fp_test (void)
88 int i, j, k, l;
89 char buf[7];
90 char *prefix = buf;
91 char tp[20];
93 puts("\nFormatted output test");
94 printf("prefix 6d 6o 6x 6X 6u\n");
95 strcpy(prefix, "%");
96 for (i = 0; i < 2; i++) {
97 for (j = 0; j < 2; j++) {
98 for (k = 0; k < 2; k++) {
99 for (l = 0; l < 2; l++) {
100 strcpy(prefix, "%");
101 if (i == 0) strcat(prefix, "-");
102 if (j == 0) strcat(prefix, "+");
103 if (k == 0) strcat(prefix, "#");
104 if (l == 0) strcat(prefix, "0");
105 printf("%5s |", prefix);
106 strcpy(tp, prefix);
107 strcat(tp, "6d |");
108 printf(tp, DEC);
109 strcpy(tp, prefix);
110 strcat(tp, "6o |");
111 printf(tp, INT);
112 strcpy(tp, prefix);
113 strcat(tp, "6x |");
114 printf(tp, INT);
115 strcpy(tp, prefix);
116 strcat(tp, "6X |");
117 printf(tp, INT);
118 strcpy(tp, prefix);
119 strcat(tp, "6u |");
120 printf(tp, UNS);
121 printf("\n");
126 printf("%10s\n", (char *) NULL);
127 printf("%-10s\n", (char *) NULL);
131 main (int argc, char *argv[])
133 static char shortstr[] = "Hi, Z.";
134 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
135 I am ready for my first lesson today.";
136 int result = 0;
138 fmtchk("%.4x");
139 fmtchk("%04x");
140 fmtchk("%4.4x");
141 fmtchk("%04.4x");
142 fmtchk("%4.3x");
143 fmtchk("%04.3x");
145 fmtst1chk("%.*x");
146 fmtst1chk("%0*x");
147 fmtst2chk("%*.*x");
148 fmtst2chk("%0*.*x");
150 #ifndef BSD
151 printf("bad format:\t\"%b\"\n");
152 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
153 #endif
155 printf("decimal negative:\t\"%d\"\n", -2345);
156 printf("octal negative:\t\"%o\"\n", -2345);
157 printf("hex negative:\t\"%x\"\n", -2345);
158 printf("long decimal number:\t\"%ld\"\n", -123456L);
159 printf("long octal negative:\t\"%lo\"\n", -2345L);
160 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
161 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
162 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
163 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
164 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
166 printf("zero-padded string:\t\"%010s\"\n", shortstr);
167 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
168 printf("space-padded string:\t\"%10s\"\n", shortstr);
169 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
170 printf("null string:\t\"%s\"\n", (char *)NULL);
171 printf("limited string:\t\"%.22s\"\n", longstr);
173 printf("e-style >= 1:\t\"%e\"\n", 12.34);
174 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
175 printf("e-style < .1:\t\"%e\"\n", 0.001234);
176 printf("e-style big:\t\"%.60e\"\n", 1e20);
177 printf ("e-style == .1:\t\"%e\"\n", 0.1);
178 printf("f-style >= 1:\t\"%f\"\n", 12.34);
179 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
180 printf("f-style < .1:\t\"%f\"\n", 0.001234);
181 printf("g-style >= 1:\t\"%g\"\n", 12.34);
182 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
183 printf("g-style < .1:\t\"%g\"\n", 0.001234);
184 printf("g-style big:\t\"%.60g\"\n", 1e20);
186 printf (" %6.5f\n", .099999999860301614);
187 printf (" %6.5f\n", .1);
188 printf ("x%5.4fx\n", .5);
190 printf ("%#03x\n", 1);
192 printf ("something really insane: %.10000f\n", 1.0);
195 double d = FLT_MIN;
196 int niter = 17;
198 while (niter-- != 0)
199 printf ("%.17e\n", d / 2);
200 fflush (stdout);
203 printf ("%15.5e\n", 4.9406564584124654e-324);
205 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
206 printf (FORMAT, 0.0, 0.0, 0.0);
207 printf (FORMAT, 1.0, 1.0, 1.0);
208 printf (FORMAT, -1.0, -1.0, -1.0);
209 printf (FORMAT, 100.0, 100.0, 100.0);
210 printf (FORMAT, 1000.0, 1000.0, 1000.0);
211 printf (FORMAT, 10000.0, 10000.0, 10000.0);
212 printf (FORMAT, 12345.0, 12345.0, 12345.0);
213 printf (FORMAT, 100000.0, 100000.0, 100000.0);
214 printf (FORMAT, 123456.0, 123456.0, 123456.0);
215 #undef FORMAT
218 char buf[20];
219 char buf2[512];
220 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
221 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
222 buf);
223 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
224 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
227 fp_test ();
229 printf ("%e should be 1.234568e+06\n", 1234567.8);
230 printf ("%f should be 1234567.800000\n", 1234567.8);
231 printf ("%g should be 1.23457e+06\n", 1234567.8);
232 printf ("%g should be 123.456\n", 123.456);
233 printf ("%g should be 1e+06\n", 1000000.0);
234 printf ("%g should be 10\n", 10.0);
235 printf ("%g should be 0.02\n", 0.02);
237 #if 0
238 /* This test rather checks the way the compiler handles constant
239 folding. gcc behavior wrt to this changed in 3.2 so it is not a
240 portable test. */
242 double x=1.0;
243 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
245 #endif
248 char buf[200];
250 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
252 result |= strcmp (buf,
253 "onetwo three ");
255 puts (result != 0 ? "Test failed!" : "Test ok.");
259 char buf[200];
261 sprintf (buf, "%07Lo", 040000000000ll);
262 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
264 if (strcmp (buf, "40000000000") != 0)
266 result = 1;
267 fputs ("\tFAILED", stdout);
269 puts ("");
272 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
273 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
274 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
275 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
277 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
278 UCHAR_MAX + 2, UCHAR_MAX + 2);
279 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
280 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
281 UCHAR_MAX + 2, UCHAR_MAX + 2);
282 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
284 puts ("--- Should be no further output. ---");
285 rfg1 ();
286 rfg2 ();
287 rfg3 ();
290 char bytes[7];
291 char buf[20];
293 memset (bytes, '\xff', sizeof bytes);
294 sprintf (buf, "foo%hhn\n", &bytes[3]);
295 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
296 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
298 puts ("%hhn overwrite more bytes");
299 result = 1;
301 if (bytes[3] != 3)
303 puts ("%hhn wrote incorrect value");
304 result = 1;
308 return result != 0;
311 static void
312 rfg1 (void)
314 char buf[100];
316 sprintf (buf, "%5.s", "xyz");
317 if (strcmp (buf, " ") != 0)
318 printf ("got: '%s', expected: '%s'\n", buf, " ");
319 sprintf (buf, "%5.f", 33.3);
320 if (strcmp (buf, " 33") != 0)
321 printf ("got: '%s', expected: '%s'\n", buf, " 33");
322 sprintf (buf, "%8.e", 33.3e7);
323 if (strcmp (buf, " 3e+08") != 0)
324 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
325 sprintf (buf, "%8.E", 33.3e7);
326 if (strcmp (buf, " 3E+08") != 0)
327 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
328 sprintf (buf, "%.g", 33.3);
329 if (strcmp (buf, "3e+01") != 0)
330 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
331 sprintf (buf, "%.G", 33.3);
332 if (strcmp (buf, "3E+01") != 0)
333 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
336 static void
337 rfg2 (void)
339 int prec;
340 char buf[100];
342 prec = 0;
343 sprintf (buf, "%.*g", prec, 3.3);
344 if (strcmp (buf, "3") != 0)
345 printf ("got: '%s', expected: '%s'\n", buf, "3");
346 prec = 0;
347 sprintf (buf, "%.*G", prec, 3.3);
348 if (strcmp (buf, "3") != 0)
349 printf ("got: '%s', expected: '%s'\n", buf, "3");
350 prec = 0;
351 sprintf (buf, "%7.*G", prec, 3.33);
352 if (strcmp (buf, " 3") != 0)
353 printf ("got: '%s', expected: '%s'\n", buf, " 3");
354 prec = 3;
355 sprintf (buf, "%04.*o", prec, 33);
356 if (strcmp (buf, " 041") != 0)
357 printf ("got: '%s', expected: '%s'\n", buf, " 041");
358 prec = 7;
359 sprintf (buf, "%09.*u", prec, 33);
360 if (strcmp (buf, " 0000033") != 0)
361 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
362 prec = 3;
363 sprintf (buf, "%04.*x", prec, 33);
364 if (strcmp (buf, " 021") != 0)
365 printf ("got: '%s', expected: '%s'\n", buf, " 021");
366 prec = 3;
367 sprintf (buf, "%04.*X", prec, 33);
368 if (strcmp (buf, " 021") != 0)
369 printf ("got: '%s', expected: '%s'\n", buf, " 021");
372 static void
373 rfg3 (void)
375 char buf[100];
376 double g = 5.0000001;
377 unsigned long l = 1234567890;
378 double d = 321.7654321;
379 const char s[] = "test-string";
380 int i = 12345;
381 int h = 1234;
383 sprintf (buf,
384 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
385 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
386 if (strcmp (buf,
387 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
388 printf ("got: '%s', expected: '%s'\n", buf,
389 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");