1 /* Copyright (C) 1991, 92, 93, 95, 96, 97, 98 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. */
20 #include </usr/include/stdio.h>
21 #define EXIT_SUCCESS 0
35 fmtchk (const char *fmt
)
37 (void) fputs(fmt
, stdout
);
38 (void) printf(":\t`");
39 (void) printf(fmt
, 0x12);
44 fmtst1chk (const char *fmt
)
46 (void) fputs(fmt
, stdout
);
47 (void) printf(":\t`");
48 (void) printf(fmt
, 4, 0x12);
53 fmtst2chk (const char *fmt
)
55 (void) fputs(fmt
, stdout
);
56 (void) printf(":\t`");
57 (void) printf(fmt
, 4, 4, 0x12);
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.
79 /* Formatted Output Test
81 * This exercises the output formatting code.
92 puts("\nFormatted output test");
93 printf("prefix 6d 6o 6x 6X 6u\n");
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
++) {
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
);
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.";
149 printf("bad format:\t\"%b\"\n");
150 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL
);
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);
195 printf ("%.17e\n", d
/ 2);
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);
215 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
216 snprintf (buf
, sizeof (buf
), "%30s", "foo"), sizeof (buf
), buf
);
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);
231 printf("%.17f\n",(1.0/x
/10.0+1.0)*x
-x
);
234 puts ("--- Should be no further output. ---");
242 sprintf(buf
,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
244 result
= strcmp (buf
,
247 puts (result
!= 0 ? "Test failed!" : "Test ok.");
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");
284 sprintf (buf
, "%.*g", prec
, 3.3);
285 if (strcmp (buf
, "3") != 0)
286 printf ("got: '%s', expected: '%s'\n", buf
, "3");
288 sprintf (buf
, "%.*G", prec
, 3.3);
289 if (strcmp (buf
, "3") != 0)
290 printf ("got: '%s', expected: '%s'\n", buf
, "3");
292 sprintf (buf
, "%7.*G", prec
, 3.33);
293 if (strcmp (buf
, " 3") != 0)
294 printf ("got: '%s', expected: '%s'\n", buf
, " 3");
296 sprintf (buf
, "%04.*o", prec
, 33);
297 if (strcmp (buf
, " 041") != 0)
298 printf ("got: '%s', expected: '%s'\n", buf
, " 041");
300 sprintf (buf
, "%09.*u", prec
, 33);
301 if (strcmp (buf
, " 0000033") != 0)
302 printf ("got: '%s', expected: '%s'\n", buf
, " 0000033");
304 sprintf (buf
, "%04.*x", prec
, 33);
305 if (strcmp (buf
, " 021") != 0)
306 printf ("got: '%s', expected: '%s'\n", buf
, " 021");
308 sprintf (buf
, "%04.*X", prec
, 33);
309 if (strcmp (buf
, " 021") != 0)
310 printf ("got: '%s', expected: '%s'\n", buf
, " 021");