1 /* Test of the gettext functions.
2 Copyright (C) 2000 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
35 #define INPUT(Str) { Str,
36 #define OUTPUT(Str) Str },
40 const char *catname
[] =
42 [LC_MESSAGES
] = "LC_MESSAGES",
43 [LC_TIME
] = "LC_TIME",
44 [LC_NUMERIC
] = "LC_NUMERIC"
48 static int positive_gettext_test (void);
49 static int negative_gettext_test (void);
50 static int positive_dgettext_test (const char *domain
);
51 static int positive_dcgettext_test (const char *domain
, int category
);
52 static int negative_dcgettext_test (const char *domain
, int category
);
56 main (int argc
, char *argv
[])
63 /* This is the place where the .mo files are placed. */
66 bindtextdomain ("existing-domain", argv
[1]);
67 bindtextdomain ("existing-time-domain", argv
[1]);
68 bindtextdomain ("non-existing-domain", argv
[1]);
71 /* The locale the catalog is created for is "existing-category". Now
72 set the various variables in question to this value and run the
74 setenv ("LANGUAGE", "existing-locale", 1);
75 setenv ("LC_ALL", "non-existing-locale", 1);
76 setenv ("LC_MESSAGES", "non-existing-locale", 1);
77 setenv ("LC_CTYPE", "non-existing-locale", 1);
78 setenv ("LANG", "non-existing-locale", 1);
79 setlocale (LC_CTYPE
, "de_DE.ISO-8859-1");
80 setlocale (LC_MESSAGES
, "de_DE.ISO-8859-1");
81 unsetenv ("OUTPUT_CHARSET");
82 /* This is the name of the existing domain with a catalog for the
83 LC_MESSAGES category. */
84 textdomain ("existing-domain");
85 puts ("test `gettext' with LANGUAGE set");
86 if (positive_gettext_test () != 0)
91 /* This is the name of a non-existing domain with a catalog for the
92 LC_MESSAGES category. We leave this value set for the `dgettext'
93 and `dcgettext' tests. */
94 textdomain ("non-existing-domain");
95 puts ("test `gettext' with LANGUAGE set");
96 if (negative_gettext_test () != 0)
101 puts ("test `dgettext' with LANGUAGE set");
102 if (positive_dgettext_test ("existing-domain") != 0)
108 /* Now the same tests with LC_ALL deciding. */
109 unsetenv ("LANGUAGE");
110 setenv ("LC_ALL", "existing-locale", 1);
111 setlocale (LC_ALL
, "");
112 puts ("test `gettext' with LC_ALL set");
113 /* This is the name of the existing domain with a catalog for the
114 LC_MESSAGES category. */
115 textdomain ("existing-domain");
116 if (positive_gettext_test () != 0)
121 /* This is the name of a non-existing domain with a catalog for the
122 LC_MESSAGES category. We leave this value set for the `dgettext'
123 and `dcgettext' tests. */
124 textdomain ("non-existing-domain");
125 puts ("test `gettext' with LC_ALL deciding");
126 if (negative_gettext_test () != 0)
131 puts ("test `dgettext' with LC_ALL deciding");
132 if (positive_dgettext_test ("existing-domain") != 0)
138 /* Now the same tests with LC_MESSAGES deciding. */
140 setenv ("LC_MESSAGES", "existing-locale", 1);
141 setlocale (LC_MESSAGES
, "");
142 setenv ("LC_TIME", "existing-locale", 1);
143 setlocale (LC_TIME
, "");
144 setenv ("LC_NUMERIC", "non-existing-locale", 1);
145 setlocale (LC_NUMERIC
, "");
146 puts ("test `gettext' with LC_ALL set");
147 /* This is the name of the existing domain with a catalog for the
148 LC_MESSAGES category. */
149 textdomain ("existing-domain");
150 if (positive_gettext_test () != 0)
155 /* This is the name of a non-existing domain with a catalog for the
156 LC_MESSAGES category. We leave this value set for the `dgettext'
157 and `dcgettext' tests. */
158 textdomain ("non-existing-domain");
159 puts ("test `gettext' with LC_xxx deciding");
160 if (negative_gettext_test () != 0)
165 puts ("test `dgettext' with LC_xxx deciding");
166 if (positive_dgettext_test ("existing-domain") != 0)
171 puts ("test `dcgettext' with category == LC_MESSAGES");
172 if (positive_dcgettext_test ("existing-domain", LC_MESSAGES
) != 0)
177 /* Try a different category. For this we also switch the domain. */
178 puts ("test `dcgettext' with LANGUAGE == LC_TIME");
179 if (positive_dcgettext_test ("existing-time-domain", LC_TIME
) != 0)
184 /* This time use a category for which there is no catalog. */
185 puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
186 if (negative_dcgettext_test ("existing-domain", LC_NUMERIC
) != 0)
192 /* Now the same tests with LANG deciding. */
193 unsetenv ("LC_MESSAGES");
194 setenv ("LANG", "existing-locale", 1);
195 setlocale (LC_ALL
, "");
196 /* This is the name of the existing domain with a catalog for the
197 LC_MESSAGES category. */
198 textdomain ("existing-domain");
199 puts ("test `gettext' with LANG set");
200 if (positive_gettext_test () != 0)
205 /* This is the name of a non-existing domain with a catalog for the
206 LC_MESSAGES category. We leave this value set for the `dgettext'
207 and `dcgettext' tests. */
208 textdomain ("non-existing-domain");
209 puts ("test `gettext' with LANG set");
210 if (negative_gettext_test () != 0)
215 puts ("test `dgettext' with LANG set");
216 if (positive_dgettext_test ("existing-domain") != 0)
222 setlocale (LC_ALL
, "C");
229 positive_gettext_test (void)
234 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
236 const char *found
= gettext (msgs
[cnt
].msgid
);
238 if (found
== NULL
|| strcmp (found
, msgs
[cnt
].msgstr
) != 0)
240 /* Oops, shouldn't happen. */
242 gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
243 msgs
[cnt
].msgid
, found
, msgs
[cnt
].msgstr
);
253 negative_gettext_test (void)
258 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
260 const char *found
= gettext (msgs
[cnt
].msgid
);
262 if (found
!= msgs
[cnt
].msgid
)
264 /* Oops, shouldn't happen. */
265 printf (" gettext (\"%s\") failed\n", msgs
[cnt
].msgid
);
275 positive_dgettext_test (const char *domain
)
280 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
282 const char *found
= dgettext (domain
, msgs
[cnt
].msgid
);
284 if (found
== NULL
|| strcmp (found
, msgs
[cnt
].msgstr
) != 0)
286 /* Oops, shouldn't happen. */
288 dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
289 domain
, msgs
[cnt
].msgid
, found
, msgs
[cnt
].msgstr
);
299 positive_dcgettext_test (const char *domain
, int category
)
304 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
306 const char *found
= dcgettext (domain
, msgs
[cnt
].msgid
, category
);
308 if (found
== NULL
|| strcmp (found
, msgs
[cnt
].msgstr
) != 0)
310 /* Oops, shouldn't happen. */
312 dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
313 domain
, msgs
[cnt
].msgid
, catname
[category
], found
,
324 negative_dcgettext_test (const char *domain
, int category
)
329 for (cnt
= 0; cnt
< sizeof (msgs
) / sizeof (msgs
[0]); ++cnt
)
331 const char *found
= dcgettext (domain
, msgs
[cnt
].msgid
, category
);
333 if (found
!= msgs
[cnt
].msgid
)
335 /* Oops, shouldn't happen. */
336 printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
337 domain
, msgs
[cnt
].msgid
, catname
[category
]);