1 /* Basic tests for __strtod_internal.
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
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
28 /* Perform a few tests in a locale with thousands separators. */
29 static int
do_test(void)30 do_test (void)
31 {
32 static const struct
33 {
34 const char *loc;
35 const char *str;
36 double exp;
37 ptrdiff_t nread;
38 } tests[] =
39 {
40 { "de_DE.UTF-8", "1,5", 1.5, 3 },
41 { "de_DE.UTF-8", "1.5", 1.0, 1 },
42 { "de_DE.UTF-8", "1.500", 1500.0, 5 },
43 { "de_DE.UTF-8", "36.893.488.147.419.103.232", 0x1.0p65, 26 }
44 };
45 #define ntests (sizeof (tests) / sizeof (tests[0]))
46 size_t n;
47 int result = 0;
48
49 puts ("\nLocale tests");
50
51 for (n = 0; n < ntests; ++n)
52 {
53 double d;
54 char *endp;
55
56 if (setlocale (LC_ALL, tests[n].loc) == NULL)
57 {
58 printf ("cannot set locale %s\n", tests[n].loc);
59 result = 1;
60 continue;
61 }
62
63 d = __strtod_internal (tests[n].str, &endp, 1);
64 if (d != tests[n].exp)
65 {
66 printf ("strtod(\"%s\") returns %g and not %g\n",
67 tests[n].str, d, tests[n].exp);
68 result = 1;
69 }
70 else if (endp - tests[n].str != tests[n].nread)
71 {
72 printf ("strtod(\"%s\") read %td bytes and not %td\n",
73 tests[n].str, endp - tests[n].str, tests[n].nread);
74 result = 1;
75 }
76 }
77
78 if (result == 0)
79 puts ("all OK");
80
81 return result ? EXIT_FAILURE : EXIT_SUCCESS;
82 }
83
84 #include <support/test-driver.c>
85