| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test of the gettext functions. | 
|  | 2 | Copyright (C) 2000-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. | 
|  | 5 |  | 
|  | 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. | 
|  | 10 |  | 
|  | 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. | 
|  | 15 |  | 
|  | 16 | You should have received a copy of the GNU Lesser General Public | 
|  | 17 | License along with the GNU C Library; if not, see | 
|  | 18 | <http://www.gnu.org/licenses/>.  */ | 
|  | 19 |  | 
|  | 20 | #include <libintl.h> | 
|  | 21 | #include <locale.h> | 
|  | 22 | #include <mcheck.h> | 
|  | 23 | #include <stdio.h> | 
|  | 24 | #include <stdlib.h> | 
|  | 25 | #include <string.h> | 
|  | 26 | #include <error.h> | 
|  | 27 | #include <errno.h> | 
|  | 28 |  | 
|  | 29 |  | 
|  | 30 | const struct | 
|  | 31 | { | 
|  | 32 | const char *msgid; | 
|  | 33 | const char *msgstr; | 
|  | 34 | } msgs[] = | 
|  | 35 | { | 
|  | 36 | #define INPUT(Str) { Str, | 
|  | 37 | #define OUTPUT(Str) Str }, | 
|  | 38 | #include TESTSTRS_H | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | const char *catname[] = | 
|  | 42 | { | 
|  | 43 | [LC_MESSAGES] = "LC_MESSAGES", | 
|  | 44 | [LC_TIME] = "LC_TIME", | 
|  | 45 | [LC_NUMERIC] = "LC_NUMERIC" | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 |  | 
|  | 49 | static int positive_gettext_test (void); | 
|  | 50 | static int negative_gettext_test (void); | 
|  | 51 | static int positive_dgettext_test (const char *domain); | 
|  | 52 | static int positive_dcgettext_test (const char *domain, int category); | 
|  | 53 | static int negative_dcgettext_test (const char *domain, int category); | 
|  | 54 |  | 
|  | 55 |  | 
|  | 56 | #define check_setlocale(cat, name) do {					      \ | 
|  | 57 | if (setlocale (cat, name) == NULL)					      \ | 
|  | 58 | {									      \ | 
|  | 59 | printf ("%s:%u: setlocale (%s, \"%s\"): %m\n",			      \ | 
|  | 60 | __FILE__, __LINE__, #cat, name);			      \ | 
|  | 61 | result = 1;							      \ | 
|  | 62 | }									      \ | 
|  | 63 | } while (0) | 
|  | 64 |  | 
|  | 65 | int | 
|  | 66 | main (int argc, char *argv[]) | 
|  | 67 | { | 
|  | 68 | int result = 0; | 
|  | 69 |  | 
|  | 70 | /* For debugging.  */ | 
|  | 71 | mtrace (); | 
|  | 72 |  | 
|  | 73 | /* This is the place where the .mo files are placed.  */ | 
|  | 74 | if (argc > 1) | 
|  | 75 | { | 
|  | 76 | bindtextdomain ("existing-domain", argv[1]); | 
|  | 77 | bindtextdomain ("existing-time-domain", argv[1]); | 
|  | 78 | bindtextdomain ("non-existing-domain", argv[1]); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | /* The locale the catalog is created for is "existing-category".  Now | 
|  | 82 | set the various variables in question to this value and run the | 
|  | 83 | test.  */ | 
|  | 84 | setenv ("LANGUAGE", "existing-locale", 1); | 
|  | 85 | setenv ("LC_ALL", "non-existing-locale", 1); | 
|  | 86 | setenv ("LC_MESSAGES", "non-existing-locale", 1); | 
|  | 87 | setenv ("LC_CTYPE", "non-existing-locale", 1); | 
|  | 88 | setenv ("LANG", "non-existing-locale", 1); | 
|  | 89 | check_setlocale (LC_CTYPE, "de_DE.UTF-8"); | 
|  | 90 | check_setlocale (LC_MESSAGES, "de_DE.UTF-8"); | 
|  | 91 | unsetenv ("OUTPUT_CHARSET"); | 
|  | 92 | /* This is the name of the existing domain with a catalog for the | 
|  | 93 | LC_MESSAGES category.  */ | 
|  | 94 | textdomain ("existing-domain"); | 
|  | 95 | puts ("test `gettext' with LANGUAGE set"); | 
|  | 96 | if (positive_gettext_test () != 0) | 
|  | 97 | { | 
|  | 98 | puts ("FAILED"); | 
|  | 99 | result = 1; | 
|  | 100 | } | 
|  | 101 | /* This is the name of a non-existing domain with a catalog for the | 
|  | 102 | LC_MESSAGES category.  We leave this value set for the `dgettext' | 
|  | 103 | and `dcgettext' tests.  */ | 
|  | 104 | textdomain ("non-existing-domain"); | 
|  | 105 | puts ("test `gettext' with LANGUAGE set"); | 
|  | 106 | if (negative_gettext_test () != 0) | 
|  | 107 | { | 
|  | 108 | puts ("FAILED"); | 
|  | 109 | result = 1; | 
|  | 110 | } | 
|  | 111 | puts ("test `dgettext' with LANGUAGE set"); | 
|  | 112 | if (positive_dgettext_test ("existing-domain") != 0) | 
|  | 113 | { | 
|  | 114 | puts ("FAILED"); | 
|  | 115 | result = 1; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | /* Now the same tests with LC_ALL deciding.  */ | 
|  | 119 | unsetenv ("LANGUAGE"); | 
|  | 120 | setenv ("LC_ALL", "existing-locale", 1); | 
|  | 121 | check_setlocale (LC_ALL, ""); | 
|  | 122 | puts ("test `gettext' with LC_ALL set"); | 
|  | 123 | /* This is the name of the existing domain with a catalog for the | 
|  | 124 | LC_MESSAGES category.  */ | 
|  | 125 | textdomain ("existing-domain"); | 
|  | 126 | if (positive_gettext_test () != 0) | 
|  | 127 | { | 
|  | 128 | puts ("FAILED"); | 
|  | 129 | result = 1; | 
|  | 130 | } | 
|  | 131 | /* This is the name of a non-existing domain with a catalog for the | 
|  | 132 | LC_MESSAGES category.  We leave this value set for the `dgettext' | 
|  | 133 | and `dcgettext' tests.  */ | 
|  | 134 | textdomain ("non-existing-domain"); | 
|  | 135 | puts ("test `gettext' with LC_ALL deciding"); | 
|  | 136 | if (negative_gettext_test () != 0) | 
|  | 137 | { | 
|  | 138 | puts ("FAILED"); | 
|  | 139 | result = 1; | 
|  | 140 | } | 
|  | 141 | puts ("test `dgettext' with LC_ALL deciding"); | 
|  | 142 | if (positive_dgettext_test ("existing-domain") != 0) | 
|  | 143 | { | 
|  | 144 | puts ("FAILED"); | 
|  | 145 | result = 1; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | /* Now the same tests with LC_MESSAGES deciding.  */ | 
|  | 149 | unsetenv ("LC_ALL"); | 
|  | 150 | setenv ("LC_MESSAGES", "existing-locale", 1); | 
|  | 151 | check_setlocale (LC_MESSAGES, ""); | 
|  | 152 | setenv ("LC_TIME", "existing-locale", 1); | 
|  | 153 | check_setlocale (LC_TIME, ""); | 
|  | 154 | setenv ("LC_NUMERIC", "non-existing-locale", 1); | 
|  | 155 | char *what = setlocale (LC_NUMERIC, ""); | 
|  | 156 | if (what != NULL) | 
|  | 157 | { | 
|  | 158 | printf ("setlocale succeeded (%s), expected failure\n", what); | 
|  | 159 | result = 1; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | puts ("test `gettext' with LC_MESSAGES set"); | 
|  | 163 | /* This is the name of the existing domain with a catalog for the | 
|  | 164 | LC_MESSAGES category.  */ | 
|  | 165 | textdomain ("existing-domain"); | 
|  | 166 | if (positive_gettext_test () != 0) | 
|  | 167 | { | 
|  | 168 | puts ("FAILED"); | 
|  | 169 | result = 1; | 
|  | 170 | } | 
|  | 171 | /* This is the name of a non-existing domain with a catalog for the | 
|  | 172 | LC_MESSAGES category.  We leave this value set for the `dgettext' | 
|  | 173 | and `dcgettext' tests.  */ | 
|  | 174 | textdomain ("non-existing-domain"); | 
|  | 175 | puts ("test `gettext' with LC_MESSAGES deciding"); | 
|  | 176 | if (negative_gettext_test () != 0) | 
|  | 177 | { | 
|  | 178 | puts ("FAILED"); | 
|  | 179 | result = 1; | 
|  | 180 | } | 
|  | 181 | puts ("test `dgettext' with LC_MESSAGES deciding"); | 
|  | 182 | if (positive_dgettext_test ("existing-domain") != 0) | 
|  | 183 | { | 
|  | 184 | puts ("FAILED"); | 
|  | 185 | result = 1; | 
|  | 186 | } | 
|  | 187 | puts ("test `dcgettext' with category == LC_MESSAGES"); | 
|  | 188 | if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0) | 
|  | 189 | { | 
|  | 190 | puts ("FAILED"); | 
|  | 191 | result = 1; | 
|  | 192 | } | 
|  | 193 | /* Try a different category.  For this we also switch the domain.  */ | 
|  | 194 | puts ("test `dcgettext' with LANGUAGE == LC_TIME"); | 
|  | 195 | if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0) | 
|  | 196 | { | 
|  | 197 | puts ("FAILED"); | 
|  | 198 | result = 1; | 
|  | 199 | } | 
|  | 200 | /* This time use a category for which there is no catalog.  */ | 
|  | 201 | puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC"); | 
|  | 202 | if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0) | 
|  | 203 | { | 
|  | 204 | puts ("FAILED"); | 
|  | 205 | result = 1; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | /* Now the same tests with LANG deciding.  */ | 
|  | 209 | unsetenv ("LC_MESSAGES"); | 
|  | 210 | unsetenv ("LC_CTYPE"); | 
|  | 211 | unsetenv ("LC_TIME"); | 
|  | 212 | unsetenv ("LC_NUMERIC"); | 
|  | 213 | setenv ("LANG", "existing-locale", 1); | 
|  | 214 | check_setlocale (LC_ALL, ""); | 
|  | 215 | /* This is the name of the existing domain with a catalog for the | 
|  | 216 | LC_MESSAGES category.  */ | 
|  | 217 | textdomain ("existing-domain"); | 
|  | 218 | puts ("test `gettext' with LANG set"); | 
|  | 219 | if (positive_gettext_test () != 0) | 
|  | 220 | { | 
|  | 221 | puts ("FAILED"); | 
|  | 222 | result = 1; | 
|  | 223 | } | 
|  | 224 | /* This is the name of a non-existing domain with a catalog for the | 
|  | 225 | LC_MESSAGES category.  We leave this value set for the `dgettext' | 
|  | 226 | and `dcgettext' tests.  */ | 
|  | 227 | textdomain ("non-existing-domain"); | 
|  | 228 | puts ("test `gettext' with LANG set"); | 
|  | 229 | if (negative_gettext_test () != 0) | 
|  | 230 | { | 
|  | 231 | puts ("FAILED"); | 
|  | 232 | result = 1; | 
|  | 233 | } | 
|  | 234 | puts ("test `dgettext' with LANG set"); | 
|  | 235 | if (positive_dgettext_test ("existing-domain") != 0) | 
|  | 236 | { | 
|  | 237 | puts ("FAILED"); | 
|  | 238 | result = 1; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | check_setlocale (LC_ALL, "C"); | 
|  | 242 |  | 
|  | 243 | return result; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 |  | 
|  | 247 | static int | 
|  | 248 | positive_gettext_test (void) | 
|  | 249 | { | 
|  | 250 | size_t cnt; | 
|  | 251 | int result = 0; | 
|  | 252 |  | 
|  | 253 | for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt) | 
|  | 254 | { | 
|  | 255 | const char *found = gettext (msgs[cnt].msgid); | 
|  | 256 |  | 
|  | 257 | if (found == NULL | 
|  | 258 | || (msgs[cnt].msgstr[0] != '\0' | 
|  | 259 | && strcmp (found, msgs[cnt].msgstr) != 0)) | 
|  | 260 | { | 
|  | 261 | /* Oops, shouldn't happen.  */ | 
|  | 262 | printf ("\ | 
|  | 263 | gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n", | 
|  | 264 | msgs[cnt].msgid, found, msgs[cnt].msgstr); | 
|  | 265 | result = 1; | 
|  | 266 | } | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | return result; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 |  | 
|  | 273 | static int | 
|  | 274 | negative_gettext_test (void) | 
|  | 275 | { | 
|  | 276 | size_t cnt; | 
|  | 277 | int result = 0; | 
|  | 278 |  | 
|  | 279 | for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt) | 
|  | 280 | { | 
|  | 281 | const char *found = gettext (msgs[cnt].msgid); | 
|  | 282 |  | 
|  | 283 | if (found != msgs[cnt].msgid) | 
|  | 284 | { | 
|  | 285 | /* Oops, shouldn't happen.  */ | 
|  | 286 | printf ("  gettext (\"%s\") failed\n", msgs[cnt].msgid); | 
|  | 287 | result = 1; | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | return result; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 |  | 
|  | 295 | static int | 
|  | 296 | positive_dgettext_test (const char *domain) | 
|  | 297 | { | 
|  | 298 | size_t cnt; | 
|  | 299 | int result = 0; | 
|  | 300 |  | 
|  | 301 | for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt) | 
|  | 302 | { | 
|  | 303 | const char *found = dgettext (domain, msgs[cnt].msgid); | 
|  | 304 |  | 
|  | 305 | if (found == NULL | 
|  | 306 | || (msgs[cnt].msgstr[0] != '\0' | 
|  | 307 | && strcmp (found, msgs[cnt].msgstr) != 0)) | 
|  | 308 | { | 
|  | 309 | /* Oops, shouldn't happen.  */ | 
|  | 310 | printf ("\ | 
|  | 311 | dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n", | 
|  | 312 | domain, msgs[cnt].msgid, found, msgs[cnt].msgstr); | 
|  | 313 | result = 1; | 
|  | 314 | } | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | return result; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 |  | 
|  | 321 | static int | 
|  | 322 | positive_dcgettext_test (const char *domain, int category) | 
|  | 323 | { | 
|  | 324 | size_t cnt; | 
|  | 325 | int result = 0; | 
|  | 326 |  | 
|  | 327 | for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt) | 
|  | 328 | { | 
|  | 329 | const char *found = dcgettext (domain, msgs[cnt].msgid, category); | 
|  | 330 |  | 
|  | 331 | if (found == NULL | 
|  | 332 | || (msgs[cnt].msgstr[0] != '\0' | 
|  | 333 | && strcmp (found, msgs[cnt].msgstr) != 0)) | 
|  | 334 | { | 
|  | 335 | /* Oops, shouldn't happen.  */ | 
|  | 336 | printf ("\ | 
|  | 337 | dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n", | 
|  | 338 | domain, msgs[cnt].msgid, catname[category], found, | 
|  | 339 | msgs[cnt].msgstr); | 
|  | 340 | result = 1; | 
|  | 341 | } | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | return result; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 |  | 
|  | 348 | static int | 
|  | 349 | negative_dcgettext_test (const char *domain, int category) | 
|  | 350 | { | 
|  | 351 | size_t cnt; | 
|  | 352 | int result = 0; | 
|  | 353 |  | 
|  | 354 | for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt) | 
|  | 355 | { | 
|  | 356 | const char *found = dcgettext (domain, msgs[cnt].msgid, category); | 
|  | 357 |  | 
|  | 358 | if (found != msgs[cnt].msgid) | 
|  | 359 | { | 
|  | 360 | /* Oops, shouldn't happen.  */ | 
|  | 361 | printf ("  dcgettext (\"%s\", \"%s\", %s) failed\n", | 
|  | 362 | domain, msgs[cnt].msgid, catname[category]); | 
|  | 363 | result = 1; | 
|  | 364 | } | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | return result; | 
|  | 368 | } |