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"
29 static struct { int cat; int mask; const char *string; } const categories[] =
31 { LC_CTYPE, LC_CTYPE_MASK },
32 { LC_NUMERIC, LC_NUMERIC_MASK },
33 { LC_TIME, LC_TIME_MASK },
34 { LC_COLLATE, LC_COLLATE_MASK },
35 { LC_MONETARY, LC_MONETARY_MASK },
36 { LC_MESSAGES, LC_MESSAGES_MASK }
38 , { LC_PAPER, LC_PAPER_MASK }
41 , { LC_NAME, LC_NAME_MASK }
44 , { LC_ADDRESS, LC_ADDRESS_MASK }
47 , { LC_TELEPHONE, LC_TELEPHONE_MASK }
49 # ifdef LC_MEASUREMENT
50 , { LC_MEASUREMENT, LC_MEASUREMENT_MASK }
52 # ifdef LC_IDENTIFICATION
53 , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
57 /* Test the gl_locale_name() function. */
59 test_locale_name (void)
63 /* Check that gl_locale_name returns non-NULL. */
64 ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
66 /* Get into a defined state, */
67 setlocale (LC_ALL, "en_US.UTF-8");
69 uselocale (LC_GLOBAL_LOCALE);
72 /* Check that when all environment variables are unset,
73 gl_locale_name returns the default locale. */
75 unsetenv ("LC_CTYPE");
76 unsetenv ("LC_MESSAGES");
77 unsetenv ("LC_NUMERIC");
79 setlocale (LC_ALL, "");
80 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
81 gl_locale_name_default ()) == 0);
82 ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
83 gl_locale_name_default ()) == 0);
85 /* Check that an empty environment variable is treated like an unset
86 environment variable. */
88 setenv ("LC_ALL", "", 1);
89 unsetenv ("LC_CTYPE");
90 unsetenv ("LC_MESSAGES");
92 setlocale (LC_ALL, "");
93 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
94 gl_locale_name_default ()) == 0);
97 setenv ("LC_CTYPE", "", 1);
98 unsetenv ("LC_MESSAGES");
100 setlocale (LC_ALL, "");
101 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
102 gl_locale_name_default ()) == 0);
105 unsetenv ("LC_CTYPE");
106 setenv ("LC_MESSAGES", "", 1);
108 setlocale (LC_ALL, "");
109 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
110 gl_locale_name_default ()) == 0);
113 unsetenv ("LC_CTYPE");
114 unsetenv ("LC_MESSAGES");
115 setenv ("LANG", "", 1);
116 setlocale (LC_ALL, "");
117 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
118 gl_locale_name_default ()) == 0);
120 /* Check that LC_ALL overrides the others, and LANG is overridden by the
123 setenv ("LC_ALL", "C", 1);
124 unsetenv ("LC_CTYPE");
125 unsetenv ("LC_MESSAGES");
127 setlocale (LC_ALL, "");
128 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
131 setenv ("LC_CTYPE", "C", 1);
132 setenv ("LC_MESSAGES", "C", 1);
134 setlocale (LC_ALL, "");
135 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
138 unsetenv ("LC_CTYPE");
139 unsetenv ("LC_MESSAGES");
140 setenv ("LANG", "C", 1);
141 setlocale (LC_ALL, "");
142 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
144 /* Check mixed situations. */
147 unsetenv ("LC_CTYPE");
148 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
149 setenv ("LANG", "de_DE.UTF-8", 1);
150 if (setlocale (LC_ALL, "") != NULL)
152 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
153 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
154 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
155 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
159 unsetenv ("LC_CTYPE");
160 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
162 if (setlocale (LC_ALL, "") != NULL)
164 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
165 ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
166 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
167 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
171 /* Check that gl_locale_name considers the thread locale. */
173 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
177 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
178 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
179 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
180 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
184 /* Check that gl_locale_name distinguishes different categories of the
185 thread locale, and that the name is the right one for each. */
189 for (i = 0; i < SIZEOF (categories); i++)
191 int category_mask = categories[i].mask;
192 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
195 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
201 for (j = 0; j < SIZEOF (categories); j++)
204 gl_locale_name (categories[j].cat, categories[j].string);
206 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
208 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
217 /* Test the gl_locale_name_thread() function. */
219 test_locale_name_thread (void)
221 /* Get into a defined state, */
222 setlocale (LC_ALL, "en_US.UTF-8");
225 /* Check that gl_locale_name_thread returns NULL when no thread locale is
227 uselocale (LC_GLOBAL_LOCALE);
228 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
229 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
231 /* Check that gl_locale_name_thread considers the thread locale. */
233 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
239 name = gl_locale_name_thread (LC_CTYPE, "LC_CTYPE");
240 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
241 name = gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES");
242 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
246 /* Check that gl_locale_name_thread distinguishes different categories of the
247 thread locale, and that the name is the right one for each. */
251 for (i = 0; i < SIZEOF (categories); i++)
253 int category_mask = categories[i].mask;
254 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
257 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
263 for (j = 0; j < SIZEOF (categories); j++)
266 gl_locale_name_thread (categories[j].cat,
267 categories[j].string);
269 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
271 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
278 /* Check that gl_locale_name_thread always returns NULL. */
279 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
280 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
284 /* Test the gl_locale_name_posix() function. */
286 test_locale_name_posix (void)
290 /* Get into a defined state, */
291 setlocale (LC_ALL, "en_US.UTF-8");
293 uselocale (LC_GLOBAL_LOCALE);
296 /* Check that when all environment variables are unset,
297 gl_locale_name_posix returns either NULL or the default locale. */
299 unsetenv ("LC_CTYPE");
300 unsetenv ("LC_MESSAGES");
301 unsetenv ("LC_NUMERIC");
303 setlocale (LC_ALL, "");
304 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
305 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
306 name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
307 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
309 /* Check that an empty environment variable is treated like an unset
310 environment variable. */
312 setenv ("LC_ALL", "", 1);
313 unsetenv ("LC_CTYPE");
314 unsetenv ("LC_MESSAGES");
316 setlocale (LC_ALL, "");
317 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
318 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
321 setenv ("LC_CTYPE", "", 1);
322 unsetenv ("LC_MESSAGES");
324 setlocale (LC_ALL, "");
325 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
326 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
329 unsetenv ("LC_CTYPE");
330 setenv ("LC_MESSAGES", "", 1);
332 setlocale (LC_ALL, "");
333 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
334 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
337 unsetenv ("LC_CTYPE");
338 unsetenv ("LC_MESSAGES");
339 setenv ("LANG", "", 1);
340 setlocale (LC_ALL, "");
341 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
342 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
344 /* Check that LC_ALL overrides the others, and LANG is overridden by the
347 setenv ("LC_ALL", "C", 1);
348 unsetenv ("LC_CTYPE");
349 unsetenv ("LC_MESSAGES");
351 setlocale (LC_ALL, "");
352 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
353 ASSERT (strcmp (name, "C") == 0);
356 setenv ("LC_CTYPE", "C", 1);
357 setenv ("LC_MESSAGES", "C", 1);
359 setlocale (LC_ALL, "");
360 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
361 ASSERT (strcmp (name, "C") == 0);
364 unsetenv ("LC_CTYPE");
365 unsetenv ("LC_MESSAGES");
366 setenv ("LANG", "C", 1);
367 setlocale (LC_ALL, "");
368 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
369 ASSERT (strcmp (name, "C") == 0);
371 /* Check mixed situations. */
374 unsetenv ("LC_CTYPE");
375 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
376 setenv ("LANG", "de_DE.UTF-8", 1);
377 if (setlocale (LC_ALL, "") != NULL)
379 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
380 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
381 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
382 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
386 unsetenv ("LC_CTYPE");
387 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
389 if (setlocale (LC_ALL, "") != NULL)
391 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
392 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
393 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
394 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
398 /* Check that gl_locale_name_posix ignores the thread locale. */
400 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
404 unsetenv ("LC_CTYPE");
405 unsetenv ("LC_MESSAGES");
406 setenv ("LANG", "C", 1);
407 setlocale (LC_ALL, "");
409 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
410 ASSERT (strcmp (name, "C") == 0);
416 /* Test the gl_locale_name_environ() function. */
418 test_locale_name_environ (void)
422 /* Get into a defined state, */
423 setlocale (LC_ALL, "en_US.UTF-8");
425 uselocale (LC_GLOBAL_LOCALE);
428 /* Check that when all environment variables are unset,
429 gl_locale_name_environ returns NULL. */
431 unsetenv ("LC_CTYPE");
432 unsetenv ("LC_MESSAGES");
433 unsetenv ("LC_NUMERIC");
435 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
436 ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
438 /* Check that an empty environment variable is treated like an unset
439 environment variable. */
441 setenv ("LC_ALL", "", 1);
442 unsetenv ("LC_CTYPE");
443 unsetenv ("LC_MESSAGES");
445 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
448 setenv ("LC_CTYPE", "", 1);
449 unsetenv ("LC_MESSAGES");
451 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
454 unsetenv ("LC_CTYPE");
455 setenv ("LC_MESSAGES", "", 1);
457 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
460 unsetenv ("LC_CTYPE");
461 unsetenv ("LC_MESSAGES");
462 setenv ("LANG", "", 1);
463 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
465 /* Check that LC_ALL overrides the others, and LANG is overridden by the
468 setenv ("LC_ALL", "C", 1);
469 unsetenv ("LC_CTYPE");
470 unsetenv ("LC_MESSAGES");
472 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
473 ASSERT (strcmp (name, "C") == 0);
476 setenv ("LC_CTYPE", "C", 1);
477 setenv ("LC_MESSAGES", "C", 1);
479 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
480 ASSERT (strcmp (name, "C") == 0);
483 unsetenv ("LC_CTYPE");
484 unsetenv ("LC_MESSAGES");
485 setenv ("LANG", "C", 1);
486 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
487 ASSERT (strcmp (name, "C") == 0);
489 /* Check mixed situations. */
492 unsetenv ("LC_CTYPE");
493 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
494 setenv ("LANG", "de_DE.UTF-8", 1);
495 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
496 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
497 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
498 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
501 unsetenv ("LC_CTYPE");
502 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
504 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
505 ASSERT (name == NULL);
506 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
507 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
510 /* Check that gl_locale_name_environ ignores the thread locale. */
512 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
516 unsetenv ("LC_CTYPE");
517 unsetenv ("LC_MESSAGES");
518 setenv ("LANG", "C", 1);
519 setlocale (LC_ALL, "");
521 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
522 ASSERT (strcmp (name, "C") == 0);
528 /* Test the gl_locale_name_default() function. */
530 test_locale_name_default (void)
532 const char *name = gl_locale_name_default ();
534 ASSERT (name != NULL);
536 /* Only MacOS X and Windows have a facility for the user to set the default
538 #if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__))
539 ASSERT (strcmp (name, "C") == 0);
543 /* Check that gl_locale_name_default ignores the thread locale. */
545 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
549 ASSERT (strcmp (gl_locale_name_default (), name) == 0);
559 test_locale_name_thread ();
560 test_locale_name_posix ();
561 test_locale_name_environ ();
562 test_locale_name_default ();