Update.
[glibc.git] / intl / tst-gettext.c
blobe6cbf6f1b2dbeff6e7c20a27d0025c8ec3113580
1 /* Test of the gettext functions.
2 Copyright (C) 2000 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 || strcmp (found, msgs[cnt].msgstr) != 0)
240 /* Oops, shouldn't happen. */
241 printf ("\
242 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
243 msgs[cnt].msgid, found, msgs[cnt].msgstr);
244 result = 1;
248 return result;
252 static int
253 negative_gettext_test (void)
255 size_t cnt;
256 int result = 0;
258 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
260 const char *found = gettext (msgs[cnt].msgid);
262 if (found != msgs[cnt].msgid)
264 /* Oops, shouldn't happen. */
265 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
266 result = 1;
270 return result;
274 static int
275 positive_dgettext_test (const char *domain)
277 size_t cnt;
278 int result = 0;
280 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
282 const char *found = dgettext (domain, msgs[cnt].msgid);
284 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
286 /* Oops, shouldn't happen. */
287 printf ("\
288 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
289 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
290 result = 1;
294 return result;
298 static int
299 positive_dcgettext_test (const char *domain, int category)
301 size_t cnt;
302 int result = 0;
304 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
306 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
308 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
310 /* Oops, shouldn't happen. */
311 printf ("\
312 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
313 domain, msgs[cnt].msgid, catname[category], found,
314 msgs[cnt].msgstr);
315 result = 1;
319 return result;
323 static int
324 negative_dcgettext_test (const char *domain, int category)
326 size_t cnt;
327 int result = 0;
329 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
331 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
333 if (found != msgs[cnt].msgid)
335 /* Oops, shouldn't happen. */
336 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
337 domain, msgs[cnt].msgid, catname[category]);
338 result = 1;
342 return result;