2 * Copyright (C) 2003, 2005 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 scanf() floating point formats.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
42 #define eq(type, a, b) _eq(type##_EPSILON, (a), (b))
43 static int _eq(long double epsilon, long double a, long double b);
46 main(int argc, char *argv[])
56 assert(setlocale(LC_NUMERIC, ""));
59 * Various tests for normalized numbers
61 sscanf("3.141592", "%e", &f);
62 assert(eq(FLT, f, 3.141592));
64 sscanf("3.141592653589793", "%lf", &d);
65 assert(eq(DBL, d, 3.141592653589793));
67 sscanf("1.234568e+06", "%E", &f);
68 assert(eq(FLT, f, 1.234568e+06));
70 sscanf("-1.234568e6", "%lF", &d);
71 assert(eq(DBL, d, -1.234568e6));
73 sscanf("+1.234568e-52", "%LG", &ld);
74 assert(eq(LDBL, ld, 1.234568e-52L));
76 sscanf("0.1", "%la", &d);
77 assert(eq(DBL, d, 0.1));
79 sscanf("00.2", "%lA", &d);
80 assert(eq(DBL, d, 0.2));
82 sscanf("123456", "%5le%s", &d, buf);
83 assert(eq(DBL, d, 12345.));
84 assert(strcmp(buf, "6") == 0);
86 sscanf("1.0Q", "%*5le%s", buf);
87 assert(strcmp(buf, "Q") == 0);
89 sscanf("-1.23e", "%e%s", &f, buf);
90 assert(eq(FLT, f, -1.23));
91 assert(strcmp(buf, "e") == 0);
93 sscanf("1.25e+", "%le%s", &d, buf);
94 assert(eq(DBL, d, 1.25));
95 assert(strcmp(buf, "e+") == 0);
97 sscanf("1.23E4E5", "%le%s", &d, buf);
98 assert(eq(DBL, d, 1.23e4));
99 assert(strcmp(buf, "E5") == 0);
101 sscanf("12e6", "%le", &d);
102 assert(eq(DBL, d, 12e6));
104 sscanf("1.a", "%le%s", &d, buf);
105 assert(eq(DBL, d, 1.0));
106 assert(strcmp(buf, "a") == 0);
108 sscanf(".0p4", "%le%s", &d, buf);
109 assert(eq(DBL, d, 0.0));
110 assert(strcmp(buf, "p4") == 0);
113 assert(sscanf(".", "%le", &d) == 0);
116 sscanf("0x08", "%le", &d);
119 sscanf("0x90a.bcdefP+09a", "%le%s", &d, buf);
120 assert(d == 0x90a.bcdefp+09);
121 assert(strcmp(buf, "a") == 0);
123 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
124 sscanf("3.14159265358979323846", "%Lg", &ld);
125 assert(eq(LDBL, ld, 3.14159265358979323846L));
127 sscanf(" 0X.0123456789abcdefffp-3g", "%Le%s", &ld, buf);
128 assert(ld == 0x0.0123456789abcdefffp-3L);
129 assert(strcmp(buf, "g") == 0);
132 sscanf("0xg", "%le%s", &d, buf);
134 assert(strcmp(buf, "xg") == 0);
136 assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
138 sscanf("1.23", "%le%s", &d, buf);
140 assert(strcmp(buf, ".23") == 0);
142 sscanf("1,23", "%le", &d);
145 assert(setlocale(LC_NUMERIC, ""));
147 printf("ok 1 - scanfloat\n");
150 * Infinity and NaN tests
152 sscanf("-Inf", "%le", &d);
153 assert(d < 0.0 && isinf(d));
155 sscanf("iNfInItY and beyond", "%le%s", &d, buf);
156 assert(d > 0.0 && isinf(d));
157 assert(strcmp(buf, " and beyond"));
159 sscanf("NaN", "%le", &d);
162 sscanf("NAN(123Y", "%le%s", &d, buf);
164 assert(strcmp(buf, "(123Y") == 0);
166 sscanf("nan(f00f)plugh", "%le%s", &d, buf);
168 assert(strcmp(buf, "plugh") == 0);
170 sscanf("-nan", "%le", &d);
173 /* Only quiet NaNs should be returned. */
174 sscanf("NaN", "%e", &f);
175 sscanf("nan", "%le", &d);
176 sscanf("nan", "%Le", &ld);
177 feclearexcept(FE_ALL_EXCEPT);
181 assert(fetestexcept(FE_INVALID) == 0);
182 sscanf("nan(1234)", "%e", &f);
183 sscanf("nan(1234)", "%le", &d);
184 sscanf("nan(1234)", "%Le", &ld);
185 feclearexcept(FE_ALL_EXCEPT);
191 * POSIX says we should only generate quiet NaNs, but the gdtoa
192 * author convincingly argues that if you ask for a NaN format
193 * based on some implementation-defined string, you should get
194 * what you asked for, even if it's a signaling NaN.
196 assert(fetestexcept(FE_INVALID) == 0);
199 printf("ok 2 - scanfloat\n");
205 fesetround(FE_DOWNWARD);
207 sscanf("1.999999999999999999999999999999999", "%le", &d);
209 sscanf("0x1.ffffffffffffffp0", "%le", &d);
211 sscanf("1.999999999999999999999999999999999", "%Le", &ld);
214 sscanf("1.0571892669084007", "%le", &d);
215 assert(d == 0x1.0ea3f4af0dc59p0);
216 sscanf("-1.0571892669084007", "%le", &d);
217 assert(d == -0x1.0ea3f4af0dc5ap0);
218 sscanf("1.0571892669084010", "%le", &d);
219 assert(d == 0x1.0ea3f4af0dc5ap0);
221 sscanf("0x1.23p-5000", "%le", &d);
224 sscanf("0x1.2345678p-1050", "%le", &d);
225 assert(d == 0x1.234567p-1050);
227 fesetround(FE_UPWARD);
229 sscanf("1.0571892669084007", "%le", &d);
230 assert(d == 0x1.0ea3f4af0dc5ap0);
231 sscanf("-1.0571892669084007", "%le", &d);
232 assert(d == -0x1.0ea3f4af0dc59p0);
233 sscanf("1.0571892669084010", "%le", &d);
234 assert(d == 0x1.0ea3f4af0dc5bp0);
236 sscanf("0x1.23p-5000", "%le", &d);
237 assert(d == 0x1p-1074);
239 sscanf("0x1.2345678p-1050", "%le", &d);
240 assert(d == 0x1.234568p-1050);
242 fesetround(FE_TOWARDZERO);
244 sscanf("1.0571892669084007", "%le", &d);
245 assert(d == 0x1.0ea3f4af0dc59p0);
246 sscanf("-1.0571892669084007", "%le", &d);
247 assert(d == -0x1.0ea3f4af0dc59p0);
248 sscanf("1.0571892669084010", "%le", &d);
249 assert(d == 0x1.0ea3f4af0dc5ap0);
251 sscanf("0x1.23p-5000", "%le", &d);
254 sscanf("0x1.2345678p-1050", "%le", &d);
255 assert(d == 0x1.234567p-1050);
257 fesetround(FE_TONEAREST);
259 /* 1.0571892669084007 is slightly closer to 0x1.0ea3f4af0dc59p0 */
260 sscanf("1.0571892669084007", "%le", &d);
261 assert(d == 0x1.0ea3f4af0dc59p0);
262 sscanf("-1.0571892669084007", "%le", &d);
263 assert(d == -0x1.0ea3f4af0dc59p0);
264 sscanf("1.0571892669084010", "%le", &d);
265 assert(d == 0x1.0ea3f4af0dc5bp0);
267 /* strtod() should round small numbers to 0. */
268 sscanf("0x1.23p-5000", "%le", &d);
271 /* Extra digits in a denormal shouldn't break anything. */
272 sscanf("0x1.2345678p-1050", "%le", &d);
273 assert(d == 0x1.234568p-1050);
275 printf("ok 3 - scanfloat\n");
281 _eq(long double epsilon, long double a, long double b)
286 if (delta < 0) /* XXX no fabsl() */
288 return (delta <= epsilon);