Add FE_NOMASK_ENV return value test.
[glibc.git] / intl / tst-gettext.c
blob4a4c9823bd01851371c81c8fbfb439c74c9bb9ad
1 /* Test of the gettext functions.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <libintl.h>
21 #include <locale.h>
22 #include <mcheck.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <error.h>
27 #include <errno.h>
30 const struct
32 const char *msgid;
33 const char *msgstr;
34 } msgs[] =
36 #define INPUT(Str) { Str,
37 #define OUTPUT(Str) Str },
38 #include TESTSTRS_H
41 const char *catname[] =
43 [LC_MESSAGES] = "LC_MESSAGES",
44 [LC_TIME] = "LC_TIME",
45 [LC_NUMERIC] = "LC_NUMERIC"
49 static int positive_gettext_test (void);
50 static int negative_gettext_test (void);
51 static int positive_dgettext_test (const char *domain);
52 static int positive_dcgettext_test (const char *domain, int category);
53 static int negative_dcgettext_test (const char *domain, int category);
56 #define check_setlocale(cat, name) do { \
57 if (setlocale (cat, name) == NULL) \
58 { \
59 printf ("%s:%u: setlocale (%s, \"%s\"): %m\n", \
60 __FILE__, __LINE__, #cat, name); \
61 result = 1; \
62 } \
63 } while (0)
65 int
66 main (int argc, char *argv[])
68 int result = 0;
70 /* For debugging. */
71 mtrace ();
73 /* This is the place where the .mo files are placed. */
74 if (argc > 1)
76 bindtextdomain ("existing-domain", argv[1]);
77 bindtextdomain ("existing-time-domain", argv[1]);
78 bindtextdomain ("non-existing-domain", argv[1]);
81 /* The locale the catalog is created for is "existing-category". Now
82 set the various variables in question to this value and run the
83 test. */
84 setenv ("LANGUAGE", "existing-locale", 1);
85 setenv ("LC_ALL", "non-existing-locale", 1);
86 setenv ("LC_MESSAGES", "non-existing-locale", 1);
87 setenv ("LC_CTYPE", "non-existing-locale", 1);
88 setenv ("LANG", "non-existing-locale", 1);
89 check_setlocale (LC_CTYPE, "de_DE.UTF-8");
90 check_setlocale (LC_MESSAGES, "de_DE.UTF-8");
91 unsetenv ("OUTPUT_CHARSET");
92 /* This is the name of the existing domain with a catalog for the
93 LC_MESSAGES category. */
94 textdomain ("existing-domain");
95 puts ("test `gettext' with LANGUAGE set");
96 if (positive_gettext_test () != 0)
98 puts ("FAILED");
99 result = 1;
101 /* This is the name of a non-existing domain with a catalog for the
102 LC_MESSAGES category. We leave this value set for the `dgettext'
103 and `dcgettext' tests. */
104 textdomain ("non-existing-domain");
105 puts ("test `gettext' with LANGUAGE set");
106 if (negative_gettext_test () != 0)
108 puts ("FAILED");
109 result = 1;
111 puts ("test `dgettext' with LANGUAGE set");
112 if (positive_dgettext_test ("existing-domain") != 0)
114 puts ("FAILED");
115 result = 1;
118 /* Now the same tests with LC_ALL deciding. */
119 unsetenv ("LANGUAGE");
120 setenv ("LC_ALL", "existing-locale", 1);
121 check_setlocale (LC_ALL, "");
122 puts ("test `gettext' with LC_ALL set");
123 /* This is the name of the existing domain with a catalog for the
124 LC_MESSAGES category. */
125 textdomain ("existing-domain");
126 if (positive_gettext_test () != 0)
128 puts ("FAILED");
129 result = 1;
131 /* This is the name of a non-existing domain with a catalog for the
132 LC_MESSAGES category. We leave this value set for the `dgettext'
133 and `dcgettext' tests. */
134 textdomain ("non-existing-domain");
135 puts ("test `gettext' with LC_ALL deciding");
136 if (negative_gettext_test () != 0)
138 puts ("FAILED");
139 result = 1;
141 puts ("test `dgettext' with LC_ALL deciding");
142 if (positive_dgettext_test ("existing-domain") != 0)
144 puts ("FAILED");
145 result = 1;
148 /* Now the same tests with LC_MESSAGES deciding. */
149 unsetenv ("LC_ALL");
150 setenv ("LC_MESSAGES", "existing-locale", 1);
151 check_setlocale (LC_MESSAGES, "");
152 setenv ("LC_TIME", "existing-locale", 1);
153 check_setlocale (LC_TIME, "");
154 setenv ("LC_NUMERIC", "non-existing-locale", 1);
155 char *what = setlocale (LC_NUMERIC, "");
156 if (what != NULL)
158 printf ("setlocale succeeded (%s), expected failure\n", what);
159 result = 1;
162 puts ("test `gettext' with LC_MESSAGES set");
163 /* This is the name of the existing domain with a catalog for the
164 LC_MESSAGES category. */
165 textdomain ("existing-domain");
166 if (positive_gettext_test () != 0)
168 puts ("FAILED");
169 result = 1;
171 /* This is the name of a non-existing domain with a catalog for the
172 LC_MESSAGES category. We leave this value set for the `dgettext'
173 and `dcgettext' tests. */
174 textdomain ("non-existing-domain");
175 puts ("test `gettext' with LC_MESSAGES deciding");
176 if (negative_gettext_test () != 0)
178 puts ("FAILED");
179 result = 1;
181 puts ("test `dgettext' with LC_MESSAGES deciding");
182 if (positive_dgettext_test ("existing-domain") != 0)
184 puts ("FAILED");
185 result = 1;
187 puts ("test `dcgettext' with category == LC_MESSAGES");
188 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
190 puts ("FAILED");
191 result = 1;
193 /* Try a different category. For this we also switch the domain. */
194 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
195 if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
197 puts ("FAILED");
198 result = 1;
200 /* This time use a category for which there is no catalog. */
201 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
202 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
204 puts ("FAILED");
205 result = 1;
208 /* Now the same tests with LANG deciding. */
209 unsetenv ("LC_MESSAGES");
210 unsetenv ("LC_CTYPE");
211 unsetenv ("LC_TIME");
212 unsetenv ("LC_NUMERIC");
213 setenv ("LANG", "existing-locale", 1);
214 check_setlocale (LC_ALL, "");
215 /* This is the name of the existing domain with a catalog for the
216 LC_MESSAGES category. */
217 textdomain ("existing-domain");
218 puts ("test `gettext' with LANG set");
219 if (positive_gettext_test () != 0)
221 puts ("FAILED");
222 result = 1;
224 /* This is the name of a non-existing domain with a catalog for the
225 LC_MESSAGES category. We leave this value set for the `dgettext'
226 and `dcgettext' tests. */
227 textdomain ("non-existing-domain");
228 puts ("test `gettext' with LANG set");
229 if (negative_gettext_test () != 0)
231 puts ("FAILED");
232 result = 1;
234 puts ("test `dgettext' with LANG set");
235 if (positive_dgettext_test ("existing-domain") != 0)
237 puts ("FAILED");
238 result = 1;
241 check_setlocale (LC_ALL, "C");
243 return result;
247 static int
248 positive_gettext_test (void)
250 size_t cnt;
251 int result = 0;
253 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
255 const char *found = gettext (msgs[cnt].msgid);
257 if (found == NULL
258 || (msgs[cnt].msgstr[0] != '\0'
259 && strcmp (found, msgs[cnt].msgstr) != 0))
261 /* Oops, shouldn't happen. */
262 printf ("\
263 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
264 msgs[cnt].msgid, found, msgs[cnt].msgstr);
265 result = 1;
269 return result;
273 static int
274 negative_gettext_test (void)
276 size_t cnt;
277 int result = 0;
279 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
281 const char *found = gettext (msgs[cnt].msgid);
283 if (found != msgs[cnt].msgid)
285 /* Oops, shouldn't happen. */
286 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
287 result = 1;
291 return result;
295 static int
296 positive_dgettext_test (const char *domain)
298 size_t cnt;
299 int result = 0;
301 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
303 const char *found = dgettext (domain, msgs[cnt].msgid);
305 if (found == NULL
306 || (msgs[cnt].msgstr[0] != '\0'
307 && strcmp (found, msgs[cnt].msgstr) != 0))
309 /* Oops, shouldn't happen. */
310 printf ("\
311 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
312 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
313 result = 1;
317 return result;
321 static int
322 positive_dcgettext_test (const char *domain, int category)
324 size_t cnt;
325 int result = 0;
327 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
329 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
331 if (found == NULL
332 || (msgs[cnt].msgstr[0] != '\0'
333 && strcmp (found, msgs[cnt].msgstr) != 0))
335 /* Oops, shouldn't happen. */
336 printf ("\
337 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
338 domain, msgs[cnt].msgid, catname[category], found,
339 msgs[cnt].msgstr);
340 result = 1;
344 return result;
348 static int
349 negative_dcgettext_test (const char *domain, int category)
351 size_t cnt;
352 int result = 0;
354 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
356 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
358 if (found != msgs[cnt].msgid)
360 /* Oops, shouldn't happen. */
361 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
362 domain, msgs[cnt].msgid, catname[category]);
363 result = 1;
367 return result;