1 | /* Test of duplicating a locale object.
|
---|
2 | Copyright (C) 2009-2023 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 | /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
|
---|
18 |
|
---|
19 | #include <locale.h>
|
---|
20 | #include <langinfo.h>
|
---|
21 | #include <monetary.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #define ASSERT(x) do { if (!(x)) abort (); } while (0)
|
---|
27 |
|
---|
28 | struct locale_dependent_values
|
---|
29 | {
|
---|
30 | char monetary[100];
|
---|
31 | char numeric[100];
|
---|
32 | char time[100];
|
---|
33 | };
|
---|
34 |
|
---|
35 | static void
|
---|
36 | get_locale_dependent_values (struct locale_dependent_values *result)
|
---|
37 | {
|
---|
38 | strfmon (result->monetary, sizeof (result->monetary),
|
---|
39 | "%n", 123.75);
|
---|
40 | /* result->monetary is usually "$123.75" */
|
---|
41 | snprintf (result->numeric, sizeof (result->numeric),
|
---|
42 | "%g", 3.5);
|
---|
43 | /* result->numeric is usually "3,5" */
|
---|
44 | strncpy (result->time, nl_langinfo (MON_1), sizeof result->time - 1);
|
---|
45 | result->time[sizeof result->time - 1] = '\0';
|
---|
46 | /* result->time is usually "janvier" */
|
---|
47 | }
|
---|
48 |
|
---|
49 | int
|
---|
50 | main (void)
|
---|
51 | {
|
---|
52 | struct locale_dependent_values expected_results;
|
---|
53 | locale_t mixed1;
|
---|
54 | locale_t mixed2;
|
---|
55 | locale_t perthread;
|
---|
56 |
|
---|
57 | /* Set up a locale which is a mix between different system locales. */
|
---|
58 | setlocale (LC_ALL, "en_US.UTF-8");
|
---|
59 | setlocale (LC_NUMERIC, "de_DE.UTF-8");
|
---|
60 | setlocale (LC_TIME, "fr_FR.UTF-8");
|
---|
61 | get_locale_dependent_values (&expected_results);
|
---|
62 |
|
---|
63 | /* Save the locale in a locale_t object. */
|
---|
64 | mixed1 = duplocale (LC_GLOBAL_LOCALE);
|
---|
65 | ASSERT (mixed1 != NULL);
|
---|
66 |
|
---|
67 | /* Use a per-thread locale. */
|
---|
68 | perthread = newlocale (LC_ALL_MASK, "es_ES.UTF-8", NULL);
|
---|
69 | if (perthread == NULL)
|
---|
70 | return 1;
|
---|
71 | uselocale (perthread);
|
---|
72 |
|
---|
73 | /* Save the locale in a locale_t object again. */
|
---|
74 | mixed2 = duplocale (LC_GLOBAL_LOCALE);
|
---|
75 | ASSERT (mixed2 != NULL);
|
---|
76 |
|
---|
77 | /* Set up a default locale. */
|
---|
78 | setlocale (LC_ALL, "C");
|
---|
79 | uselocale (LC_GLOBAL_LOCALE);
|
---|
80 | {
|
---|
81 | struct locale_dependent_values c_results;
|
---|
82 | get_locale_dependent_values (&c_results);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* Now use the saved locale mixed1 again. */
|
---|
86 | setlocale (LC_ALL, "C");
|
---|
87 | uselocale (LC_GLOBAL_LOCALE);
|
---|
88 | uselocale (mixed1);
|
---|
89 | {
|
---|
90 | struct locale_dependent_values results;
|
---|
91 | get_locale_dependent_values (&results);
|
---|
92 | printf ("monetary value:\n");
|
---|
93 | printf (" Expected: %s\n", expected_results.monetary);
|
---|
94 | printf (" Actual: %s\n", results.monetary);
|
---|
95 | printf ("numeric value:\n");
|
---|
96 | printf (" Expected: %s\n", expected_results.numeric);
|
---|
97 | printf (" Actual: %s\n", results.numeric);
|
---|
98 | printf ("time value:\n");
|
---|
99 | printf (" Expected: %s\n", expected_results.time);
|
---|
100 | printf (" Actual: %s\n", results.time);
|
---|
101 | fflush (stdout);
|
---|
102 | // The following assertions fail.
|
---|
103 | ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
|
---|
104 | ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
|
---|
105 | ASSERT (strcmp (results.time, expected_results.time) == 0);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Now use the saved locale mixed2 again. */
|
---|
109 | setlocale (LC_ALL, "C");
|
---|
110 | uselocale (LC_GLOBAL_LOCALE);
|
---|
111 | uselocale (mixed2);
|
---|
112 | {
|
---|
113 | struct locale_dependent_values results;
|
---|
114 | get_locale_dependent_values (&results);
|
---|
115 | ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
|
---|
116 | ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
|
---|
117 | ASSERT (strcmp (results.time, expected_results.time) == 0);
|
---|
118 | }
|
---|
119 |
|
---|
120 | setlocale (LC_ALL, "C");
|
---|
121 | uselocale (LC_GLOBAL_LOCALE);
|
---|
122 | freelocale (mixed1);
|
---|
123 | freelocale (mixed2);
|
---|
124 | freelocale (perthread);
|
---|
125 | return 0;
|
---|
126 | }
|
---|