Mercurial > gnulib
comparison tests/test-strtold1.c @ 40172:31ab89a208b9
strtod, strtold: Use the locale's decimal point.
* lib/strtod.c: Include <locale.h>, <stdio.h>, <langinfo.h>.
(decimal_point_char): New function, copied from lib/vasnprintf.c.
(parse_number): Add a radixchar argument. Use it instead of '.'.
(STRTOD): Invoke decimal_point_char and pass the result to parse_number.
* m4/strtod.m4 (gl_PREREQ_STRTOD): Test whether nl_langinfo exists.
* m4/strtold.m4 (gl_PREREQ_STRTOLD): Likewise.
* tests/test-strtod1.c: New file.
* tests/test-strtod1.sh: New file.
* modules/strtod-tests (Files): Add test-strtod1.{sh,c}. Add
locale-fr.m4 and its dependencies.
(configure.ac): Invoke gt_LOCALE_FR, gt_LOCALE_FR_UTF8.
(Makefile.am): Arrange to compile test-strtod1.c and run
test-strtod1.sh.
* tests/test-strtold1.c: New file.
* tests/test-strtold1.sh: New file.
* modules/strtold-tests (Files): Add test-strtold1.{sh,c}. Add
locale-fr.m4 and its dependencies.
(configure.ac): Invoke gt_LOCALE_FR, gt_LOCALE_FR_UTF8.
(Makefile.am): Arrange to compile test-strtold1.c and run
test-strtold1.sh.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 01 Feb 2019 03:12:28 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
40171:fbfa1d6417d9 | 40172:31ab89a208b9 |
---|---|
1 /* Test of strtold() in a French locale. | |
2 Copyright (C) 2019 Free Software Foundation, Inc. | |
3 | |
4 This program is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 3 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program 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 | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ | |
16 | |
17 #include <config.h> | |
18 | |
19 #include <stdlib.h> | |
20 | |
21 #include <errno.h> | |
22 #include <locale.h> | |
23 | |
24 #include "macros.h" | |
25 | |
26 int | |
27 main (int argc, char *argv[]) | |
28 { | |
29 /* Try to set the locale by implicitly looking at the LC_ALL environment | |
30 variable. | |
31 configure should already have checked that the locale is supported. */ | |
32 if (setlocale (LC_ALL, "") == NULL) | |
33 return 1; | |
34 | |
35 { | |
36 const char input[] = "1,"; | |
37 char *ptr; | |
38 long double result; | |
39 errno = 0; | |
40 result = strtold (input, &ptr); | |
41 ASSERT (result == 1.0L); | |
42 ASSERT (ptr == input + 2); | |
43 ASSERT (errno == 0); | |
44 } | |
45 { | |
46 const char input[] = ",5"; | |
47 char *ptr; | |
48 long double result; | |
49 errno = 0; | |
50 result = strtold (input, &ptr); | |
51 ASSERT (result == 0.5L); | |
52 ASSERT (ptr == input + 2); | |
53 ASSERT (errno == 0); | |
54 } | |
55 { | |
56 const char input[] = "1,5"; | |
57 char *ptr; | |
58 long double result; | |
59 errno = 0; | |
60 result = strtold (input, &ptr); | |
61 ASSERT (result == 1.5L); | |
62 ASSERT (ptr == input + 3); | |
63 ASSERT (errno == 0); | |
64 } | |
65 { | |
66 const char input[] = "1.5"; | |
67 char *ptr; | |
68 long double result; | |
69 errno = 0; | |
70 result = strtold (input, &ptr); | |
71 ASSERT (result == 1.0L); | |
72 ASSERT (ptr == input + 1); | |
73 ASSERT (errno == 0); | |
74 } | |
75 { | |
76 const char input[] = "123.456,789"; | |
77 char *ptr; | |
78 long double result; | |
79 errno = 0; | |
80 result = strtold (input, &ptr); | |
81 ASSERT (result == 123.0L); | |
82 ASSERT (ptr == input + 3); | |
83 ASSERT (errno == 0); | |
84 } | |
85 { | |
86 const char input[] = "123,456.789"; | |
87 char *ptr; | |
88 long double result; | |
89 errno = 0; | |
90 result = strtold (input, &ptr); | |
91 ASSERT (result > 123.45L && result < 123.46L); | |
92 ASSERT (ptr == input + 7); | |
93 ASSERT (errno == 0); | |
94 } | |
95 | |
96 return 0; | |
97 } |