Add missing files
[glibc/history.git] / stdio-common / tst-printf.c
blob12212b87b2bde9150f7621d98c413227d13f1130
1 /* Copyright (C) 1991, 1992, 1993, 1995, 1996 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 ();
237 char buf[200];
238 int result;
240 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
242 result = strcmp (buf,
243 "onetwo three ");
245 puts (result != 0 ? "Test failed!" : "Test ok.");
246 return result != 0;
250 rfg1 ()
252 char buf[100];
254 sprintf (buf, "%5.s", "xyz");
255 if (strcmp (buf, " ") != 0)
256 printf ("got: '%s', expected: '%s'\n", buf, " ");
257 sprintf (buf, "%5.f", 33.3);
258 if (strcmp (buf, " 33") != 0)
259 printf ("got: '%s', expected: '%s'\n", buf, " 33");
260 sprintf (buf, "%8.e", 33.3e7);
261 if (strcmp (buf, " 3e+08") != 0)
262 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
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, "%.g", 33.3);
267 if (strcmp (buf, "3e+01") != 0)
268 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
269 sprintf (buf, "%.G", 33.3);
270 if (strcmp (buf, "3E+01") != 0)
271 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
272 return 0;
275 rfg2 ()
277 int prec;
278 char buf[100];
280 prec = 0;
281 sprintf (buf, "%.*g", prec, 3.3);
282 if (strcmp (buf, "3") != 0)
283 printf ("got: '%s', expected: '%s'\n", buf, "3");
284 prec = 0;
285 sprintf (buf, "%.*G", prec, 3.3);
286 if (strcmp (buf, "3") != 0)
287 printf ("got: '%s', expected: '%s'\n", buf, "3");
288 prec = 0;
289 sprintf (buf, "%7.*G", prec, 3.33);
290 if (strcmp (buf, " 3") != 0)
291 printf ("got: '%s', expected: '%s'\n", buf, " 3");
292 prec = 3;
293 sprintf (buf, "%04.*o", prec, 33);
294 if (strcmp (buf, " 041") != 0)
295 printf ("got: '%s', expected: '%s'\n", buf, " 041");
296 prec = 7;
297 sprintf (buf, "%09.*u", prec, 33);
298 if (strcmp (buf, " 0000033") != 0)
299 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
300 prec = 3;
301 sprintf (buf, "%04.*x", prec, 33);
302 if (strcmp (buf, " 021") != 0)
303 printf ("got: '%s', expected: '%s'\n", buf, " 021");
304 prec = 3;
305 sprintf (buf, "%04.*X", prec, 33);
306 if (strcmp (buf, " 021") != 0)
307 printf ("got: '%s', expected: '%s'\n", buf, " 021");
308 return 0;