]> git.ipfire.org Git - thirdparty/glibc.git/blame - intl/tst-gettext.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / intl / tst-gettext.c
CommitLineData
abbffdf9 1/* Test of the gettext functions.
04277e02 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
abbffdf9
UD
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
abbffdf9
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
abbffdf9 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
abbffdf9
UD
19
20#include <libintl.h>
a8e4c924 21#include <locale.h>
55e2d5c7 22#include <mcheck.h>
abbffdf9
UD
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
5d0bbaaf
RM
26#include <error.h>
27#include <errno.h>
abbffdf9
UD
28
29
30const struct
31{
32 const char *msgid;
33 const char *msgstr;
34} msgs[] =
35{
36#define INPUT(Str) { Str,
37#define OUTPUT(Str) Str },
38#include TESTSTRS_H
39};
40
41const char *catname[] =
42{
43 [LC_MESSAGES] = "LC_MESSAGES",
44 [LC_TIME] = "LC_TIME",
45 [LC_NUMERIC] = "LC_NUMERIC"
46};
47
48
49static int positive_gettext_test (void);
50static int negative_gettext_test (void);
51static int positive_dgettext_test (const char *domain);
52static int positive_dcgettext_test (const char *domain, int category);
53static int negative_dcgettext_test (const char *domain, int category);
54
55
5d0bbaaf
RM
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)
64
abbffdf9
UD
65int
66main (int argc, char *argv[])
67{
68 int result = 0;
69
55e2d5c7
UD
70 /* For debugging. */
71 mtrace ();
72
abbffdf9
UD
73 /* This is the place where the .mo files are placed. */
74 if (argc > 1)
75 {
76 bindtextdomain ("existing-domain", argv[1]);
77 bindtextdomain ("existing-time-domain", argv[1]);
78 bindtextdomain ("non-existing-domain", argv[1]);
79 }
80
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);
45eca4d1 87 setenv ("LC_CTYPE", "non-existing-locale", 1);
abbffdf9 88 setenv ("LANG", "non-existing-locale", 1);
1e6d2101
UD
89 check_setlocale (LC_CTYPE, "de_DE.UTF-8");
90 check_setlocale (LC_MESSAGES, "de_DE.UTF-8");
45eca4d1 91 unsetenv ("OUTPUT_CHARSET");
abbffdf9
UD
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)
97 {
98 puts ("FAILED");
99 result = 1;
100 }
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)
107 {
108 puts ("FAILED");
109 result = 1;
110 }
111 puts ("test `dgettext' with LANGUAGE set");
112 if (positive_dgettext_test ("existing-domain") != 0)
113 {
114 puts ("FAILED");
115 result = 1;
116 }
117
118 /* Now the same tests with LC_ALL deciding. */
119 unsetenv ("LANGUAGE");
120 setenv ("LC_ALL", "existing-locale", 1);
5d0bbaaf 121 check_setlocale (LC_ALL, "");
abbffdf9
UD
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)
127 {
128 puts ("FAILED");
129 result = 1;
130 }
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");
cdfb970d 135 puts ("test `gettext' with LC_ALL deciding");
abbffdf9
UD
136 if (negative_gettext_test () != 0)
137 {
138 puts ("FAILED");
139 result = 1;
140 }
cdfb970d 141 puts ("test `dgettext' with LC_ALL deciding");
abbffdf9
UD
142 if (positive_dgettext_test ("existing-domain") != 0)
143 {
144 puts ("FAILED");
145 result = 1;
146 }
147
148 /* Now the same tests with LC_MESSAGES deciding. */
149 unsetenv ("LC_ALL");
150 setenv ("LC_MESSAGES", "existing-locale", 1);
5d0bbaaf 151 check_setlocale (LC_MESSAGES, "");
abbffdf9 152 setenv ("LC_TIME", "existing-locale", 1);
5d0bbaaf 153 check_setlocale (LC_TIME, "");
abbffdf9 154 setenv ("LC_NUMERIC", "non-existing-locale", 1);
5d0bbaaf
RM
155 char *what = setlocale (LC_NUMERIC, "");
156 if (what != NULL)
157 {
158 printf ("setlocale succeeded (%s), expected failure\n", what);
159 result = 1;
160 }
161
f58dc022 162 puts ("test `gettext' with LC_MESSAGES set");
abbffdf9
UD
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)
167 {
168 puts ("FAILED");
169 result = 1;
170 }
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");
f58dc022 175 puts ("test `gettext' with LC_MESSAGES deciding");
abbffdf9
UD
176 if (negative_gettext_test () != 0)
177 {
178 puts ("FAILED");
179 result = 1;
180 }
f58dc022 181 puts ("test `dgettext' with LC_MESSAGES deciding");
abbffdf9
UD
182 if (positive_dgettext_test ("existing-domain") != 0)
183 {
184 puts ("FAILED");
185 result = 1;
186 }
cdfb970d 187 puts ("test `dcgettext' with category == LC_MESSAGES");
abbffdf9
UD
188 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
189 {
190 puts ("FAILED");
191 result = 1;
192 }
193 /* Try a different category. For this we also switch the domain. */
cdfb970d 194 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
abbffdf9
UD
195 if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
196 {
197 puts ("FAILED");
198 result = 1;
199 }
200 /* This time use a category for which there is no catalog. */
cdfb970d 201 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
abbffdf9
UD
202 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
203 {
204 puts ("FAILED");
205 result = 1;
206 }
207
208 /* Now the same tests with LANG deciding. */
209 unsetenv ("LC_MESSAGES");
5d0bbaaf
RM
210 unsetenv ("LC_CTYPE");
211 unsetenv ("LC_TIME");
212 unsetenv ("LC_NUMERIC");
abbffdf9 213 setenv ("LANG", "existing-locale", 1);
5d0bbaaf 214 check_setlocale (LC_ALL, "");
abbffdf9
UD
215 /* This is the name of the existing domain with a catalog for the
216 LC_MESSAGES category. */
217 textdomain ("existing-domain");
cdfb970d 218 puts ("test `gettext' with LANG set");
abbffdf9
UD
219 if (positive_gettext_test () != 0)
220 {
221 puts ("FAILED");
222 result = 1;
223 }
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");
cdfb970d 228 puts ("test `gettext' with LANG set");
abbffdf9
UD
229 if (negative_gettext_test () != 0)
230 {
231 puts ("FAILED");
232 result = 1;
233 }
cdfb970d 234 puts ("test `dgettext' with LANG set");
abbffdf9
UD
235 if (positive_dgettext_test ("existing-domain") != 0)
236 {
237 puts ("FAILED");
238 result = 1;
239 }
240
5d0bbaaf 241 check_setlocale (LC_ALL, "C");
cdfb970d 242
abbffdf9
UD
243 return result;
244}
245
246
247static int
248positive_gettext_test (void)
249{
250 size_t cnt;
251 int result = 0;
252
253 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
254 {
255 const char *found = gettext (msgs[cnt].msgid);
256
d2afebcc
UD
257 if (found == NULL
258 || (msgs[cnt].msgstr[0] != '\0'
259 && strcmp (found, msgs[cnt].msgstr) != 0))
abbffdf9
UD
260 {
261 /* Oops, shouldn't happen. */
7f455351
UD
262 printf ("\
263 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
264 msgs[cnt].msgid, found, msgs[cnt].msgstr);
abbffdf9
UD
265 result = 1;
266 }
267 }
268
269 return result;
270}
271
272
273static int
274negative_gettext_test (void)
275{
276 size_t cnt;
277 int result = 0;
278
279 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
280 {
281 const char *found = gettext (msgs[cnt].msgid);
282
283 if (found != msgs[cnt].msgid)
284 {
285 /* Oops, shouldn't happen. */
286 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
287 result = 1;
288 }
289 }
290
291 return result;
292}
293
294
295static int
296positive_dgettext_test (const char *domain)
297{
298 size_t cnt;
299 int result = 0;
300
301 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
302 {
303 const char *found = dgettext (domain, msgs[cnt].msgid);
304
d2afebcc
UD
305 if (found == NULL
306 || (msgs[cnt].msgstr[0] != '\0'
307 && strcmp (found, msgs[cnt].msgstr) != 0))
abbffdf9
UD
308 {
309 /* Oops, shouldn't happen. */
7f455351
UD
310 printf ("\
311 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
312 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
abbffdf9
UD
313 result = 1;
314 }
315 }
316
317 return result;
318}
319
320
321static int
322positive_dcgettext_test (const char *domain, int category)
323{
324 size_t cnt;
325 int result = 0;
326
327 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
328 {
329 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
330
d2afebcc
UD
331 if (found == NULL
332 || (msgs[cnt].msgstr[0] != '\0'
333 && strcmp (found, msgs[cnt].msgstr) != 0))
abbffdf9
UD
334 {
335 /* Oops, shouldn't happen. */
7f455351
UD
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);
abbffdf9
UD
340 result = 1;
341 }
342 }
343
344 return result;
345}
346
347
348static int
349negative_dcgettext_test (const char *domain, int category)
350{
351 size_t cnt;
352 int result = 0;
353
354 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
355 {
356 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
357
358 if (found != msgs[cnt].msgid)
359 {
360 /* Oops, shouldn't happen. */
361 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
362 domain, msgs[cnt].msgid, catname[category]);
363 result = 1;
364 }
365 }
366
367 return result;
368}