memset: fix define usage for shared libs
[glibc.git] / stdlib / tst-strtod.c
blob25bee78f2e999cf88b3485d0d02d8bcf2bb400f8
1 /* Copyright (C) 1991-2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <ctype.h>
19 #include <locale.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <math.h>
27 struct ltest
29 const char *str; /* Convert this. */
30 double expect; /* To get this. */
31 char left; /* With this left over. */
32 int err; /* And this in errno. */
34 static const struct ltest tests[] =
36 { "12.345", 12.345, '\0', 0 },
37 { "12.345e19", 12.345e19, '\0', 0 },
38 { "-.1e+9", -.1e+9, '\0', 0 },
39 { ".125", .125, '\0', 0 },
40 { "1e20", 1e20, '\0', 0 },
41 { "0e-19", 0, '\0', 0 },
42 { "4\00012", 4.0, '\0', 0 },
43 { "5.9e-76", 5.9e-76, '\0', 0 },
44 { "0x1.4p+3", 10.0, '\0', 0 },
45 { "0xAp0", 10.0, '\0', 0 },
46 { "0x0Ap0", 10.0, '\0', 0 },
47 { "0x0A", 10.0, '\0', 0 },
48 { "0xA0", 160.0, '\0', 0 },
49 { "0x0.A0p8", 160.0, '\0', 0 },
50 { "0x0.50p9", 160.0, '\0', 0 },
51 { "0x0.28p10", 160.0, '\0', 0 },
52 { "0x0.14p11", 160.0, '\0', 0 },
53 { "0x0.0A0p12", 160.0, '\0', 0 },
54 { "0x0.050p13", 160.0, '\0', 0 },
55 { "0x0.028p14", 160.0, '\0', 0 },
56 { "0x0.014p15", 160.0, '\0', 0 },
57 { "0x00.00A0p16", 160.0, '\0', 0 },
58 { "0x00.0050p17", 160.0, '\0', 0 },
59 { "0x00.0028p18", 160.0, '\0', 0 },
60 { "0x00.0014p19", 160.0, '\0', 0 },
61 { "0x1p-1023",
62 1.11253692925360069154511635866620203210960799023116591527666e-308,
63 '\0', ERANGE },
64 { "0x0.8p-1022",
65 1.11253692925360069154511635866620203210960799023116591527666e-308,
66 '\0', ERANGE },
67 { "Inf", HUGE_VAL, '\0', 0 },
68 { "-Inf", -HUGE_VAL, '\0', 0 },
69 { "+InFiNiTy", HUGE_VAL, '\0', 0 },
70 { "0x80000Ap-23", 0x80000Ap-23, '\0', 0 },
71 { "1e-324", 0, '\0', ERANGE },
72 { NULL, 0, '\0', 0 }
75 static void expand (char *dst, int c);
76 static int long_dbl (void);
77 static int locale_test (void);
79 int
80 main (int argc, char ** argv)
82 char buf[100];
83 register const struct ltest *lt;
84 char *ep;
85 int status = 0;
86 int save_errno;
88 for (lt = tests; lt->str != NULL; ++lt)
90 double d;
92 errno = 0;
93 d = strtod(lt->str, &ep);
94 save_errno = errno;
95 printf ("strtod (\"%s\") test %u",
96 lt->str, (unsigned int) (lt - tests));
97 if (d == lt->expect && *ep == lt->left && save_errno == lt->err)
98 puts ("\tOK");
99 else
101 puts ("\tBAD");
102 if (d != lt->expect)
103 printf (" returns %.60g, expected %.60g\n", d, lt->expect);
104 if (lt->left != *ep)
106 char exp1[5], exp2[5];
107 expand (exp1, *ep);
108 expand (exp2, lt->left);
109 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
111 if (save_errno != lt->err)
112 printf (" errno %d (%s) instead of %d (%s)\n",
113 save_errno, strerror (save_errno),
114 lt->err, strerror (lt->err));
115 status = 1;
119 sprintf (buf, "%f", strtod ("-0.0", NULL));
120 if (strcmp (buf, "-0.000000") != 0)
122 printf (" strtod (\"-0.0\", NULL) returns \"%s\"\n", buf);
123 status = 1;
126 const char input[] = "3752432815e-39";
128 float f1 = strtold (input, NULL);
129 float f2;
130 float f3 = strtof (input, NULL);
131 sscanf (input, "%g", &f2);
133 if (f1 != f2)
135 printf ("f1 = %a != f2 = %a\n", f1, f2);
136 status = 1;
138 if (f1 != f3)
140 printf ("f1 = %a != f3 = %a\n", f1, f3);
141 status = 1;
143 if (f2 != f3)
145 printf ("f2 = %a != f3 = %a\n", f2, f3);
146 status = 1;
149 const char input2[] = "+1.000000000116415321826934814453125";
150 if (strtold (input2, NULL) != +1.000000000116415321826934814453125L)
152 printf ("input2: %La != %La\n", strtold (input2, NULL),
153 +1.000000000116415321826934814453125L);
154 status = 1;
157 static struct { const char *str; long double l; } ltests[] =
159 { "42.0000000000000000001", 42.0000000000000000001L },
160 { "42.00000000000000000001", 42.00000000000000000001L },
161 { "42.000000000000000000001", 42.000000000000000000001L }
163 int n;
164 for (n = 0; n < sizeof (ltests) / sizeof (ltests[0]); ++n)
165 if (strtold (ltests[n].str, NULL) != ltests[n].l)
167 printf ("ltests[%d]: %La != %La\n", n,
168 strtold (ltests[n].str, NULL), ltests[n].l);
169 status = 1;
172 status |= long_dbl ();
174 status |= locale_test ();
176 return status ? EXIT_FAILURE : EXIT_SUCCESS;
179 static void
180 expand (dst, c)
181 char *dst;
182 register int c;
184 if (isprint (c))
186 dst[0] = c;
187 dst[1] = '\0';
189 else
190 (void) sprintf (dst, "%#.3o", (unsigned int) c);
193 static int
194 long_dbl (void)
196 /* Regenerate this string using
198 echo '(2^53-1)*2^(1024-53)' | bc | sed 's/\([^\]*\)\\*$/ "\1"/'
201 static const char longestdbl[] =
202 "17976931348623157081452742373170435679807056752584499659891747680315"
203 "72607800285387605895586327668781715404589535143824642343213268894641"
204 "82768467546703537516986049910576551282076245490090389328944075868508"
205 "45513394230458323690322294816580855933212334827479782620414472316873"
206 "8177180919299881250404026184124858368";
207 double d = strtod (longestdbl, NULL);
209 printf ("strtod (\"%s\", NULL) = %g\n", longestdbl, d);
211 if (d != 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)
212 return 1;
214 return 0;
217 /* Perform a few tests in a locale with thousands separators. */
218 static int
219 locale_test (void)
221 static const struct
223 const char *loc;
224 const char *str;
225 double exp;
226 ptrdiff_t nread;
227 } tests[] =
229 { "de_DE.UTF-8", "1,5", 1.5, 3 },
230 { "de_DE.UTF-8", "1.5", 1.0, 1 },
231 { "de_DE.UTF-8", "1.500", 1500.0, 5 },
232 { "de_DE.UTF-8", "36.893.488.147.419.103.232", 0x1.0p65, 26 }
234 #define ntests (sizeof (tests) / sizeof (tests[0]))
235 size_t n;
236 int result = 0;
238 puts ("\nLocale tests");
240 for (n = 0; n < ntests; ++n)
242 double d;
243 char *endp;
245 if (setlocale (LC_ALL, tests[n].loc) == NULL)
247 printf ("cannot set locale %s\n", tests[n].loc);
248 result = 1;
249 continue;
252 /* We call __strtod_interal here instead of strtod to tests the
253 handling of grouping. */
254 d = __strtod_internal (tests[n].str, &endp, 1);
255 if (d != tests[n].exp)
257 printf ("strtod(\"%s\") returns %g and not %g\n",
258 tests[n].str, d, tests[n].exp);
259 result = 1;
261 else if (endp - tests[n].str != tests[n].nread)
263 printf ("strtod(\"%s\") read %td bytes and not %td\n",
264 tests[n].str, endp - tests[n].str, tests[n].nread);
265 result = 1;
269 if (result == 0)
270 puts ("all OK");
272 return result;