chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / stdlib / tst-strtod.c
1 /* Copyright (C) 1991,1996-2001,2003,2009 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <ctype.h>
20 #include <locale.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <math.h>
27 #include <gnu/option-groups.h>
28
29 struct ltest
30   {
31     const char *str;            /* Convert this.  */
32     double expect;              /* To get this.  */
33     char left;                  /* With this left over.  */
34     int err;                    /* And this in errno.  */
35   };
36 static const struct ltest tests[] =
37   {
38     { "12.345", 12.345, '\0', 0 },
39     { "12.345e19", 12.345e19, '\0', 0 },
40     { "-.1e+9", -.1e+9, '\0', 0 },
41     { ".125", .125, '\0', 0 },
42     { "1e20", 1e20, '\0', 0 },
43     { "0e-19", 0, '\0', 0 },
44     { "4\00012", 4.0, '\0', 0 },
45     { "5.9e-76", 5.9e-76, '\0', 0 },
46     { "0x1.4p+3", 10.0, '\0', 0 },
47     { "0xAp0", 10.0, '\0', 0 },
48     { "0x0Ap0", 10.0, '\0', 0 },
49     { "0x0A", 10.0, '\0', 0 },
50     { "0xA0", 160.0, '\0', 0 },
51     { "0x0.A0p8", 160.0, '\0', 0 },
52     { "0x0.50p9", 160.0, '\0', 0 },
53     { "0x0.28p10", 160.0, '\0', 0 },
54     { "0x0.14p11", 160.0, '\0', 0 },
55     { "0x0.0A0p12", 160.0, '\0', 0 },
56     { "0x0.050p13", 160.0, '\0', 0 },
57     { "0x0.028p14", 160.0, '\0', 0 },
58     { "0x0.014p15", 160.0, '\0', 0 },
59     { "0x00.00A0p16", 160.0, '\0', 0 },
60     { "0x00.0050p17", 160.0, '\0', 0 },
61     { "0x00.0028p18", 160.0, '\0', 0 },
62     { "0x00.0014p19", 160.0, '\0', 0 },
63     { "0x1p-1023",
64       1.11253692925360069154511635866620203210960799023116591527666e-308,
65       '\0', ERANGE },
66     { "0x0.8p-1022",
67       1.11253692925360069154511635866620203210960799023116591527666e-308,
68       '\0', ERANGE },
69 #if __GNUC_PREREQ(2,96)
70     /* For older GCC release HUGE_VAL is not a constant.  */
71     { "Inf", HUGE_VAL, '\0', 0 },
72     { "-Inf", -HUGE_VAL, '\0', 0 },
73     { "+InFiNiTy", HUGE_VAL, '\0', 0 },
74 #endif
75     { "0x80000Ap-23", 0x80000Ap-23, '\0', 0 },
76     { NULL, 0, '\0', 0 }
77   };
78
79 static void expand (char *dst, int c);
80 static int long_dbl (void);
81 static int locale_test (void);
82
83 int
84 main (int argc, char ** argv)
85 {
86   char buf[100];
87   register const struct ltest *lt;
88   char *ep;
89   int status = 0;
90   int save_errno;
91
92   for (lt = tests; lt->str != NULL; ++lt)
93     {
94       double d;
95
96       errno = 0;
97       d = strtod(lt->str, &ep);
98       save_errno = errno;
99       printf ("strtod (\"%s\") test %u",
100              lt->str, (unsigned int) (lt - tests));
101       if (d == lt->expect && *ep == lt->left && save_errno == lt->err)
102         puts ("\tOK");
103       else
104         {
105           puts ("\tBAD");
106           if (d != lt->expect)
107             printf ("  returns %.60g, expected %.60g\n", d, lt->expect);
108           if (lt->left != *ep)
109             {
110               char exp1[5], exp2[5];
111               expand (exp1, *ep);
112               expand (exp2, lt->left);
113               printf ("  leaves '%s', expected '%s'\n", exp1, exp2);
114             }
115           if (save_errno != lt->err)
116             printf ("  errno %d (%s)  instead of %d (%s)\n",
117                     save_errno, strerror (save_errno),
118                     lt->err, strerror (lt->err));
119           status = 1;
120         }
121     }
122
123   sprintf (buf, "%f", strtod ("-0.0", NULL));
124   if (strcmp (buf, "-0.000000") != 0)
125     {
126       printf ("  strtod (\"-0.0\", NULL) returns \"%s\"\n", buf);
127       status = 1;
128     }
129
130   const char input[] = "3752432815e-39";
131
132   float f1 = strtold (input, NULL);
133   float f2;
134   float f3 = strtof (input, NULL);
135   sscanf (input, "%g", &f2);
136
137   if (f1 != f2)
138     {
139       printf ("f1 = %a != f2 = %a\n", f1, f2);
140       status = 1;
141     }
142   if (f1 != f3)
143     {
144       printf ("f1 = %a != f3 = %a\n", f1, f3);
145       status = 1;
146     }
147   if (f2 != f3)
148     {
149       printf ("f2 = %a != f3 = %a\n", f2, f3);
150       status = 1;
151     }
152
153   const char input2[] = "+1.000000000116415321826934814453125";
154   if (strtold (input2, NULL) != +1.000000000116415321826934814453125L)
155     {
156       printf ("input2: %La != %La\n", strtold (input2, NULL),
157               +1.000000000116415321826934814453125L);
158       status = 1;
159     }
160
161   static struct { const char *str; long double l; } ltests[] =
162     {
163       { "42.0000000000000000001", 42.0000000000000000001L },
164       { "42.00000000000000000001", 42.00000000000000000001L },
165       { "42.000000000000000000001", 42.000000000000000000001L }
166     };
167   int n;
168   for (n = 0; n < sizeof (ltests) / sizeof (ltests[0]); ++n)
169     if (strtold (ltests[n].str, NULL) != ltests[n].l)
170       {
171         printf ("ltests[%d]: %La != %La\n", n,
172                 strtold (ltests[n].str, NULL), ltests[n].l);
173         status = 1;
174       }
175
176   status |= long_dbl ();
177
178 #if __OPTION_EGLIBC_LOCALE_CODE
179   status |= locale_test ();
180 #endif
181
182   return status ? EXIT_FAILURE : EXIT_SUCCESS;
183 }
184
185 static void
186 expand (dst, c)
187      char *dst;
188      register int c;
189 {
190   if (isprint (c))
191     {
192       dst[0] = c;
193       dst[1] = '\0';
194     }
195   else
196     (void) sprintf (dst, "%#.3o", (unsigned int) c);
197 }
198
199 static int
200 long_dbl (void)
201 {
202   /* Regenerate this string using
203
204      echo '(2^53-1)*2^(1024-53)' | bc | sed 's/\([^\]*\)\\*$/    "\1"/'
205
206   */
207   static const char longestdbl[] =
208     "17976931348623157081452742373170435679807056752584499659891747680315"
209     "72607800285387605895586327668781715404589535143824642343213268894641"
210     "82768467546703537516986049910576551282076245490090389328944075868508"
211     "45513394230458323690322294816580855933212334827479782620414472316873"
212     "8177180919299881250404026184124858368";
213   double d = strtod (longestdbl, NULL);
214
215   printf ("strtod (\"%s\", NULL) = %g\n", longestdbl, d);
216
217   if (d != 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)
218     return 1;
219
220   return 0;
221 }
222
223 #if __OPTION_EGLIBC_LOCALE_CODE
224 /* Perform a few tests in a locale with thousands separators.  */
225 static int
226 locale_test (void)
227 {
228   static const struct
229   {
230     const char *loc;
231     const char *str;
232     double exp;
233     ptrdiff_t nread;
234   } tests[] =
235     {
236       { "de_DE.UTF-8", "1,5", 1.5, 3 },
237       { "de_DE.UTF-8", "1.5", 1.0, 1 },
238       { "de_DE.UTF-8", "1.500", 1500.0, 5 },
239       { "de_DE.UTF-8", "36.893.488.147.419.103.232", 0x1.0p65, 26 }
240     };
241 #define ntests (sizeof (tests) / sizeof (tests[0]))
242   size_t n;
243   int result = 0;
244
245   puts ("\nLocale tests");
246
247   for (n = 0; n < ntests; ++n)
248     {
249       double d;
250       char *endp;
251
252       if (setlocale (LC_ALL, tests[n].loc) == NULL)
253         {
254           printf ("cannot set locale %s\n", tests[n].loc);
255           result = 1;
256           continue;
257         }
258
259       /* We call __strtod_interal here instead of strtod to tests the
260          handling of grouping.  */
261       d = __strtod_internal (tests[n].str, &endp, 1);
262       if (d != tests[n].exp)
263         {
264           printf ("strtod(\"%s\") returns %g and not %g\n",
265                   tests[n].str, d, tests[n].exp);
266           result = 1;
267         }
268       else if (endp - tests[n].str != tests[n].nread)
269         {
270           printf ("strtod(\"%s\") read %td bytes and not %td\n",
271                   tests[n].str, endp - tests[n].str, tests[n].nread);
272           result = 1;
273         }
274     }
275
276   if (result == 0)
277     puts ("all OK");
278
279   return result;
280 }
281 #endif /* __OPTION_EGLIBC_LOCALE_CODE */