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. */
21 #include </usr/include/stdio.h>
22 #define EXIT_SUCCESS 0
33 DEFUN(fmtchk
, (fmt
), CONST
char *fmt
)
35 (void) fputs(fmt
, stdout
);
36 (void) printf(":\t`");
37 (void) printf(fmt
, 0x12);
42 DEFUN(fmtst1chk
, (fmt
), CONST
char *fmt
)
44 (void) fputs(fmt
, stdout
);
45 (void) printf(":\t`");
46 (void) printf(fmt
, 4, 0x12);
51 DEFUN(fmtst2chk
, (fmt
), CONST
char *fmt
)
53 (void) fputs(fmt
, stdout
);
54 (void) printf(":\t`");
55 (void) printf(fmt
, 4, 4, 0x12);
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.
77 /* Formatted Output Test
79 * This exercises the output formatting code.
90 puts("\nFormatted output test");
91 printf("prefix 6d 6o 6x 6X 6u\n");
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
++) {
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
);
123 printf("%10s\n", (char *) NULL
);
124 printf("%-10s\n", (char *) NULL
);
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.";
147 printf("bad format:\t\"%z\"\n");
148 printf("nil pointer (padded):\t\"%10p\"\n", (PTR
) NULL
);
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);
193 printf ("%.17e\n", d
/ 2);
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);
213 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
214 snprintf (buf
, sizeof (buf
), "%30s", "foo"), sizeof (buf
), buf
);
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);
229 printf("%.17f\n",(1.0/x
/10.0+1.0)*x
-x
);
232 puts ("--- Should be no further output. ---");
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");
270 sprintf (buf
, "%.*g", prec
, 3.3);
271 if (strcmp (buf
, "3") != 0)
272 printf ("got: '%s', expected: '%s'\n", buf
, "3");
274 sprintf (buf
, "%.*G", prec
, 3.3);
275 if (strcmp (buf
, "3") != 0)
276 printf ("got: '%s', expected: '%s'\n", buf
, "3");
278 sprintf (buf
, "%7.*G", prec
, 3.33);
279 if (strcmp (buf
, " 3") != 0)
280 printf ("got: '%s', expected: '%s'\n", buf
, " 3");
282 sprintf (buf
, "%04.*o", prec
, 33);
283 if (strcmp (buf
, " 041") != 0)
284 printf ("got: '%s', expected: '%s'\n", buf
, " 041");
286 sprintf (buf
, "%09.*u", prec
, 33);
287 if (strcmp (buf
, " 0000033") != 0)
288 printf ("got: '%s', expected: '%s'\n", buf
, " 0000033");
290 sprintf (buf
, "%04.*x", prec
, 33);
291 if (strcmp (buf
, " 021") != 0)
292 printf ("got: '%s', expected: '%s'\n", buf
, " 021");
294 sprintf (buf
, "%04.*X", prec
, 33);
295 if (strcmp (buf
, " 021") != 0)
296 printf ("got: '%s', expected: '%s'\n", buf
, " 021");