xref: /freebsd/lib/libc/tests/stdio/scanfloat_test.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (C) 2003, 2005 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 scanf() floating point formats.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <fenv.h>
33 #include <float.h>
34 #include <locale.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <atf-c.h>
41 
42 #define	eq(type, a, b)	_eq(type##_EPSILON, (a), (b))
43 static int
44 _eq(long double epsilon, long double a, long double b)
45 {
46 	long double delta;
47 
48 	delta = fabsl(a - b);
49 	return (delta <= epsilon);
50 }
51 
52 ATF_TC_WITHOUT_HEAD(normalized_numbers);
53 ATF_TC_BODY(normalized_numbers, tc)
54 {
55 	char buf[128];
56 	long double ld = 0.0;
57 	double d = 0.0;
58 	float f = 0.0;
59 
60 	buf[0] = '\0';
61 	ATF_REQUIRE(setlocale(LC_NUMERIC, ""));
62 
63 	sscanf("3.141592", "%e", &f);
64 	ATF_REQUIRE(eq(FLT, f, 3.141592));
65 
66 	sscanf("3.141592653589793", "%lf", &d);
67 	ATF_REQUIRE(eq(DBL, d, 3.141592653589793));
68 
69 	sscanf("1.234568e+06", "%E", &f);
70 	ATF_REQUIRE(eq(FLT, f, 1.234568e+06));
71 
72 	sscanf("-1.234568e6", "%lF", &d);
73 	ATF_REQUIRE(eq(DBL, d, -1.234568e6));
74 
75 	sscanf("+1.234568e-52", "%LG", &ld);
76 	ATF_REQUIRE(eq(LDBL, ld, 1.234568e-52L));
77 
78 	sscanf("0.1", "%la", &d);
79 	ATF_REQUIRE(eq(DBL, d, 0.1));
80 
81 	sscanf("00.2", "%lA", &d);
82 	ATF_REQUIRE(eq(DBL, d, 0.2));
83 
84 	sscanf("123456", "%5le%s", &d, buf);
85 	ATF_REQUIRE(eq(DBL, d, 12345.));
86 	ATF_REQUIRE(strcmp(buf, "6") == 0);
87 
88 	sscanf("1.0Q", "%*5le%s", buf);
89 	ATF_REQUIRE(strcmp(buf, "Q") == 0);
90 
91 	sscanf("-1.23e", "%e%s", &f, buf);
92 	ATF_REQUIRE(eq(FLT, f, -1.23));
93 	ATF_REQUIRE(strcmp(buf, "e") == 0);
94 
95 	sscanf("1.25e+", "%le%s", &d, buf);
96 	ATF_REQUIRE(eq(DBL, d, 1.25));
97 	ATF_REQUIRE(strcmp(buf, "e+") == 0);
98 
99 	sscanf("1.23E4E5", "%le%s", &d, buf);
100 	ATF_REQUIRE(eq(DBL, d, 1.23e4));
101 	ATF_REQUIRE(strcmp(buf, "E5") == 0);
102 
103 	sscanf("12e6", "%le", &d);
104 	ATF_REQUIRE(eq(DBL, d, 12e6));
105 
106 	sscanf("1.a", "%le%s", &d, buf);
107 	ATF_REQUIRE(eq(DBL, d, 1.0));
108 	ATF_REQUIRE(strcmp(buf, "a") == 0);
109 
110 	sscanf(".0p4", "%le%s", &d, buf);
111 	ATF_REQUIRE(eq(DBL, d, 0.0));
112 	ATF_REQUIRE(strcmp(buf, "p4") == 0);
113 
114 	d = 0.25;
115 	ATF_REQUIRE(sscanf(".", "%le", &d) == 0);
116 	ATF_REQUIRE(d == 0.25);
117 
118 	sscanf("0x08", "%le", &d);
119 	ATF_REQUIRE(d == 0x8p0);
120 
121 	sscanf("0x90a.bcdefP+09a", "%le%s", &d, buf);
122 	ATF_REQUIRE(d == 0x90a.bcdefp+09);
123 	ATF_REQUIRE(strcmp(buf, "a") == 0);
124 
125 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
126 	sscanf("3.14159265358979323846", "%Lg", &ld);
127 	ATF_REQUIRE(eq(LDBL, ld, 3.14159265358979323846L));
128 
129 	sscanf("  0X.0123456789abcdefffp-3g", "%Le%s", &ld, buf);
130 	ATF_REQUIRE(ld == 0x0.0123456789abcdefffp-3L);
131 	ATF_REQUIRE(strcmp(buf, "g") == 0);
132 #endif
133 
134 	sscanf("0xg", "%le%s", &d, buf);
135 	ATF_REQUIRE(d == 0.0);
136 	ATF_REQUIRE(strcmp(buf, "xg") == 0);
137 
138 	ATF_REQUIRE(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
139 
140 	sscanf("1.23", "%le%s", &d, buf);
141 	ATF_REQUIRE(d == 1.0);
142 	ATF_REQUIRE(strcmp(buf, ".23") == 0);
143 
144 	sscanf("1,23", "%le", &d);
145 	ATF_REQUIRE(d == 1.23);
146 
147 	ATF_REQUIRE(setlocale(LC_NUMERIC, ""));
148 }
149 
150 ATF_TC_WITHOUT_HEAD(infinities_and_nans);
151 ATF_TC_BODY(infinities_and_nans, tc)
152 {
153 	char buf[128];
154 	long double ld = 0.0;
155 	double d = 0.0;
156 	float f = 0.0;
157 
158 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
159 
160 	sscanf("-Inf", "%le", &d);
161 	ATF_REQUIRE(d < 0.0 && isinf(d));
162 
163 	sscanf("iNfInItY and beyond", "%le%s", &d, buf);
164 	ATF_REQUIRE(d > 0.0 && isinf(d));
165 	ATF_REQUIRE(strcmp(buf, " and beyond"));
166 
167 	sscanf("NaN", "%le", &d);
168 	ATF_REQUIRE(isnan(d));
169 
170 	sscanf("NAN(123Y", "%le%s", &d, buf);
171 	ATF_REQUIRE(isnan(d));
172 	ATF_REQUIRE(strcmp(buf, "(123Y") == 0);
173 
174 	sscanf("nan(f00f)plugh", "%le%s", &d, buf);
175 	ATF_REQUIRE(isnan(d));
176 	ATF_REQUIRE(strcmp(buf, "plugh") == 0);
177 
178 	sscanf("-nan", "%le", &d);
179 	ATF_REQUIRE(isnan(d));
180 
181 	/* Only quiet NaNs should be returned. */
182 	sscanf("NaN", "%e", &f);
183 	sscanf("nan", "%le", &d);
184 	sscanf("nan", "%Le", &ld);
185 	feclearexcept(FE_ALL_EXCEPT);
186 	ATF_REQUIRE(f != f);
187 	ATF_REQUIRE(d != d);
188 	ATF_REQUIRE(ld != ld);
189 	ATF_REQUIRE(fetestexcept(FE_INVALID) == 0);
190 	sscanf("nan(1234)", "%e", &f);
191 	sscanf("nan(1234)", "%le", &d);
192 	sscanf("nan(1234)", "%Le", &ld);
193 	feclearexcept(FE_ALL_EXCEPT);
194 	ATF_REQUIRE(f != f);
195 	ATF_REQUIRE(d != d);
196 	ATF_REQUIRE(ld != ld);
197 	/* POSIX says we should only generate quiet NaNs. */
198 	ATF_REQUIRE(fetestexcept(FE_INVALID) == 0);
199 }
200 
201 ATF_TC_WITHOUT_HEAD(rounding_tests);
202 ATF_TC_BODY(rounding_tests, tc)
203 {
204 	long double ld = 0.0;
205 	double d = 0.0;
206 
207 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
208 
209 	fesetround(FE_DOWNWARD);
210 
211 	sscanf("1.999999999999999999999999999999999", "%le", &d);
212 	ATF_REQUIRE(d < 2.0);
213 	sscanf("0x1.ffffffffffffffp0", "%le", &d);
214 	ATF_REQUIRE(d < 2.0);
215 	sscanf("1.999999999999999999999999999999999", "%Le", &ld);
216 	ATF_REQUIRE(ld < 2.0);
217 
218 	sscanf("1.0571892669084007", "%le", &d);
219 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc59p0);
220 	sscanf("-1.0571892669084007", "%le", &d);
221 	ATF_REQUIRE(d == -0x1.0ea3f4af0dc5ap0);
222 	sscanf("1.0571892669084010", "%le", &d);
223 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc5ap0);
224 
225 	sscanf("0x1.23p-5000", "%le", &d);
226 	ATF_REQUIRE(d == 0.0);
227 
228 	sscanf("0x1.2345678p-1050", "%le", &d);
229 	ATF_REQUIRE(d == 0x1.234567p-1050);
230 
231 	fesetround(FE_UPWARD);
232 
233 	sscanf("1.0571892669084007", "%le", &d);
234 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc5ap0);
235 	sscanf("-1.0571892669084007", "%le", &d);
236 	ATF_REQUIRE(d == -0x1.0ea3f4af0dc59p0);
237 	sscanf("1.0571892669084010", "%le", &d);
238 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc5bp0);
239 
240 	sscanf("0x1.23p-5000", "%le", &d);
241 	ATF_REQUIRE(d == 0x1p-1074);
242 
243 	sscanf("0x1.2345678p-1050", "%le", &d);
244 	ATF_REQUIRE(d == 0x1.234568p-1050);
245 
246 	fesetround(FE_TOWARDZERO);
247 
248 	sscanf("1.0571892669084007", "%le", &d);
249 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc59p0);
250 	sscanf("-1.0571892669084007", "%le", &d);
251 	ATF_REQUIRE(d == -0x1.0ea3f4af0dc59p0);
252 	sscanf("1.0571892669084010", "%le", &d);
253 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc5ap0);
254 
255 	sscanf("0x1.23p-5000", "%le", &d);
256 	ATF_REQUIRE(d == 0.0);
257 
258 	sscanf("0x1.2345678p-1050", "%le", &d);
259 	ATF_REQUIRE(d == 0x1.234567p-1050);
260 
261 	fesetround(FE_TONEAREST);
262 
263 	/* 1.0571892669084007 is slightly closer to 0x1.0ea3f4af0dc59p0 */
264 	sscanf("1.0571892669084007", "%le", &d);
265 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc59p0);
266 	sscanf("-1.0571892669084007", "%le", &d);
267 	ATF_REQUIRE(d == -0x1.0ea3f4af0dc59p0);
268 	sscanf("1.0571892669084010", "%le", &d);
269 	ATF_REQUIRE(d == 0x1.0ea3f4af0dc5bp0);
270 
271 	/* strtod() should round small numbers to 0. */
272 	sscanf("0x1.23p-5000", "%le", &d);
273 	ATF_REQUIRE(d == 0.0);
274 
275 	/* Extra digits in a denormal shouldn't break anything. */
276 	sscanf("0x1.2345678p-1050", "%le", &d);
277 	ATF_REQUIRE(d == 0x1.234568p-1050);
278 }
279 
280 ATF_TC_WITHOUT_HEAD(strtod);
281 ATF_TC_BODY(strtod, tc)
282 {
283 	char *endp;
284 
285 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
286 
287 	ATF_REQUIRE(strtod("0xy", &endp) == 0);
288 	ATF_REQUIRE(strcmp("xy", endp) == 0);
289 
290 	/* This used to cause an infinite loop and round the wrong way. */
291 	fesetround(FE_DOWNWARD);
292 	ATF_REQUIRE(strtof("3.5e38", &endp) == FLT_MAX);
293 	ATF_REQUIRE(strtod("2e308", &endp) == DBL_MAX);
294 	fesetround(FE_UPWARD);
295 	ATF_REQUIRE(strtof("3.5e38", &endp) == INFINITY);
296 	ATF_REQUIRE(strtod("2e308", &endp) == INFINITY);
297 	fesetround(FE_TOWARDZERO);
298 	ATF_REQUIRE(strtof("3.5e38", &endp) == FLT_MAX);
299 	ATF_REQUIRE(strtod("2e308", &endp) == DBL_MAX);
300 	fesetround(FE_TONEAREST);
301 	ATF_REQUIRE(strtof("3.5e38", &endp) == INFINITY);
302 	ATF_REQUIRE(strtod("2e308", &endp) == INFINITY);
303 }
304 
305 ATF_TP_ADD_TCS(tp)
306 {
307 
308 	ATF_TP_ADD_TC(tp, normalized_numbers);
309 	ATF_TP_ADD_TC(tp, infinities_and_nans);
310 	ATF_TP_ADD_TC(tp, rounding_tests);
311 	ATF_TP_ADD_TC(tp, strtod);
312 
313 	return (atf_no_error());
314 }
315