]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/regression/lib/libc/stdio/test-scanfloat.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / regression / lib / libc / stdio / test-scanfloat.c
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 __FBSDID("$FreeBSD$");
33
34 #include <assert.h>
35 #include <fenv.h>
36 #include <float.h>
37 #include <locale.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #define eq(type, a, b)  _eq(type##_EPSILON, (a), (b))
43 static int _eq(long double epsilon, long double a, long double b);
44
45 extern int __scanfdebug;
46
47 int
48 main(int argc, char *argv[])
49 {
50         char buf[128];
51         long double ld = 0.0;
52         double d = 0.0;
53         float f = 0.0;
54
55         printf("1..3\n");
56
57         buf[0] = '\0';
58         assert(setlocale(LC_NUMERIC, ""));
59
60         __scanfdebug = 1;
61
62         /*
63          * Various tests for normalized numbers
64          */
65         sscanf("3.141592", "%e", &f);
66         assert(eq(FLT, f, 3.141592));
67
68         sscanf("3.141592653589793", "%lf", &d);
69         assert(eq(DBL, d, 3.141592653589793));
70
71         sscanf("1.234568e+06", "%E", &f);
72         assert(eq(FLT, f, 1.234568e+06));
73
74         sscanf("-1.234568e6", "%lF", &d);
75         assert(eq(DBL, d, -1.234568e6));
76
77         sscanf("+1.234568e-52", "%LG", &ld);
78         assert(eq(LDBL, ld, 1.234568e-52L));
79
80         sscanf("0.1", "%la", &d);
81         assert(eq(DBL, d, 0.1));
82
83         sscanf("00.2", "%lA", &d);
84         assert(eq(DBL, d, 0.2));
85
86         sscanf("123456", "%5le%s", &d, buf);
87         assert(eq(DBL, d, 12345.));
88         assert(strcmp(buf, "6") == 0);
89
90         sscanf("1.0Q", "%*5le%s", buf);
91         assert(strcmp(buf, "Q") == 0);
92
93         sscanf("-1.23e", "%e%s", &f, buf);
94         assert(eq(FLT, f, -1.23));
95         assert(strcmp(buf, "e") == 0);
96
97         sscanf("1.25e+", "%le%s", &d, buf);
98         assert(eq(DBL, d, 1.25));
99         assert(strcmp(buf, "e+") == 0);
100
101         sscanf("1.23E4E5", "%le%s", &d, buf);
102         assert(eq(DBL, d, 1.23e4));
103         assert(strcmp(buf, "E5") == 0);
104
105         sscanf("12e6", "%le", &d);
106         assert(eq(DBL, d, 12e6));
107
108         sscanf("1.a", "%le%s", &d, buf);
109         assert(eq(DBL, d, 1.0));
110         assert(strcmp(buf, "a") == 0);
111
112         sscanf(".0p4", "%le%s", &d, buf);
113         assert(eq(DBL, d, 0.0));
114         assert(strcmp(buf, "p4") == 0);
115
116         d = 0.25;
117         assert(sscanf(".", "%le", &d) == 0);
118         assert(d == 0.25);
119
120         sscanf("0x08", "%le", &d);
121         assert(d == 0x8p0);
122
123         sscanf("0x90a.bcdefP+09a", "%le%s", &d, buf);
124         assert(d == 0x90a.bcdefp+09);
125         assert(strcmp(buf, "a") == 0);
126
127 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
128         sscanf("3.14159265358979323846", "%Lg", &ld);
129         assert(eq(LDBL, ld, 3.14159265358979323846L));
130
131         sscanf("  0X.0123456789abcdefffp-3g", "%Le%s", &ld, buf);
132         assert(ld == 0x0.0123456789abcdefffp-3L);
133         assert(strcmp(buf, "g") == 0);
134 #endif
135
136         sscanf("0xg", "%le%s", &d, buf);
137         assert(d == 0.0);
138         assert(strcmp(buf, "xg") == 0);
139
140         assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */
141
142         sscanf("1.23", "%le%s", &d, buf);
143         assert(d == 1.0);
144         assert(strcmp(buf, ".23") == 0);
145
146         sscanf("1,23", "%le", &d);
147         assert(d == 1.23);
148
149         assert(setlocale(LC_NUMERIC, ""));
150
151         printf("ok 1 - scanfloat\n");
152
153         /*
154          * Infinity and NaN tests
155          */
156         sscanf("-Inf", "%le", &d);
157         assert(d < 0.0 && isinf(d));
158
159         sscanf("iNfInItY and beyond", "%le%s", &d, buf);
160         assert(d > 0.0 && isinf(d));
161         assert(strcmp(buf, " and beyond"));
162
163         sscanf("NaN", "%le", &d);
164         assert(isnan(d));
165
166         sscanf("NAN(123Y", "%le%s", &d, buf);
167         assert(isnan(d));
168         assert(strcmp(buf, "(123Y") == 0);
169
170         sscanf("nan(f00f)plugh", "%le%s", &d, buf);
171         assert(isnan(d));
172         assert(strcmp(buf, "plugh") == 0);
173
174         sscanf("-nan", "%le", &d);
175         assert(isnan(d));
176
177         /* Only quiet NaNs should be returned. */
178         sscanf("NaN", "%e", &f);
179         sscanf("nan", "%le", &d);
180         sscanf("nan", "%Le", &ld);
181         feclearexcept(FE_ALL_EXCEPT);
182         assert(f != f);
183         assert(d != d);
184         assert(ld != ld);
185         assert(fetestexcept(FE_INVALID) == 0);
186         sscanf("nan(1234)", "%e", &f);
187         sscanf("nan(1234)", "%le", &d);
188         sscanf("nan(1234)", "%Le", &ld);
189         feclearexcept(FE_ALL_EXCEPT);
190         assert(f != f);
191         assert(d != d);
192         assert(ld != ld);
193 #if 0
194         /*
195          * POSIX says we should only generate quiet NaNs, but the gdtoa
196          * author convincingly argues that if you ask for a NaN format
197          * based on some implementation-defined string, you should get
198          * what you asked for, even if it's a signaling NaN.
199          */
200         assert(fetestexcept(FE_INVALID) == 0);
201 #endif
202
203         printf("ok 2 - scanfloat\n");
204
205         /*
206          * Rounding tests
207          */
208
209         fesetround(FE_DOWNWARD);
210
211         sscanf("1.999999999999999999999999999999999", "%le", &d);
212         assert(d < 2.0);
213         sscanf("0x1.ffffffffffffffp0", "%le", &d);
214         assert(d < 2.0);
215         sscanf("1.999999999999999999999999999999999", "%Le", &ld);
216         assert(ld < 2.0);
217
218         sscanf("1.0571892669084007", "%le", &d);
219         assert(d == 0x1.0ea3f4af0dc59p0);
220         sscanf("-1.0571892669084007", "%le", &d);
221         assert(d == -0x1.0ea3f4af0dc5ap0);
222         sscanf("1.0571892669084010", "%le", &d);
223         assert(d == 0x1.0ea3f4af0dc5ap0);
224
225         sscanf("0x1.23p-5000", "%le", &d);
226         assert(d == 0.0);
227
228         sscanf("0x1.2345678p-1050", "%le", &d);
229         assert(d == 0x1.234567p-1050);
230
231         fesetround(FE_UPWARD);
232
233         sscanf("1.0571892669084007", "%le", &d);
234         assert(d == 0x1.0ea3f4af0dc5ap0);
235         sscanf("-1.0571892669084007", "%le", &d);
236         assert(d == -0x1.0ea3f4af0dc59p0);
237         sscanf("1.0571892669084010", "%le", &d);
238         assert(d == 0x1.0ea3f4af0dc5bp0);
239
240         sscanf("0x1.23p-5000", "%le", &d);
241         assert(d == 0x1p-1074);
242
243         sscanf("0x1.2345678p-1050", "%le", &d);
244         assert(d == 0x1.234568p-1050);
245
246         fesetround(FE_TOWARDZERO);
247
248         sscanf("1.0571892669084007", "%le", &d);
249         assert(d == 0x1.0ea3f4af0dc59p0);
250         sscanf("-1.0571892669084007", "%le", &d);
251         assert(d == -0x1.0ea3f4af0dc59p0);
252         sscanf("1.0571892669084010", "%le", &d);
253         assert(d == 0x1.0ea3f4af0dc5ap0);
254
255         sscanf("0x1.23p-5000", "%le", &d);
256         assert(d == 0.0);
257
258         sscanf("0x1.2345678p-1050", "%le", &d);
259         assert(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         assert(d == 0x1.0ea3f4af0dc59p0);
266         sscanf("-1.0571892669084007", "%le", &d);
267         assert(d == -0x1.0ea3f4af0dc59p0);
268         sscanf("1.0571892669084010", "%le", &d);
269         assert(d == 0x1.0ea3f4af0dc5bp0);
270
271         /* strtod() should round small numbers to 0. */
272         sscanf("0x1.23p-5000", "%le", &d);
273         assert(d == 0.0);
274
275         /* Extra digits in a denormal shouldn't break anything. */
276         sscanf("0x1.2345678p-1050", "%le", &d);
277         assert(d == 0x1.234568p-1050);
278
279         printf("ok 3 - scanfloat\n");
280
281         return (0);
282 }
283
284 static int
285 _eq(long double epsilon, long double a, long double b)
286 {
287         long double delta;
288
289         delta = a - b;
290         if (delta < 0)          /* XXX no fabsl() */
291                 delta = -delta;
292         return (delta <= epsilon);
293 }