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"
32 static struct { int cat; int mask; const char *string; } const categories[] =
34 { LC_CTYPE, LC_CTYPE_MASK, "LC_CTYPE" },
35 { LC_NUMERIC, LC_NUMERIC_MASK, "LC_NUMERIC" },
36 { LC_TIME, LC_TIME_MASK, "LC_TIME" },
37 { LC_COLLATE, LC_COLLATE_MASK, "LC_COLLATE" },
38 { LC_MONETARY, LC_MONETARY_MASK, "LC_MONETARY" },
39 { LC_MESSAGES, LC_MESSAGES_MASK, "LC_MESSAGES" }
41 , { LC_PAPER, LC_PAPER_MASK, "LC_PAPER" }
44 , { LC_NAME, LC_NAME_MASK, "LC_NAME" }
47 , { LC_ADDRESS, LC_ADDRESS_MASK, "LC_ADDRESS" }
50 , { LC_TELEPHONE, LC_TELEPHONE_MASK, "LC_TELEPHONE" }
52 # ifdef LC_MEASUREMENT
53 , { LC_MEASUREMENT, LC_MEASUREMENT_MASK, "LC_MEASUREMENT" }
55 # ifdef LC_IDENTIFICATION
56 , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK, "LC_IDENTIFICATION" }
62 /* Test the gl_locale_name() function. */
64 test_locale_name (void)
68 /* Check that gl_locale_name returns non-NULL. */
69 ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
71 /* Get into a defined state, */
72 setlocale (LC_ALL, "en_US.UTF-8");
74 uselocale (LC_GLOBAL_LOCALE);
77 /* Check that when all environment variables are unset,
78 gl_locale_name returns the default locale. */
80 unsetenv ("LC_CTYPE");
81 unsetenv ("LC_MESSAGES");
82 unsetenv ("LC_NUMERIC");
84 setlocale (LC_ALL, "");
85 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
86 gl_locale_name_default ()) == 0);
87 ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
88 gl_locale_name_default ()) == 0);
90 /* Check that an empty environment variable is treated like an unset
91 environment variable. */
93 setenv ("LC_ALL", "", 1);
94 unsetenv ("LC_CTYPE");
95 unsetenv ("LC_MESSAGES");
97 setlocale (LC_ALL, "");
98 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
99 gl_locale_name_default ()) == 0);
102 setenv ("LC_CTYPE", "", 1);
103 unsetenv ("LC_MESSAGES");
105 setlocale (LC_ALL, "");
106 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
107 gl_locale_name_default ()) == 0);
110 unsetenv ("LC_CTYPE");
111 setenv ("LC_MESSAGES", "", 1);
113 setlocale (LC_ALL, "");
114 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
115 gl_locale_name_default ()) == 0);
118 unsetenv ("LC_CTYPE");
119 unsetenv ("LC_MESSAGES");
120 setenv ("LANG", "", 1);
121 setlocale (LC_ALL, "");
122 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
123 gl_locale_name_default ()) == 0);
125 /* Check that LC_ALL overrides the others, and LANG is overridden by the
128 setenv ("LC_ALL", "C", 1);
129 unsetenv ("LC_CTYPE");
130 unsetenv ("LC_MESSAGES");
132 setlocale (LC_ALL, "");
133 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
136 setenv ("LC_CTYPE", "C", 1);
137 setenv ("LC_MESSAGES", "C", 1);
139 setlocale (LC_ALL, "");
140 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
143 unsetenv ("LC_CTYPE");
144 unsetenv ("LC_MESSAGES");
145 setenv ("LANG", "C", 1);
146 setlocale (LC_ALL, "");
147 ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
149 /* Check mixed situations. */
152 unsetenv ("LC_CTYPE");
153 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
154 setenv ("LANG", "de_DE.UTF-8", 1);
155 if (setlocale (LC_ALL, "") != NULL)
157 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
158 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
159 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
160 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
164 unsetenv ("LC_CTYPE");
165 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
167 if (setlocale (LC_ALL, "") != NULL)
169 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
170 ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
171 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
172 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
176 /* Check that gl_locale_name considers the thread locale. */
178 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
182 name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
183 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
184 name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
185 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
189 /* Check that gl_locale_name distinguishes different categories of the
190 thread locale, and that the name is the right one for each. */
194 for (i = 0; i < SIZEOF (categories); i++)
196 int category_mask = categories[i].mask;
197 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
200 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
206 for (j = 0; j < SIZEOF (categories); j++)
209 gl_locale_name (categories[j].cat, categories[j].string);
211 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
213 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
222 /* Test the gl_locale_name_thread() function. */
224 test_locale_name_thread (void)
226 /* Get into a defined state, */
227 setlocale (LC_ALL, "en_US.UTF-8");
230 /* Check that gl_locale_name_thread returns NULL when no thread locale is
232 uselocale (LC_GLOBAL_LOCALE);
233 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
234 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
236 /* Check that gl_locale_name_thread considers the thread locale. */
238 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
244 name = gl_locale_name_thread (LC_CTYPE, "LC_CTYPE");
245 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
246 name = gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES");
247 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
251 /* Check that gl_locale_name_thread distinguishes different categories of the
252 thread locale, and that the name is the right one for each. */
256 for (i = 0; i < SIZEOF (categories); i++)
258 int category_mask = categories[i].mask;
259 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
262 locale = newlocale (category_mask, "de_DE.UTF-8", locale);
268 for (j = 0; j < SIZEOF (categories); j++)
271 gl_locale_name_thread (categories[j].cat,
272 categories[j].string);
274 ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
276 ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
283 /* Check that gl_locale_name_thread returns a string that is allocated with
284 indefinite extent. */
286 /* Try many locale names in turn, in order to defeat possible caches. */
287 static const char * const choices[] =
393 /* Remember which locales are available. */
394 unsigned char /* bool */ available[SIZEOF (choices)];
395 /* Array of remembered results of gl_locale_name_thread. */
396 const char *unsaved_names[SIZEOF (choices)][SIZEOF (categories)];
397 /* Array of remembered results of gl_locale_name_thread, stored in safe
399 char *saved_names[SIZEOF (choices)][SIZEOF (categories)];
402 for (j = 0; j < SIZEOF (choices); j++)
404 locale_t locale = newlocale (LC_ALL_MASK, choices[j], NULL);
405 available[j] = (locale != NULL);
411 for (i = 0; i < SIZEOF (categories); i++)
413 unsaved_names[j][i] = gl_locale_name_thread (categories[i].cat, categories[i].string);
414 saved_names[j][i] = strdup (unsaved_names[j][i]);
416 uselocale (LC_GLOBAL_LOCALE);
420 /* Verify the unsaved_names are still valid. */
421 for (j = 0; j < SIZEOF (choices); j++)
425 for (i = 0; i < SIZEOF (categories); i++)
426 ASSERT (strcmp (unsaved_names[j][i], saved_names[j][i]) == 0);
428 /* Allocate many locales, without freeing them. This is an attempt at
429 overwriting as much of the previously allocated memory as possible. */
430 for (j = SIZEOF (choices); j > 0; )
435 locale_t locale = newlocale (LC_ALL_MASK, choices[j], NULL);
438 ASSERT (locale != NULL);
440 for (i = 0; i < SIZEOF (categories); i++)
442 const char *name = gl_locale_name_thread (categories[i].cat, categories[i].string);
443 ASSERT (strcmp (unsaved_names[j][i], name) == 0);
445 uselocale (LC_GLOBAL_LOCALE);
448 /* Verify the unsaved_names are still valid. */
449 for (j = 0; j < SIZEOF (choices); j++)
453 for (i = 0; i < SIZEOF (categories); i++)
454 ASSERT (strcmp (unsaved_names[j][i], saved_names[j][i]) == 0);
458 /* Check that gl_locale_name_thread always returns NULL. */
459 ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
460 ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
464 /* Test the gl_locale_name_posix() function. */
466 test_locale_name_posix (void)
470 /* Get into a defined state, */
471 setlocale (LC_ALL, "en_US.UTF-8");
473 uselocale (LC_GLOBAL_LOCALE);
476 /* Check that when all environment variables are unset,
477 gl_locale_name_posix returns either NULL or the default locale. */
479 unsetenv ("LC_CTYPE");
480 unsetenv ("LC_MESSAGES");
481 unsetenv ("LC_NUMERIC");
483 setlocale (LC_ALL, "");
484 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
485 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
486 name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
487 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
489 /* Check that an empty environment variable is treated like an unset
490 environment variable. */
492 setenv ("LC_ALL", "", 1);
493 unsetenv ("LC_CTYPE");
494 unsetenv ("LC_MESSAGES");
496 setlocale (LC_ALL, "");
497 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
498 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
501 setenv ("LC_CTYPE", "", 1);
502 unsetenv ("LC_MESSAGES");
504 setlocale (LC_ALL, "");
505 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
506 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
509 unsetenv ("LC_CTYPE");
510 setenv ("LC_MESSAGES", "", 1);
512 setlocale (LC_ALL, "");
513 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
514 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
517 unsetenv ("LC_CTYPE");
518 unsetenv ("LC_MESSAGES");
519 setenv ("LANG", "", 1);
520 setlocale (LC_ALL, "");
521 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
522 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
524 /* Check that LC_ALL overrides the others, and LANG is overridden by the
527 setenv ("LC_ALL", "C", 1);
528 unsetenv ("LC_CTYPE");
529 unsetenv ("LC_MESSAGES");
531 setlocale (LC_ALL, "");
532 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
533 ASSERT (strcmp (name, "C") == 0);
536 setenv ("LC_CTYPE", "C", 1);
537 setenv ("LC_MESSAGES", "C", 1);
539 setlocale (LC_ALL, "");
540 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
541 ASSERT (strcmp (name, "C") == 0);
544 unsetenv ("LC_CTYPE");
545 unsetenv ("LC_MESSAGES");
546 setenv ("LANG", "C", 1);
547 setlocale (LC_ALL, "");
548 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
549 ASSERT (strcmp (name, "C") == 0);
551 /* Check mixed situations. */
554 unsetenv ("LC_CTYPE");
555 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
556 setenv ("LANG", "de_DE.UTF-8", 1);
557 if (setlocale (LC_ALL, "") != NULL)
559 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
560 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
561 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
562 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
566 unsetenv ("LC_CTYPE");
567 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
569 if (setlocale (LC_ALL, "") != NULL)
571 name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
572 ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
573 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
574 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
578 /* Check that gl_locale_name_posix ignores the thread locale. */
580 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
584 unsetenv ("LC_CTYPE");
585 unsetenv ("LC_MESSAGES");
586 setenv ("LANG", "C", 1);
587 setlocale (LC_ALL, "");
589 name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
590 ASSERT (strcmp (name, "C") == 0);
596 /* Test the gl_locale_name_environ() function. */
598 test_locale_name_environ (void)
602 /* Get into a defined state, */
603 setlocale (LC_ALL, "en_US.UTF-8");
605 uselocale (LC_GLOBAL_LOCALE);
608 /* Check that when all environment variables are unset,
609 gl_locale_name_environ returns NULL. */
611 unsetenv ("LC_CTYPE");
612 unsetenv ("LC_MESSAGES");
613 unsetenv ("LC_NUMERIC");
615 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
616 ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
618 /* Check that an empty environment variable is treated like an unset
619 environment variable. */
621 setenv ("LC_ALL", "", 1);
622 unsetenv ("LC_CTYPE");
623 unsetenv ("LC_MESSAGES");
625 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
628 setenv ("LC_CTYPE", "", 1);
629 unsetenv ("LC_MESSAGES");
631 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
634 unsetenv ("LC_CTYPE");
635 setenv ("LC_MESSAGES", "", 1);
637 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
640 unsetenv ("LC_CTYPE");
641 unsetenv ("LC_MESSAGES");
642 setenv ("LANG", "", 1);
643 ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
645 /* Check that LC_ALL overrides the others, and LANG is overridden by the
648 setenv ("LC_ALL", "C", 1);
649 unsetenv ("LC_CTYPE");
650 unsetenv ("LC_MESSAGES");
652 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
653 ASSERT (strcmp (name, "C") == 0);
656 setenv ("LC_CTYPE", "C", 1);
657 setenv ("LC_MESSAGES", "C", 1);
659 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
660 ASSERT (strcmp (name, "C") == 0);
663 unsetenv ("LC_CTYPE");
664 unsetenv ("LC_MESSAGES");
665 setenv ("LANG", "C", 1);
666 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
667 ASSERT (strcmp (name, "C") == 0);
669 /* Check mixed situations. */
672 unsetenv ("LC_CTYPE");
673 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
674 setenv ("LANG", "de_DE.UTF-8", 1);
675 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
676 ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
677 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
678 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
681 unsetenv ("LC_CTYPE");
682 setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
684 name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
685 ASSERT (name == NULL);
686 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
687 ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
690 /* Check that gl_locale_name_environ ignores the thread locale. */
692 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
696 unsetenv ("LC_CTYPE");
697 unsetenv ("LC_MESSAGES");
698 setenv ("LANG", "C", 1);
699 setlocale (LC_ALL, "");
701 name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
702 ASSERT (strcmp (name, "C") == 0);
708 /* Test the gl_locale_name_default() function. */
710 test_locale_name_default (void)
712 const char *name = gl_locale_name_default ();
714 ASSERT (name != NULL);
716 /* Only MacOS X and Windows have a facility for the user to set the default
718 #if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__))
719 ASSERT (strcmp (name, "C") == 0);
723 /* Check that gl_locale_name_default ignores the thread locale. */
725 locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
729 ASSERT (strcmp (gl_locale_name_default (), name) == 0);
739 test_locale_name_thread ();
740 test_locale_name_posix ();
741 test_locale_name_environ ();
742 test_locale_name_default ();