Mercurial > gnulib
annotate 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 |
rev | line source |
---|---|
40172
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Test of strtold() in a French locale. |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
2 Copyright (C) 2019 Free Software Foundation, Inc. |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 (at your option) any later version. |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 #include <config.h> |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 #include <stdlib.h> |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include <errno.h> |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 #include <locale.h> |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 #include "macros.h" |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 int |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 main (int argc, char *argv[]) |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 /* Try to set the locale by implicitly looking at the LC_ALL environment |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 variable. |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 configure should already have checked that the locale is supported. */ |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 if (setlocale (LC_ALL, "") == NULL) |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 return 1; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 const char input[] = "1,"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 ASSERT (result == 1.0L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 ASSERT (ptr == input + 2); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 const char input[] = ",5"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 ASSERT (result == 0.5L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 ASSERT (ptr == input + 2); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 const char input[] = "1,5"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 ASSERT (result == 1.5L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 ASSERT (ptr == input + 3); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 const char input[] = "1.5"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 ASSERT (result == 1.0L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 ASSERT (ptr == input + 1); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 const char input[] = "123.456,789"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
81 ASSERT (result == 123.0L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
82 ASSERT (ptr == input + 3); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
84 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 { |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 const char input[] = "123,456.789"; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
87 char *ptr; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
88 long double result; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 errno = 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 result = strtold (input, &ptr); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 ASSERT (result > 123.45L && result < 123.46L); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 ASSERT (ptr == input + 7); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 ASSERT (errno == 0); |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 } |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
95 |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 return 0; |
31ab89a208b9
strtod, strtold: Use the locale's decimal point.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 } |