Update.
[glibc.git] / stdlib / tst-strtod.c
blobc6a4b0914fb42fca43a4c5a5d742f1c19a7deeb3
1 /* Copyright (C) 1991, 1996, 1997, 1998, 1999 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
25 struct ltest
27 const char *str; /* Convert this. */
28 double expect; /* To get this. */
29 char left; /* With this left over. */
30 int err; /* And this in errno. */
32 static const struct ltest tests[] =
34 { "12.345", 12.345, '\0', 0 },
35 { "12.345e19", 12.345e19, '\0', 0 },
36 { "-.1e+9", -.1e+9, '\0', 0 },
37 { ".125", .125, '\0', 0 },
38 { "1e20", 1e20, '\0', 0 },
39 { "0e-19", 0, '\0', 0 },
40 { "4\00012", 4.0, '\0', 0 },
41 { "5.9e-76", 5.9e-76, '\0', 0 },
42 { "0x1.4p+3", 10.0, '\0', 0 },
43 { "0xAp0", 10.0, '\0', 0 },
44 { "0x0Ap0", 10.0, '\0', 0 },
45 { "0x0A", 10.0, '\0', 0 },
46 { "0xA0", 160.0, '\0', 0 },
47 { "0x0.A0p8", 160.0, '\0', 0 },
48 { "0x0.50p9", 160.0, '\0', 0 },
49 { "0x0.28p10", 160.0, '\0', 0 },
50 { "0x0.14p11", 160.0, '\0', 0 },
51 { "0x0.0A0p12", 160.0, '\0', 0 },
52 { "0x0.050p13", 160.0, '\0', 0 },
53 { "0x0.028p14", 160.0, '\0', 0 },
54 { "0x0.014p15", 160.0, '\0', 0 },
55 { "0x00.00A0p16", 160.0, '\0', 0 },
56 { "0x00.0050p17", 160.0, '\0', 0 },
57 { "0x00.0028p18", 160.0, '\0', 0 },
58 { "0x00.0014p19", 160.0, '\0', 0 },
59 { NULL, 0, '\0', 0 }
62 static void expand (char *dst, int c);
63 static int long_dbl (void);
65 int
66 main (int argc, char ** argv)
68 char buf[100];
69 register const struct ltest *lt;
70 char *ep;
71 int status = 0;
72 int save_errno;
74 for (lt = tests; lt->str != NULL; ++lt)
76 double d;
78 errno = 0;
79 d = strtod(lt->str, &ep);
80 save_errno = errno;
81 printf ("strtod (\"%s\") test %u",
82 lt->str, (unsigned int) (lt - tests));
83 if (d == lt->expect && *ep == lt->left && save_errno == lt->err)
84 puts ("\tOK");
85 else
87 puts ("\tBAD");
88 if (d != lt->expect)
89 printf (" returns %.60g, expected %.60g\n", d, lt->expect);
90 if (lt->left != *ep)
92 char exp1[5], exp2[5];
93 expand (exp1, *ep);
94 expand (exp2, lt->left);
95 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
97 if (save_errno != lt->err)
98 printf (" errno %d (%s) instead of %d (%s)\n",
99 save_errno, strerror (save_errno),
100 lt->err, strerror (lt->err));
101 status = 1;
105 sprintf (buf, "%f", strtod ("-0.0", NULL));
106 if (strcmp (buf, "-0.000000") != 0)
108 printf (" strtod (\"-0.0\", NULL) returns \"%s\"\n", buf);
109 status = 1;
112 status |= long_dbl ();
114 exit (status ? EXIT_FAILURE : EXIT_SUCCESS);
117 static void
118 expand (dst, c)
119 char *dst;
120 register int c;
122 if (isprint (c))
124 dst[0] = c;
125 dst[1] = '\0';
127 else
128 (void) sprintf (dst, "%#.3o", (unsigned int) c);
131 static int
132 long_dbl (void)
134 /* Regenerate this string using
136 echo '(2^53-1)*2^(1024-53)' | bc | sed 's/\([^\]*\)\\*$/ "\1"/'
139 static const char longestdbl[] =
140 "17976931348623157081452742373170435679807056752584499659891747680315"
141 "72607800285387605895586327668781715404589535143824642343213268894641"
142 "82768467546703537516986049910576551282076245490090389328944075868508"
143 "45513394230458323690322294816580855933212334827479782620414472316873"
144 "8177180919299881250404026184124858368";
145 double d = strtod (longestdbl, NULL);
147 printf ("strtod (\"%s\", NULL) = %g\n", longestdbl, d);
149 if (d != 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)
150 return 1;
152 return 0;