Hurd: Fix value of __libc_stack_end
[glibc.git] / stdio-common / tst-printf.c
blobdbc8019ca52712d54e50d6f0f158a709ef7f0912
1 /* Copyright (C) 1991,92,93,95,96,97,98,99, 2000, 2002, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifdef BSD
20 #include </usr/include/stdio.h>
21 #define EXIT_SUCCESS 0
22 #else
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #endif
29 #include <float.h>
31 static void rfg1 (void);
32 static void rfg2 (void);
33 static void rfg3 (void);
36 static void
37 fmtchk (const char *fmt)
39 (void) fputs(fmt, stdout);
40 (void) printf(":\t`");
41 (void) printf(fmt, 0x12);
42 (void) printf("'\n");
45 static void
46 fmtst1chk (const char *fmt)
48 (void) fputs(fmt, stdout);
49 (void) printf(":\t`");
50 (void) printf(fmt, 4, 0x12);
51 (void) printf("'\n");
54 static void
55 fmtst2chk (const char *fmt)
57 (void) fputs(fmt, stdout);
58 (void) printf(":\t`");
59 (void) printf(fmt, 4, 4, 0x12);
60 (void) printf("'\n");
63 /* This page is covered by the following copyright: */
65 /* (C) Copyright C E Chew
67 * Feel free to copy, use and distribute this software provided:
69 * 1. you do not pretend that you wrote it
70 * 2. you leave this copyright notice intact.
74 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
77 #define DEC -123
78 #define INT 255
79 #define UNS (~0)
81 /* Formatted Output Test
83 * This exercises the output formatting code.
86 static void
87 fp_test (void)
89 int i, j, k, l;
90 char buf[7];
91 char *prefix = buf;
92 char tp[20];
94 puts("\nFormatted output test");
95 printf("prefix 6d 6o 6x 6X 6u\n");
96 strcpy(prefix, "%");
97 for (i = 0; i < 2; i++) {
98 for (j = 0; j < 2; j++) {
99 for (k = 0; k < 2; k++) {
100 for (l = 0; l < 2; l++) {
101 strcpy(prefix, "%");
102 if (i == 0) strcat(prefix, "-");
103 if (j == 0) strcat(prefix, "+");
104 if (k == 0) strcat(prefix, "#");
105 if (l == 0) strcat(prefix, "0");
106 printf("%5s |", prefix);
107 strcpy(tp, prefix);
108 strcat(tp, "6d |");
109 printf(tp, DEC);
110 strcpy(tp, prefix);
111 strcat(tp, "6o |");
112 printf(tp, INT);
113 strcpy(tp, prefix);
114 strcat(tp, "6x |");
115 printf(tp, INT);
116 strcpy(tp, prefix);
117 strcat(tp, "6X |");
118 printf(tp, INT);
119 strcpy(tp, prefix);
120 strcat(tp, "6u |");
121 printf(tp, UNS);
122 printf("\n");
127 printf("%10s\n", (char *) NULL);
128 printf("%-10s\n", (char *) NULL);
132 main (int argc, char *argv[])
134 static char shortstr[] = "Hi, Z.";
135 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
136 I am ready for my first lesson today.";
137 int result = 0;
139 fmtchk("%.4x");
140 fmtchk("%04x");
141 fmtchk("%4.4x");
142 fmtchk("%04.4x");
143 fmtchk("%4.3x");
144 fmtchk("%04.3x");
146 fmtst1chk("%.*x");
147 fmtst1chk("%0*x");
148 fmtst2chk("%*.*x");
149 fmtst2chk("%0*.*x");
151 #ifndef BSD
152 printf("bad format:\t\"%b\"\n");
153 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
154 #endif
156 printf("decimal negative:\t\"%d\"\n", -2345);
157 printf("octal negative:\t\"%o\"\n", -2345);
158 printf("hex negative:\t\"%x\"\n", -2345);
159 printf("long decimal number:\t\"%ld\"\n", -123456L);
160 printf("long octal negative:\t\"%lo\"\n", -2345L);
161 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
162 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
163 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
164 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
165 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
167 printf("zero-padded string:\t\"%010s\"\n", shortstr);
168 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
169 printf("space-padded string:\t\"%10s\"\n", shortstr);
170 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
171 printf("null string:\t\"%s\"\n", (char *)NULL);
172 printf("limited string:\t\"%.22s\"\n", longstr);
174 printf("e-style >= 1:\t\"%e\"\n", 12.34);
175 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
176 printf("e-style < .1:\t\"%e\"\n", 0.001234);
177 printf("e-style big:\t\"%.60e\"\n", 1e20);
178 printf ("e-style == .1:\t\"%e\"\n", 0.1);
179 printf("f-style >= 1:\t\"%f\"\n", 12.34);
180 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
181 printf("f-style < .1:\t\"%f\"\n", 0.001234);
182 printf("g-style >= 1:\t\"%g\"\n", 12.34);
183 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
184 printf("g-style < .1:\t\"%g\"\n", 0.001234);
185 printf("g-style big:\t\"%.60g\"\n", 1e20);
187 printf (" %6.5f\n", .099999999860301614);
188 printf (" %6.5f\n", .1);
189 printf ("x%5.4fx\n", .5);
191 printf ("%#03x\n", 1);
193 printf ("something really insane: %.10000f\n", 1.0);
196 double d = FLT_MIN;
197 int niter = 17;
199 while (niter-- != 0)
200 printf ("%.17e\n", d / 2);
201 fflush (stdout);
204 printf ("%15.5e\n", 4.9406564584124654e-324);
206 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
207 printf (FORMAT, 0.0, 0.0, 0.0);
208 printf (FORMAT, 1.0, 1.0, 1.0);
209 printf (FORMAT, -1.0, -1.0, -1.0);
210 printf (FORMAT, 100.0, 100.0, 100.0);
211 printf (FORMAT, 1000.0, 1000.0, 1000.0);
212 printf (FORMAT, 10000.0, 10000.0, 10000.0);
213 printf (FORMAT, 12345.0, 12345.0, 12345.0);
214 printf (FORMAT, 100000.0, 100000.0, 100000.0);
215 printf (FORMAT, 123456.0, 123456.0, 123456.0);
216 #undef FORMAT
219 char buf[20];
220 char buf2[512];
221 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
222 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
223 buf);
224 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
225 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
228 fp_test ();
230 printf ("%e should be 1.234568e+06\n", 1234567.8);
231 printf ("%f should be 1234567.800000\n", 1234567.8);
232 printf ("%g should be 1.23457e+06\n", 1234567.8);
233 printf ("%g should be 123.456\n", 123.456);
234 printf ("%g should be 1e+06\n", 1000000.0);
235 printf ("%g should be 10\n", 10.0);
236 printf ("%g should be 0.02\n", 0.02);
238 #if 0
239 /* This test rather checks the way the compiler handles constant
240 folding. gcc behavior wrt to this changed in 3.2 so it is not a
241 portable test. */
243 double x=1.0;
244 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
246 #endif
249 char buf[200];
251 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
253 result |= strcmp (buf,
254 "onetwo three ");
256 puts (result != 0 ? "Test failed!" : "Test ok.");
260 char buf[200];
262 sprintf (buf, "%07Lo", 040000000000ll);
263 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
265 if (strcmp (buf, "40000000000") != 0)
267 result = 1;
268 fputs ("\tFAILED", stdout);
270 puts ("");
273 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
274 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
275 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
276 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
278 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
279 UCHAR_MAX + 2, UCHAR_MAX + 2);
280 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
281 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
282 UCHAR_MAX + 2, UCHAR_MAX + 2);
283 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
285 puts ("--- Should be no further output. ---");
286 rfg1 ();
287 rfg2 ();
288 rfg3 ();
291 char bytes[7];
292 char buf[20];
294 memset (bytes, '\xff', sizeof bytes);
295 sprintf (buf, "foo%hhn\n", &bytes[3]);
296 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
297 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
299 puts ("%hhn overwrite more bytes");
300 result = 1;
302 if (bytes[3] != 3)
304 puts ("%hhn wrote incorrect value");
305 result = 1;
309 return result != 0;
312 static void
313 rfg1 (void)
315 char buf[100];
317 sprintf (buf, "%5.s", "xyz");
318 if (strcmp (buf, " ") != 0)
319 printf ("got: '%s', expected: '%s'\n", buf, " ");
320 sprintf (buf, "%5.f", 33.3);
321 if (strcmp (buf, " 33") != 0)
322 printf ("got: '%s', expected: '%s'\n", buf, " 33");
323 sprintf (buf, "%8.e", 33.3e7);
324 if (strcmp (buf, " 3e+08") != 0)
325 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
326 sprintf (buf, "%8.E", 33.3e7);
327 if (strcmp (buf, " 3E+08") != 0)
328 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
329 sprintf (buf, "%.g", 33.3);
330 if (strcmp (buf, "3e+01") != 0)
331 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
332 sprintf (buf, "%.G", 33.3);
333 if (strcmp (buf, "3E+01") != 0)
334 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
337 static void
338 rfg2 (void)
340 int prec;
341 char buf[100];
343 prec = 0;
344 sprintf (buf, "%.*g", prec, 3.3);
345 if (strcmp (buf, "3") != 0)
346 printf ("got: '%s', expected: '%s'\n", buf, "3");
347 prec = 0;
348 sprintf (buf, "%.*G", prec, 3.3);
349 if (strcmp (buf, "3") != 0)
350 printf ("got: '%s', expected: '%s'\n", buf, "3");
351 prec = 0;
352 sprintf (buf, "%7.*G", prec, 3.33);
353 if (strcmp (buf, " 3") != 0)
354 printf ("got: '%s', expected: '%s'\n", buf, " 3");
355 prec = 3;
356 sprintf (buf, "%04.*o", prec, 33);
357 if (strcmp (buf, " 041") != 0)
358 printf ("got: '%s', expected: '%s'\n", buf, " 041");
359 prec = 7;
360 sprintf (buf, "%09.*u", prec, 33);
361 if (strcmp (buf, " 0000033") != 0)
362 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
363 prec = 3;
364 sprintf (buf, "%04.*x", prec, 33);
365 if (strcmp (buf, " 021") != 0)
366 printf ("got: '%s', expected: '%s'\n", buf, " 021");
367 prec = 3;
368 sprintf (buf, "%04.*X", prec, 33);
369 if (strcmp (buf, " 021") != 0)
370 printf ("got: '%s', expected: '%s'\n", buf, " 021");
373 static void
374 rfg3 (void)
376 char buf[100];
377 double g = 5.0000001;
378 unsigned long l = 1234567890;
379 double d = 321.7654321;
380 const char s[] = "test-string";
381 int i = 12345;
382 int h = 1234;
384 sprintf (buf,
385 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
386 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
387 if (strcmp (buf,
388 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
389 printf ("got: '%s', expected: '%s'\n", buf,
390 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");