Fri Dec 15 04:41:22 1995 Ulrich Drepper <drepper@gnu.ai.mit.edu>
[glibc.git] / stdio-common / tst-printf.c
blobc177da18b2adf0f8f8b8cacb04c3f8eed63c5a52
1 /* Copyright (C) 1991, 1992, 1993, 1995 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #ifdef BSD
21 #include </usr/include/stdio.h>
22 #define EXIT_SUCCESS 0
23 #else
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #endif
29 #include <float.h>
32 void
33 DEFUN(fmtchk, (fmt), CONST char *fmt)
35 (void) fputs(fmt, stdout);
36 (void) printf(":\t`");
37 (void) printf(fmt, 0x12);
38 (void) printf("'\n");
41 void
42 DEFUN(fmtst1chk, (fmt), CONST char *fmt)
44 (void) fputs(fmt, stdout);
45 (void) printf(":\t`");
46 (void) printf(fmt, 4, 0x12);
47 (void) printf("'\n");
50 void
51 DEFUN(fmtst2chk, (fmt), CONST char *fmt)
53 (void) fputs(fmt, stdout);
54 (void) printf(":\t`");
55 (void) printf(fmt, 4, 4, 0x12);
56 (void) printf("'\n");
59 /* This page is covered by the following copyright: */
61 /* (C) Copyright C E Chew
63 * Feel free to copy, use and distribute this software provided:
65 * 1. you do not pretend that you wrote it
66 * 2. you leave this copyright notice intact.
70 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
73 #define DEC -123
74 #define INT 255
75 #define UNS (~0)
77 /* Formatted Output Test
79 * This exercises the output formatting code.
82 void
83 DEFUN_VOID(fp_test)
85 int i, j, k, l;
86 char buf[7];
87 char *prefix = buf;
88 char tp[20];
90 puts("\nFormatted output test");
91 printf("prefix 6d 6o 6x 6X 6u\n");
92 strcpy(prefix, "%");
93 for (i = 0; i < 2; i++) {
94 for (j = 0; j < 2; j++) {
95 for (k = 0; k < 2; k++) {
96 for (l = 0; l < 2; l++) {
97 strcpy(prefix, "%");
98 if (i == 0) strcat(prefix, "-");
99 if (j == 0) strcat(prefix, "+");
100 if (k == 0) strcat(prefix, "#");
101 if (l == 0) strcat(prefix, "0");
102 printf("%5s |", prefix);
103 strcpy(tp, prefix);
104 strcat(tp, "6d |");
105 printf(tp, DEC);
106 strcpy(tp, prefix);
107 strcat(tp, "6o |");
108 printf(tp, INT);
109 strcpy(tp, prefix);
110 strcat(tp, "6x |");
111 printf(tp, INT);
112 strcpy(tp, prefix);
113 strcat(tp, "6X |");
114 printf(tp, INT);
115 strcpy(tp, prefix);
116 strcat(tp, "6u |");
117 printf(tp, UNS);
118 printf("\n");
123 printf("%10s\n", (char *) NULL);
124 printf("%-10s\n", (char *) NULL);
128 DEFUN_VOID(main)
130 static char shortstr[] = "Hi, Z.";
131 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
132 I am ready for my first lesson today.";
134 fmtchk("%.4x");
135 fmtchk("%04x");
136 fmtchk("%4.4x");
137 fmtchk("%04.4x");
138 fmtchk("%4.3x");
139 fmtchk("%04.3x");
141 fmtst1chk("%.*x");
142 fmtst1chk("%0*x");
143 fmtst2chk("%*.*x");
144 fmtst2chk("%0*.*x");
146 #ifndef BSD
147 printf("bad format:\t\"%z\"\n");
148 printf("nil pointer (padded):\t\"%10p\"\n", (PTR) NULL);
149 #endif
151 printf("decimal negative:\t\"%d\"\n", -2345);
152 printf("octal negative:\t\"%o\"\n", -2345);
153 printf("hex negative:\t\"%x\"\n", -2345);
154 printf("long decimal number:\t\"%ld\"\n", -123456L);
155 printf("long octal negative:\t\"%lo\"\n", -2345L);
156 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
157 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
158 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
159 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
160 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
162 printf("zero-padded string:\t\"%010s\"\n", shortstr);
163 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
164 printf("space-padded string:\t\"%10s\"\n", shortstr);
165 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
166 printf("null string:\t\"%s\"\n", (char *)NULL);
167 printf("limited string:\t\"%.22s\"\n", longstr);
169 printf("e-style >= 1:\t\"%e\"\n", 12.34);
170 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
171 printf("e-style < .1:\t\"%e\"\n", 0.001234);
172 printf("e-style big:\t\"%.60e\"\n", 1e20);
173 printf ("e-style == .1:\t\"%e\"\n", 0.1);
174 printf("f-style >= 1:\t\"%f\"\n", 12.34);
175 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
176 printf("f-style < .1:\t\"%f\"\n", 0.001234);
177 printf("g-style >= 1:\t\"%g\"\n", 12.34);
178 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
179 printf("g-style < .1:\t\"%g\"\n", 0.001234);
180 printf("g-style big:\t\"%.60g\"\n", 1e20);
182 printf (" %6.5f\n", .099999999860301614);
183 printf (" %6.5f\n", .1);
184 printf ("x%5.4fx\n", .5);
186 printf ("%#03x\n", 1);
189 double d = FLT_MIN;
190 int niter = 17;
192 while (niter-- != 0)
193 printf ("%.17e\n", d / 2);
194 fflush (stdout);
197 printf ("%15.5e\n", 4.9406564584124654e-324);
199 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
200 printf (FORMAT, 0.0, 0.0, 0.0);
201 printf (FORMAT, 1.0, 1.0, 1.0);
202 printf (FORMAT, -1.0, -1.0, -1.0);
203 printf (FORMAT, 100.0, 100.0, 100.0);
204 printf (FORMAT, 1000.0, 1000.0, 1000.0);
205 printf (FORMAT, 10000.0, 10000.0, 10000.0);
206 printf (FORMAT, 12345.0, 12345.0, 12345.0);
207 printf (FORMAT, 100000.0, 100000.0, 100000.0);
208 printf (FORMAT, 123456.0, 123456.0, 123456.0);
209 #undef FORMAT
212 char buf[20];
213 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
214 snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
217 fp_test ();
219 printf ("%e should be 1.234568e+06\n", 1234567.8);
220 printf ("%f should be 1234567.800000\n", 1234567.8);
221 printf ("%g should be 1.23457e+06\n", 1234567.8);
222 printf ("%g should be 123.456\n", 123.456);
223 printf ("%g should be 1e+06\n", 1000000.0);
224 printf ("%g should be 10\n", 10.0);
225 printf ("%g should be 0.02\n", 0.02);
228 double x=1.0;
229 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
232 puts ("--- Should be no further output. ---");
233 rfg1 ();
234 rfg2 ();
236 exit(EXIT_SUCCESS);
239 rfg1 ()
241 char buf[100];
243 sprintf (buf, "%5.s", "xyz");
244 if (strcmp (buf, " ") != 0)
245 printf ("got: '%s', expected: '%s'\n", buf, " ");
246 sprintf (buf, "%5.f", 33.3);
247 if (strcmp (buf, " 33") != 0)
248 printf ("got: '%s', expected: '%s'\n", buf, " 33");
249 sprintf (buf, "%8.e", 33.3e7);
250 if (strcmp (buf, " 3e+08") != 0)
251 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
252 sprintf (buf, "%8.E", 33.3e7);
253 if (strcmp (buf, " 3E+08") != 0)
254 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
255 sprintf (buf, "%.g", 33.3);
256 if (strcmp (buf, "3e+01") != 0)
257 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
258 sprintf (buf, "%.G", 33.3);
259 if (strcmp (buf, "3E+01") != 0)
260 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
261 return 0;
264 rfg2 ()
266 int prec;
267 char buf[100];
269 prec = 0;
270 sprintf (buf, "%.*g", prec, 3.3);
271 if (strcmp (buf, "3") != 0)
272 printf ("got: '%s', expected: '%s'\n", buf, "3");
273 prec = 0;
274 sprintf (buf, "%.*G", prec, 3.3);
275 if (strcmp (buf, "3") != 0)
276 printf ("got: '%s', expected: '%s'\n", buf, "3");
277 prec = 0;
278 sprintf (buf, "%7.*G", prec, 3.33);
279 if (strcmp (buf, " 3") != 0)
280 printf ("got: '%s', expected: '%s'\n", buf, " 3");
281 prec = 3;
282 sprintf (buf, "%04.*o", prec, 33);
283 if (strcmp (buf, " 041") != 0)
284 printf ("got: '%s', expected: '%s'\n", buf, " 041");
285 prec = 7;
286 sprintf (buf, "%09.*u", prec, 33);
287 if (strcmp (buf, " 0000033") != 0)
288 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
289 prec = 3;
290 sprintf (buf, "%04.*x", prec, 33);
291 if (strcmp (buf, " 021") != 0)
292 printf ("got: '%s', expected: '%s'\n", buf, " 021");
293 prec = 3;
294 sprintf (buf, "%04.*X", prec, 33);
295 if (strcmp (buf, " 021") != 0)
296 printf ("got: '%s', expected: '%s'\n", buf, " 021");
297 return 0;