Update.
[glibc.git] / intl / tst-gettext.c
blob1f5e6f19185278fd503774e3623b677f5d6f0434
1 /* Test of the gettext functions.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <libintl.h>
21 #include <locale.h>
22 #include <mcheck.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
28 const struct
30 const char *msgid;
31 const char *msgstr;
32 } msgs[] =
34 #define INPUT(Str) { Str,
35 #define OUTPUT(Str) Str },
36 #include TESTSTRS_H
39 const char *catname[] =
41 [LC_MESSAGES] = "LC_MESSAGES",
42 [LC_TIME] = "LC_TIME",
43 [LC_NUMERIC] = "LC_NUMERIC"
47 static int positive_gettext_test (void);
48 static int negative_gettext_test (void);
49 static int positive_dgettext_test (const char *domain);
50 static int positive_dcgettext_test (const char *domain, int category);
51 static int negative_dcgettext_test (const char *domain, int category);
54 int
55 main (int argc, char *argv[])
57 int result = 0;
59 /* For debugging. */
60 mtrace ();
62 /* This is the place where the .mo files are placed. */
63 if (argc > 1)
65 bindtextdomain ("existing-domain", argv[1]);
66 bindtextdomain ("existing-time-domain", argv[1]);
67 bindtextdomain ("non-existing-domain", argv[1]);
70 /* The locale the catalog is created for is "existing-category". Now
71 set the various variables in question to this value and run the
72 test. */
73 setenv ("LANGUAGE", "existing-locale", 1);
74 setenv ("LC_ALL", "non-existing-locale", 1);
75 setenv ("LC_MESSAGES", "non-existing-locale", 1);
76 setenv ("LC_CTYPE", "non-existing-locale", 1);
77 setenv ("LANG", "non-existing-locale", 1);
78 setlocale (LC_CTYPE, "de_DE.ISO-8859-1");
79 setlocale (LC_MESSAGES, "de_DE.ISO-8859-1");
80 unsetenv ("OUTPUT_CHARSET");
81 /* This is the name of the existing domain with a catalog for the
82 LC_MESSAGES category. */
83 textdomain ("existing-domain");
84 puts ("test `gettext' with LANGUAGE set");
85 if (positive_gettext_test () != 0)
87 puts ("FAILED");
88 result = 1;
90 /* This is the name of a non-existing domain with a catalog for the
91 LC_MESSAGES category. We leave this value set for the `dgettext'
92 and `dcgettext' tests. */
93 textdomain ("non-existing-domain");
94 puts ("test `gettext' with LANGUAGE set");
95 if (negative_gettext_test () != 0)
97 puts ("FAILED");
98 result = 1;
100 puts ("test `dgettext' with LANGUAGE set");
101 if (positive_dgettext_test ("existing-domain") != 0)
103 puts ("FAILED");
104 result = 1;
107 /* Now the same tests with LC_ALL deciding. */
108 unsetenv ("LANGUAGE");
109 setenv ("LC_ALL", "existing-locale", 1);
110 setlocale (LC_ALL, "");
111 puts ("test `gettext' with LC_ALL set");
112 /* This is the name of the existing domain with a catalog for the
113 LC_MESSAGES category. */
114 textdomain ("existing-domain");
115 if (positive_gettext_test () != 0)
117 puts ("FAILED");
118 result = 1;
120 /* This is the name of a non-existing domain with a catalog for the
121 LC_MESSAGES category. We leave this value set for the `dgettext'
122 and `dcgettext' tests. */
123 textdomain ("non-existing-domain");
124 puts ("test `gettext' with LC_ALL deciding");
125 if (negative_gettext_test () != 0)
127 puts ("FAILED");
128 result = 1;
130 puts ("test `dgettext' with LC_ALL deciding");
131 if (positive_dgettext_test ("existing-domain") != 0)
133 puts ("FAILED");
134 result = 1;
137 /* Now the same tests with LC_MESSAGES deciding. */
138 unsetenv ("LC_ALL");
139 setenv ("LC_MESSAGES", "existing-locale", 1);
140 setlocale (LC_MESSAGES, "");
141 setenv ("LC_TIME", "existing-locale", 1);
142 setlocale (LC_TIME, "");
143 setenv ("LC_NUMERIC", "non-existing-locale", 1);
144 setlocale (LC_NUMERIC, "");
145 puts ("test `gettext' with LC_ALL set");
146 /* This is the name of the existing domain with a catalog for the
147 LC_MESSAGES category. */
148 textdomain ("existing-domain");
149 if (positive_gettext_test () != 0)
151 puts ("FAILED");
152 result = 1;
154 /* This is the name of a non-existing domain with a catalog for the
155 LC_MESSAGES category. We leave this value set for the `dgettext'
156 and `dcgettext' tests. */
157 textdomain ("non-existing-domain");
158 puts ("test `gettext' with LC_xxx deciding");
159 if (negative_gettext_test () != 0)
161 puts ("FAILED");
162 result = 1;
164 puts ("test `dgettext' with LC_xxx deciding");
165 if (positive_dgettext_test ("existing-domain") != 0)
167 puts ("FAILED");
168 result = 1;
170 puts ("test `dcgettext' with category == LC_MESSAGES");
171 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
173 puts ("FAILED");
174 result = 1;
176 /* Try a different category. For this we also switch the domain. */
177 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
178 if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
180 puts ("FAILED");
181 result = 1;
183 /* This time use a category for which there is no catalog. */
184 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
185 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
187 puts ("FAILED");
188 result = 1;
191 /* Now the same tests with LANG deciding. */
192 unsetenv ("LC_MESSAGES");
193 setenv ("LANG", "existing-locale", 1);
194 setlocale (LC_ALL, "");
195 /* This is the name of the existing domain with a catalog for the
196 LC_MESSAGES category. */
197 textdomain ("existing-domain");
198 puts ("test `gettext' with LANG set");
199 if (positive_gettext_test () != 0)
201 puts ("FAILED");
202 result = 1;
204 /* This is the name of a non-existing domain with a catalog for the
205 LC_MESSAGES category. We leave this value set for the `dgettext'
206 and `dcgettext' tests. */
207 textdomain ("non-existing-domain");
208 puts ("test `gettext' with LANG set");
209 if (negative_gettext_test () != 0)
211 puts ("FAILED");
212 result = 1;
214 puts ("test `dgettext' with LANG set");
215 if (positive_dgettext_test ("existing-domain") != 0)
217 puts ("FAILED");
218 result = 1;
221 setlocale (LC_ALL, "C");
223 return result;
227 static int
228 positive_gettext_test (void)
230 size_t cnt;
231 int result = 0;
233 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
235 const char *found = gettext (msgs[cnt].msgid);
237 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
239 /* Oops, shouldn't happen. */
240 printf ("\
241 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
242 msgs[cnt].msgid, found, msgs[cnt].msgstr);
243 result = 1;
247 return result;
251 static int
252 negative_gettext_test (void)
254 size_t cnt;
255 int result = 0;
257 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
259 const char *found = gettext (msgs[cnt].msgid);
261 if (found != msgs[cnt].msgid)
263 /* Oops, shouldn't happen. */
264 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
265 result = 1;
269 return result;
273 static int
274 positive_dgettext_test (const char *domain)
276 size_t cnt;
277 int result = 0;
279 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
281 const char *found = dgettext (domain, msgs[cnt].msgid);
283 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
285 /* Oops, shouldn't happen. */
286 printf ("\
287 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
288 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
289 result = 1;
293 return result;
297 static int
298 positive_dcgettext_test (const char *domain, int category)
300 size_t cnt;
301 int result = 0;
303 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
305 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
307 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
309 /* Oops, shouldn't happen. */
310 printf ("\
311 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
312 domain, msgs[cnt].msgid, catname[category], found,
313 msgs[cnt].msgstr);
314 result = 1;
318 return result;
322 static int
323 negative_dcgettext_test (const char *domain, int category)
325 size_t cnt;
326 int result = 0;
328 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
330 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
332 if (found != msgs[cnt].msgid)
334 /* Oops, shouldn't happen. */
335 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
336 domain, msgs[cnt].msgid, catname[category]);
337 result = 1;
341 return result;