* MODULES.html.sh: Add include_next.
[gnulib.git] / tests / test-localename.c
1 /* Test of gl_locale_name function.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "localename.h"
23
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "setenv.h"
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main ()
44 {
45   /* Check that gl_locale_name returns non-NULL.  */
46   ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
47
48   /* Check that when all environment variables are unset,
49      gl_locale_name_posix returns NULL.  */
50   unsetenv ("LC_ALL");
51   unsetenv ("LC_CTYPE");
52   unsetenv ("LC_MESSAGES");
53   unsetenv ("LC_NUMERIC");
54   unsetenv ("LANG");
55   setlocale (LC_ALL, "");
56   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
57                   gl_locale_name_default ()) == 0);
58   ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
59                   gl_locale_name_default ()) == 0);
60
61   /* Check that an empty environment variable is treated like an unset
62      environment variable.  */
63
64   setenv ("LC_ALL", "", 1);
65   unsetenv ("LC_CTYPE");
66   unsetenv ("LC_MESSAGES");
67   unsetenv ("LANG");
68   setlocale (LC_ALL, "");
69   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
70                   gl_locale_name_default ()) == 0);
71
72   unsetenv ("LC_ALL");
73   setenv ("LC_CTYPE", "", 1);
74   unsetenv ("LC_MESSAGES");
75   unsetenv ("LANG");
76   setlocale (LC_ALL, "");
77   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
78                   gl_locale_name_default ()) == 0);
79
80   unsetenv ("LC_ALL");
81   unsetenv ("LC_CTYPE");
82   setenv ("LC_MESSAGES", "", 1);
83   unsetenv ("LANG");
84   setlocale (LC_ALL, "");
85   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
86                   gl_locale_name_default ()) == 0);
87
88   unsetenv ("LC_ALL");
89   unsetenv ("LC_CTYPE");
90   unsetenv ("LC_MESSAGES");
91   setenv ("LANG", "", 1);
92   setlocale (LC_ALL, "");
93   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
94                   gl_locale_name_default ()) == 0);
95
96   /* Check that LC_ALL overrides the others, and LANG is overridden by the
97      others.  */
98
99   setenv ("LC_ALL", "C", 1);
100   unsetenv ("LC_CTYPE");
101   unsetenv ("LC_MESSAGES");
102   unsetenv ("LANG");
103   setlocale (LC_ALL, "");
104   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
105   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
106
107   unsetenv ("LC_ALL");
108   setenv ("LC_CTYPE", "C", 1);
109   setenv ("LC_MESSAGES", "C", 1);
110   unsetenv ("LANG");
111   setlocale (LC_ALL, "");
112   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
113   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
114
115   unsetenv ("LC_ALL");
116   unsetenv ("LC_CTYPE");
117   unsetenv ("LC_MESSAGES");
118   setenv ("LANG", "C", 1);
119   setlocale (LC_ALL, "");
120   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
121   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
122
123   return 0;
124 }