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