1 /* Test of the gettext functions.
2 Copyright (C) 2000, 2002, 2004 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
37 #define INPUT(Str) { Str,
38 #define OUTPUT(Str) Str },
42 const char *catname
[] =
44 [LC_MESSAGES
] = "LC_MESSAGES",
45 [LC_TIME
] = "LC_TIME",
46 [LC_NUMERIC
] = "LC_NUMERIC"
50 static int positive_gettext_test (void);
51 static int negative_gettext_test (void);
52 static int positive_dgettext_test (const char *domain
);
53 static int positive_dcgettext_test (const char *domain
, int category
);
54 static int negative_dcgettext_test (const char *domain
, int category
);
57 #define check_setlocale(cat, name) do { \
58 if (setlocale (cat, name) == NULL) \
60 printf ("%s:%u: setlocale (%s, \"%s\"): %m\n", \
61 __FILE__, __LINE__, #cat, name); \
67 main (int argc
, char *argv
[])
74 /* This is the place where the .mo files are placed. */
77 bindtextdomain ("existing-domain", argv
[1]);
78 bindtextdomain ("existing-time-domain", argv
[1]);
79 bindtextdomain ("non-existing-domain", argv
[1]);
82 /* The locale the catalog is created for is "existing-category". Now
83 set the various variables in question to this value and run the
85 setenv ("LANGUAGE", "existing-locale", 1);
86 setenv ("LC_ALL", "non-existing-locale", 1);
87 setenv ("LC_MESSAGES", "non-existing-locale", 1);
88 setenv ("LC_CTYPE", "non-existing-locale", 1);
89 setenv ("LANG", "non-existing-locale", 1);
90 check_setlocale (LC_CTYPE
, "de_DE.UTF-8");
91 check_setlocale (LC_MESSAGES
, "de_DE.UTF-8");
92 unsetenv ("OUTPUT_CHARSET");
93 /* This is the name of the existing domain with a catalog for the
94 LC_MESSAGES category. */
95 textdomain ("existing-domain");
96 puts ("test `gettext' with LANGUAGE set");
97 if (positive_gettext_test () != 0)
102 /* This is the name of a non-existing domain with a catalog for the
103 LC_MESSAGES category. We leave this value set for the `dgettext'
104 and `dcgettext' tests. */
105 textdomain ("non-existing-domain");
106 puts ("test `gettext' with LANGUAGE set");
107 if (negative_gettext_test () != 0)
112 puts ("test `dgettext' with LANGUAGE set");
113 if (positive_dgettext_test ("existing-domain") != 0)
119 /* Now the same tests with LC_ALL deciding. */
120 unsetenv ("LANGUAGE");
121 setenv ("LC_ALL", "existing-locale", 1);
122 check_setlocale (LC_ALL
, "");
123 puts ("test `gettext' with LC_ALL set");
124 /* This is the name of the existing domain with a catalog for the
125 LC_MESSAGES category. */
126 textdomain ("existing-domain");
127 if (positive_gettext_test () != 0)
132 /* This is the name of a non-existing domain with a catalog for the
133 LC_MESSAGES category. We leave this value set for the `dgettext'
134 and `dcgettext' tests. */
135 textdomain ("non-existing-domain");
136 puts ("test `gettext' with LC_ALL deciding");
137 if (negative_gettext_test () != 0)
142 puts ("test `dgettext' with LC_ALL deciding");
143 if (positive_dgettext_test ("existing-domain") != 0)
149 /* Now the same tests with LC_MESSAGES deciding. */
151 setenv ("LC_MESSAGES", "existing-locale", 1);
152 check_setlocale (LC_MESSAGES
, "");
153 setenv ("LC_TIME", "existing-locale", 1);
154 check_setlocale (LC_TIME
, "");
155 setenv ("LC_NUMERIC", "non-existing-locale", 1);
156 char *what
= setlocale (LC_NUMERIC
, "");
159 printf ("setlocale succeeded (%s), expected failure\n", what
);
163 puts ("test `gettext' with LC_MESSAGES set");
164 /* This is the name of the existing domain with a catalog for the
165 LC_MESSAGES category. */
166 textdomain ("existing-domain");
167 if (positive_gettext_test () != 0)
172 /* This is the name of a non-existing domain with a catalog for the
173 LC_MESSAGES category. We leave this value set for the `dgettext'
174 and `dcgettext' tests. */
175 textdomain ("non-existing-domain");
176 puts ("test `gettext' with LC_MESSAGES deciding");
177 if (negative_gettext_test () != 0)
182 puts ("test `dgettext' with LC_MESSAGES deciding");
183 if (positive_dgettext_test ("existing-domain") != 0)
188 puts ("test `dcgettext' with category == LC_MESSAGES");
189 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES
) != 0)
194 /* Try a different category. For this we also switch the domain. */
195 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
196 if (positive_dcgettext_test ("existing-time-domain", LC_TIME
) != 0)
201 /* This time use a category for which there is no catalog. */
202 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
203 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC
) != 0)
209 /* Now the same tests with LANG deciding. */
210 unsetenv ("LC_MESSAGES");
211 unsetenv ("LC_CTYPE");
212 unsetenv ("LC_TIME");
213 unsetenv ("LC_NUMERIC");
214 setenv ("LANG", "existing-locale", 1);
215 check_setlocale (LC_ALL
, "");
216 /* This is the name of the existing domain with a catalog for the
217 LC_MESSAGES category. */
218 textdomain ("existing-domain");
219 puts ("test `gettext' with LANG set");
220 if (positive_gettext_test () != 0)
225 /* This is the name of a non-existing domain with a catalog for the
226 LC_MESSAGES category. We leave this value set for the `dgettext'
227 and `dcgettext' tests. */
228 textdomain ("non-existing-domain");
229 puts ("test `gettext' with LANG set");
230 if (negative_gettext_test () != 0)
235 puts ("test `dgettext' with LANG set");
236 if (positive_dgettext_test ("existing-domain") != 0)
242 check_setlocale (LC_ALL
, "C");
249 positive_gettext_test (void)
254 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
256 const char *found
= gettext (msgs
[cnt
].msgid
);
259 || (msgs
[cnt
].msgstr
[0] != '\0'
260 && strcmp (found
, msgs
[cnt
].msgstr
) != 0))
262 /* Oops, shouldn't happen. */
264 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
265 msgs
[cnt
].msgid
, found
, msgs
[cnt
].msgstr
);
275 negative_gettext_test (void)
280 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
282 const char *found
= gettext (msgs
[cnt
].msgid
);
284 if (found
!= msgs
[cnt
].msgid
)
286 /* Oops, shouldn't happen. */
287 printf (" gettext (\"%s\") failed\n", msgs
[cnt
].msgid
);
297 positive_dgettext_test (const char *domain
)
302 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
304 const char *found
= dgettext (domain
, msgs
[cnt
].msgid
);
307 || (msgs
[cnt
].msgstr
[0] != '\0'
308 && strcmp (found
, msgs
[cnt
].msgstr
) != 0))
310 /* Oops, shouldn't happen. */
312 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
313 domain
, msgs
[cnt
].msgid
, found
, msgs
[cnt
].msgstr
);
323 positive_dcgettext_test (const char *domain
, int category
)
328 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
330 const char *found
= dcgettext (domain
, msgs
[cnt
].msgid
, category
);
333 || (msgs
[cnt
].msgstr
[0] != '\0'
334 && strcmp (found
, msgs
[cnt
].msgstr
) != 0))
336 /* Oops, shouldn't happen. */
338 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
339 domain
, msgs
[cnt
].msgid
, catname
[category
], found
,
350 negative_dcgettext_test (const char *domain
, int category
)
355 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
357 const char *found
= dcgettext (domain
, msgs
[cnt
].msgid
, category
);
359 if (found
!= msgs
[cnt
].msgid
)
361 /* Oops, shouldn't happen. */
362 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
363 domain
, msgs
[cnt
].msgid
, catname
[category
]);