Update.
[glibc.git] / intl / tst-gettext.c
blob55b8310dfad3d54dea95847d56c622322654e66a
1 /* Test of the gettext functions.
2 Copyright (C) 2000, 2002 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <libintl.h>
22 #include <locale.h>
23 #include <mcheck.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
29 const struct
31 const char *msgid;
32 const char *msgstr;
33 } msgs[] =
35 #define INPUT(Str) { Str,
36 #define OUTPUT(Str) Str },
37 #include TESTSTRS_H
40 const char *catname[] =
42 [LC_MESSAGES] = "LC_MESSAGES",
43 [LC_TIME] = "LC_TIME",
44 [LC_NUMERIC] = "LC_NUMERIC"
48 static int positive_gettext_test (void);
49 static int negative_gettext_test (void);
50 static int positive_dgettext_test (const char *domain);
51 static int positive_dcgettext_test (const char *domain, int category);
52 static int negative_dcgettext_test (const char *domain, int category);
55 int
56 main (int argc, char *argv[])
58 int result = 0;
60 /* For debugging. */
61 mtrace ();
63 /* This is the place where the .mo files are placed. */
64 if (argc > 1)
66 bindtextdomain ("existing-domain", argv[1]);
67 bindtextdomain ("existing-time-domain", argv[1]);
68 bindtextdomain ("non-existing-domain", argv[1]);
71 /* The locale the catalog is created for is "existing-category". Now
72 set the various variables in question to this value and run the
73 test. */
74 setenv ("LANGUAGE", "existing-locale", 1);
75 setenv ("LC_ALL", "non-existing-locale", 1);
76 setenv ("LC_MESSAGES", "non-existing-locale", 1);
77 setenv ("LC_CTYPE", "non-existing-locale", 1);
78 setenv ("LANG", "non-existing-locale", 1);
79 setlocale (LC_CTYPE, "de_DE.ISO-8859-1");
80 setlocale (LC_MESSAGES, "de_DE.ISO-8859-1");
81 unsetenv ("OUTPUT_CHARSET");
82 /* This is the name of the existing domain with a catalog for the
83 LC_MESSAGES category. */
84 textdomain ("existing-domain");
85 puts ("test `gettext' with LANGUAGE set");
86 if (positive_gettext_test () != 0)
88 puts ("FAILED");
89 result = 1;
91 /* This is the name of a non-existing domain with a catalog for the
92 LC_MESSAGES category. We leave this value set for the `dgettext'
93 and `dcgettext' tests. */
94 textdomain ("non-existing-domain");
95 puts ("test `gettext' with LANGUAGE set");
96 if (negative_gettext_test () != 0)
98 puts ("FAILED");
99 result = 1;
101 puts ("test `dgettext' with LANGUAGE set");
102 if (positive_dgettext_test ("existing-domain") != 0)
104 puts ("FAILED");
105 result = 1;
108 /* Now the same tests with LC_ALL deciding. */
109 unsetenv ("LANGUAGE");
110 setenv ("LC_ALL", "existing-locale", 1);
111 setlocale (LC_ALL, "");
112 puts ("test `gettext' with LC_ALL set");
113 /* This is the name of the existing domain with a catalog for the
114 LC_MESSAGES category. */
115 textdomain ("existing-domain");
116 if (positive_gettext_test () != 0)
118 puts ("FAILED");
119 result = 1;
121 /* This is the name of a non-existing domain with a catalog for the
122 LC_MESSAGES category. We leave this value set for the `dgettext'
123 and `dcgettext' tests. */
124 textdomain ("non-existing-domain");
125 puts ("test `gettext' with LC_ALL deciding");
126 if (negative_gettext_test () != 0)
128 puts ("FAILED");
129 result = 1;
131 puts ("test `dgettext' with LC_ALL deciding");
132 if (positive_dgettext_test ("existing-domain") != 0)
134 puts ("FAILED");
135 result = 1;
138 /* Now the same tests with LC_MESSAGES deciding. */
139 unsetenv ("LC_ALL");
140 setenv ("LC_MESSAGES", "existing-locale", 1);
141 setlocale (LC_MESSAGES, "");
142 setenv ("LC_TIME", "existing-locale", 1);
143 setlocale (LC_TIME, "");
144 setenv ("LC_NUMERIC", "non-existing-locale", 1);
145 setlocale (LC_NUMERIC, "");
146 puts ("test `gettext' with LC_ALL set");
147 /* This is the name of the existing domain with a catalog for the
148 LC_MESSAGES category. */
149 textdomain ("existing-domain");
150 if (positive_gettext_test () != 0)
152 puts ("FAILED");
153 result = 1;
155 /* This is the name of a non-existing domain with a catalog for the
156 LC_MESSAGES category. We leave this value set for the `dgettext'
157 and `dcgettext' tests. */
158 textdomain ("non-existing-domain");
159 puts ("test `gettext' with LC_xxx deciding");
160 if (negative_gettext_test () != 0)
162 puts ("FAILED");
163 result = 1;
165 puts ("test `dgettext' with LC_xxx deciding");
166 if (positive_dgettext_test ("existing-domain") != 0)
168 puts ("FAILED");
169 result = 1;
171 puts ("test `dcgettext' with category == LC_MESSAGES");
172 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
174 puts ("FAILED");
175 result = 1;
177 /* Try a different category. For this we also switch the domain. */
178 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
179 if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
181 puts ("FAILED");
182 result = 1;
184 /* This time use a category for which there is no catalog. */
185 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
186 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
188 puts ("FAILED");
189 result = 1;
192 /* Now the same tests with LANG deciding. */
193 unsetenv ("LC_MESSAGES");
194 setenv ("LANG", "existing-locale", 1);
195 setlocale (LC_ALL, "");
196 /* This is the name of the existing domain with a catalog for the
197 LC_MESSAGES category. */
198 textdomain ("existing-domain");
199 puts ("test `gettext' with LANG set");
200 if (positive_gettext_test () != 0)
202 puts ("FAILED");
203 result = 1;
205 /* This is the name of a non-existing domain with a catalog for the
206 LC_MESSAGES category. We leave this value set for the `dgettext'
207 and `dcgettext' tests. */
208 textdomain ("non-existing-domain");
209 puts ("test `gettext' with LANG set");
210 if (negative_gettext_test () != 0)
212 puts ("FAILED");
213 result = 1;
215 puts ("test `dgettext' with LANG set");
216 if (positive_dgettext_test ("existing-domain") != 0)
218 puts ("FAILED");
219 result = 1;
222 setlocale (LC_ALL, "C");
224 return result;
228 static int
229 positive_gettext_test (void)
231 size_t cnt;
232 int result = 0;
234 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
236 const char *found = gettext (msgs[cnt].msgid);
238 if (found == NULL
239 || (msgs[cnt].msgstr[0] != '\0'
240 && strcmp (found, msgs[cnt].msgstr) != 0))
242 /* Oops, shouldn't happen. */
243 printf ("\
244 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
245 msgs[cnt].msgid, found, msgs[cnt].msgstr);
246 result = 1;
250 return result;
254 static int
255 negative_gettext_test (void)
257 size_t cnt;
258 int result = 0;
260 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
262 const char *found = gettext (msgs[cnt].msgid);
264 if (found != msgs[cnt].msgid)
266 /* Oops, shouldn't happen. */
267 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
268 result = 1;
272 return result;
276 static int
277 positive_dgettext_test (const char *domain)
279 size_t cnt;
280 int result = 0;
282 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
284 const char *found = dgettext (domain, msgs[cnt].msgid);
286 if (found == NULL
287 || (msgs[cnt].msgstr[0] != '\0'
288 && strcmp (found, msgs[cnt].msgstr) != 0))
290 /* Oops, shouldn't happen. */
291 printf ("\
292 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
293 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
294 result = 1;
298 return result;
302 static int
303 positive_dcgettext_test (const char *domain, int category)
305 size_t cnt;
306 int result = 0;
308 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
310 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
312 if (found == NULL
313 || (msgs[cnt].msgstr[0] != '\0'
314 && strcmp (found, msgs[cnt].msgstr) != 0))
316 /* Oops, shouldn't happen. */
317 printf ("\
318 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
319 domain, msgs[cnt].msgid, catname[category], found,
320 msgs[cnt].msgstr);
321 result = 1;
325 return result;
329 static int
330 negative_dcgettext_test (const char *domain, int category)
332 size_t cnt;
333 int result = 0;
335 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
337 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
339 if (found != msgs[cnt].msgid)
341 /* Oops, shouldn't happen. */
342 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
343 domain, msgs[cnt].msgid, catname[category]);
344 result = 1;
348 return result;