2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
17 * This program tests that newlocale and uselocale work properly in
18 * multi-threaded programs. In order for it to work, it requires that
19 * some additional locales be installed.
32 #include "test_common.h"
35 * Note that on some platforms, different symbols are used. For example,
36 * MacOS Mavericks uses "Eu" for Euro symbol, instead of €. If the locale
37 * data changes, then this program will need to update to reflect that.
39 * Note also that this file is easiest edited with a UTF-8 capable editor,
40 * as there are embedded UTF-8 symbols in some of the strings.
42 struct langinfo_test
{
47 struct langinfo_test C_data
[] = {
49 { D_T_FMT
, "%a %b %e %H:%M:%S %Y" },
50 { D_FMT
, "%m/%d/%y" },
51 { T_FMT
, "%H:%M:%S" },
52 { T_FMT_AMPM
, "%I:%M:%S %p" },
60 { DAY_7
, "Saturday" },
64 { MON_12
, "December" },
77 struct langinfo_test en_us_utf8_data
[] = {
79 { D_T_FMT
, "%B %e, %Y at %I:%M:%S %p %Z" },
80 { D_FMT
, "%m/%e/%y" },
81 { T_FMT
, "%I:%M:%S %p" },
82 { T_FMT_AMPM
, "%I:%M:%S %p" },
90 { DAY_7
, "Saturday" },
94 { MON_12
, "December" },
101 { YESEXPR
, "^(([yY]([eE][sS])?)|([yY]))" },
102 { NOEXPR
, "^(([nN]([oO])?)|([nN]))" },
107 struct langinfo_test en_gb_latin15_data
[] = {
108 { CODESET
, "ISO8859-15" },
109 { D_T_FMT
, "%e %B %Y at %H:%M:%S %Z" },
110 { D_FMT
, "%d/%m/%Y" },
111 { T_FMT
, "%H:%M:%S" },
112 { T_FMT_AMPM
, "%I:%M:%S %p" },
120 { DAY_7
, "Saturday" },
123 { MON_1
, "January" },
124 { MON_12
, "December" },
131 { YESEXPR
, "^(([yY]([eE][sS])?)|([yY]))" },
132 { NOEXPR
, "^(([nN]([oO])?)|([nN]))" },
133 { CRNCYSTR
, "-\243" },
137 struct langinfo_test ru_ru_utf8_data
[] = {
138 { CODESET
, "UTF-8" },
139 { D_T_FMT
, "%e %B %Y г., %H:%M:%S %Z"},
140 { D_FMT
, "%d.%m.%Y" },
141 { T_FMT
, "%H:%M:%S" },
142 { T_FMT_AMPM
, "%I:%M:%S %p" },
149 { DAY_1
, "воскресенье" },
150 { DAY_7
, "суббота" },
154 { MON_12
, "декабря" },
156 { ABMON_12
, "дек." },
161 { YESEXPR
, "^(([дД]([аА])?)|([дД])|([yY]([eE][sS])?)|([yY]))" },
162 { NOEXPR
, "^(([нН]([еЕ][тТ])?)|([нН])|([nN]([oO])?)|([nN]))" },
169 struct langinfo_test
*loctest
;
172 { "en_US.UTF-8", en_us_utf8_data
},
173 { "en_GB.ISO8859-15", en_gb_latin15_data
},
174 { "ru_RU.UTF-8", ru_ru_utf8_data
},
179 test_nl_langinfo_1(const char *locale
, struct langinfo_test
*test
)
185 (void) snprintf(tname
, sizeof (tname
), "nl_langinfo (locale %s)",
187 t
= test_start(tname
);
189 v
= setlocale(LC_ALL
, locale
);
191 test_failed(t
, "setlocale failed: %s", strerror(errno
));
193 if (strcmp(v
, locale
) != 0) {
194 test_failed(t
, "setlocale got %s instead of %s", v
, locale
);
197 for (int i
= 0; test
[i
].value
!= NULL
; i
++) {
198 v
= nl_langinfo(test
[i
].param
);
199 test_debugf(t
, "%d: expect [%s], got [%s]",
200 test
[i
].param
, test
[i
].value
, v
);
201 if (strcmp(v
, test
[i
].value
) != 0) {
203 "param %d wrong, expected [%s], got [%s]",
204 test
[i
].param
, test
[i
].value
, v
);
211 test_nl_langinfo_l(const char *locale
, struct langinfo_test
*test
)
218 (void) snprintf(tname
, sizeof (tname
), "nl_langinfo_l (locale %s)",
220 t
= test_start(tname
);
222 v
= setlocale(LC_ALL
, "C");
224 test_failed(t
, "setlocale failed: %s", strerror(errno
));
226 if (strcmp(v
, "C") != 0) {
227 test_failed(t
, "setlocale got %s instead of %s", v
, "C");
230 loc
= newlocale(LC_ALL_MASK
, locale
, NULL
);
232 test_failed(t
, "newlocale failed: %s", strerror(errno
));
235 for (int i
= 0; test
[i
].value
!= NULL
; i
++) {
236 v
= nl_langinfo_l(test
[i
].param
, loc
);
237 test_debugf(t
, "%d: expect [%s], got [%s]",
238 test
[i
].param
, test
[i
].value
, v
);
239 if (strcmp(v
, test
[i
].value
) != 0) {
241 "param %d wrong, expected [%s], got [%s]",
242 test
[i
].param
, test
[i
].value
, v
);
248 test_nl_langinfo(void)
250 for (int i
= 0; locales
[i
].locale
!= NULL
; i
++) {
251 test_nl_langinfo_1(locales
[i
].locale
, locales
[i
].loctest
);
252 test_nl_langinfo_l(locales
[i
].locale
, locales
[i
].loctest
);
257 main(int argc
, char **argv
)
261 while ((optc
= getopt(argc
, argv
, "df")) != EOF
) {
270 (void) fprintf(stderr
, "Usage: %s [-df]\n", argv
[0]);