1 /* Test of gl_locale_name function and its variants.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "localename.h"
28 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 static struct { int cat; int mask; const char *string; } const categories[] =
43 { LC_CTYPE, LC_CTYPE_MASK },
44 { LC_NUMERIC, LC_NUMERIC_MASK },
45 { LC_TIME, LC_TIME_MASK },
46 { LC_COLLATE, LC_COLLATE_MASK },
47 { LC_MONETARY, LC_MONETARY_MASK },
48 { LC_MESSAGES, LC_MESSAGES_MASK }
50 , { LC_PAPER, LC_PAPER_MASK }
53 , { LC_NAME, LC_NAME_MASK }
56 , { LC_ADDRESS, LC_ADDRESS_MASK }
59 , { LC_TELEPHONE, LC_TELEPHONE_MASK }
61 # ifdef LC_MEASUREMENT
62 , { LC_MEASUREMENT, LC_MEASUREMENT_MASK }
64 # ifdef LC_IDENTIFICATION
65 , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
69 /* Test the gl_locale_name() function. */
71 test_locale_name (void)
75 /* Check that gl_locale_name returns non-NULL. */
76 ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
78 /* Get into a defined state, */
79 setlocale (LC_ALL, "en_US.UTF-8");
81 uselocale (LC_GLOBAL_LOCALE);
84 /* Check that when all environment variables are unset,
85 gl_locale_name returns the default locale. */
87 unsetenv ("LC_CTYPE");
88 unsetenv ("LC_MESSAGES");
89 unsetenv ("LC_NUMERIC");
91 setlocale (LC_ALL, "");
92 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
93 gl_locale_name_default ()) == 0);
94 ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
95 gl_locale_name_default ()) == 0);
97 /* Check that an empty environment variable is treated like an unset
98 environment variable. */
100 setenv ("LC_ALL", "", 1);
101 unsetenv ("LC_CTYPE");
102 unsetenv ("LC_MESSAGES");
104 setlocale (LC_ALL, "");
105 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
106 gl_locale_name_default ()) == 0);
109 setenv ("LC_CTYPE", "", 1);
110 unsetenv ("LC_MESSAGES");
112 setlocale (LC_ALL, "");
113 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
114 gl_locale_name_default ()) == 0);
117 unsetenv ("LC_CTYPE");
118 setenv ("LC_MESSAGES", "", 1);
120 setlocale (LC_ALL, "");
121 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
122 gl_locale_name_default ()) == 0);
125 unsetenv ("LC_CTYPE");
126 unsetenv ("LC_MESSAGES");
127 setenv ("LANG", "", 1);
128 setlocale (LC_ALL, "");
129 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
130 gl_locale_name_default ()) == 0);
132 /* Check that LC_ALL overrides the others, and LANG is overridden by the
135 setenv ("LC_ALL", "C", 1);
136 unsetenv ("LC_CTYPE");
137 unsetenv ("LC_MESSAGES");
139 setlocale (LC_ALL, "");
140 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
143 setenv ("LC_CTYPE", "C", 1);
144 setenv ("LC_MESSAGES", "C", 1);
146 setlocale (LC_ALL, "");
147 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
150 unsetenv ("LC_CTYPE");
151 unsetenv ("LC_MESSAGES");
152 setenv ("LANG", "C", 1);
153 setlocale (LC_ALL, "");
154 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
156 /* Check mixed situations. */
159 unsetenv ("LC_CTYPE");
160 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
161 setenv ("LANG", "de_DE.UTF-8", 1);
162 if (setlocale (LC_ALL, "") != NULL)
164 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
165 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
166 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
167 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
171 unsetenv ("LC_CTYPE");
172 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
174 if (setlocale (LC_ALL, "") != NULL)
176 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
177 ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
178 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
179 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
183 /* Check that gl_locale_name considers the thread locale. */
185 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
189 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
190 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
191 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
192 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
196 /* Check that gl_locale_name distinguishes different categories of the
197 thread locale, and that the name is the right one for each. */
201 for (i = 0; i < SIZEOF (categories); i++)
203 int category_mask = categories[i].mask;
204 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
207 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
213 for (j = 0; j < SIZEOF (categories); j++)
216 gl_locale_name (categories[j].cat, categories[j].string);
218 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
220 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
229 /* Test the gl_locale_name_thread() function. */
231 test_locale_name_thread (void)
233 /* Get into a defined state, */
234 setlocale (LC_ALL, "en_US.UTF-8");
237 /* Check that gl_locale_name_thread returns NULL when no thread locale is
239 uselocale (LC_GLOBAL_LOCALE);
240 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
241 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
243 /* Check that gl_locale_name_thread considers the thread locale. */
245 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
251 name = gl_locale_name_thread (LC_CTYPE, "LC_CTYPE");
252 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
253 name = gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES");
254 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
258 /* Check that gl_locale_name_thread distinguishes different categories of the
259 thread locale, and that the name is the right one for each. */
263 for (i = 0; i < SIZEOF (categories); i++)
265 int category_mask = categories[i].mask;
266 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
269 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
275 for (j = 0; j < SIZEOF (categories); j++)
278 gl_locale_name_thread (categories[j].cat,
279 categories[j].string);
281 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
283 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
290 /* Check that gl_locale_name_thread always returns NULL. */
291 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
292 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
296 /* Test the gl_locale_name_posix() function. */
298 test_locale_name_posix (void)
302 /* Get into a defined state, */
303 setlocale (LC_ALL, "en_US.UTF-8");
305 uselocale (LC_GLOBAL_LOCALE);
308 /* Check that when all environment variables are unset,
309 gl_locale_name_posix returns either NULL or the default locale. */
311 unsetenv ("LC_CTYPE");
312 unsetenv ("LC_MESSAGES");
313 unsetenv ("LC_NUMERIC");
315 setlocale (LC_ALL, "");
316 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
317 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
318 name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
319 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
321 /* Check that an empty environment variable is treated like an unset
322 environment variable. */
324 setenv ("LC_ALL", "", 1);
325 unsetenv ("LC_CTYPE");
326 unsetenv ("LC_MESSAGES");
328 setlocale (LC_ALL, "");
329 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
330 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
333 setenv ("LC_CTYPE", "", 1);
334 unsetenv ("LC_MESSAGES");
336 setlocale (LC_ALL, "");
337 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
338 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
341 unsetenv ("LC_CTYPE");
342 setenv ("LC_MESSAGES", "", 1);
344 setlocale (LC_ALL, "");
345 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
346 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
349 unsetenv ("LC_CTYPE");
350 unsetenv ("LC_MESSAGES");
351 setenv ("LANG", "", 1);
352 setlocale (LC_ALL, "");
353 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
354 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
356 /* Check that LC_ALL overrides the others, and LANG is overridden by the
359 setenv ("LC_ALL", "C", 1);
360 unsetenv ("LC_CTYPE");
361 unsetenv ("LC_MESSAGES");
363 setlocale (LC_ALL, "");
364 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
365 ASSERT (strcmp (name, "C") == 0);
368 setenv ("LC_CTYPE", "C", 1);
369 setenv ("LC_MESSAGES", "C", 1);
371 setlocale (LC_ALL, "");
372 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
373 ASSERT (strcmp (name, "C") == 0);
376 unsetenv ("LC_CTYPE");
377 unsetenv ("LC_MESSAGES");
378 setenv ("LANG", "C", 1);
379 setlocale (LC_ALL, "");
380 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
381 ASSERT (strcmp (name, "C") == 0);
383 /* Check mixed situations. */
386 unsetenv ("LC_CTYPE");
387 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
388 setenv ("LANG", "de_DE.UTF-8", 1);
389 if (setlocale (LC_ALL, "") != NULL)
391 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
392 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
393 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
394 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
398 unsetenv ("LC_CTYPE");
399 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
401 if (setlocale (LC_ALL, "") != NULL)
403 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
404 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
405 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
406 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
410 /* Check that gl_locale_name_posix ignores the thread locale. */
412 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
416 unsetenv ("LC_CTYPE");
417 unsetenv ("LC_MESSAGES");
418 setenv ("LANG", "C", 1);
419 setlocale (LC_ALL, "");
421 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
422 ASSERT (strcmp (name, "C") == 0);
428 /* Test the gl_locale_name_environ() function. */
430 test_locale_name_environ (void)
434 /* Get into a defined state, */
435 setlocale (LC_ALL, "en_US.UTF-8");
437 uselocale (LC_GLOBAL_LOCALE);
440 /* Check that when all environment variables are unset,
441 gl_locale_name_environ returns NULL. */
443 unsetenv ("LC_CTYPE");
444 unsetenv ("LC_MESSAGES");
445 unsetenv ("LC_NUMERIC");
447 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
448 ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
450 /* Check that an empty environment variable is treated like an unset
451 environment variable. */
453 setenv ("LC_ALL", "", 1);
454 unsetenv ("LC_CTYPE");
455 unsetenv ("LC_MESSAGES");
457 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
460 setenv ("LC_CTYPE", "", 1);
461 unsetenv ("LC_MESSAGES");
463 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
466 unsetenv ("LC_CTYPE");
467 setenv ("LC_MESSAGES", "", 1);
469 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
472 unsetenv ("LC_CTYPE");
473 unsetenv ("LC_MESSAGES");
474 setenv ("LANG", "", 1);
475 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
477 /* Check that LC_ALL overrides the others, and LANG is overridden by the
480 setenv ("LC_ALL", "C", 1);
481 unsetenv ("LC_CTYPE");
482 unsetenv ("LC_MESSAGES");
484 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
485 ASSERT (strcmp (name, "C") == 0);
488 setenv ("LC_CTYPE", "C", 1);
489 setenv ("LC_MESSAGES", "C", 1);
491 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
492 ASSERT (strcmp (name, "C") == 0);
495 unsetenv ("LC_CTYPE");
496 unsetenv ("LC_MESSAGES");
497 setenv ("LANG", "C", 1);
498 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
499 ASSERT (strcmp (name, "C") == 0);
501 /* Check mixed situations. */
504 unsetenv ("LC_CTYPE");
505 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
506 setenv ("LANG", "de_DE.UTF-8", 1);
507 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
508 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
509 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
510 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
513 unsetenv ("LC_CTYPE");
514 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
516 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
517 ASSERT (name == NULL);
518 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
519 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
522 /* Check that gl_locale_name_environ ignores the thread locale. */
524 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
528 unsetenv ("LC_CTYPE");
529 unsetenv ("LC_MESSAGES");
530 setenv ("LANG", "C", 1);
531 setlocale (LC_ALL, "");
533 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
534 ASSERT (strcmp (name, "C") == 0);
540 /* Test the gl_locale_name_default() function. */
542 test_locale_name_default (void)
544 const char *name = gl_locale_name_default ();
546 ASSERT (name != NULL);
548 /* Only MacOS X and Windows have a facility for the user to set the default
550 #if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__))
551 ASSERT (strcmp (name, "C") == 0);
555 /* Check that gl_locale_name_default ignores the thread locale. */
557 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
561 ASSERT (strcmp (gl_locale_name_default (), name) == 0);
571 test_locale_name_thread ();
572 test_locale_name_posix ();
573 test_locale_name_environ ();
574 test_locale_name_default ();