Use long double not double for _Float64 with old GCC if values the same.
[glibc.git] / stdlib / tst-strfmon_l.c
blob0276d7f9579160ed13afc1ef8591491b9d8fd356
1 /* Test locale dependence of strfmon_l.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <monetary.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <locale.h>
26 static const char *const en_us_name = "en_US.ISO-8859-1";
28 /* Locale value to be used by tests. */
29 static locale_t loc;
30 static const char *loc_name;
32 /* Set the global locale to GLOBAL_NAME, and the locale referenced by
33 the loc variable above to LOCAL_NAME. */
34 static void
35 init_loc (const char *global_name, const char *local_name)
37 loc = newlocale (LC_ALL_MASK, local_name, 0);
38 if (loc == 0)
40 printf ("error: newlocale (%s): %m\n", local_name);
41 abort ();
43 loc_name = local_name;
45 if (setlocale (LC_ALL, global_name) == NULL)
47 printf ("error: setlocale (%s): %m\n", global_name);
48 abort ();
52 /* Expected strings for a positive or negative value. */
53 struct testcase
55 const char *i; /* %i */
56 const char *n; /* %n */
57 const char *i_ungrouped; /* %^i */
58 const char *n_ungrouped; /* %^n */
61 /* Collected expected strings for both positive and negative
62 values. */
63 struct testcase_pair
65 struct testcase positive; /* 1234567.89 */
66 struct testcase negative; /* -1234567.89 */
69 static bool errors;
71 /* Test one value using the locale loc. */
72 static void
73 test_one (const char *format, double value, const char *expected)
75 static char actual[64];
76 int result = strfmon_l (actual, sizeof (actual), loc, format, value);
77 if (result < 0)
79 printf ("error: locale %s, format \"%s\", value %g: strfmon_l: %m\n",
80 loc_name, format, value);
81 errors = true;
83 else if (strcmp (actual, expected) != 0)
85 printf ("error: locale %s, format \"%s\", value %g: mismatch\n",
86 loc_name, format, value);
87 printf ("error: expected: \"%s\"\n", expected);
88 printf ("error: actual: \"%s\"\n", actual);
89 errors = true;
93 static void
94 test_pair (const struct testcase_pair *pair)
96 double positive = 1234567.89;
97 test_one ("%i", positive, pair->positive.i);
98 test_one ("%n", positive, pair->positive.n);
99 test_one ("%^i", positive, pair->positive.i_ungrouped);
100 test_one ("%^n", positive, pair->positive.n_ungrouped);
101 double negative = -1234567.89;
102 test_one ("%i", negative, pair->negative.i);
103 test_one ("%n", negative, pair->negative.n);
104 test_one ("%^i", negative, pair->negative.i_ungrouped);
105 test_one ("%^n", negative, pair->negative.n_ungrouped);
108 static const struct testcase_pair en_us =
111 "USD 1,234,567.89", "$1,234,567.89",
112 "USD 1234567.89", "$1234567.89"
115 "-USD 1,234,567.89", "-$1,234,567.89",
116 "-USD 1234567.89", "-$1234567.89"
120 static void
121 test_en_us (const char *other_name)
123 init_loc (other_name, en_us_name);
124 test_pair (&en_us);
125 freelocale (loc);
128 struct locale_pair
130 const char *locale_name;
131 struct testcase_pair pair;
134 static const struct locale_pair tests[] =
137 "de_DE.UTF-8",
140 "1.234.567,89 EUR", "1.234.567,89 \u20ac",
141 "1234567,89 EUR", "1234567,89 \u20ac"
144 "-1.234.567,89 EUR", "-1.234.567,89 \u20ac",
145 "-1234567,89 EUR", "-1234567,89 \u20ac"
150 "tg_TJ.UTF-8",
153 "1\u202f234\u202f567.89 TJS", "1\u202f234\u202f567.89 \u0440\u0443\u0431",
154 "1234567.89 TJS", "1234567.89 \u0440\u0443\u0431"
157 "-1\u202f234\u202f567.89 TJS", "-1\u202f234\u202f567.89 \u0440\u0443\u0431",
158 "-1234567.89 TJS", "-1234567.89 \u0440\u0443\u0431"
163 "hr_HR.UTF-8",
166 "HRK 1\u202f234\u202f567,89", "1\u202f234\u202f567,89 kn",
167 "HRK 1234567,89", "1234567,89 kn"
170 "-HRK 1\u202f234\u202f567,89", "-1\u202f234\u202f567,89 kn",
171 "-HRK 1234567,89", "-1234567,89 kn"
176 "hi_IN.UTF-8",
179 "INR12,34,567.89", "\u20b912,34,567.89",
180 "INR1234567.89", "\u20b91234567.89"
183 "-INR12,34,567.89", "-\u20b912,34,567.89",
184 "-INR1234567.89", "-\u20b91234567.89"
189 "el_GR.UTF-8",
192 "1.234.567,89EUR", "1.234.567,89\u20ac",
193 "1234567,89EUR", "1234567,89\u20ac"
196 "-1.234.567,89EUR", "-1.234.567,89\u20ac",
197 "-1234567,89EUR", "-1234567,89\u20ac",
204 static int
205 do_test (void)
207 for (const struct locale_pair *test = tests;
208 test->locale_name != NULL; ++test)
210 init_loc (en_us_name, test->locale_name);
211 test_pair (&test->pair);
212 freelocale (loc);
213 test_en_us (test->locale_name);
216 return errors;
219 #define TEST_FUNCTION do_test ()
220 #include "../test-skeleton.c"