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/>. */
36 #define INPUT(Str) { Str,
37 #define OUTPUT(Str) Str },
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) \
59 printf ("%s:%u: setlocale (%s, \"%s\"): %m\n", \
60 __FILE__, __LINE__, #cat, name); \
66 main (int argc, char *argv[])
73 /* This is the place where the .mo files are placed. */
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
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)
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)
111 puts ("test `dgettext' with LANGUAGE set");
112 if (positive_dgettext_test ("existing-domain") != 0)
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)
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)
141 puts ("test `dgettext' with LC_ALL deciding");
142 if (positive_dgettext_test ("existing-domain") != 0)
148 /* Now the same tests with LC_MESSAGES deciding. */
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, "");
158 printf ("setlocale succeeded (%s), expected failure\n", what);
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)
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)
181 puts ("test `dgettext' with LC_MESSAGES deciding");
182 if (positive_dgettext_test ("existing-domain") != 0)
187 puts ("test `dcgettext' with category == LC_MESSAGES");
188 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
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)
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)
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)
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)
234 puts ("test `dgettext' with LANG set");
235 if (positive_dgettext_test ("existing-domain") != 0)
241 check_setlocale (LC_ALL, "C");
248 positive_gettext_test (void)
253 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
255 const char *found = gettext (msgs[cnt].msgid);
258 || (msgs[cnt].msgstr[0] != '\0'
259 && strcmp (found, msgs[cnt].msgstr) != 0))
261 /* Oops, shouldn't happen. */
263 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
264 msgs[cnt].msgid, found, msgs[cnt].msgstr);
274 negative_gettext_test (void)
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);
296 positive_dgettext_test (const char *domain)
301 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
303 const char *found = dgettext (domain, msgs[cnt].msgid);
306 || (msgs[cnt].msgstr[0] != '\0'
307 && strcmp (found, msgs[cnt].msgstr) != 0))
309 /* Oops, shouldn't happen. */
311 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
312 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
322 positive_dcgettext_test (const char *domain, int category)
327 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
329 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
332 || (msgs[cnt].msgstr[0] != '\0'
333 && strcmp (found, msgs[cnt].msgstr) != 0))
335 /* Oops, shouldn't happen. */
337 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
338 domain, msgs[cnt].msgid, catname[category], found,
349 negative_dcgettext_test (const char *domain, int category)
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]);