printfloat_test.c revision 113469
1/*-
2 * Copyright (c) 2002 David Schultz <das@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Test for printf() floating point formats.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-printfloat.c 113469 2003-04-14 11:26:32Z das $");
33
34#include <assert.h>
35#include <err.h>
36#include <float.h>
37#include <locale.h>
38#include <math.h>
39#include <stdio.h>
40#include <stdarg.h>
41#include <stdint.h>
42#include <stdlib.h>
43#include <string.h>
44
45#define	testfmt(result, fmt, ...)	\
46	_testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__)
47void _testfmt(const char *, int, const char *, const char *, ...);
48void smash_stack(void);
49
50int
51main(int argc, char *argv[])
52{
53
54	assert(setlocale(LC_NUMERIC, ""));
55
56	testfmt(" 1.000000E+00", "%13E", 1.0);
57	testfmt("     1.000000", "%13f", 1.0);
58	testfmt("            1", "%13G", 1.0);
59	testfmt(" 1.000000E+00", "%13LE", 1.0L);
60	testfmt("     1.000000", "%13Lf", 1.0L);
61	testfmt("            1", "%13LG", 1.0L);
62
63	testfmt("2.718282", "%.*f", -2, 2.7182818);
64
65	testfmt("1.234568e+06", "%e", 1234567.8);
66	testfmt("1234567.800000", "%f", 1234567.8);
67	testfmt("1.23457E+06", "%G", 1234567.8);
68	testfmt("1.234568e+06", "%Le", 1234567.8L);
69	testfmt("1234567.800000", "%Lf", 1234567.8L);
70	testfmt("1.23457E+06", "%LG", 1234567.8L);
71
72#if LDBL_MANT_DIG > DBL_MANT_DIG
73	testfmt("123456789.864210", "%Lf", 123456789.8642097531L);
74	testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);
75	testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);
76	testfmt(" 3.141592653589793238e-4000", "%L27.18Le",
77	    3.14159265358979323846e-4000L);
78#endif
79
80	testfmt("nan", "%e", NAN);
81	testfmt("NAN", "%F", NAN);
82	testfmt("nan", "%g", NAN);
83	testfmt("NAN", "%LE", (long double)NAN);
84
85	testfmt("INF", "%E", HUGE_VAL);
86	testfmt("-inf", "%f", -HUGE_VAL);
87	testfmt("+inf", "%+g", HUGE_VAL);
88	testfmt(" inf", "%4.2Le", HUGE_VALL);
89	testfmt("-inf", "%Lf", -HUGE_VALL);
90
91	testfmt("0.000000e+00", "%e", 0.0);
92	testfmt("0.000000", "%F", (double)0.0);
93	testfmt("0", "%G", 0.0);
94	testfmt("  0", "%3.0Lg", 0.0L);
95
96	testfmt("    0", "%5.0f", 0.001);
97
98	testfmt("1.0123e+00", "%.4e", 1.0123456789);
99	testfmt("1.0123", "%.4f", 1.0123456789);
100	testfmt("1.012", "%.4g", 1.0123456789);
101	testfmt("1.2346e-02", "%.4e", 0.0123456789);
102	testfmt("0.0123", "%.4f", 0.0123456789);
103	testfmt("0.01235", "%.4g", 0.0123456789);
104
105	testfmt("12345678.0625", "%'.04f", 12345678.0625);
106	testfmt("0012345678.0625", "%'015.4F", 12345678.0625);
107
108	assert(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */
109	testfmt("123,456,78.0625", "%'.4f", 12345678.0625);
110	testfmt("00123,456,78.0625", "%'017.4F", 12345678.0625);
111	testfmt(" 90,00", "%'6.0f", 9000.0);
112	testfmt("90,00.0", "%'.1f", 9000.0);
113	assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
114	testfmt("3,1415", "%g", 3.1415);
115	assert(setlocale(LC_NUMERIC, ""));
116	testfmt("12345678.062500", "%'f", 12345678.0625);
117	testfmt("9000.000000", "%'f", 9000.0);
118
119	testfmt("+2.500000e-01", "%+e", 0.25);
120	testfmt("+0.000000", "%+F", 0.0);
121	testfmt("-1", "%+g", -1.0);
122
123	testfmt("-1.000000e+00", "% e", -1.0);
124	testfmt("+1.000000", "% +f", 1.0);
125	testfmt(" 1", "% g", 1.0);
126	testfmt(" 0", "% g", 0.0);
127
128	testfmt("1.250e+00", "%#.3e", 1.25);
129	testfmt("123.000000", "%#f", 123.0);
130	testfmt(" 12345.", "%#7.5g", 12345.0);
131	testfmt(" 1.00000", "%#8g", 1.0);
132	testfmt("0.0", "%#.2g", 0.0);
133
134	testfmt("03.2E+00", "%08.1E", 3.25);
135	testfmt("003.25", "%06.2F", 3.25);
136	testfmt("0003.25", "%07.4G", 3.25);
137
138	testfmt("3.14159e-05", "%g", 3.14159e-5);
139	testfmt("0.000314159", "%g", 3.14159e-4);
140	testfmt("3.14159e+06", "%g", 3.14159e6);
141	testfmt("314159", "%g", 3.14159e5);
142	testfmt("314159.", "%#g", 3.14159e5);
143
144	testfmt(" 0.0", "%4.1f", 0.0);
145	testfmt("90.0", "%4.1f", 90.0);
146	testfmt(" 100", "%4.0f", 100.0);
147	testfmt("9.0e+01", "%4.1e", 90.0);
148	testfmt("1e+02", "%4.0e", 100.0);
149
150	printf("PASS printfloat\n");
151
152	return (0);
153}
154
155void
156smash_stack(void)
157{
158	static uint32_t junk = 0xdeadbeef;
159	uint32_t buf[512];
160	int i;
161
162	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
163		buf[i] = junk;
164}
165
166void
167_testfmt(const char *result, int line, const char *argstr, const char *fmt,...)
168{
169	char s[100];
170	va_list ap;
171
172	va_start(ap, fmt);
173	smash_stack();
174	vsnprintf(s, sizeof(s), fmt, ap);
175	if (strcmp(result, s) != 0) {
176		fprintf(stderr,
177		    "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n",
178		    line, fmt, argstr, s, result);
179		abort();
180	}
181}
182