Update.
[platform/upstream/glibc.git] / intl / tst-gettext.c
1 /* Test of the gettext functions.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
4
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.
9
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.
14
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.  */
19
20 #include <libintl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25
26 const struct
27 {
28   const char *msgid;
29   const char *msgstr;
30 } msgs[] =
31 {
32 #define INPUT(Str) { Str,
33 #define OUTPUT(Str) Str },
34 #include TESTSTRS_H
35 };
36
37 const char *catname[] =
38 {
39   [LC_MESSAGES] = "LC_MESSAGES",
40   [LC_TIME] = "LC_TIME",
41   [LC_NUMERIC] = "LC_NUMERIC"
42 };
43
44
45 static int positive_gettext_test (void);
46 static int negative_gettext_test (void);
47 static int positive_dgettext_test (const char *domain);
48 static int positive_dcgettext_test (const char *domain, int category);
49 static int negative_dcgettext_test (const char *domain, int category);
50
51
52 int
53 main (int argc, char *argv[])
54 {
55   int result = 0;
56
57   /* This is the place where the .mo files are placed.  */
58   if (argc > 1)
59     {
60       bindtextdomain ("existing-domain", argv[1]);
61       bindtextdomain ("existing-time-domain", argv[1]);
62       bindtextdomain ("non-existing-domain", argv[1]);
63     }
64
65   /* The locale the catalog is created for is "existing-category".  Now
66      set the various variables in question to this value and run the
67      test.  */
68   setenv ("LANGUAGE", "existing-locale", 1);
69   setenv ("LC_ALL", "non-existing-locale", 1);
70   setenv ("LC_MESSAGES", "non-existing-locale", 1);
71   setenv ("LANG", "non-existing-locale", 1);
72   /* This is the name of the existing domain with a catalog for the
73      LC_MESSAGES category.  */
74   textdomain ("existing-domain");
75   puts ("test `gettext' with LANGUAGE set");
76   if (positive_gettext_test () != 0)
77     {
78       puts ("FAILED");
79       result = 1;
80     }
81   /* This is the name of a non-existing domain with a catalog for the
82      LC_MESSAGES category.  We leave this value set for the `dgettext'
83      and `dcgettext' tests.  */
84   textdomain ("non-existing-domain");
85   puts ("test `gettext' with LANGUAGE set");
86   if (negative_gettext_test () != 0)
87     {
88       puts ("FAILED");
89       result = 1;
90     }
91   puts ("test `dgettext' with LANGUAGE set");
92   if (positive_dgettext_test ("existing-domain") != 0)
93     {
94       puts ("FAILED");
95       result = 1;
96     }
97
98   /* Now the same tests with LC_ALL deciding.  */
99   unsetenv ("LANGUAGE");
100   setenv ("LC_ALL", "existing-locale", 1);
101   puts ("test `gettext' with LC_ALL set");
102   /* This is the name of the existing domain with a catalog for the
103      LC_MESSAGES category.  */
104   textdomain ("existing-domain");
105   if (positive_gettext_test () != 0)
106     {
107       puts ("FAILED");
108       result = 1;
109     }
110   /* This is the name of a non-existing domain with a catalog for the
111      LC_MESSAGES category.  We leave this value set for the `dgettext'
112      and `dcgettext' tests.  */
113   textdomain ("non-existing-domain");
114   puts ("test `gettext' with LANGUAGE set");
115   if (negative_gettext_test () != 0)
116     {
117       puts ("FAILED");
118       result = 1;
119     }
120   puts ("test `dgettext' with LANGUAGE set");
121   if (positive_dgettext_test ("existing-domain") != 0)
122     {
123       puts ("FAILED");
124       result = 1;
125     }
126
127   /* Now the same tests with LC_MESSAGES deciding.  */
128   unsetenv ("LC_ALL");
129   setenv ("LC_MESSAGES", "existing-locale", 1);
130   setenv ("LC_TIME", "existing-locale", 1);
131   setenv ("LC_NUMERIC", "non-existing-locale", 1);
132   puts ("test `gettext' with LC_ALL set");
133   /* This is the name of the existing domain with a catalog for the
134      LC_MESSAGES category.  */
135   textdomain ("existing-domain");
136   if (positive_gettext_test () != 0)
137     {
138       puts ("FAILED");
139       result = 1;
140     }
141   /* This is the name of a non-existing domain with a catalog for the
142      LC_MESSAGES category.  We leave this value set for the `dgettext'
143      and `dcgettext' tests.  */
144   textdomain ("non-existing-domain");
145   puts ("test `gettext' with LANGUAGE set");
146   if (negative_gettext_test () != 0)
147     {
148       puts ("FAILED");
149       result = 1;
150     }
151   puts ("test `dgettext' with LANGUAGE set");
152   if (positive_dgettext_test ("existing-domain") != 0)
153     {
154       puts ("FAILED");
155       result = 1;
156     }
157   puts ("test `dcgettext' with LANGUAGE set (LC_MESSAGES)");
158   if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
159     {
160       puts ("FAILED");
161       result = 1;
162     }
163   /* Try a different category.  For this we also switch the domain.  */
164   puts ("test `dcgettext' with LANGUAGE set (LC_TIME)");
165   if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
166     {
167       puts ("FAILED");
168       result = 1;
169     }
170   /* This time use a category for which there is no catalog.  */
171   puts ("test `dcgettext' with LANGUAGE set (LC_NUMERIC)");
172   if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
173     {
174       puts ("FAILED");
175       result = 1;
176     }
177
178   /* Now the same tests with LANG deciding.  */
179   unsetenv ("LC_MESSAGES");
180   setenv ("LANG", "existing-locale", 1);
181   /* This is the name of the existing domain with a catalog for the
182      LC_MESSAGES category.  */
183   textdomain ("existing-domain");
184   puts ("test `gettext' with LC_ALL set");
185   if (positive_gettext_test () != 0)
186     {
187       puts ("FAILED");
188       result = 1;
189     }
190   /* This is the name of a non-existing domain with a catalog for the
191      LC_MESSAGES category.  We leave this value set for the `dgettext'
192      and `dcgettext' tests.  */
193   textdomain ("non-existing-domain");
194   puts ("test `gettext' with LANGUAGE set");
195   if (negative_gettext_test () != 0)
196     {
197       puts ("FAILED");
198       result = 1;
199     }
200   puts ("test `dgettext' with LANGUAGE set");
201   if (positive_dgettext_test ("existing-domain") != 0)
202     {
203       puts ("FAILED");
204       result = 1;
205     }
206
207   return result;
208 }
209
210
211 static int
212 positive_gettext_test (void)
213 {
214   size_t cnt;
215   int result = 0;
216
217   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
218     {
219       const char *found = gettext (msgs[cnt].msgid);
220
221       if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
222         {
223           /* Oops, shouldn't happen.  */
224           printf ("  gettext (\"%s\") failed, returned \"%s\"\n",
225                   msgs[cnt].msgid, found);
226           result = 1;
227         }
228     }
229
230   return result;
231 }
232
233
234 static int
235 negative_gettext_test (void)
236 {
237   size_t cnt;
238   int result = 0;
239
240   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
241     {
242       const char *found = gettext (msgs[cnt].msgid);
243
244       if (found != msgs[cnt].msgid)
245         {
246           /* Oops, shouldn't happen.  */
247           printf ("  gettext (\"%s\") failed\n", msgs[cnt].msgid);
248           result = 1;
249         }
250     }
251
252   return result;
253 }
254
255
256 static int
257 positive_dgettext_test (const char *domain)
258 {
259   size_t cnt;
260   int result = 0;
261
262   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
263     {
264       const char *found = dgettext (domain, msgs[cnt].msgid);
265
266       if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
267         {
268           /* Oops, shouldn't happen.  */
269           printf ("  dgettext (\"%s\", \"%s\") failed, returned \"%s\"\n",
270                   domain, msgs[cnt].msgid, found);
271           result = 1;
272         }
273     }
274
275   return result;
276 }
277
278
279 static int
280 positive_dcgettext_test (const char *domain, int category)
281 {
282   size_t cnt;
283   int result = 0;
284
285   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
286     {
287       const char *found = dcgettext (domain, msgs[cnt].msgid, category);
288
289       if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
290         {
291           /* Oops, shouldn't happen.  */
292           printf ("  dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\"\n",
293                   domain, msgs[cnt].msgid, catname[category], found);
294           result = 1;
295         }
296     }
297
298   return result;
299 }
300
301
302 static int
303 negative_dcgettext_test (const char *domain, int category)
304 {
305   size_t cnt;
306   int result = 0;
307
308   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
309     {
310       const char *found = dcgettext (domain, msgs[cnt].msgid, category);
311
312       if (found != msgs[cnt].msgid)
313         {
314           /* Oops, shouldn't happen.  */
315           printf ("  dcgettext (\"%s\", \"%s\", %s) failed\n",
316                   domain, msgs[cnt].msgid, catname[category]);
317           result = 1;
318         }
319     }
320
321   return result;
322 }