(modules-names): Strip before using it.
[glibc.git] / intl / tst-gettext.c
blobb897d715826e7c69421b8d6714db96eaf1e7f904
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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
26 const struct
28 const char *msgid;
29 const char *msgstr;
30 } msgs[] =
32 #define INPUT(Str) { Str,
33 #define OUTPUT(Str) Str },
34 #include TESTSTRS_H
37 const char *catname[] =
39 [LC_MESSAGES] = "LC_MESSAGES",
40 [LC_TIME] = "LC_TIME",
41 [LC_NUMERIC] = "LC_NUMERIC"
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);
52 int
53 main (int argc, char *argv[])
55 int result = 0;
57 /* This is the place where the .mo files are placed. */
58 if (argc > 1)
60 bindtextdomain ("existing-domain", argv[1]);
61 bindtextdomain ("existing-time-domain", argv[1]);
62 bindtextdomain ("non-existing-domain", argv[1]);
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 ("LC_CTYPE", "non-existing-locale", 1);
72 setenv ("LANG", "non-existing-locale", 1);
73 unsetenv ("OUTPUT_CHARSET");
74 /* This is the name of the existing domain with a catalog for the
75 LC_MESSAGES category. */
76 textdomain ("existing-domain");
77 puts ("test `gettext' with LANGUAGE set");
78 if (positive_gettext_test () != 0)
80 puts ("FAILED");
81 result = 1;
83 /* This is the name of a non-existing domain with a catalog for the
84 LC_MESSAGES category. We leave this value set for the `dgettext'
85 and `dcgettext' tests. */
86 textdomain ("non-existing-domain");
87 puts ("test `gettext' with LANGUAGE set");
88 if (negative_gettext_test () != 0)
90 puts ("FAILED");
91 result = 1;
93 puts ("test `dgettext' with LANGUAGE set");
94 if (positive_dgettext_test ("existing-domain") != 0)
96 puts ("FAILED");
97 result = 1;
100 /* Now the same tests with LC_ALL deciding. */
101 unsetenv ("LANGUAGE");
102 setenv ("LC_ALL", "existing-locale", 1);
103 puts ("test `gettext' with LC_ALL set");
104 /* This is the name of the existing domain with a catalog for the
105 LC_MESSAGES category. */
106 textdomain ("existing-domain");
107 if (positive_gettext_test () != 0)
109 puts ("FAILED");
110 result = 1;
112 /* This is the name of a non-existing domain with a catalog for the
113 LC_MESSAGES category. We leave this value set for the `dgettext'
114 and `dcgettext' tests. */
115 textdomain ("non-existing-domain");
116 puts ("test `gettext' with LANGUAGE set");
117 if (negative_gettext_test () != 0)
119 puts ("FAILED");
120 result = 1;
122 puts ("test `dgettext' with LANGUAGE set");
123 if (positive_dgettext_test ("existing-domain") != 0)
125 puts ("FAILED");
126 result = 1;
129 /* Now the same tests with LC_MESSAGES deciding. */
130 unsetenv ("LC_ALL");
131 setenv ("LC_MESSAGES", "existing-locale", 1);
132 setenv ("LC_TIME", "existing-locale", 1);
133 setenv ("LC_NUMERIC", "non-existing-locale", 1);
134 puts ("test `gettext' with LC_ALL set");
135 /* This is the name of the existing domain with a catalog for the
136 LC_MESSAGES category. */
137 textdomain ("existing-domain");
138 if (positive_gettext_test () != 0)
140 puts ("FAILED");
141 result = 1;
143 /* This is the name of a non-existing domain with a catalog for the
144 LC_MESSAGES category. We leave this value set for the `dgettext'
145 and `dcgettext' tests. */
146 textdomain ("non-existing-domain");
147 puts ("test `gettext' with LANGUAGE set");
148 if (negative_gettext_test () != 0)
150 puts ("FAILED");
151 result = 1;
153 puts ("test `dgettext' with LANGUAGE set");
154 if (positive_dgettext_test ("existing-domain") != 0)
156 puts ("FAILED");
157 result = 1;
159 puts ("test `dcgettext' with LANGUAGE set (LC_MESSAGES)");
160 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
162 puts ("FAILED");
163 result = 1;
165 /* Try a different category. For this we also switch the domain. */
166 puts ("test `dcgettext' with LANGUAGE set (LC_TIME)");
167 if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
169 puts ("FAILED");
170 result = 1;
172 /* This time use a category for which there is no catalog. */
173 puts ("test `dcgettext' with LANGUAGE set (LC_NUMERIC)");
174 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
176 puts ("FAILED");
177 result = 1;
180 /* Now the same tests with LANG deciding. */
181 unsetenv ("LC_MESSAGES");
182 setenv ("LANG", "existing-locale", 1);
183 /* This is the name of the existing domain with a catalog for the
184 LC_MESSAGES category. */
185 textdomain ("existing-domain");
186 puts ("test `gettext' with LC_ALL set");
187 if (positive_gettext_test () != 0)
189 puts ("FAILED");
190 result = 1;
192 /* This is the name of a non-existing domain with a catalog for the
193 LC_MESSAGES category. We leave this value set for the `dgettext'
194 and `dcgettext' tests. */
195 textdomain ("non-existing-domain");
196 puts ("test `gettext' with LANGUAGE set");
197 if (negative_gettext_test () != 0)
199 puts ("FAILED");
200 result = 1;
202 puts ("test `dgettext' with LANGUAGE set");
203 if (positive_dgettext_test ("existing-domain") != 0)
205 puts ("FAILED");
206 result = 1;
209 return result;
213 static int
214 positive_gettext_test (void)
216 size_t cnt;
217 int result = 0;
219 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
221 const char *found = gettext (msgs[cnt].msgid);
223 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
225 /* Oops, shouldn't happen. */
226 printf (" gettext (\"%s\") failed, returned \"%s\"\n",
227 msgs[cnt].msgid, found);
228 result = 1;
232 return result;
236 static int
237 negative_gettext_test (void)
239 size_t cnt;
240 int result = 0;
242 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
244 const char *found = gettext (msgs[cnt].msgid);
246 if (found != msgs[cnt].msgid)
248 /* Oops, shouldn't happen. */
249 printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
250 result = 1;
254 return result;
258 static int
259 positive_dgettext_test (const char *domain)
261 size_t cnt;
262 int result = 0;
264 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
266 const char *found = dgettext (domain, msgs[cnt].msgid);
268 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
270 /* Oops, shouldn't happen. */
271 printf (" dgettext (\"%s\", \"%s\") failed, returned \"%s\"\n",
272 domain, msgs[cnt].msgid, found);
273 result = 1;
277 return result;
281 static int
282 positive_dcgettext_test (const char *domain, int category)
284 size_t cnt;
285 int result = 0;
287 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
289 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
291 if (found == NULL || strcmp (found, msgs[cnt].msgstr) != 0)
293 /* Oops, shouldn't happen. */
294 printf (" dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\"\n",
295 domain, msgs[cnt].msgid, catname[category], found);
296 result = 1;
300 return result;
304 static int
305 negative_dcgettext_test (const char *domain, int category)
307 size_t cnt;
308 int result = 0;
310 for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
312 const char *found = dcgettext (domain, msgs[cnt].msgid, category);
314 if (found != msgs[cnt].msgid)
316 /* Oops, shouldn't happen. */
317 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
318 domain, msgs[cnt].msgid, catname[category]);
319 result = 1;
323 return result;