Avoid some warnings from "gcc -Wshadow".
[gnulib.git] / tests / test-localename.c
1 /* Test of gl_locale_name function.
2    Copyright (C) 2007, 2008 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           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main ()
41 {
42   /* Check that gl_locale_name returns non-NULL.  */
43   ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
44
45   /* Check that when all environment variables are unset,
46      gl_locale_name_posix returns NULL.  */
47   unsetenv ("LC_ALL");
48   unsetenv ("LC_CTYPE");
49   unsetenv ("LC_MESSAGES");
50   unsetenv ("LC_NUMERIC");
51   unsetenv ("LANG");
52   setlocale (LC_ALL, "");
53   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
54                   gl_locale_name_default ()) == 0);
55   ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
56                   gl_locale_name_default ()) == 0);
57
58   /* Check that an empty environment variable is treated like an unset
59      environment variable.  */
60
61   setenv ("LC_ALL", "", 1);
62   unsetenv ("LC_CTYPE");
63   unsetenv ("LC_MESSAGES");
64   unsetenv ("LANG");
65   setlocale (LC_ALL, "");
66   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
67                   gl_locale_name_default ()) == 0);
68
69   unsetenv ("LC_ALL");
70   setenv ("LC_CTYPE", "", 1);
71   unsetenv ("LC_MESSAGES");
72   unsetenv ("LANG");
73   setlocale (LC_ALL, "");
74   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
75                   gl_locale_name_default ()) == 0);
76
77   unsetenv ("LC_ALL");
78   unsetenv ("LC_CTYPE");
79   setenv ("LC_MESSAGES", "", 1);
80   unsetenv ("LANG");
81   setlocale (LC_ALL, "");
82   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
83                   gl_locale_name_default ()) == 0);
84
85   unsetenv ("LC_ALL");
86   unsetenv ("LC_CTYPE");
87   unsetenv ("LC_MESSAGES");
88   setenv ("LANG", "", 1);
89   setlocale (LC_ALL, "");
90   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
91                   gl_locale_name_default ()) == 0);
92
93   /* Check that LC_ALL overrides the others, and LANG is overridden by the
94      others.  */
95
96   setenv ("LC_ALL", "C", 1);
97   unsetenv ("LC_CTYPE");
98   unsetenv ("LC_MESSAGES");
99   unsetenv ("LANG");
100   setlocale (LC_ALL, "");
101   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
102   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
103
104   unsetenv ("LC_ALL");
105   setenv ("LC_CTYPE", "C", 1);
106   setenv ("LC_MESSAGES", "C", 1);
107   unsetenv ("LANG");
108   setlocale (LC_ALL, "");
109   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
110   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
111
112   unsetenv ("LC_ALL");
113   unsetenv ("LC_CTYPE");
114   unsetenv ("LC_MESSAGES");
115   setenv ("LANG", "C", 1);
116   setlocale (LC_ALL, "");
117   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
118   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
119
120   return 0;
121 }