Fix spelling in memusagestat.c
[glibc.git] / stdio-common / tst-printf.c
blob06fa38ab55445f85606a3c07225cb8de96b5bba3
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifdef BSD
21 #include </usr/include/stdio.h>
22 #define EXIT_SUCCESS 0
23 #else
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #endif
30 #include <float.h>
32 static void rfg1 (void);
33 static void rfg2 (void);
34 static void rfg3 (void);
37 static void
38 fmtchk (const char *fmt)
40 (void) fputs(fmt, stdout);
41 (void) printf(":\t`");
42 (void) printf(fmt, 0x12);
43 (void) printf("'\n");
46 static void
47 fmtst1chk (const char *fmt)
49 (void) fputs(fmt, stdout);
50 (void) printf(":\t`");
51 (void) printf(fmt, 4, 0x12);
52 (void) printf("'\n");
55 static void
56 fmtst2chk (const char *fmt)
58 (void) fputs(fmt, stdout);
59 (void) printf(":\t`");
60 (void) printf(fmt, 4, 4, 0x12);
61 (void) printf("'\n");
64 /* This page is covered by the following copyright: */
66 /* (C) Copyright C E Chew
68 * Feel free to copy, use and distribute this software provided:
70 * 1. you do not pretend that you wrote it
71 * 2. you leave this copyright notice intact.
75 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
78 #define DEC -123
79 #define INT 255
80 #define UNS (~0)
82 /* Formatted Output Test
84 * This exercises the output formatting code.
87 static void
88 fp_test (void)
90 int i, j, k, l;
91 char buf[7];
92 char *prefix = buf;
93 char tp[20];
95 puts("\nFormatted output test");
96 printf("prefix 6d 6o 6x 6X 6u\n");
97 strcpy(prefix, "%");
98 for (i = 0; i < 2; i++) {
99 for (j = 0; j < 2; j++) {
100 for (k = 0; k < 2; k++) {
101 for (l = 0; l < 2; l++) {
102 strcpy(prefix, "%");
103 if (i == 0) strcat(prefix, "-");
104 if (j == 0) strcat(prefix, "+");
105 if (k == 0) strcat(prefix, "#");
106 if (l == 0) strcat(prefix, "0");
107 printf("%5s |", prefix);
108 strcpy(tp, prefix);
109 strcat(tp, "6d |");
110 printf(tp, DEC);
111 strcpy(tp, prefix);
112 strcat(tp, "6o |");
113 printf(tp, INT);
114 strcpy(tp, prefix);
115 strcat(tp, "6x |");
116 printf(tp, INT);
117 strcpy(tp, prefix);
118 strcat(tp, "6X |");
119 printf(tp, INT);
120 strcpy(tp, prefix);
121 strcat(tp, "6u |");
122 printf(tp, UNS);
123 printf("\n");
128 printf("%10s\n", (char *) NULL);
129 printf("%-10s\n", (char *) NULL);
133 main (int argc, char *argv[])
135 static char shortstr[] = "Hi, Z.";
136 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
137 I am ready for my first lesson today.";
138 int result = 0;
140 fmtchk("%.4x");
141 fmtchk("%04x");
142 fmtchk("%4.4x");
143 fmtchk("%04.4x");
144 fmtchk("%4.3x");
145 fmtchk("%04.3x");
147 fmtst1chk("%.*x");
148 fmtst1chk("%0*x");
149 fmtst2chk("%*.*x");
150 fmtst2chk("%0*.*x");
152 #ifndef BSD
153 printf("bad format:\t\"%b\"\n");
154 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
155 #endif
157 printf("decimal negative:\t\"%d\"\n", -2345);
158 printf("octal negative:\t\"%o\"\n", -2345);
159 printf("hex negative:\t\"%x\"\n", -2345);
160 printf("long decimal number:\t\"%ld\"\n", -123456L);
161 printf("long octal negative:\t\"%lo\"\n", -2345L);
162 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
163 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
164 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
165 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
166 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
168 printf("zero-padded string:\t\"%010s\"\n", shortstr);
169 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
170 printf("space-padded string:\t\"%10s\"\n", shortstr);
171 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
172 printf("null string:\t\"%s\"\n", (char *)NULL);
173 printf("limited string:\t\"%.22s\"\n", longstr);
175 printf("e-style >= 1:\t\"%e\"\n", 12.34);
176 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
177 printf("e-style < .1:\t\"%e\"\n", 0.001234);
178 printf("e-style big:\t\"%.60e\"\n", 1e20);
179 printf ("e-style == .1:\t\"%e\"\n", 0.1);
180 printf("f-style >= 1:\t\"%f\"\n", 12.34);
181 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
182 printf("f-style < .1:\t\"%f\"\n", 0.001234);
183 printf("g-style >= 1:\t\"%g\"\n", 12.34);
184 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
185 printf("g-style < .1:\t\"%g\"\n", 0.001234);
186 printf("g-style big:\t\"%.60g\"\n", 1e20);
188 printf (" %6.5f\n", .099999999860301614);
189 printf (" %6.5f\n", .1);
190 printf ("x%5.4fx\n", .5);
192 printf ("%#03x\n", 1);
194 printf ("something really insane: %.10000f\n", 1.0);
197 double d = FLT_MIN;
198 int niter = 17;
200 while (niter-- != 0)
201 printf ("%.17e\n", d / 2);
202 fflush (stdout);
205 printf ("%15.5e\n", 4.9406564584124654e-324);
207 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
208 printf (FORMAT, 0.0, 0.0, 0.0);
209 printf (FORMAT, 1.0, 1.0, 1.0);
210 printf (FORMAT, -1.0, -1.0, -1.0);
211 printf (FORMAT, 100.0, 100.0, 100.0);
212 printf (FORMAT, 1000.0, 1000.0, 1000.0);
213 printf (FORMAT, 10000.0, 10000.0, 10000.0);
214 printf (FORMAT, 12345.0, 12345.0, 12345.0);
215 printf (FORMAT, 100000.0, 100000.0, 100000.0);
216 printf (FORMAT, 123456.0, 123456.0, 123456.0);
217 #undef FORMAT
220 char buf[20];
221 char buf2[512];
222 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
223 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
224 buf);
225 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
226 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
229 fp_test ();
231 printf ("%e should be 1.234568e+06\n", 1234567.8);
232 printf ("%f should be 1234567.800000\n", 1234567.8);
233 printf ("%g should be 1.23457e+06\n", 1234567.8);
234 printf ("%g should be 123.456\n", 123.456);
235 printf ("%g should be 1e+06\n", 1000000.0);
236 printf ("%g should be 10\n", 10.0);
237 printf ("%g should be 0.02\n", 0.02);
239 #if 0
240 /* This test rather checks the way the compiler handles constant
241 folding. gcc behavior wrt to this changed in 3.2 so it is not a
242 portable test. */
244 double x=1.0;
245 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
247 #endif
250 char buf[200];
252 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
254 result |= strcmp (buf,
255 "onetwo three ");
257 puts (result != 0 ? "Test failed!" : "Test ok.");
261 char buf[200];
263 sprintf (buf, "%07Lo", 040000000000ll);
264 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
266 if (strcmp (buf, "40000000000") != 0)
268 result = 1;
269 fputs ("\tFAILED", stdout);
271 puts ("");
274 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
275 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
276 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
277 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
279 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
280 UCHAR_MAX + 2, UCHAR_MAX + 2);
281 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
282 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
283 UCHAR_MAX + 2, UCHAR_MAX + 2);
284 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
286 puts ("--- Should be no further output. ---");
287 rfg1 ();
288 rfg2 ();
289 rfg3 ();
292 char bytes[7];
293 char buf[20];
295 memset (bytes, '\xff', sizeof bytes);
296 sprintf (buf, "foo%hhn\n", &bytes[3]);
297 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
298 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
300 puts ("%hhn overwrite more bytes");
301 result = 1;
303 if (bytes[3] != 3)
305 puts ("%hhn wrote incorrect value");
306 result = 1;
310 return result != 0;
313 static void
314 rfg1 (void)
316 char buf[100];
318 sprintf (buf, "%5.s", "xyz");
319 if (strcmp (buf, " ") != 0)
320 printf ("got: '%s', expected: '%s'\n", buf, " ");
321 sprintf (buf, "%5.f", 33.3);
322 if (strcmp (buf, " 33") != 0)
323 printf ("got: '%s', expected: '%s'\n", buf, " 33");
324 sprintf (buf, "%8.e", 33.3e7);
325 if (strcmp (buf, " 3e+08") != 0)
326 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
327 sprintf (buf, "%8.E", 33.3e7);
328 if (strcmp (buf, " 3E+08") != 0)
329 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
330 sprintf (buf, "%.g", 33.3);
331 if (strcmp (buf, "3e+01") != 0)
332 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
333 sprintf (buf, "%.G", 33.3);
334 if (strcmp (buf, "3E+01") != 0)
335 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
338 static void
339 rfg2 (void)
341 int prec;
342 char buf[100];
344 prec = 0;
345 sprintf (buf, "%.*g", prec, 3.3);
346 if (strcmp (buf, "3") != 0)
347 printf ("got: '%s', expected: '%s'\n", buf, "3");
348 prec = 0;
349 sprintf (buf, "%.*G", prec, 3.3);
350 if (strcmp (buf, "3") != 0)
351 printf ("got: '%s', expected: '%s'\n", buf, "3");
352 prec = 0;
353 sprintf (buf, "%7.*G", prec, 3.33);
354 if (strcmp (buf, " 3") != 0)
355 printf ("got: '%s', expected: '%s'\n", buf, " 3");
356 prec = 3;
357 sprintf (buf, "%04.*o", prec, 33);
358 if (strcmp (buf, " 041") != 0)
359 printf ("got: '%s', expected: '%s'\n", buf, " 041");
360 prec = 7;
361 sprintf (buf, "%09.*u", prec, 33);
362 if (strcmp (buf, " 0000033") != 0)
363 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
364 prec = 3;
365 sprintf (buf, "%04.*x", prec, 33);
366 if (strcmp (buf, " 021") != 0)
367 printf ("got: '%s', expected: '%s'\n", buf, " 021");
368 prec = 3;
369 sprintf (buf, "%04.*X", prec, 33);
370 if (strcmp (buf, " 021") != 0)
371 printf ("got: '%s', expected: '%s'\n", buf, " 021");
374 static void
375 rfg3 (void)
377 char buf[100];
378 double g = 5.0000001;
379 unsigned long l = 1234567890;
380 double d = 321.7654321;
381 const char s[] = "test-string";
382 int i = 12345;
383 int h = 1234;
385 sprintf (buf,
386 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
387 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
388 if (strcmp (buf,
389 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
390 printf ("got: '%s', expected: '%s'\n", buf,
391 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");