Change copyright notice from GPLv2+ to GPLv3+.
[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 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 #include "setenv.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main ()
43 {
44   /* Check that gl_locale_name returns non-NULL.  */
45   ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
46
47   /* Check that when all environment variables are unset,
48      gl_locale_name_posix returns NULL.  */
49   unsetenv ("LC_ALL");
50   unsetenv ("LC_CTYPE");
51   unsetenv ("LC_MESSAGES");
52   unsetenv ("LC_NUMERIC");
53   unsetenv ("LANG");
54   setlocale (LC_ALL, "");
55   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
56                   gl_locale_name_default ()) == 0);
57   ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
58                   gl_locale_name_default ()) == 0);
59
60   /* Check that an empty environment variable is treated like an unset
61      environment variable.  */
62
63   setenv ("LC_ALL", "", 1);
64   unsetenv ("LC_CTYPE");
65   unsetenv ("LC_MESSAGES");
66   unsetenv ("LANG");
67   setlocale (LC_ALL, "");
68   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
69                   gl_locale_name_default ()) == 0);
70
71   unsetenv ("LC_ALL");
72   setenv ("LC_CTYPE", "", 1);
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   unsetenv ("LC_CTYPE");
81   setenv ("LC_MESSAGES", "", 1);
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   unsetenv ("LC_MESSAGES");
90   setenv ("LANG", "", 1);
91   setlocale (LC_ALL, "");
92   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
93                   gl_locale_name_default ()) == 0);
94
95   /* Check that LC_ALL overrides the others, and LANG is overridden by the
96      others.  */
97
98   setenv ("LC_ALL", "C", 1);
99   unsetenv ("LC_CTYPE");
100   unsetenv ("LC_MESSAGES");
101   unsetenv ("LANG");
102   setlocale (LC_ALL, "");
103   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
104   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
105
106   unsetenv ("LC_ALL");
107   setenv ("LC_CTYPE", "C", 1);
108   setenv ("LC_MESSAGES", "C", 1);
109   unsetenv ("LANG");
110   setlocale (LC_ALL, "");
111   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
112   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
113
114   unsetenv ("LC_ALL");
115   unsetenv ("LC_CTYPE");
116   unsetenv ("LC_MESSAGES");
117   setenv ("LANG", "C", 1);
118   setlocale (LC_ALL, "");
119   ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
120   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
121
122   return 0;
123 }