More localename tests.
[gnulib.git] / tests / test-localename.c
1 /* Test of gl_locale_name function and its variants.
2    Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "localename.h"
22
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 /* Test the gl_locale_name() function.  */
41 static void
42 test_locale_name (void)
43 {
44   const char *name;
45
46   /* Check that gl_locale_name returns non-NULL.  */
47   ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
48
49   /* Get into a defined state,  */
50   setlocale (LC_ALL, "en_US.UTF-8");
51 #if HAVE_NEWLOCALE
52   uselocale (LC_GLOBAL_LOCALE);
53 #endif
54
55   /* Check that when all environment variables are unset,
56      gl_locale_name returns the default locale.  */
57   unsetenv ("LC_ALL");
58   unsetenv ("LC_CTYPE");
59   unsetenv ("LC_MESSAGES");
60   unsetenv ("LC_NUMERIC");
61   unsetenv ("LANG");
62   setlocale (LC_ALL, "");
63   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
64                   gl_locale_name_default ()) == 0);
65   ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
66                   gl_locale_name_default ()) == 0);
67
68   /* Check that an empty environment variable is treated like an unset
69      environment variable.  */
70
71   setenv ("LC_ALL", "", 1);
72   unsetenv ("LC_CTYPE");
73   unsetenv ("LC_MESSAGES");
74   unsetenv ("LANG");
75   setlocale (LC_ALL, "");
76   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
77                   gl_locale_name_default ()) == 0);
78
79   unsetenv ("LC_ALL");
80   setenv ("LC_CTYPE", "", 1);
81   unsetenv ("LC_MESSAGES");
82   unsetenv ("LANG");
83   setlocale (LC_ALL, "");
84   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
85                   gl_locale_name_default ()) == 0);
86
87   unsetenv ("LC_ALL");
88   unsetenv ("LC_CTYPE");
89   setenv ("LC_MESSAGES", "", 1);
90   unsetenv ("LANG");
91   setlocale (LC_ALL, "");
92   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
93                   gl_locale_name_default ()) == 0);
94
95   unsetenv ("LC_ALL");
96   unsetenv ("LC_CTYPE");
97   unsetenv ("LC_MESSAGES");
98   setenv ("LANG", "", 1);
99   setlocale (LC_ALL, "");
100   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
101                   gl_locale_name_default ()) == 0);
102
103   /* Check that LC_ALL overrides the others, and LANG is overridden by the
104      others.  */
105
106   setenv ("LC_ALL", "C", 1);
107   unsetenv ("LC_CTYPE");
108   unsetenv ("LC_MESSAGES");
109   unsetenv ("LANG");
110   setlocale (LC_ALL, "");
111   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
112
113   unsetenv ("LC_ALL");
114   setenv ("LC_CTYPE", "C", 1);
115   setenv ("LC_MESSAGES", "C", 1);
116   unsetenv ("LANG");
117   setlocale (LC_ALL, "");
118   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
119
120   unsetenv ("LC_ALL");
121   unsetenv ("LC_CTYPE");
122   unsetenv ("LC_MESSAGES");
123   setenv ("LANG", "C", 1);
124   setlocale (LC_ALL, "");
125   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
126
127   /* Check mixed situations.  */
128
129   unsetenv ("LC_ALL");
130   unsetenv ("LC_CTYPE");
131   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
132   setenv ("LANG", "de_DE.UTF-8", 1);
133   if (setlocale (LC_ALL, "") != NULL)
134     {
135       name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
136       ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
137       name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
138       ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
139     }
140
141   unsetenv ("LC_ALL");
142   unsetenv ("LC_CTYPE");
143   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
144   unsetenv ("LANG");
145   if (setlocale (LC_ALL, "") != NULL)
146     {
147       name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
148       ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
149       name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
150       ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
151     }
152 }
153
154 /* Test the gl_locale_name_posix() function.  */
155 static void
156 test_locale_name_posix (void)
157 {
158   const char *name;
159
160   /* Get into a defined state,  */
161   setlocale (LC_ALL, "en_US.UTF-8");
162 #if HAVE_NEWLOCALE
163   uselocale (LC_GLOBAL_LOCALE);
164 #endif
165
166   /* Check that when all environment variables are unset,
167      gl_locale_name_posix returns either NULL or the default locale.  */
168   unsetenv ("LC_ALL");
169   unsetenv ("LC_CTYPE");
170   unsetenv ("LC_MESSAGES");
171   unsetenv ("LC_NUMERIC");
172   unsetenv ("LANG");
173   setlocale (LC_ALL, "");
174   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
175   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
176   name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
177   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
178
179   /* Check that an empty environment variable is treated like an unset
180      environment variable.  */
181
182   setenv ("LC_ALL", "", 1);
183   unsetenv ("LC_CTYPE");
184   unsetenv ("LC_MESSAGES");
185   unsetenv ("LANG");
186   setlocale (LC_ALL, "");
187   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
188   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
189
190   unsetenv ("LC_ALL");
191   setenv ("LC_CTYPE", "", 1);
192   unsetenv ("LC_MESSAGES");
193   unsetenv ("LANG");
194   setlocale (LC_ALL, "");
195   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
196   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
197
198   unsetenv ("LC_ALL");
199   unsetenv ("LC_CTYPE");
200   setenv ("LC_MESSAGES", "", 1);
201   unsetenv ("LANG");
202   setlocale (LC_ALL, "");
203   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
204   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
205
206   unsetenv ("LC_ALL");
207   unsetenv ("LC_CTYPE");
208   unsetenv ("LC_MESSAGES");
209   setenv ("LANG", "", 1);
210   setlocale (LC_ALL, "");
211   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
212   ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
213
214   /* Check that LC_ALL overrides the others, and LANG is overridden by the
215      others.  */
216
217   setenv ("LC_ALL", "C", 1);
218   unsetenv ("LC_CTYPE");
219   unsetenv ("LC_MESSAGES");
220   unsetenv ("LANG");
221   setlocale (LC_ALL, "");
222   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
223   ASSERT (strcmp (name, "C") == 0);
224
225   unsetenv ("LC_ALL");
226   setenv ("LC_CTYPE", "C", 1);
227   setenv ("LC_MESSAGES", "C", 1);
228   unsetenv ("LANG");
229   setlocale (LC_ALL, "");
230   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
231   ASSERT (strcmp (name, "C") == 0);
232
233   unsetenv ("LC_ALL");
234   unsetenv ("LC_CTYPE");
235   unsetenv ("LC_MESSAGES");
236   setenv ("LANG", "C", 1);
237   setlocale (LC_ALL, "");
238   name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
239   ASSERT (strcmp (name, "C") == 0);
240
241   /* Check mixed situations.  */
242
243   unsetenv ("LC_ALL");
244   unsetenv ("LC_CTYPE");
245   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
246   setenv ("LANG", "de_DE.UTF-8", 1);
247   if (setlocale (LC_ALL, "") != NULL)
248     {
249       name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
250       ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
251       name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
252       ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
253     }
254
255   unsetenv ("LC_ALL");
256   unsetenv ("LC_CTYPE");
257   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
258   unsetenv ("LANG");
259   if (setlocale (LC_ALL, "") != NULL)
260     {
261       name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
262       ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
263       name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
264       ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
265     }
266 }
267
268 /* Test the gl_locale_name_environ() function.  */
269 static void
270 test_locale_name_environ (void)
271 {
272   const char *name;
273
274   /* Get into a defined state,  */
275   setlocale (LC_ALL, "en_US.UTF-8");
276 #if HAVE_NEWLOCALE
277   uselocale (LC_GLOBAL_LOCALE);
278 #endif
279
280   /* Check that when all environment variables are unset,
281      gl_locale_name_environ returns NULL.  */
282   unsetenv ("LC_ALL");
283   unsetenv ("LC_CTYPE");
284   unsetenv ("LC_MESSAGES");
285   unsetenv ("LC_NUMERIC");
286   unsetenv ("LANG");
287   ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
288   ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
289
290   /* Check that an empty environment variable is treated like an unset
291      environment variable.  */
292
293   setenv ("LC_ALL", "", 1);
294   unsetenv ("LC_CTYPE");
295   unsetenv ("LC_MESSAGES");
296   unsetenv ("LANG");
297   ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
298
299   unsetenv ("LC_ALL");
300   setenv ("LC_CTYPE", "", 1);
301   unsetenv ("LC_MESSAGES");
302   unsetenv ("LANG");
303   ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
304
305   unsetenv ("LC_ALL");
306   unsetenv ("LC_CTYPE");
307   setenv ("LC_MESSAGES", "", 1);
308   unsetenv ("LANG");
309   ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
310
311   unsetenv ("LC_ALL");
312   unsetenv ("LC_CTYPE");
313   unsetenv ("LC_MESSAGES");
314   setenv ("LANG", "", 1);
315   ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
316
317   /* Check that LC_ALL overrides the others, and LANG is overridden by the
318      others.  */
319
320   setenv ("LC_ALL", "C", 1);
321   unsetenv ("LC_CTYPE");
322   unsetenv ("LC_MESSAGES");
323   unsetenv ("LANG");
324   name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
325   ASSERT (strcmp (name, "C") == 0);
326
327   unsetenv ("LC_ALL");
328   setenv ("LC_CTYPE", "C", 1);
329   setenv ("LC_MESSAGES", "C", 1);
330   unsetenv ("LANG");
331   name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
332   ASSERT (strcmp (name, "C") == 0);
333
334   unsetenv ("LC_ALL");
335   unsetenv ("LC_CTYPE");
336   unsetenv ("LC_MESSAGES");
337   setenv ("LANG", "C", 1);
338   name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
339   ASSERT (strcmp (name, "C") == 0);
340
341   /* Check mixed situations.  */
342
343   unsetenv ("LC_ALL");
344   unsetenv ("LC_CTYPE");
345   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
346   setenv ("LANG", "de_DE.UTF-8", 1);
347   name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
348   ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
349   name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
350   ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
351
352   unsetenv ("LC_ALL");
353   unsetenv ("LC_CTYPE");
354   setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
355   unsetenv ("LANG");
356   name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
357   ASSERT (name == NULL);
358   name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
359   ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
360 }
361
362 /* Test the gl_locale_name_default() function.  */
363 static void
364 test_locale_name_default (void)
365 {
366   const char *name = gl_locale_name_default ();
367
368   ASSERT (name != NULL);
369
370   /* Only MacOS X and Windows have a facility for the user to set the default
371      locale.  */
372 #if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__))
373   ASSERT (strcmp (name, "C") == 0);
374 #endif
375 }
376
377 int
378 main ()
379 {
380   test_locale_name ();
381   test_locale_name_posix ();
382   test_locale_name_environ ();
383   test_locale_name_default ();
384
385   return 0;
386 }