source: vendor/glibc-tests/glibc/stdio-common/tst-printf.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/* Copyright (C) 1991,92,93,95,96,97,98,99, 2000, 2002
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#ifdef BSD
21#include </usr/include/stdio.h>
22#define EXIT_SUCCESS 0
23#else
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#endif
29
30#include <float.h>
31
32static void rfg1 (void);
33static void rfg2 (void);
34static void rfg3 (void);
35
36
37static void
38fmtchk (const char *fmt)
39{
40 (void) fputs(fmt, stdout);
41 (void) printf(":\t`");
42 (void) printf(fmt, 0x12);
43 (void) printf("'\n");
44}
45
46static void
47fmtst1chk (const char *fmt)
48{
49 (void) fputs(fmt, stdout);
50 (void) printf(":\t`");
51 (void) printf(fmt, 4, 0x12);
52 (void) printf("'\n");
53}
54
55static void
56fmtst2chk (const char *fmt)
57{
58 (void) fputs(fmt, stdout);
59 (void) printf(":\t`");
60 (void) printf(fmt, 4, 4, 0x12);
61 (void) printf("'\n");
62}
63
64
65/* This page is covered by the following copyright: */
66
67/* (C) Copyright C E Chew
68 *
69 * Feel free to copy, use and distribute this software provided:
70 *
71 * 1. you do not pretend that you wrote it
72 * 2. you leave this copyright notice intact.
73 */
74
75/*
76 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
77 */
78
79#define DEC -123
80#define INT 255
81#define UNS (~0)
82
83/* Formatted Output Test
84 *
85 * This exercises the output formatting code.
86 */
87
88static void
89fp_test (void)
90{
91 int i, j, k, l;
92 char buf[7];
93 char *prefix = buf;
94 char tp[20];
95
96 puts("\nFormatted output test");
97 printf("prefix 6d 6o 6x 6X 6u\n");
98 strcpy(prefix, "%");
99 for (i = 0; i < 2; i++) {
100 for (j = 0; j < 2; j++) {
101 for (k = 0; k < 2; k++) {
102 for (l = 0; l < 2; l++) {
103 strcpy(prefix, "%");
104 if (i == 0) strcat(prefix, "-");
105 if (j == 0) strcat(prefix, "+");
106 if (k == 0) strcat(prefix, "#");
107 if (l == 0) strcat(prefix, "0");
108 printf("%5s |", prefix);
109 strcpy(tp, prefix);
110 strcat(tp, "6d |");
111 printf(tp, DEC);
112 strcpy(tp, prefix);
113 strcat(tp, "6o |");
114 printf(tp, INT);
115 strcpy(tp, prefix);
116 strcat(tp, "6x |");
117 printf(tp, INT);
118 strcpy(tp, prefix);
119 strcat(tp, "6X |");
120 printf(tp, INT);
121 strcpy(tp, prefix);
122 strcat(tp, "6u |");
123 printf(tp, UNS);
124 printf("\n");
125 }
126 }
127 }
128 }
129 printf("%10s\n", (char *) NULL);
130 printf("%-10s\n", (char *) NULL);
131}
132
133
134int
135main (int argc, char *argv[])
136{
137 static char shortstr[] = "Hi, Z.";
138 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
139I am ready for my first lesson today.";
140 int result = 0;
141
142 fmtchk("%.4x");
143 fmtchk("%04x");
144 fmtchk("%4.4x");
145 fmtchk("%04.4x");
146 fmtchk("%4.3x");
147 fmtchk("%04.3x");
148
149 fmtst1chk("%.*x");
150 fmtst1chk("%0*x");
151 fmtst2chk("%*.*x");
152 fmtst2chk("%0*.*x");
153
154#ifndef BSD
155 printf("bad format:\t\"%b\"\n");
156 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
157#endif
158
159 printf("decimal negative:\t\"%d\"\n", -2345);
160 printf("octal negative:\t\"%o\"\n", -2345);
161 printf("hex negative:\t\"%x\"\n", -2345);
162 printf("long decimal number:\t\"%ld\"\n", -123456L);
163 printf("long octal negative:\t\"%lo\"\n", -2345L);
164 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
165 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
166 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
167 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
168 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
169
170 printf("zero-padded string:\t\"%010s\"\n", shortstr);
171 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
172 printf("space-padded string:\t\"%10s\"\n", shortstr);
173 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
174 printf("null string:\t\"%s\"\n", (char *)NULL);
175 printf("limited string:\t\"%.22s\"\n", longstr);
176
177 printf("e-style >= 1:\t\"%e\"\n", 12.34);
178 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
179 printf("e-style < .1:\t\"%e\"\n", 0.001234);
180 printf("e-style big:\t\"%.60e\"\n", 1e20);
181 printf ("e-style == .1:\t\"%e\"\n", 0.1);
182 printf("f-style >= 1:\t\"%f\"\n", 12.34);
183 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
184 printf("f-style < .1:\t\"%f\"\n", 0.001234);
185 printf("g-style >= 1:\t\"%g\"\n", 12.34);
186 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
187 printf("g-style < .1:\t\"%g\"\n", 0.001234);
188 printf("g-style big:\t\"%.60g\"\n", 1e20);
189
190 printf (" %6.5f\n", .099999999860301614);
191 printf (" %6.5f\n", .1);
192 printf ("x%5.4fx\n", .5);
193
194 printf ("%#03x\n", 1);
195
196 printf ("something really insane: %.10000f\n", 1.0);
197
198 {
199 double d = FLT_MIN;
200 int niter = 17;
201
202 while (niter-- != 0)
203 printf ("%.17e\n", d / 2);
204 fflush (stdout);
205 }
206
207 printf ("%15.5e\n", 4.9406564584124654e-324);
208
209#define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
210 printf (FORMAT, 0.0, 0.0, 0.0);
211 printf (FORMAT, 1.0, 1.0, 1.0);
212 printf (FORMAT, -1.0, -1.0, -1.0);
213 printf (FORMAT, 100.0, 100.0, 100.0);
214 printf (FORMAT, 1000.0, 1000.0, 1000.0);
215 printf (FORMAT, 10000.0, 10000.0, 10000.0);
216 printf (FORMAT, 12345.0, 12345.0, 12345.0);
217 printf (FORMAT, 100000.0, 100000.0, 100000.0);
218 printf (FORMAT, 123456.0, 123456.0, 123456.0);
219#undef FORMAT
220
221 {
222 char buf[20];
223 char buf2[512];
224 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
225 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
226 buf);
227 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
228 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
229 }
230
231 fp_test ();
232
233 printf ("%e should be 1.234568e+06\n", 1234567.8);
234 printf ("%f should be 1234567.800000\n", 1234567.8);
235 printf ("%g should be 1.23457e+06\n", 1234567.8);
236 printf ("%g should be 123.456\n", 123.456);
237 printf ("%g should be 1e+06\n", 1000000.0);
238 printf ("%g should be 10\n", 10.0);
239 printf ("%g should be 0.02\n", 0.02);
240
241#if 0
242 /* This test rather checks the way the compiler handles constant
243 folding. gcc behavior wrt to this changed in 3.2 so it is not a
244 portable test. */
245 {
246 double x=1.0;
247 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
248 }
249#endif
250
251 {
252 char buf[200];
253
254 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
255
256 result |= strcmp (buf,
257 "onetwo three ");
258
259 puts (result != 0 ? "Test failed!" : "Test ok.");
260 }
261
262 {
263 char buf[200];
264
265 sprintf (buf, "%07Lo", 040000000000ll);
266 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
267
268 if (strcmp (buf, "40000000000") != 0)
269 {
270 result = 1;
271 fputs ("\tFAILED", stdout);
272 }
273 puts ("");
274 }
275
276 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
277 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
278
279 puts ("--- Should be no further output. ---");
280 rfg1 ();
281 rfg2 ();
282 rfg3 ();
283
284 {
285 char bytes[7];
286 char buf[20];
287
288 memset (bytes, '\xff', sizeof bytes);
289 sprintf (buf, "foo%hhn\n", &bytes[3]);
290 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
291 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
292 {
293 puts ("%hhn overwrite more bytes");
294 result = 1;
295 }
296 if (bytes[3] != 3)
297 {
298 puts ("%hhn wrote incorrect value");
299 result = 1;
300 }
301 }
302
303 return result != 0;
304}
305
306
307static void
308rfg1 (void)
309{
310 char buf[100];
311
312 sprintf (buf, "%5.s", "xyz");
313 if (strcmp (buf, " ") != 0)
314 printf ("got: '%s', expected: '%s'\n", buf, " ");
315 sprintf (buf, "%5.f", 33.3);
316 if (strcmp (buf, " 33") != 0)
317 printf ("got: '%s', expected: '%s'\n", buf, " 33");
318 sprintf (buf, "%8.e", 33.3e7);
319 if (strcmp (buf, " 3e+08") != 0)
320 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
321 sprintf (buf, "%8.E", 33.3e7);
322 if (strcmp (buf, " 3E+08") != 0)
323 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
324 sprintf (buf, "%.g", 33.3);
325 if (strcmp (buf, "3e+01") != 0)
326 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
327 sprintf (buf, "%.G", 33.3);
328 if (strcmp (buf, "3E+01") != 0)
329 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
330}
331
332static void
333rfg2 (void)
334{
335 int prec;
336 char buf[100];
337
338 prec = 0;
339 sprintf (buf, "%.*g", prec, 3.3);
340 if (strcmp (buf, "3") != 0)
341 printf ("got: '%s', expected: '%s'\n", buf, "3");
342 prec = 0;
343 sprintf (buf, "%.*G", prec, 3.3);
344 if (strcmp (buf, "3") != 0)
345 printf ("got: '%s', expected: '%s'\n", buf, "3");
346 prec = 0;
347 sprintf (buf, "%7.*G", prec, 3.33);
348 if (strcmp (buf, " 3") != 0)
349 printf ("got: '%s', expected: '%s'\n", buf, " 3");
350 prec = 3;
351 sprintf (buf, "%04.*o", prec, 33);
352 if (strcmp (buf, " 041") != 0)
353 printf ("got: '%s', expected: '%s'\n", buf, " 041");
354 prec = 7;
355 sprintf (buf, "%09.*u", prec, 33);
356 if (strcmp (buf, " 0000033") != 0)
357 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
358 prec = 3;
359 sprintf (buf, "%04.*x", prec, 33);
360 if (strcmp (buf, " 021") != 0)
361 printf ("got: '%s', expected: '%s'\n", buf, " 021");
362 prec = 3;
363 sprintf (buf, "%04.*X", prec, 33);
364 if (strcmp (buf, " 021") != 0)
365 printf ("got: '%s', expected: '%s'\n", buf, " 021");
366}
367
368static void
369rfg3 (void)
370{
371 char buf[100];
372 double g = 5.0000001;
373 unsigned long l = 1234567890;
374 double d = 321.7654321;
375 const char s[] = "test-string";
376 int i = 12345;
377 int h = 1234;
378
379 sprintf (buf,
380 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
381 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
382 if (strcmp (buf,
383 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
384 printf ("got: '%s', expected: '%s'\n", buf,
385 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");
386}
Note: See TracBrowser for help on using the repository browser.