Update.
[glibc.git] / stdio-common / tst-printf.c
blobc50c88202867648c5d0512dfd72d507fe4c82919
1 /* Copyright (C) 1991,92,93,95,96,97,98,99 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifdef BSD
20 #include </usr/include/stdio.h>
21 #define EXIT_SUCCESS 0
22 #else
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #endif
28 #include <float.h>
30 void rfg1 (void);
31 void rfg2 (void);
34 void
35 fmtchk (const char *fmt)
37 (void) fputs(fmt, stdout);
38 (void) printf(":\t`");
39 (void) printf(fmt, 0x12);
40 (void) printf("'\n");
43 void
44 fmtst1chk (const char *fmt)
46 (void) fputs(fmt, stdout);
47 (void) printf(":\t`");
48 (void) printf(fmt, 4, 0x12);
49 (void) printf("'\n");
52 void
53 fmtst2chk (const char *fmt)
55 (void) fputs(fmt, stdout);
56 (void) printf(":\t`");
57 (void) printf(fmt, 4, 4, 0x12);
58 (void) printf("'\n");
61 /* This page is covered by the following copyright: */
63 /* (C) Copyright C E Chew
65 * Feel free to copy, use and distribute this software provided:
67 * 1. you do not pretend that you wrote it
68 * 2. you leave this copyright notice intact.
72 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
75 #define DEC -123
76 #define INT 255
77 #define UNS (~0)
79 /* Formatted Output Test
81 * This exercises the output formatting code.
84 void
85 fp_test (void)
87 int i, j, k, l;
88 char buf[7];
89 char *prefix = buf;
90 char tp[20];
92 puts("\nFormatted output test");
93 printf("prefix 6d 6o 6x 6X 6u\n");
94 strcpy(prefix, "%");
95 for (i = 0; i < 2; i++) {
96 for (j = 0; j < 2; j++) {
97 for (k = 0; k < 2; k++) {
98 for (l = 0; l < 2; l++) {
99 strcpy(prefix, "%");
100 if (i == 0) strcat(prefix, "-");
101 if (j == 0) strcat(prefix, "+");
102 if (k == 0) strcat(prefix, "#");
103 if (l == 0) strcat(prefix, "0");
104 printf("%5s |", prefix);
105 strcpy(tp, prefix);
106 strcat(tp, "6d |");
107 printf(tp, DEC);
108 strcpy(tp, prefix);
109 strcat(tp, "6o |");
110 printf(tp, INT);
111 strcpy(tp, prefix);
112 strcat(tp, "6x |");
113 printf(tp, INT);
114 strcpy(tp, prefix);
115 strcat(tp, "6X |");
116 printf(tp, INT);
117 strcpy(tp, prefix);
118 strcat(tp, "6u |");
119 printf(tp, UNS);
120 printf("\n");
125 printf("%10s\n", (char *) NULL);
126 printf("%-10s\n", (char *) NULL);
130 main (int argc, char *argv[])
132 static char shortstr[] = "Hi, Z.";
133 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
134 I am ready for my first lesson today.";
135 int result = 0;
137 fmtchk("%.4x");
138 fmtchk("%04x");
139 fmtchk("%4.4x");
140 fmtchk("%04.4x");
141 fmtchk("%4.3x");
142 fmtchk("%04.3x");
144 fmtst1chk("%.*x");
145 fmtst1chk("%0*x");
146 fmtst2chk("%*.*x");
147 fmtst2chk("%0*.*x");
149 #ifndef BSD
150 printf("bad format:\t\"%b\"\n");
151 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
152 #endif
154 printf("decimal negative:\t\"%d\"\n", -2345);
155 printf("octal negative:\t\"%o\"\n", -2345);
156 printf("hex negative:\t\"%x\"\n", -2345);
157 printf("long decimal number:\t\"%ld\"\n", -123456L);
158 printf("long octal negative:\t\"%lo\"\n", -2345L);
159 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
160 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
161 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
162 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
163 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
165 printf("zero-padded string:\t\"%010s\"\n", shortstr);
166 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
167 printf("space-padded string:\t\"%10s\"\n", shortstr);
168 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
169 printf("null string:\t\"%s\"\n", (char *)NULL);
170 printf("limited string:\t\"%.22s\"\n", longstr);
172 printf("e-style >= 1:\t\"%e\"\n", 12.34);
173 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
174 printf("e-style < .1:\t\"%e\"\n", 0.001234);
175 printf("e-style big:\t\"%.60e\"\n", 1e20);
176 printf ("e-style == .1:\t\"%e\"\n", 0.1);
177 printf("f-style >= 1:\t\"%f\"\n", 12.34);
178 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
179 printf("f-style < .1:\t\"%f\"\n", 0.001234);
180 printf("g-style >= 1:\t\"%g\"\n", 12.34);
181 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
182 printf("g-style < .1:\t\"%g\"\n", 0.001234);
183 printf("g-style big:\t\"%.60g\"\n", 1e20);
185 printf (" %6.5f\n", .099999999860301614);
186 printf (" %6.5f\n", .1);
187 printf ("x%5.4fx\n", .5);
189 printf ("%#03x\n", 1);
192 double d = FLT_MIN;
193 int niter = 17;
195 while (niter-- != 0)
196 printf ("%.17e\n", d / 2);
197 fflush (stdout);
200 printf ("%15.5e\n", 4.9406564584124654e-324);
202 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
203 printf (FORMAT, 0.0, 0.0, 0.0);
204 printf (FORMAT, 1.0, 1.0, 1.0);
205 printf (FORMAT, -1.0, -1.0, -1.0);
206 printf (FORMAT, 100.0, 100.0, 100.0);
207 printf (FORMAT, 1000.0, 1000.0, 1000.0);
208 printf (FORMAT, 10000.0, 10000.0, 10000.0);
209 printf (FORMAT, 12345.0, 12345.0, 12345.0);
210 printf (FORMAT, 100000.0, 100000.0, 100000.0);
211 printf (FORMAT, 123456.0, 123456.0, 123456.0);
212 #undef FORMAT
215 char buf[20];
216 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
217 snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
220 fp_test ();
222 printf ("%e should be 1.234568e+06\n", 1234567.8);
223 printf ("%f should be 1234567.800000\n", 1234567.8);
224 printf ("%g should be 1.23457e+06\n", 1234567.8);
225 printf ("%g should be 123.456\n", 123.456);
226 printf ("%g should be 1e+06\n", 1000000.0);
227 printf ("%g should be 10\n", 10.0);
228 printf ("%g should be 0.02\n", 0.02);
231 double x=1.0;
232 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
235 puts ("--- Should be no further output. ---");
236 rfg1 ();
237 rfg2 ();
240 char buf[200];
242 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
244 result |= strcmp (buf,
245 "onetwo three ");
247 puts (result != 0 ? "Test failed!" : "Test ok.");
251 char buf[200];
253 sprintf (buf, "%07Lo", 040000000000ll);
254 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s\n", buf);
256 if (strcmp (buf, "40000000000") != 0)
258 result = 1;
259 fputs ("\tFAILED", stdout);
261 puts ("");
264 return result != 0;
267 void
268 rfg1 (void)
270 char buf[100];
272 sprintf (buf, "%5.s", "xyz");
273 if (strcmp (buf, " ") != 0)
274 printf ("got: '%s', expected: '%s'\n", buf, " ");
275 sprintf (buf, "%5.f", 33.3);
276 if (strcmp (buf, " 33") != 0)
277 printf ("got: '%s', expected: '%s'\n", buf, " 33");
278 sprintf (buf, "%8.e", 33.3e7);
279 if (strcmp (buf, " 3e+08") != 0)
280 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
281 sprintf (buf, "%8.E", 33.3e7);
282 if (strcmp (buf, " 3E+08") != 0)
283 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
284 sprintf (buf, "%.g", 33.3);
285 if (strcmp (buf, "3e+01") != 0)
286 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
287 sprintf (buf, "%.G", 33.3);
288 if (strcmp (buf, "3E+01") != 0)
289 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
292 void
293 rfg2 (void)
295 int prec;
296 char buf[100];
298 prec = 0;
299 sprintf (buf, "%.*g", prec, 3.3);
300 if (strcmp (buf, "3") != 0)
301 printf ("got: '%s', expected: '%s'\n", buf, "3");
302 prec = 0;
303 sprintf (buf, "%.*G", prec, 3.3);
304 if (strcmp (buf, "3") != 0)
305 printf ("got: '%s', expected: '%s'\n", buf, "3");
306 prec = 0;
307 sprintf (buf, "%7.*G", prec, 3.33);
308 if (strcmp (buf, " 3") != 0)
309 printf ("got: '%s', expected: '%s'\n", buf, " 3");
310 prec = 3;
311 sprintf (buf, "%04.*o", prec, 33);
312 if (strcmp (buf, " 041") != 0)
313 printf ("got: '%s', expected: '%s'\n", buf, " 041");
314 prec = 7;
315 sprintf (buf, "%09.*u", prec, 33);
316 if (strcmp (buf, " 0000033") != 0)
317 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
318 prec = 3;
319 sprintf (buf, "%04.*x", prec, 33);
320 if (strcmp (buf, " 021") != 0)
321 printf ("got: '%s', expected: '%s'\n", buf, " 021");
322 prec = 3;
323 sprintf (buf, "%04.*X", prec, 33);
324 if (strcmp (buf, " 021") != 0)
325 printf ("got: '%s', expected: '%s'\n", buf, " 021");