]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/tst-strtod.c
(_dl_close): Add cast to avoid warning about const incorrectness.
[thirdparty/glibc.git] / stdlib / tst-strtod.c
CommitLineData
fb5663ca 1/* Copyright (C) 1991, 1996, 1997, 1998 Free Software Foundation, Inc.
afd4eb37 2 This file is part of the GNU C Library.
28f540f4 3
afd4eb37
UD
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.
28f540f4 8
afd4eb37
UD
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.
28f540f4 13
afd4eb37
UD
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. */
28f540f4 18
28f540f4
RM
19#include <ctype.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <errno.h>
23#include <string.h>
24
25struct ltest
26 {
afd4eb37 27 const char *str; /* Convert this. */
28f540f4
RM
28 double expect; /* To get this. */
29 char left; /* With this left over. */
30 int err; /* And this in errno. */
31 };
afd4eb37 32static const struct ltest tests[] =
28f540f4
RM
33 {
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 },
afd4eb37 39 { "0e-19", 0, '\0', 0 },
6259ec0d 40 { "4\00012", 4.0, '\0', 0 },
fb5663ca 41 { "5.9e-76", 5.9e-76, '\0', 0 },
28f540f4
RM
42 { NULL, 0, '\0', 0 }
43 };
44
46827b5c
UD
45static void expand (char *dst, int c);
46static int long_dbl (void);
28f540f4
RM
47
48int
84384f5b 49main (int argc, char ** argv)
28f540f4 50{
936365c1 51 char buf[100];
afd4eb37 52 register const struct ltest *lt;
28f540f4
RM
53 char *ep;
54 int status = 0;
5ae9d168 55 int save_errno;
28f540f4
RM
56
57 for (lt = tests; lt->str != NULL; ++lt)
58 {
59 double d;
60
61 errno = 0;
62 d = strtod(lt->str, &ep);
5ae9d168
UD
63 save_errno = errno;
64 printf ("strtod (\"%s\") test %u",
28f540f4 65 lt->str, (unsigned int) (lt - tests));
5ae9d168
UD
66 if (d == lt->expect && *ep == lt->left && save_errno == lt->err)
67 puts ("\tOK");
28f540f4
RM
68 else
69 {
5ae9d168 70 puts ("\tBAD");
28f540f4 71 if (d != lt->expect)
5ae9d168 72 printf (" returns %.60g, expected %.60g\n", d, lt->expect);
28f540f4
RM
73 if (lt->left != *ep)
74 {
75 char exp1[5], exp2[5];
5ae9d168
UD
76 expand (exp1, *ep);
77 expand (exp2, lt->left);
78 printf (" leaves '%s', expected '%s'\n", exp1, exp2);
28f540f4 79 }
5ae9d168
UD
80 if (save_errno != lt->err)
81 printf (" errno %d (%s) instead of %d (%s)\n",
82 save_errno, strerror (save_errno),
83 lt->err, strerror (lt->err));
28f540f4
RM
84 status = 1;
85 }
86 }
87
936365c1
UD
88 sprintf (buf, "%f", strtod ("-0.0", NULL));
89 if (strcmp (buf, "-0.000000") != 0)
90 {
91 printf (" strtod (\"-0.0\', NULL) returns \"%s\"\n", buf);
92 status = 1;
93 }
94
46827b5c
UD
95 status |= long_dbl ();
96
5ae9d168 97 exit (status ? EXIT_FAILURE : EXIT_SUCCESS);
28f540f4
RM
98}
99
100static void
afd4eb37
UD
101expand (dst, c)
102 char *dst;
103 register int c;
28f540f4 104{
5ae9d168 105 if (isprint (c))
28f540f4
RM
106 {
107 dst[0] = c;
108 dst[1] = '\0';
109 }
110 else
5ae9d168 111 (void) sprintf (dst, "%#.3o", (unsigned int) c);
28f540f4 112}
46827b5c
UD
113
114static int
115long_dbl (void)
116{
2827300f
UD
117 /* Regenerate this string using
118
119 echo '(2^53-1)*2^(1024-53)' | bc | sed 's/\([^\]*\)\\*$/ "\1"/'
120
121 */
122 static const char longestdbl[] =
123 "17976931348623157081452742373170435679807056752584499659891747680315"
124 "72607800285387605895586327668781715404589535143824642343213268894641"
125 "82768467546703537516986049910576551282076245490090389328944075868508"
126 "45513394230458323690322294816580855933212334827479782620414472316873"
127 "8177180919299881250404026184124858368";
46827b5c
UD
128 double d = strtod (longestdbl, NULL);
129
130 printf ("strtod (\"%s\", NULL) = %g\n", longestdbl, d);
131
132 if (d != 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)
133 return 1;
134
135 return 0;
136}