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