]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/regression/lib/libc/stdio/test-printfloat.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / regression / lib / libc / stdio / test-printfloat.c
1 /*-
2  * Copyright (c) 2002-2009 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$");
33
34 #include <assert.h>
35 #include <err.h>
36 #include <fenv.h>
37 #include <float.h>
38 #include <locale.h>
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <wchar.h>
46
47 #define testfmt(result, fmt, ...)       \
48         _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__)
49 void _testfmt(const char *, int, const char *, const char *, ...);
50 void smash_stack(void);
51
52 int
53 main(int argc, char *argv[])
54 {
55
56         printf("1..11\n");
57         assert(setlocale(LC_NUMERIC, "C"));
58
59         /*
60          * Basic tests of decimal output functionality.
61          */
62         testfmt(" 1.000000E+00", "%13E", 1.0);
63         testfmt("     1.000000", "%13f", 1.0);
64         testfmt("            1", "%13G", 1.0);
65         testfmt(" 1.000000E+00", "%13LE", 1.0L);
66         testfmt("     1.000000", "%13Lf", 1.0L);
67         testfmt("            1", "%13LG", 1.0L);
68
69         testfmt("2.718282", "%.*f", -2, 2.7182818);
70
71         testfmt("1.234568e+06", "%e", 1234567.8);
72         testfmt("1234567.800000", "%f", 1234567.8);
73         testfmt("1.23457E+06", "%G", 1234567.8);
74         testfmt("1.234568e+06", "%Le", 1234567.8L);
75         testfmt("1234567.800000", "%Lf", 1234567.8L);
76         testfmt("1.23457E+06", "%LG", 1234567.8L);
77
78 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
79         testfmt("123456789.864210", "%Lf", 123456789.8642097531L);
80         testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);
81         testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);
82         testfmt(" 3.141592653589793238e-4000", "%L27.18Le",
83             3.14159265358979323846e-4000L);
84 #endif
85
86         printf("ok 1 - printfloat\n");
87
88         /*
89          * Infinities and NaNs
90          */
91         testfmt("nan", "%e", NAN);
92         testfmt("NAN", "%F", NAN);
93         testfmt("nan", "%g", NAN);
94         testfmt("NAN", "%LE", (long double)NAN);
95         testfmt("  nan", "%05e", NAN);
96
97         testfmt("INF", "%E", HUGE_VAL);
98         testfmt("-inf", "%f", -HUGE_VAL);
99         testfmt("+inf", "%+g", HUGE_VAL);
100         testfmt(" inf", "%4.2Le", HUGE_VALL);
101         testfmt("-inf", "%Lf", -HUGE_VALL);
102         testfmt("  inf", "%05e", HUGE_VAL);
103         testfmt(" -inf", "%05e", -HUGE_VAL);
104
105         printf("ok 2 - printfloat\n");
106
107         /*
108          * Padding
109          */
110         testfmt("0.000000e+00", "%e", 0.0);
111         testfmt("0.000000", "%F", (double)0.0);
112         testfmt("0", "%G", 0.0);
113         testfmt("  0", "%3.0Lg", 0.0L);
114         testfmt("    0", "%5.0f", 0.001);
115         printf("ok 3 - printfloat\n");
116
117         /*
118          * Precision specifiers
119          */
120         testfmt("1.0123e+00", "%.4e", 1.0123456789);
121         testfmt("1.0123", "%.4f", 1.0123456789);
122         testfmt("1.012", "%.4g", 1.0123456789);
123         testfmt("1.2346e-02", "%.4e", 0.0123456789);
124         testfmt("0.0123", "%.4f", 0.0123456789);
125         testfmt("0.01235", "%.4g", 0.0123456789);
126         printf("ok 4 - printfloat\n");
127
128         /*
129          * Thousands' separators and other locale fun
130          */
131         testfmt("12345678.0625", "%'.04f", 12345678.0625);
132         testfmt("0012345678.0625", "%'015.4F", 12345678.0625);
133
134         assert(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */
135         testfmt("123,456,78.0625", "%'.4f", 12345678.0625);
136         testfmt("00123,456,78.0625", "%'017.4F", 12345678.0625);
137         testfmt(" 90,00", "%'6.0f", 9000.0);
138         testfmt("90,00.0", "%'.1f", 9000.0);
139
140         assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
141         testfmt("3,1415", "%g", 3.1415);
142
143         /* thousands=. decimalpoint=, grouping=3;3 */
144         assert(setlocale(LC_NUMERIC, "el_GR.ISO8859-7")); /* decimalpoint==, */
145         testfmt("1.234,00", "%'.2f", 1234.00);
146         testfmt("123.456,789", "%'.3f", 123456.789);
147
148         assert(setlocale(LC_NUMERIC, "C"));
149         testfmt("12345678.062500", "%'f", 12345678.0625);
150         testfmt("9000.000000", "%'f", 9000.0);
151
152         printf("ok 5 - printfloat\n");
153
154         /*
155          * Signed conversions
156          */
157         testfmt("+2.500000e-01", "%+e", 0.25);
158         testfmt("+0.000000", "%+F", 0.0);
159         testfmt("-1", "%+g", -1.0);
160
161         testfmt("-1.000000e+00", "% e", -1.0);
162         testfmt("+1.000000", "% +f", 1.0);
163         testfmt(" 1", "% g", 1.0);
164         testfmt(" 0", "% g", 0.0);
165
166         printf("ok 6 - printfloat\n");
167
168         /*
169          * ``Alternate form''
170          */
171         testfmt("1.250e+00", "%#.3e", 1.25);
172         testfmt("123.000000", "%#f", 123.0);
173         testfmt(" 12345.", "%#7.5g", 12345.0);
174         testfmt(" 1.00000", "%#8g", 1.0);
175         testfmt("0.0", "%#.2g", 0.0);
176         printf("ok 7 - printfloat\n");
177
178         /*
179          * Padding and decimal point placement
180          */
181         testfmt("03.2E+00", "%08.1E", 3.25);
182         testfmt("003.25", "%06.2F", 3.25);
183         testfmt("0003.25", "%07.4G", 3.25);
184
185         testfmt("3.14159e-05", "%g", 3.14159e-5);
186         testfmt("0.000314159", "%g", 3.14159e-4);
187         testfmt("3.14159e+06", "%g", 3.14159e6);
188         testfmt("314159", "%g", 3.14159e5);
189         testfmt("314159.", "%#g", 3.14159e5);
190
191         testfmt(" 9.000000e+03", "%13e", 9000.0);
192         testfmt(" 9000.000000", "%12f", 9000.0);
193         testfmt(" 9000", "%5g", 9000.0);
194         testfmt(" 900000.", "%#8g", 900000.0);
195         testfmt(" 9e+06", "%6g", 9000000.0);
196         testfmt(" 9.000000e-04", "%13e", 0.0009);
197         testfmt(" 0.000900", "%9f", 0.0009);
198         testfmt(" 0.0009", "%7g", 0.0009);
199         testfmt(" 9e-05", "%6g", 0.00009);
200         testfmt(" 9.00000e-05", "%#12g", 0.00009);
201         testfmt(" 9.e-05", "%#7.1g", 0.00009);
202
203         testfmt(" 0.0", "%4.1f", 0.0);
204         testfmt("90.0", "%4.1f", 90.0);
205         testfmt(" 100", "%4.0f", 100.0);
206         testfmt("9.0e+01", "%4.1e", 90.0);
207         testfmt("1e+02", "%4.0e", 100.0);
208
209         printf("ok 8 - printfloat\n");
210
211         /*
212          * Decimal rounding
213          */
214         fesetround(FE_DOWNWARD);
215         testfmt("4.437", "%.3f", 4.4375);
216         testfmt("-4.438", "%.3f", -4.4375);
217         testfmt("4.437", "%.3Lf", 4.4375L);
218         testfmt("-4.438", "%.3Lf", -4.4375L);
219
220         fesetround(FE_UPWARD);
221         testfmt("4.438", "%.3f", 4.4375);
222         testfmt("-4.437", "%.3f", -4.4375);
223         testfmt("4.438", "%.3Lf", 4.4375L);
224         testfmt("-4.437", "%.3Lf", -4.4375L);
225
226         fesetround(FE_TOWARDZERO);
227         testfmt("4.437", "%.3f", 4.4375);
228         testfmt("-4.437", "%.3f", -4.4375);
229         testfmt("4.437", "%.3Lf", 4.4375L);
230         testfmt("-4.437", "%.3Lf", -4.4375L);
231
232         fesetround(FE_TONEAREST);
233         testfmt("4.438", "%.3f", 4.4375);
234         testfmt("-4.438", "%.3f", -4.4375);
235         testfmt("4.438", "%.3Lf", 4.4375L);
236         testfmt("-4.438", "%.3Lf", -4.4375L);
237
238         printf("ok 9 - printfloat\n");
239
240         /*
241          * Hexadecimal floating point (%a, %A) tests.  Some of these
242          * are only valid if the implementation converts to hex digits
243          * on nibble boundaries.
244          */
245         testfmt("0x0p+0", "%a", 0x0.0p0);
246         testfmt("0X0.P+0", "%#LA", 0x0.0p0L);
247         testfmt("inf", "%La", (long double)INFINITY);
248         testfmt("+INF", "%+A", INFINITY);
249         testfmt("nan", "%La", (long double)NAN);
250         testfmt("NAN", "%A", NAN);
251
252         testfmt(" 0x1.23p+0", "%10a", 0x1.23p0);
253         testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500);
254         testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40);
255         testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4);
256         testfmt("0x1p-1074", "%a", 0x1p-1074);
257         testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024);
258
259 #if (LDBL_MANT_DIG == 64) && !defined(__i386__)
260         testfmt("0x1.921fb54442d18468p+1", "%La", 0x3.243f6a8885a308dp0L);
261         testfmt("0x1p-16445", "%La", 0x1p-16445L);
262         testfmt("0x1.30ecap-16381", "%La", 0x9.8765p-16384L);
263 #elif (LDBL_MANT_DIG == 113)
264         testfmt("0x1.921fb54442d18469898cc51701b8p+1", "%La",
265             0x3.243f6a8885a308d313198a2e037p0L);
266         testfmt("0x1p-16494", "%La", 0x1p-16494L);
267         testfmt("0x1.2345p-16384", "%La", 0x1.2345p-16384L);
268 #else
269         testfmt("0xc.90fdaa22168cp-2", "%La", 0x3.243f6a8885a31p0L);
270         testfmt("0x8p-1077", "%La", 0x1p-1074L);
271         testfmt("0x9.8765p-1024", "%La", 0x9.8765p-1024L);
272 #endif
273
274         printf("ok 10 - printfloat\n");
275
276         /*
277          * Hexadecimal rounding
278          */
279         fesetround(FE_TOWARDZERO);
280         testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
281         testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
282         testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
283         testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
284         testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
285
286         fesetround(FE_DOWNWARD);
287         testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
288         testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0);
289         testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
290         testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
291         testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
292
293         fesetround(FE_UPWARD);
294         testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
295         testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
296         testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0);
297         testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
298         testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
299
300         fesetround(FE_TONEAREST);
301         testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4);
302         testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
303         testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
304         testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
305         testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
306         testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
307         testfmt("0x1.00p-1029", "%.2a", 0x1.fffp-1030);
308         testfmt("0x1.00p-1026", "%.2a", 0xf.fffp-1030);
309         testfmt("0x1.83p+0", "%.2a", 1.51);
310
311         printf("ok 11 - printfloat\n");
312
313         return (0);
314 }
315
316 void
317 smash_stack(void)
318 {
319         static uint32_t junk = 0xdeadbeef;
320         uint32_t buf[512];
321         int i;
322
323         for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
324                 buf[i] = junk;
325 }
326
327 void
328 _testfmt(const char *result, int line, const char *argstr, const char *fmt,...)
329 {
330 #define BUF     100
331         wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
332         char s[BUF];
333         va_list ap, ap2;
334
335         va_start(ap, fmt);
336         va_copy(ap2, ap);
337         smash_stack();
338         vsnprintf(s, sizeof(s), fmt, ap);
339         if (strcmp(result, s) != 0) {
340                 fprintf(stderr,
341                     "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n",
342                     line, fmt, argstr, s, result);
343                 abort();
344         }
345
346         smash_stack();
347         mbstowcs(ws, s, BUF - 1);
348         mbstowcs(wfmt, fmt, BUF - 1);
349         mbstowcs(wresult, result, BUF - 1);
350         vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
351         if (wcscmp(wresult, ws) != 0) {
352                 fprintf(stderr,
353                     "%d: wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n",
354                     line, wfmt, argstr, ws, wresult);
355                 abort();
356         }       
357 }