]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tst-printf.c
update from main archive 961008
[thirdparty/glibc.git] / stdio-common / tst-printf.c
CommitLineData
299a95b9 1/* Copyright (C) 1991, 1992, 1993, 1995, 1996 Free Software Foundation, Inc.
28f540f4
RM
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
28f540f4
RM
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
27
28#include <float.h>
29
11336c16
UD
30void rfg1 (void);
31void rfg2 (void);
32
28f540f4
RM
33
34void
11336c16 35fmtchk (const char *fmt)
28f540f4
RM
36{
37 (void) fputs(fmt, stdout);
38 (void) printf(":\t`");
39 (void) printf(fmt, 0x12);
40 (void) printf("'\n");
41}
42
43void
11336c16 44fmtst1chk (const char *fmt)
28f540f4
RM
45{
46 (void) fputs(fmt, stdout);
47 (void) printf(":\t`");
48 (void) printf(fmt, 4, 0x12);
49 (void) printf("'\n");
50}
51
52void
11336c16 53fmtst2chk (const char *fmt)
28f540f4
RM
54{
55 (void) fputs(fmt, stdout);
56 (void) printf(":\t`");
57 (void) printf(fmt, 4, 4, 0x12);
58 (void) printf("'\n");
59}
60\f
61/* This page is covered by the following copyright: */
62
63/* (C) Copyright C E Chew
64 *
65 * Feel free to copy, use and distribute this software provided:
66 *
67 * 1. you do not pretend that you wrote it
68 * 2. you leave this copyright notice intact.
69 */
70
71/*
72 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
73 */
74
75#define DEC -123
76#define INT 255
77#define UNS (~0)
78
79/* Formatted Output Test
80 *
81 * This exercises the output formatting code.
82 */
83
84void
11336c16 85fp_test (void)
28f540f4
RM
86{
87 int i, j, k, l;
88 char buf[7];
89 char *prefix = buf;
90 char tp[20];
91
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");
121 }
122 }
123 }
124 }
125 printf("%10s\n", (char *) NULL);
126 printf("%-10s\n", (char *) NULL);
127}
128\f
129int
11336c16 130main (int argc, char *argv[])
28f540f4
RM
131{
132 static char shortstr[] = "Hi, Z.";
133 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
134I am ready for my first lesson today.";
135
136 fmtchk("%.4x");
137 fmtchk("%04x");
138 fmtchk("%4.4x");
139 fmtchk("%04.4x");
140 fmtchk("%4.3x");
141 fmtchk("%04.3x");
142
143 fmtst1chk("%.*x");
144 fmtst1chk("%0*x");
145 fmtst2chk("%*.*x");
146 fmtst2chk("%0*.*x");
147
148#ifndef BSD
149 printf("bad format:\t\"%z\"\n");
150 printf("nil pointer (padded):\t\"%10p\"\n", (PTR) NULL);
151#endif
152
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);
163
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);
170
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);
183
184 printf (" %6.5f\n", .099999999860301614);
185 printf (" %6.5f\n", .1);
186 printf ("x%5.4fx\n", .5);
187
188 printf ("%#03x\n", 1);
189
190 {
191 double d = FLT_MIN;
192 int niter = 17;
299a95b9 193
28f540f4
RM
194 while (niter-- != 0)
195 printf ("%.17e\n", d / 2);
196 fflush (stdout);
197 }
198
199 printf ("%15.5e\n", 4.9406564584124654e-324);
200
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
212
213 {
214 char buf[20];
215 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
216 snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
217 }
218
219 fp_test ();
220
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);
228
229 {
230 double x=1.0;
231 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
232 }
233
234 puts ("--- Should be no further output. ---");
235 rfg1 ();
236 rfg2 ();
237
299a95b9
RM
238 {
239 char buf[200];
240 int result;
241
242 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
243
244 result = strcmp (buf,
245 "onetwo three ");
246
247 puts (result != 0 ? "Test failed!" : "Test ok.");
248 return result != 0;
249 }
28f540f4
RM
250}
251\f
11336c16
UD
252void
253rfg1 (void)
28f540f4
RM
254{
255 char buf[100];
256
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");
275 return 0;
276}
277
11336c16
UD
278void
279rfg2 (void)
28f540f4
RM
280{
281 int prec;
282 char buf[100];
283
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, "%.*G", prec, 3.3);
290 if (strcmp (buf, "3") != 0)
291 printf ("got: '%s', expected: '%s'\n", buf, "3");
292 prec = 0;
293 sprintf (buf, "%7.*G", prec, 3.33);
294 if (strcmp (buf, " 3") != 0)
295 printf ("got: '%s', expected: '%s'\n", buf, " 3");
296 prec = 3;
297 sprintf (buf, "%04.*o", prec, 33);
298 if (strcmp (buf, " 041") != 0)
299 printf ("got: '%s', expected: '%s'\n", buf, " 041");
300 prec = 7;
301 sprintf (buf, "%09.*u", prec, 33);
302 if (strcmp (buf, " 0000033") != 0)
303 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
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 prec = 3;
309 sprintf (buf, "%04.*X", prec, 33);
310 if (strcmp (buf, " 021") != 0)
311 printf ("got: '%s', expected: '%s'\n", buf, " 021");
312 return 0;
313}