| 1 | /* Basic tests for __strtod_internal. |
| 2 | Copyright (C) 1991-2024 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 | #include "tst-strtod.h" |
| 29 | |
| 30 | /* This tests internal interfaces, which are only defined for types |
| 31 | with distinct ABIs, so disable testing for types without distinct |
| 32 | ABIs. */ |
| 33 | #undef IF_FLOAT32 |
| 34 | #define IF_FLOAT32(x) |
| 35 | #undef IF_FLOAT64 |
| 36 | #define IF_FLOAT64(x) |
| 37 | #undef IF_FLOAT32X |
| 38 | #define IF_FLOAT32X(x) |
| 39 | #undef IF_FLOAT64X |
| 40 | #define IF_FLOAT64X(x) |
| 41 | #if !__HAVE_DISTINCT_FLOAT128 |
| 42 | # undef IF_FLOAT128 |
| 43 | # define IF_FLOAT128(x) |
| 44 | #endif |
| 45 | |
| 46 | #define ntests (sizeof (tests) / sizeof (tests[0])) |
| 47 | |
| 48 | /* Perform a few tests in a locale with thousands separators. */ |
| 49 | #define TEST_STRTOD(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \ |
| 50 | static int \ |
| 51 | test_strto ## FSUF (void) \ |
| 52 | { \ |
| 53 | static const struct \ |
| 54 | { \ |
| 55 | const char *loc; \ |
| 56 | const char *str; \ |
| 57 | FTYPE exp; \ |
| 58 | ptrdiff_t nread; \ |
| 59 | } tests[] = \ |
| 60 | { \ |
| 61 | { "de_DE.UTF-8", "1,5", 1.5 ## LSUF, 3 }, \ |
| 62 | { "de_DE.UTF-8", "1.5", 1.0 ## LSUF, 1 }, \ |
| 63 | { "de_DE.UTF-8", "1.500", 1500.0 ## LSUF, 5 }, \ |
| 64 | { "de_DE.UTF-8", "36.893.488.147.419.103.232", 0x1.0p65 ## LSUF, 26 } \ |
| 65 | }; \ |
| 66 | size_t n; \ |
| 67 | int result = 0; \ |
| 68 | \ |
| 69 | puts ("\nLocale tests"); \ |
| 70 | \ |
| 71 | for (n = 0; n < ntests; ++n) \ |
| 72 | { \ |
| 73 | FTYPE d; \ |
| 74 | char *endp; \ |
| 75 | \ |
| 76 | if (setlocale (LC_ALL, tests[n].loc) == NULL) \ |
| 77 | { \ |
| 78 | printf ("cannot set locale %s\n", tests[n].loc); \ |
| 79 | result = 1; \ |
| 80 | continue; \ |
| 81 | } \ |
| 82 | \ |
| 83 | d = __strto ## FSUF ## _internal (tests[n].str, &endp, 1); \ |
| 84 | if (d != tests[n].exp) \ |
| 85 | { \ |
| 86 | char buf1[FSTRLENMAX], buf2[FSTRLENMAX]; \ |
| 87 | FTOSTR (buf1, sizeof (buf1), "%g", d); \ |
| 88 | FTOSTR (buf2, sizeof (buf2), "%g", tests[n].exp); \ |
| 89 | printf ("strto" # FSUF "(\"%s\") returns %s and not %s\n", \ |
| 90 | tests[n].str, buf1, buf2); \ |
| 91 | result = 1; \ |
| 92 | } \ |
| 93 | else if (endp - tests[n].str != tests[n].nread) \ |
| 94 | { \ |
| 95 | printf ("strto" # FSUF "(\"%s\") read %td bytes and not %td\n", \ |
| 96 | tests[n].str, endp - tests[n].str, tests[n].nread); \ |
| 97 | result = 1; \ |
| 98 | } \ |
| 99 | } \ |
| 100 | \ |
| 101 | if (result == 0) \ |
| 102 | puts ("all OK"); \ |
| 103 | \ |
| 104 | return result ? EXIT_FAILURE : EXIT_SUCCESS; \ |
| 105 | } |
| 106 | |
| 107 | GEN_TEST_STRTOD_FOREACH (TEST_STRTOD) |
| 108 | |
| 109 | static int |
| 110 | do_test (void) |
| 111 | { |
| 112 | return STRTOD_TEST_FOREACH (test_strto); |
| 113 | } |
| 114 | |
| 115 | #include <support/test-driver.c> |
| 116 | |