1 /* Copyright (C) 1991,92,93,95,96,97,98,99 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.";
150 printf("bad format:\t\"%b\"\n");
151 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL
);
154 printf("decimal negative:\t\"%d\"\n", -2345);
155 printf("octal negative:\t\"%o\"\n", -2345);
156 printf("hex negative:\t\"%x\"\n", -2345);
157 printf("long decimal number:\t\"%ld\"\n", -123456L);
158 printf("long octal negative:\t\"%lo\"\n", -2345L);
159 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
160 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
161 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
162 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
163 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
165 printf("zero-padded string:\t\"%010s\"\n", shortstr
);
166 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr
);
167 printf("space-padded string:\t\"%10s\"\n", shortstr
);
168 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr
);
169 printf("null string:\t\"%s\"\n", (char *)NULL
);
170 printf("limited string:\t\"%.22s\"\n", longstr
);
172 printf("e-style >= 1:\t\"%e\"\n", 12.34);
173 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
174 printf("e-style < .1:\t\"%e\"\n", 0.001234);
175 printf("e-style big:\t\"%.60e\"\n", 1e20
);
176 printf ("e-style == .1:\t\"%e\"\n", 0.1);
177 printf("f-style >= 1:\t\"%f\"\n", 12.34);
178 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
179 printf("f-style < .1:\t\"%f\"\n", 0.001234);
180 printf("g-style >= 1:\t\"%g\"\n", 12.34);
181 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
182 printf("g-style < .1:\t\"%g\"\n", 0.001234);
183 printf("g-style big:\t\"%.60g\"\n", 1e20
);
185 printf (" %6.5f\n", .099999999860301614);
186 printf (" %6.5f\n", .1);
187 printf ("x%5.4fx\n", .5);
189 printf ("%#03x\n", 1);
196 printf ("%.17e\n", d
/ 2);
200 printf ("%15.5e\n", 4.9406564584124654e-324);
202 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
203 printf (FORMAT
, 0.0, 0.0, 0.0);
204 printf (FORMAT
, 1.0, 1.0, 1.0);
205 printf (FORMAT
, -1.0, -1.0, -1.0);
206 printf (FORMAT
, 100.0, 100.0, 100.0);
207 printf (FORMAT
, 1000.0, 1000.0, 1000.0);
208 printf (FORMAT
, 10000.0, 10000.0, 10000.0);
209 printf (FORMAT
, 12345.0, 12345.0, 12345.0);
210 printf (FORMAT
, 100000.0, 100000.0, 100000.0);
211 printf (FORMAT
, 123456.0, 123456.0, 123456.0);
217 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
218 snprintf (buf
, sizeof (buf
), "%30s", "foo"), sizeof (buf
), buf
);
219 printf ("snprintf (\"%%.999999u\", 10)\n",
220 snprintf(buf2
, sizeof(buf2
), "%.999999u", 10));
225 printf ("%e should be 1.234568e+06\n", 1234567.8);
226 printf ("%f should be 1234567.800000\n", 1234567.8);
227 printf ("%g should be 1.23457e+06\n", 1234567.8);
228 printf ("%g should be 123.456\n", 123.456);
229 printf ("%g should be 1e+06\n", 1000000.0);
230 printf ("%g should be 10\n", 10.0);
231 printf ("%g should be 0.02\n", 0.02);
235 printf("%.17f\n",(1.0/x
/10.0+1.0)*x
-x
);
238 puts ("--- Should be no further output. ---");
245 sprintf(buf
,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
247 result
|= strcmp (buf
,
250 puts (result
!= 0 ? "Test failed!" : "Test ok.");
256 sprintf (buf
, "%07Lo", 040000000000ll);
257 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s\n", buf
);
259 if (strcmp (buf
, "40000000000") != 0)
262 fputs ("\tFAILED", stdout
);
275 sprintf (buf
, "%5.s", "xyz");
276 if (strcmp (buf
, " ") != 0)
277 printf ("got: '%s', expected: '%s'\n", buf
, " ");
278 sprintf (buf
, "%5.f", 33.3);
279 if (strcmp (buf
, " 33") != 0)
280 printf ("got: '%s', expected: '%s'\n", buf
, " 33");
281 sprintf (buf
, "%8.e", 33.3e7
);
282 if (strcmp (buf
, " 3e+08") != 0)
283 printf ("got: '%s', expected: '%s'\n", buf
, " 3e+08");
284 sprintf (buf
, "%8.E", 33.3e7
);
285 if (strcmp (buf
, " 3E+08") != 0)
286 printf ("got: '%s', expected: '%s'\n", buf
, " 3E+08");
287 sprintf (buf
, "%.g", 33.3);
288 if (strcmp (buf
, "3e+01") != 0)
289 printf ("got: '%s', expected: '%s'\n", buf
, "3e+01");
290 sprintf (buf
, "%.G", 33.3);
291 if (strcmp (buf
, "3E+01") != 0)
292 printf ("got: '%s', expected: '%s'\n", buf
, "3E+01");
302 sprintf (buf
, "%.*g", prec
, 3.3);
303 if (strcmp (buf
, "3") != 0)
304 printf ("got: '%s', expected: '%s'\n", buf
, "3");
306 sprintf (buf
, "%.*G", prec
, 3.3);
307 if (strcmp (buf
, "3") != 0)
308 printf ("got: '%s', expected: '%s'\n", buf
, "3");
310 sprintf (buf
, "%7.*G", prec
, 3.33);
311 if (strcmp (buf
, " 3") != 0)
312 printf ("got: '%s', expected: '%s'\n", buf
, " 3");
314 sprintf (buf
, "%04.*o", prec
, 33);
315 if (strcmp (buf
, " 041") != 0)
316 printf ("got: '%s', expected: '%s'\n", buf
, " 041");
318 sprintf (buf
, "%09.*u", prec
, 33);
319 if (strcmp (buf
, " 0000033") != 0)
320 printf ("got: '%s', expected: '%s'\n", buf
, " 0000033");
322 sprintf (buf
, "%04.*x", prec
, 33);
323 if (strcmp (buf
, " 021") != 0)
324 printf ("got: '%s', expected: '%s'\n", buf
, " 021");
326 sprintf (buf
, "%04.*X", prec
, 33);
327 if (strcmp (buf
, " 021") != 0)
328 printf ("got: '%s', expected: '%s'\n", buf
, " 021");