2 * Copyright (c) 2002-2009 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Test for printf() floating point formats.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
51 static uint32_t junk = 0xdeadbeef;
55 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
59 #define testfmt(result, fmt, ...) \
60 _testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)
62 _testfmt(const char *result, const char *argstr, const char *fmt,...)
65 wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
72 vsnprintf(s, sizeof(s), fmt, ap);
73 if (strcmp(result, s) != 0) {
75 "printf(\"%s\", %s) ==> [%s], expected [%s]\n",
76 fmt, argstr, s, result);
80 mbstowcs(ws, s, BUF - 1);
81 mbstowcs(wfmt, fmt, BUF - 1);
82 mbstowcs(wresult, result, BUF - 1);
83 vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
84 if (wcscmp(wresult, ws) != 0) {
86 "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n",
87 wfmt, argstr, ws, wresult);
91 ATF_TC_WITHOUT_HEAD(float_within_limits);
92 ATF_TC_BODY(float_within_limits, tc)
95 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
97 /* Basic tests of decimal output functionality. */
98 testfmt(" 1.000000E+00", "%13E", 1.0);
99 testfmt(" 1.000000", "%13f", 1.0);
100 testfmt(" 1", "%13G", 1.0);
101 testfmt(" 1.000000E+00", "%13LE", 1.0L);
102 testfmt(" 1.000000", "%13Lf", 1.0L);
103 testfmt(" 1", "%13LG", 1.0L);
105 testfmt("2.718282", "%.*f", -2, 2.7182818);
107 testfmt("1.234568e+06", "%e", 1234567.8);
108 testfmt("1234567.800000", "%f", 1234567.8);
109 testfmt("1.23457E+06", "%G", 1234567.8);
110 testfmt("1.234568e+06", "%Le", 1234567.8L);
111 testfmt("1234567.800000", "%Lf", 1234567.8L);
112 testfmt("1.23457E+06", "%LG", 1234567.8L);
114 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
115 testfmt("123456789.864210", "%Lf", 123456789.8642097531L);
116 testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);
117 testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);
118 testfmt(" 3.141592653589793238e-4000", "%L27.18Le",
119 3.14159265358979323846e-4000L);
123 ATF_TC_WITHOUT_HEAD(infinities_and_nans);
124 ATF_TC_BODY(infinities_and_nans, tc)
127 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
129 testfmt("nan", "%e", NAN);
130 testfmt("NAN", "%F", NAN);
131 testfmt("nan", "%g", NAN);
132 testfmt("NAN", "%LE", (long double)NAN);
133 testfmt(" nan", "%05e", NAN);
135 testfmt("INF", "%E", HUGE_VAL);
136 testfmt("-inf", "%f", -HUGE_VAL);
137 testfmt("+inf", "%+g", HUGE_VAL);
138 testfmt(" inf", "%4.2Le", HUGE_VALL);
139 testfmt("-inf", "%Lf", -HUGE_VALL);
140 testfmt(" inf", "%05e", HUGE_VAL);
141 testfmt(" -inf", "%05e", -HUGE_VAL);
144 ATF_TC_WITHOUT_HEAD(padding);
145 ATF_TC_BODY(padding, tc)
148 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
150 testfmt("0.000000e+00", "%e", 0.0);
151 testfmt("0.000000", "%F", (double)0.0);
152 testfmt("0", "%G", 0.0);
153 testfmt(" 0", "%3.0Lg", 0.0L);
154 testfmt(" 0", "%5.0f", 0.001);
157 ATF_TC_WITHOUT_HEAD(precision_specifiers);
158 ATF_TC_BODY(precision_specifiers, tc)
161 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
163 testfmt("1.0123e+00", "%.4e", 1.0123456789);
164 testfmt("1.0123", "%.4f", 1.0123456789);
165 testfmt("1.012", "%.4g", 1.0123456789);
166 testfmt("1.2346e-02", "%.4e", 0.0123456789);
167 testfmt("0.0123", "%.4f", 0.0123456789);
168 testfmt("0.01235", "%.4g", 0.0123456789);
171 ATF_TC_WITHOUT_HEAD(thousands_separator_and_other_locale_tests);
172 ATF_TC_BODY(thousands_separator_and_other_locale_tests, tc)
175 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
177 testfmt("12345678.0625", "%'.04f", 12345678.0625);
178 testfmt("0012345678.0625", "%'015.4F", 12345678.0625);
180 ATF_REQUIRE(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */
181 testfmt("123,456,78.0625", "%'.4f", 12345678.0625);
182 testfmt("00123,456,78.0625", "%'017.4F", 12345678.0625);
183 testfmt(" 90,00", "%'6.0f", 9000.0);
184 testfmt("90,00.0", "%'.1f", 9000.0);
186 ATF_REQUIRE(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
187 testfmt("3,1415", "%g", 3.1415);
189 /* thousands=. decimalpoint=, grouping=3;3 */
190 ATF_REQUIRE(setlocale(LC_NUMERIC, "el_GR.ISO8859-7")); /* decimalpoint==, */
191 testfmt("1.234,00", "%'.2f", 1234.00);
192 testfmt("123.456,789", "%'.3f", 123456.789);
194 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
195 testfmt("12345678.062500", "%'f", 12345678.0625);
196 testfmt("9000.000000", "%'f", 9000.0);
199 ATF_TC_WITHOUT_HEAD(signed_conversions);
200 ATF_TC_BODY(signed_conversions, tc)
203 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
205 testfmt("+2.500000e-01", "%+e", 0.25);
206 testfmt("+0.000000", "%+F", 0.0);
207 testfmt("-1", "%+g", -1.0);
209 testfmt("-1.000000e+00", "% e", -1.0);
210 testfmt("+1.000000", "% +f", 1.0);
211 testfmt(" 1", "% g", 1.0);
212 testfmt(" 0", "% g", 0.0);
215 ATF_TC_WITHOUT_HEAD(alternate_form);
216 ATF_TC_BODY(alternate_form, tc)
219 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
221 testfmt("1.250e+00", "%#.3e", 1.25);
222 testfmt("123.000000", "%#f", 123.0);
223 testfmt(" 12345.", "%#7.5g", 12345.0);
224 testfmt(" 1.00000", "%#8g", 1.0);
225 testfmt("0.0", "%#.2g", 0.0);
228 ATF_TC_WITHOUT_HEAD(padding_and_decimal_point_placement);
229 ATF_TC_BODY(padding_and_decimal_point_placement, tc)
232 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
234 testfmt("03.2E+00", "%08.1E", 3.25);
235 testfmt("003.25", "%06.2F", 3.25);
236 testfmt("0003.25", "%07.4G", 3.25);
238 testfmt("3.14159e-05", "%g", 3.14159e-5);
239 testfmt("0.000314159", "%g", 3.14159e-4);
240 testfmt("3.14159e+06", "%g", 3.14159e6);
241 testfmt("314159", "%g", 3.14159e5);
242 testfmt("314159.", "%#g", 3.14159e5);
244 testfmt(" 9.000000e+03", "%13e", 9000.0);
245 testfmt(" 9000.000000", "%12f", 9000.0);
246 testfmt(" 9000", "%5g", 9000.0);
247 testfmt(" 900000.", "%#8g", 900000.0);
248 testfmt(" 9e+06", "%6g", 9000000.0);
249 testfmt(" 9.000000e-04", "%13e", 0.0009);
250 testfmt(" 0.000900", "%9f", 0.0009);
251 testfmt(" 0.0009", "%7g", 0.0009);
252 testfmt(" 9e-05", "%6g", 0.00009);
253 testfmt(" 9.00000e-05", "%#12g", 0.00009);
254 testfmt(" 9.e-05", "%#7.1g", 0.00009);
256 testfmt(" 0.0", "%4.1f", 0.0);
257 testfmt("90.0", "%4.1f", 90.0);
258 testfmt(" 100", "%4.0f", 100.0);
259 testfmt("9.0e+01", "%4.1e", 90.0);
260 testfmt("1e+02", "%4.0e", 100.0);
263 ATF_TC_WITHOUT_HEAD(decimal_rounding);
264 ATF_TC_BODY(decimal_rounding, tc)
267 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
269 fesetround(FE_DOWNWARD);
270 testfmt("4.437", "%.3f", 4.4375);
271 testfmt("-4.438", "%.3f", -4.4375);
272 testfmt("4.437", "%.3Lf", 4.4375L);
273 testfmt("-4.438", "%.3Lf", -4.4375L);
275 fesetround(FE_UPWARD);
276 testfmt("4.438", "%.3f", 4.4375);
277 testfmt("-4.437", "%.3f", -4.4375);
278 testfmt("4.438", "%.3Lf", 4.4375L);
279 testfmt("-4.437", "%.3Lf", -4.4375L);
281 fesetround(FE_TOWARDZERO);
282 testfmt("4.437", "%.3f", 4.4375);
283 testfmt("-4.437", "%.3f", -4.4375);
284 testfmt("4.437", "%.3Lf", 4.4375L);
285 testfmt("-4.437", "%.3Lf", -4.4375L);
287 fesetround(FE_TONEAREST);
288 testfmt("4.438", "%.3f", 4.4375);
289 testfmt("-4.438", "%.3f", -4.4375);
290 testfmt("4.438", "%.3Lf", 4.4375L);
291 testfmt("-4.438", "%.3Lf", -4.4375L);
294 ATF_TC_WITHOUT_HEAD(hexadecimal_floating_point);
295 ATF_TC_BODY(hexadecimal_floating_point, tc)
298 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
301 * Hexadecimal floating point (%a, %A) tests. Some of these
302 * are only valid if the implementation converts to hex digits
303 * on nibble boundaries.
305 testfmt("0x0p+0", "%a", 0x0.0p0);
306 testfmt("0X0.P+0", "%#LA", 0x0.0p0L);
307 testfmt("inf", "%La", (long double)INFINITY);
308 testfmt("+INF", "%+A", INFINITY);
309 testfmt("nan", "%La", (long double)NAN);
310 testfmt("NAN", "%A", NAN);
312 testfmt(" 0x1.23p+0", "%10a", 0x1.23p0);
313 testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500);
314 testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40);
315 testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4);
316 testfmt("0x1p-1074", "%a", 0x1p-1074);
317 testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024);
319 #if (LDBL_MANT_DIG == 64) && !defined(__i386__)
320 testfmt("0x1.921fb54442d18468p+1", "%La", 0x3.243f6a8885a308dp0L);
321 testfmt("0x1p-16445", "%La", 0x1p-16445L);
322 testfmt("0x1.30ecap-16381", "%La", 0x9.8765p-16384L);
323 #elif (LDBL_MANT_DIG == 113)
324 testfmt("0x1.921fb54442d18469898cc51701b8p+1", "%La",
325 0x3.243f6a8885a308d313198a2e037p0L);
326 testfmt("0x1p-16494", "%La", 0x1p-16494L);
327 testfmt("0x1.2345p-16384", "%La", 0x1.2345p-16384L);
329 testfmt("0x1.921fb54442d18p+1", "%La", 0x3.243f6a8885a31p0L);
330 testfmt("0x1p-1074", "%La", 0x1p-1074L);
331 testfmt("0x1.30ecap-1021", "%La", 0x9.8765p-1024L);
335 ATF_TC_WITHOUT_HEAD(hexadecimal_rounding);
336 ATF_TC_BODY(hexadecimal_rounding, tc)
339 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
341 fesetround(FE_TOWARDZERO);
342 testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
343 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
344 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
345 testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
346 testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
348 fesetround(FE_DOWNWARD);
349 testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);
350 testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0);
351 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
352 testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);
353 testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
355 fesetround(FE_UPWARD);
356 testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
357 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
358 testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0);
359 testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
360 testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);
362 fesetround(FE_TONEAREST);
363 testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4);
364 testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);
365 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);
366 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);
367 testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);
368 testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);
369 testfmt("0x1.00p-1029", "%.2a", 0x1.fffp-1030);
370 testfmt("0x1.00p-1026", "%.2a", 0xf.fffp-1030);
371 testfmt("0x1.83p+0", "%.2a", 1.51);
377 ATF_TP_ADD_TC(tp, float_within_limits);
378 ATF_TP_ADD_TC(tp, infinities_and_nans);
379 ATF_TP_ADD_TC(tp, padding);
380 ATF_TP_ADD_TC(tp, precision_specifiers);
381 ATF_TP_ADD_TC(tp, thousands_separator_and_other_locale_tests);
382 ATF_TP_ADD_TC(tp, signed_conversions);
383 ATF_TP_ADD_TC(tp, alternate_form);
384 ATF_TP_ADD_TC(tp, padding_and_decimal_point_placement);
385 ATF_TP_ADD_TC(tp, decimal_rounding);
386 ATF_TP_ADD_TC(tp, hexadecimal_floating_point);
387 ATF_TP_ADD_TC(tp, hexadecimal_rounding);
389 return (atf_no_error());