xref: /freebsd/lib/libc/tests/stdio/printfloat_test.c (revision 069ac184)
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 <err.h>
32 #include <fenv.h>
33 #include <float.h>
34 #include <locale.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wchar.h>
42 
43 #include <atf-c.h>
44 
45 static void
46 smash_stack(void)
47 {
48 	static uint32_t junk = 0xdeadbeef;
49 	uint32_t buf[512];
50 	size_t i;
51 
52 	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
53 		buf[i] = junk;
54 }
55 
56 #define	testfmt(result, fmt, ...)       \
57 	_testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)
58 static void
59 _testfmt(const char *result, const char *argstr, const char *fmt,...)
60 {
61 #define	BUF	100
62 	wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
63 	char s[BUF];
64 	va_list ap, ap2;
65 
66 	va_start(ap, fmt);
67 	va_copy(ap2, ap);
68 	smash_stack();
69 	vsnprintf(s, sizeof(s), fmt, ap);
70 	ATF_CHECK_MSG(strcmp(result, s) == 0,
71 	    "printf(\"%s\", %s) ==> [%s], expected [%s]",
72 	    fmt, argstr, s, result);
73 
74 	smash_stack();
75 	mbstowcs(ws, s, BUF - 1);
76 	mbstowcs(wfmt, fmt, BUF - 1);
77 	mbstowcs(wresult, result, BUF - 1);
78 	vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
79 	ATF_CHECK_MSG(wcscmp(wresult, ws) == 0,
80 	    "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]",
81 	    wfmt, argstr, ws, wresult);
82 
83 	va_end(ap);
84 	va_end(ap2);
85 }
86 
87 ATF_TC_WITHOUT_HEAD(float_within_limits);
88 ATF_TC_BODY(float_within_limits, tc)
89 {
90 
91 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
92 
93 	/* Basic tests of decimal output functionality. */
94 	testfmt(" 1.000000E+00", "%13E", 1.0);
95 	testfmt("     1.000000", "%13f", 1.0);
96 	testfmt("            1", "%13G", 1.0);
97 	testfmt(" 1.000000E+00", "%13LE", 1.0L);
98 	testfmt("     1.000000", "%13Lf", 1.0L);
99 	testfmt("            1", "%13LG", 1.0L);
100 
101 	testfmt("2.718282", "%.*f", -2, 2.7182818);
102 
103 	testfmt("1.234568e+06", "%e", 1234567.8);
104 	testfmt("1234567.800000", "%f", 1234567.8);
105 	testfmt("1.23457E+06", "%G", 1234567.8);
106 	testfmt("1.234568e+06", "%Le", 1234567.8L);
107 	testfmt("1234567.800000", "%Lf", 1234567.8L);
108 	testfmt("1.23457E+06", "%LG", 1234567.8L);
109 
110 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
111 	testfmt("123456789.864210", "%Lf", 123456789.8642097531L);
112 	testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);
113 	testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);
114 	testfmt(" 3.141592653589793238e-4000", "%L27.18Le",
115 	    3.14159265358979323846e-4000L);
116 #endif
117 }
118 
119 ATF_TC_WITHOUT_HEAD(infinities_and_nans);
120 ATF_TC_BODY(infinities_and_nans, tc)
121 {
122 
123 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
124 
125 	testfmt("nan", "%e", NAN);
126 	testfmt("NAN", "%F", NAN);
127 	testfmt("nan", "%g", NAN);
128 	testfmt("NAN", "%LE", (long double)NAN);
129 	testfmt("  nan", "%05e", NAN);
130 
131 	testfmt("INF", "%E", HUGE_VAL);
132 	testfmt("-inf", "%f", -HUGE_VAL);
133 	testfmt("+inf", "%+g", HUGE_VAL);
134 	testfmt(" inf", "%4.2Le", HUGE_VALL);
135 	testfmt("-inf", "%Lf", -HUGE_VALL);
136 	testfmt("  inf", "%05e", HUGE_VAL);
137 	testfmt(" -inf", "%05e", -HUGE_VAL);
138 }
139 
140 ATF_TC_WITHOUT_HEAD(padding);
141 ATF_TC_BODY(padding, tc)
142 {
143 
144 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
145 
146 	testfmt("0.000000e+00", "%e", 0.0);
147 	testfmt("0.000000", "%F", (double)0.0);
148 	testfmt("0", "%G", 0.0);
149 	testfmt("  0", "%3.0Lg", 0.0L);
150 	testfmt("    0", "%5.0f", 0.001);
151 }
152 
153 ATF_TC_WITHOUT_HEAD(precision_specifiers);
154 ATF_TC_BODY(precision_specifiers, tc)
155 {
156 
157 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
158 
159 	testfmt("1.0123e+00", "%.4e", 1.0123456789);
160 	testfmt("1.0123", "%.4f", 1.0123456789);
161 	testfmt("1.012", "%.4g", 1.0123456789);
162 	testfmt("1.2346e-02", "%.4e", 0.0123456789);
163 	testfmt("0.0123", "%.4f", 0.0123456789);
164 	testfmt("0.01235", "%.4g", 0.0123456789);
165 }
166 
167 ATF_TC_WITHOUT_HEAD(thousands_separator_and_other_locale_tests);
168 ATF_TC_BODY(thousands_separator_and_other_locale_tests, tc)
169 {
170 
171 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
172 
173 	testfmt("12345678.0625", "%'.04f", 12345678.0625);
174 	testfmt("0012345678.0625", "%'015.4F", 12345678.0625);
175 
176 	ATF_REQUIRE(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */
177 	testfmt("1,23,45,678.0625", "%'.4f", 12345678.0625);
178 	testfmt("01,23,45,678.0625", "%'017.4F", 12345678.0625);
179 	testfmt(" 9,000", "%'6.0f", 9000.0);
180 	testfmt("9,000.0", "%'.1f", 9000.0);
181 
182 	ATF_REQUIRE(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
183 	testfmt("3,1415", "%g", 3.1415);
184 
185 	/* thousands=. decimalpoint=, grouping=3;3 */
186 	ATF_REQUIRE(setlocale(LC_NUMERIC, "el_GR.ISO8859-7")); /* decimalpoint==, */
187 	testfmt("1.234,00", "%'.2f", 1234.00);
188 	testfmt("123.456,789", "%'.3f", 123456.789);
189 
190 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
191 	testfmt("12345678.062500", "%'f", 12345678.0625);
192 	testfmt("9000.000000", "%'f", 9000.0);
193 }
194 
195 ATF_TC_WITHOUT_HEAD(signed_conversions);
196 ATF_TC_BODY(signed_conversions, tc)
197 {
198 
199 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
200 
201 	testfmt("+2.500000e-01", "%+e", 0.25);
202 	testfmt("+0.000000", "%+F", 0.0);
203 	testfmt("-1", "%+g", -1.0);
204 
205 	testfmt("-1.000000e+00", "% e", -1.0);
206 	testfmt("+1.000000", "% +f", 1.0);
207 	testfmt(" 1", "% g", 1.0);
208 	testfmt(" 0", "% g", 0.0);
209 }
210 
211 ATF_TC_WITHOUT_HEAD(alternate_form);
212 ATF_TC_BODY(alternate_form, tc)
213 {
214 
215 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
216 
217 	testfmt("1.250e+00", "%#.3e", 1.25);
218 	testfmt("123.000000", "%#f", 123.0);
219 	testfmt(" 12345.", "%#7.5g", 12345.0);
220 	testfmt(" 1.00000", "%#8g", 1.0);
221 	testfmt("0.0", "%#.2g", 0.0);
222 }
223 
224 ATF_TC_WITHOUT_HEAD(padding_and_decimal_point_placement);
225 ATF_TC_BODY(padding_and_decimal_point_placement, tc)
226 {
227 
228 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
229 
230 	testfmt("03.2E+00", "%08.1E", 3.25);
231 	testfmt("003.25", "%06.2F", 3.25);
232 	testfmt("0003.25", "%07.4G", 3.25);
233 
234 	testfmt("3.14159e-05", "%g", 3.14159e-5);
235 	testfmt("0.000314159", "%g", 3.14159e-4);
236 	testfmt("3.14159e+06", "%g", 3.14159e6);
237 	testfmt("314159", "%g", 3.14159e5);
238 	testfmt("314159.", "%#g", 3.14159e5);
239 
240 	testfmt(" 9.000000e+03", "%13e", 9000.0);
241 	testfmt(" 9000.000000", "%12f", 9000.0);
242 	testfmt(" 9000", "%5g", 9000.0);
243 	testfmt(" 900000.", "%#8g", 900000.0);
244 	testfmt(" 9e+06", "%6g", 9000000.0);
245 	testfmt(" 9.000000e-04", "%13e", 0.0009);
246 	testfmt(" 0.000900", "%9f", 0.0009);
247 	testfmt(" 0.0009", "%7g", 0.0009);
248 	testfmt(" 9e-05", "%6g", 0.00009);
249 	testfmt(" 9.00000e-05", "%#12g", 0.00009);
250 	testfmt(" 9.e-05", "%#7.1g", 0.00009);
251 
252 	testfmt(" 0.0", "%4.1f", 0.0);
253 	testfmt("90.0", "%4.1f", 90.0);
254 	testfmt(" 100", "%4.0f", 100.0);
255 	testfmt("9.0e+01", "%4.1e", 90.0);
256 	testfmt("1e+02", "%4.0e", 100.0);
257 }
258 
259 ATF_TC_WITHOUT_HEAD(decimal_rounding);
260 ATF_TC_BODY(decimal_rounding, tc)
261 {
262 
263 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
264 
265 	fesetround(FE_DOWNWARD);
266 	testfmt("4.437", "%.3f", 4.4375);
267 	testfmt("-4.438", "%.3f", -4.4375);
268 	testfmt("4.437", "%.3Lf", 4.4375L);
269 	testfmt("-4.438", "%.3Lf", -4.4375L);
270 
271 	fesetround(FE_UPWARD);
272 	testfmt("4.438", "%.3f", 4.4375);
273 	testfmt("-4.437", "%.3f", -4.4375);
274 	testfmt("4.438", "%.3Lf", 4.4375L);
275 	testfmt("-4.437", "%.3Lf", -4.4375L);
276 
277 	fesetround(FE_TOWARDZERO);
278 	testfmt("4.437", "%.3f", 4.4375);
279 	testfmt("-4.437", "%.3f", -4.4375);
280 	testfmt("4.437", "%.3Lf", 4.4375L);
281 	testfmt("-4.437", "%.3Lf", -4.4375L);
282 
283 	fesetround(FE_TONEAREST);
284 	testfmt("4.438", "%.3f", 4.4375);
285 	testfmt("-4.438", "%.3f", -4.4375);
286 	testfmt("4.438", "%.3Lf", 4.4375L);
287 	testfmt("-4.438", "%.3Lf", -4.4375L);
288 }
289 
290 ATF_TC_WITHOUT_HEAD(hexadecimal_floating_point);
291 ATF_TC_BODY(hexadecimal_floating_point, tc)
292 {
293 
294 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
295 
296 	/*
297 	 * Hexadecimal floating point (%a, %A) tests.  Some of these
298 	 * are only valid if the implementation converts to hex digits
299 	 * on nibble boundaries.
300 	 */
301 	testfmt("0x0p+0", "%a", 0x0.0p0);
302 	testfmt("0X0.P+0", "%#LA", 0x0.0p0L);
303 	testfmt("inf", "%La", (long double)INFINITY);
304 	testfmt("+INF", "%+A", INFINITY);
305 	testfmt("nan", "%La", (long double)NAN);
306 	testfmt("NAN", "%A", NAN);
307 
308 	testfmt(" 0x1.23p+0", "%10a", 0x1.23p0);
309 	testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500);
310 	testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40);
311 	testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4);
312 	testfmt("0x1p-1074", "%a", 0x1p-1074);
313 	testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024);
314 
315 #if (LDBL_MANT_DIG == 64)
316 	testfmt("0x1.921fb54442d18468p+1", "%La", 0x3.243f6a8885a308dp0L);
317 	testfmt("0x1p-16445", "%La", 0x1p-16445L);
318 	testfmt("0x1.30ecap-16381", "%La", 0x9.8765p-16384L);
319 #elif (LDBL_MANT_DIG == 113)
320 	testfmt("0x1.921fb54442d18469898cc51701b8p+1", "%La",
321 	    0x3.243f6a8885a308d313198a2e037p0L);
322 	testfmt("0x1p-16494", "%La", 0x1p-16494L);
323 	testfmt("0x1.2345p-16384", "%La", 0x1.2345p-16384L);
324 #else
325 	testfmt("0x1.921fb54442d18p+1", "%La", 0x3.243f6a8885a31p0L);
326 	testfmt("0x1p-1074", "%La", 0x1p-1074L);
327 	testfmt("0x1.30ecap-1021", "%La", 0x9.8765p-1024L);
328 #endif
329 }
330 
331 ATF_TC_WITHOUT_HEAD(hexadecimal_rounding);
332 ATF_TC_BODY(hexadecimal_rounding, tc)
333 {
334 
335 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
336 
337 	fesetround(FE_TOWARDZERO);
338 	testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
339 	testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
340 	testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
341 	testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
342 	testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
343 
344 	fesetround(FE_DOWNWARD);
345 	testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
346 	testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0);
347 	testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
348 	testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
349 	testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
350 
351 	fesetround(FE_UPWARD);
352 	testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
353 	testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
354 	testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0);
355 	testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
356 	testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
357 
358 	fesetround(FE_TONEAREST);
359 	testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4);
360 	testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
361 	testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
362 	testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
363 	testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
364 	testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
365 	testfmt("0x1.00p-1029", "%.2a", 0x1.fffp-1030);
366 	testfmt("0x1.00p-1026", "%.2a", 0xf.fffp-1030);
367 	testfmt("0x1.83p+0", "%.2a", 1.51);
368 }
369 
370 ATF_TC_WITHOUT_HEAD(subnormal_double);
371 ATF_TC_BODY(subnormal_double, tc)
372 {
373 	/* Regression test for https://bugs.freebsd.org/253847 */
374 	double positive = __DBL_DENORM_MIN__;
375 	testfmt("4.9406564584124654418e-324", "%20.20g", positive);
376 	testfmt("4.9406564584124654418E-324", "%20.20G", positive);
377 	testfmt("0x1p-1074", "%a", positive);
378 	testfmt("0X1P-1074", "%A", positive);
379 	double negative = -__DBL_DENORM_MIN__;
380 	testfmt("-4.9406564584124654418e-324", "%20.20g", negative);
381 	testfmt("-4.9406564584124654418E-324", "%20.20G", negative);
382 	testfmt("-0x1p-1074", "%a", negative);
383 	testfmt("-0X1P-1074", "%A", negative);
384 }
385 
386 ATF_TC_WITHOUT_HEAD(subnormal_float);
387 ATF_TC_BODY(subnormal_float, tc)
388 {
389 	float positive = __FLT_DENORM_MIN__;
390 	testfmt("1.4012984643248170709e-45", "%20.20g", positive);
391 	testfmt("1.4012984643248170709E-45", "%20.20G", positive);
392 	testfmt("0x1p-149", "%a", positive);
393 	testfmt("0X1P-149", "%A", positive);
394 	float negative = -__FLT_DENORM_MIN__;
395 	testfmt("-1.4012984643248170709e-45", "%20.20g", negative);
396 	testfmt("-1.4012984643248170709E-45", "%20.20G", negative);
397 	testfmt("-0x1p-149", "%a", negative);
398 	testfmt("-0X1P-149", "%A", negative);
399 }
400 
401 ATF_TP_ADD_TCS(tp)
402 {
403 
404 	ATF_TP_ADD_TC(tp, float_within_limits);
405 	ATF_TP_ADD_TC(tp, infinities_and_nans);
406 	ATF_TP_ADD_TC(tp, padding);
407 	ATF_TP_ADD_TC(tp, precision_specifiers);
408 	ATF_TP_ADD_TC(tp, thousands_separator_and_other_locale_tests);
409 	ATF_TP_ADD_TC(tp, signed_conversions);
410 	ATF_TP_ADD_TC(tp, alternate_form);
411 	ATF_TP_ADD_TC(tp, padding_and_decimal_point_placement);
412 	ATF_TP_ADD_TC(tp, decimal_rounding);
413 	ATF_TP_ADD_TC(tp, hexadecimal_floating_point);
414 	ATF_TP_ADD_TC(tp, hexadecimal_rounding);
415 	ATF_TP_ADD_TC(tp, subnormal_double);
416 	ATF_TP_ADD_TC(tp, subnormal_float);
417 
418 	return (atf_no_error());
419 }
420