]>
Commit | Line | Data |
---|---|---|
985fc132 | 1 | /* Test locale dependence of strfmon_l. |
bfff8b1b | 2 | Copyright (C) 2016-2017 Free Software Foundation, Inc. |
985fc132 FW |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <stdbool.h> | |
20 | #include <stdio.h> | |
21 | #include <monetary.h> | |
22 | #include <string.h> | |
23 | #include <stdlib.h> | |
24 | #include <locale.h> | |
25 | ||
26 | static const char *const en_us_name = "en_US.ISO-8859-1"; | |
27 | ||
28 | /* Locale value to be used by tests. */ | |
29 | static locale_t loc; | |
30 | static const char *loc_name; | |
31 | ||
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) | |
36 | { | |
37 | loc = newlocale (LC_ALL_MASK, local_name, 0); | |
38 | if (loc == 0) | |
39 | { | |
40 | printf ("error: newlocale (%s): %m\n", local_name); | |
41 | abort (); | |
42 | } | |
43 | loc_name = local_name; | |
44 | ||
45 | if (setlocale (LC_ALL, global_name) == NULL) | |
46 | { | |
47 | printf ("error: setlocale (%s): %m\n", global_name); | |
48 | abort (); | |
49 | } | |
50 | } | |
51 | ||
52 | /* Expected strings for a positive or negative value. */ | |
53 | struct testcase | |
54 | { | |
55 | const char *i; /* %i */ | |
56 | const char *n; /* %n */ | |
57 | const char *i_ungrouped; /* %^i */ | |
58 | const char *n_ungrouped; /* %^n */ | |
59 | }; | |
60 | ||
61 | /* Collected expected strings for both positive and negative | |
62 | values. */ | |
63 | struct testcase_pair | |
64 | { | |
65 | struct testcase positive; /* 1234567.89 */ | |
66 | struct testcase negative; /* -1234567.89 */ | |
67 | }; | |
68 | ||
69 | static bool errors; | |
70 | ||
71 | /* Test one value using the locale loc. */ | |
72 | static void | |
73 | test_one (const char *format, double value, const char *expected) | |
74 | { | |
75 | static char actual[64]; | |
76 | int result = strfmon_l (actual, sizeof (actual), loc, format, value); | |
77 | if (result < 0) | |
78 | { | |
79 | printf ("error: locale %s, format \"%s\", value %g: strfmon_l: %m\n", | |
80 | loc_name, format, value); | |
81 | errors = true; | |
82 | } | |
83 | else if (strcmp (actual, expected) != 0) | |
84 | { | |
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; | |
90 | } | |
91 | } | |
92 | ||
93 | static void | |
94 | test_pair (const struct testcase_pair *pair) | |
95 | { | |
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); | |
106 | } | |
107 | ||
108 | static const struct testcase_pair en_us = | |
109 | { | |
110 | { | |
111 | "USD 1,234,567.89", "$1,234,567.89", | |
112 | "USD 1234567.89", "$1234567.89" | |
113 | }, | |
114 | { | |
115 | "-USD 1,234,567.89", "-$1,234,567.89", | |
116 | "-USD 1234567.89", "-$1234567.89" | |
117 | } | |
118 | }; | |
119 | ||
120 | static void | |
121 | test_en_us (const char *other_name) | |
122 | { | |
123 | init_loc (other_name, en_us_name); | |
124 | test_pair (&en_us); | |
125 | freelocale (loc); | |
126 | } | |
127 | ||
128 | struct locale_pair | |
129 | { | |
130 | const char *locale_name; | |
131 | struct testcase_pair pair; | |
132 | }; | |
133 | ||
134 | static const struct locale_pair tests[] = | |
135 | { | |
136 | { | |
137 | "de_DE.UTF-8", | |
138 | { | |
139 | { | |
140 | "1.234.567,89 EUR", "1.234.567,89 \u20ac", | |
141 | "1234567,89 EUR", "1234567,89 \u20ac" | |
142 | }, | |
143 | { | |
144 | "-1.234.567,89 EUR", "-1.234.567,89 \u20ac", | |
145 | "-1234567,89 EUR", "-1234567,89 \u20ac" | |
146 | } | |
147 | }, | |
148 | }, | |
149 | { | |
150 | "tg_TJ.UTF-8", | |
151 | { | |
152 | { | |
153 | "1 234 567.89 TJS", "1 234 567.89 \u0440\u0443\u0431", | |
154 | "1234567.89 TJS", "1234567.89 \u0440\u0443\u0431" | |
155 | }, | |
156 | { | |
157 | "-1 234 567.89 TJS", "-1 234 567.89 \u0440\u0443\u0431", | |
158 | "-1234567.89 TJS", "-1234567.89 \u0440\u0443\u0431" | |
159 | } | |
160 | } | |
161 | }, | |
162 | { | |
163 | "te_IN.UTF-8", | |
164 | { | |
165 | { | |
166 | "INR12,34,567.89", "\u20b912,34,567.89", | |
167 | "INR1234567.89", "\u20b91234567.89" | |
168 | }, | |
169 | { | |
170 | "-INR12,34,567.89", "-\u20b912,34,567.89", | |
171 | "-INR1234567.89", "-\u20b91234567.89" | |
172 | } | |
173 | } | |
174 | }, | |
175 | { | |
176 | "bn_IN.UTF-8", | |
177 | { | |
178 | { | |
179 | "INR 12,345,67.89", "\u20b9 12,345,67.89", | |
180 | "INR 1234567.89", "\u20b9 1234567.89" | |
181 | }, | |
182 | { | |
183 | "-INR 12,345,67.89", "-\u20b9 12,345,67.89", | |
184 | "-INR 1234567.89", "-\u20b9 1234567.89" | |
185 | } | |
186 | } | |
187 | }, | |
188 | { | |
189 | "el_GR.UTF-8", | |
190 | { | |
191 | { | |
192 | "1.234.567,89EUR", "1.234.567,89\u20ac", | |
193 | "1234567,89EUR", "1234567,89\u20ac" | |
194 | }, | |
195 | { | |
196 | "-EUR1.234.567,89", "-\u20ac1.234.567,89", | |
197 | "-EUR1234567,89", "-\u20ac1234567,89", | |
198 | } | |
199 | } | |
200 | }, | |
201 | {} | |
202 | }; | |
203 | ||
204 | static int | |
205 | do_test (void) | |
206 | { | |
207 | for (const struct locale_pair *test = tests; | |
208 | test->locale_name != NULL; ++test) | |
209 | { | |
210 | init_loc (en_us_name, test->locale_name); | |
211 | test_pair (&test->pair); | |
212 | freelocale (loc); | |
213 | test_en_us (test->locale_name); | |
214 | } | |
215 | ||
216 | return errors; | |
217 | } | |
218 | ||
219 | #define TEST_FUNCTION do_test () | |
220 | #include "../test-skeleton.c" |