1 /* Test locale dependence of strfmon_l.
2 Copyright (C) 2016-2022 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 <https://www.gnu.org/licenses/>. */
26 static const char *const en_us_name
= "en_US.ISO-8859-1";
28 /* Locale value to be used by tests. */
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. */
35 init_loc (const char *global_name
, const char *local_name
)
37 loc
= newlocale (LC_ALL_MASK
, local_name
, 0);
40 printf ("error: newlocale (%s): %m\n", local_name
);
43 loc_name
= local_name
;
45 if (setlocale (LC_ALL
, global_name
) == NULL
)
47 printf ("error: setlocale (%s): %m\n", global_name
);
52 /* Expected strings for a positive or negative value. */
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
65 struct testcase positive
; /* 1234567.89 */
66 struct testcase negative
; /* -1234567.89 */
71 /* Test one value using the locale loc. */
73 test_one (const char *format
, double value
, const char *ldformat
,
74 long double ldvalue
, const char *expected
)
76 static char actual
[64], actualld
[64];
77 int result
= strfmon_l (actual
, sizeof (actual
), loc
, format
, value
);
78 int res_ld
= strfmon_l (actualld
, sizeof (actualld
), loc
, ldformat
, ldvalue
);
81 printf ("error: locale %s, format \"%s\", value %g: strfmon_l: %m\n",
82 loc_name
, format
, value
);
87 printf ("error: locale %s, format \"%s\", value %Lg: strfmon_l: %m\n",
88 loc_name
, ldformat
, ldvalue
);
91 else if (strcmp (actual
, expected
) != 0)
93 printf ("error: locale %s, format \"%s\", value %g: mismatch\n",
94 loc_name
, format
, value
);
95 printf ("error: expected: \"%s\"\n", expected
);
96 printf ("error: actual: \"%s\"\n", actual
);
99 else if (strcmp (actualld
, expected
) != 0)
101 printf ("error: locale %s, format \"%s\", value %Lg: mismatch\n",
102 loc_name
, ldformat
, ldvalue
);
103 printf ("error: expected: \"%s\"\n", expected
);
104 printf ("error: actual: \"%s\"\n", actualld
);
110 test_pair (const struct testcase_pair
*pair
)
112 double positive
= 1234567.89;
113 long double pos
= 1234567.89L;
114 test_one ("%i", positive
, "%Li", pos
, pair
->positive
.i
);
115 test_one ("%n", positive
, "%Ln", pos
, pair
->positive
.n
);
116 test_one ("%^i", positive
, "%^Li", pos
, pair
->positive
.i_ungrouped
);
117 test_one ("%^n", positive
, "%^Ln", pos
, pair
->positive
.n_ungrouped
);
118 double negative
= -1234567.89;
119 long double neg
= -1234567.89L;
120 test_one ("%i", negative
, "%Li", neg
, pair
->negative
.i
);
121 test_one ("%n", negative
, "%Ln", neg
, pair
->negative
.n
);
122 test_one ("%^i", negative
, "%^Li", neg
, pair
->negative
.i_ungrouped
);
123 test_one ("%^n", negative
, "%^Ln", neg
, pair
->negative
.n_ungrouped
);
126 static const struct testcase_pair en_us
=
129 "USD 1,234,567.89", "$1,234,567.89",
130 "USD 1234567.89", "$1234567.89"
133 "-USD 1,234,567.89", "-$1,234,567.89",
134 "-USD 1234567.89", "-$1234567.89"
139 test_en_us (const char *other_name
)
141 init_loc (other_name
, en_us_name
);
148 const char *locale_name
;
149 struct testcase_pair pair
;
152 static const struct locale_pair tests
[] =
158 "1.234.567,89 EUR", "1.234.567,89 \u20ac",
159 "1234567,89 EUR", "1234567,89 \u20ac"
162 "-1.234.567,89 EUR", "-1.234.567,89 \u20ac",
163 "-1234567,89 EUR", "-1234567,89 \u20ac"
171 "1\u202f234\u202f567.89 TJS", "1\u202f234\u202f567.89 \u0440\u0443\u0431",
172 "1234567.89 TJS", "1234567.89 \u0440\u0443\u0431"
175 "-1\u202f234\u202f567.89 TJS", "-1\u202f234\u202f567.89 \u0440\u0443\u0431",
176 "-1234567.89 TJS", "-1234567.89 \u0440\u0443\u0431"
184 "HRK 1.234.567,89", "1.234.567,89 kn",
185 "HRK 1234567,89", "1234567,89 kn"
188 "-HRK 1.234.567,89", "-1.234.567,89 kn",
189 "-HRK 1234567,89", "-1234567,89 kn"
197 "INR12,34,567.89", "\u20b912,34,567.89",
198 "INR1234567.89", "\u20b91234567.89"
201 "-INR12,34,567.89", "-\u20b912,34,567.89",
202 "-INR1234567.89", "-\u20b91234567.89"
210 "1.234.567,89EUR", "1.234.567,89\u20ac",
211 "1234567,89EUR", "1234567,89\u20ac"
214 "-1.234.567,89EUR", "-1.234.567,89\u20ac",
215 "-1234567,89EUR", "-1234567,89\u20ac",
225 for (const struct locale_pair
*test
= tests
;
226 test
->locale_name
!= NULL
; ++test
)
228 init_loc (en_us_name
, test
->locale_name
);
229 test_pair (&test
->pair
);
231 test_en_us (test
->locale_name
);
237 #define TEST_FUNCTION do_test ()
238 #include "../test-skeleton.c"