Update to 2.1.x development version
[glibc.git] / stdio-common / tst-printf.c
blob99eb3ef709419aed14eb73627cd9318b9d6bae8b
1 /* Copyright (C) 1991, 92, 93, 95, 96, 97 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.";
136 fmtchk("%.4x");
137 fmtchk("%04x");
138 fmtchk("%4.4x");
139 fmtchk("%04.4x");
140 fmtchk("%4.3x");
141 fmtchk("%04.3x");
143 fmtst1chk("%.*x");
144 fmtst1chk("%0*x");
145 fmtst2chk("%*.*x");
146 fmtst2chk("%0*.*x");
148 #ifndef BSD
149 printf("bad format:\t\"%z\"\n");
150 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
151 #endif
153 printf("decimal negative:\t\"%d\"\n", -2345);
154 printf("octal negative:\t\"%o\"\n", -2345);
155 printf("hex negative:\t\"%x\"\n", -2345);
156 printf("long decimal number:\t\"%ld\"\n", -123456L);
157 printf("long octal negative:\t\"%lo\"\n", -2345L);
158 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
159 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
160 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
161 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
162 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
164 printf("zero-padded string:\t\"%010s\"\n", shortstr);
165 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
166 printf("space-padded string:\t\"%10s\"\n", shortstr);
167 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
168 printf("null string:\t\"%s\"\n", (char *)NULL);
169 printf("limited string:\t\"%.22s\"\n", longstr);
171 printf("e-style >= 1:\t\"%e\"\n", 12.34);
172 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
173 printf("e-style < .1:\t\"%e\"\n", 0.001234);
174 printf("e-style big:\t\"%.60e\"\n", 1e20);
175 printf ("e-style == .1:\t\"%e\"\n", 0.1);
176 printf("f-style >= 1:\t\"%f\"\n", 12.34);
177 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
178 printf("f-style < .1:\t\"%f\"\n", 0.001234);
179 printf("g-style >= 1:\t\"%g\"\n", 12.34);
180 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
181 printf("g-style < .1:\t\"%g\"\n", 0.001234);
182 printf("g-style big:\t\"%.60g\"\n", 1e20);
184 printf (" %6.5f\n", .099999999860301614);
185 printf (" %6.5f\n", .1);
186 printf ("x%5.4fx\n", .5);
188 printf ("%#03x\n", 1);
191 double d = FLT_MIN;
192 int niter = 17;
194 while (niter-- != 0)
195 printf ("%.17e\n", d / 2);
196 fflush (stdout);
199 printf ("%15.5e\n", 4.9406564584124654e-324);
201 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
202 printf (FORMAT, 0.0, 0.0, 0.0);
203 printf (FORMAT, 1.0, 1.0, 1.0);
204 printf (FORMAT, -1.0, -1.0, -1.0);
205 printf (FORMAT, 100.0, 100.0, 100.0);
206 printf (FORMAT, 1000.0, 1000.0, 1000.0);
207 printf (FORMAT, 10000.0, 10000.0, 10000.0);
208 printf (FORMAT, 12345.0, 12345.0, 12345.0);
209 printf (FORMAT, 100000.0, 100000.0, 100000.0);
210 printf (FORMAT, 123456.0, 123456.0, 123456.0);
211 #undef FORMAT
214 char buf[20];
215 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
216 snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
219 fp_test ();
221 printf ("%e should be 1.234568e+06\n", 1234567.8);
222 printf ("%f should be 1234567.800000\n", 1234567.8);
223 printf ("%g should be 1.23457e+06\n", 1234567.8);
224 printf ("%g should be 123.456\n", 123.456);
225 printf ("%g should be 1e+06\n", 1000000.0);
226 printf ("%g should be 10\n", 10.0);
227 printf ("%g should be 0.02\n", 0.02);
230 double x=1.0;
231 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
234 puts ("--- Should be no further output. ---");
235 rfg1 ();
236 rfg2 ();
239 char buf[200];
240 int result;
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.");
248 return result != 0;
252 void
253 rfg1 (void)
255 char buf[100];
257 sprintf (buf, "%5.s", "xyz");
258 if (strcmp (buf, " ") != 0)
259 printf ("got: '%s', expected: '%s'\n", buf, " ");
260 sprintf (buf, "%5.f", 33.3);
261 if (strcmp (buf, " 33") != 0)
262 printf ("got: '%s', expected: '%s'\n", buf, " 33");
263 sprintf (buf, "%8.e", 33.3e7);
264 if (strcmp (buf, " 3e+08") != 0)
265 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
266 sprintf (buf, "%8.E", 33.3e7);
267 if (strcmp (buf, " 3E+08") != 0)
268 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
269 sprintf (buf, "%.g", 33.3);
270 if (strcmp (buf, "3e+01") != 0)
271 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
272 sprintf (buf, "%.G", 33.3);
273 if (strcmp (buf, "3E+01") != 0)
274 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
277 void
278 rfg2 (void)
280 int prec;
281 char buf[100];
283 prec = 0;
284 sprintf (buf, "%.*g", prec, 3.3);
285 if (strcmp (buf, "3") != 0)
286 printf ("got: '%s', expected: '%s'\n", buf, "3");
287 prec = 0;
288 sprintf (buf, "%.*G", prec, 3.3);
289 if (strcmp (buf, "3") != 0)
290 printf ("got: '%s', expected: '%s'\n", buf, "3");
291 prec = 0;
292 sprintf (buf, "%7.*G", prec, 3.33);
293 if (strcmp (buf, " 3") != 0)
294 printf ("got: '%s', expected: '%s'\n", buf, " 3");
295 prec = 3;
296 sprintf (buf, "%04.*o", prec, 33);
297 if (strcmp (buf, " 041") != 0)
298 printf ("got: '%s', expected: '%s'\n", buf, " 041");
299 prec = 7;
300 sprintf (buf, "%09.*u", prec, 33);
301 if (strcmp (buf, " 0000033") != 0)
302 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
303 prec = 3;
304 sprintf (buf, "%04.*x", prec, 33);
305 if (strcmp (buf, " 021") != 0)
306 printf ("got: '%s', expected: '%s'\n", buf, " 021");
307 prec = 3;
308 sprintf (buf, "%04.*X", prec, 33);
309 if (strcmp (buf, " 021") != 0)
310 printf ("got: '%s', expected: '%s'\n", buf, " 021");