tst-gettext.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Test of the gettext functions.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <libintl.h>
  16. #include <locale.h>
  17. #include <mcheck.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <error.h>
  22. #include <errno.h>
  23. const struct
  24. {
  25. const char *msgid;
  26. const char *msgstr;
  27. } msgs[] =
  28. {
  29. #define INPUT(Str) { Str,
  30. #define OUTPUT(Str) Str },
  31. #include TESTSTRS_H
  32. };
  33. const char *catname[] =
  34. {
  35. [LC_MESSAGES] = "LC_MESSAGES",
  36. [LC_TIME] = "LC_TIME",
  37. [LC_NUMERIC] = "LC_NUMERIC"
  38. };
  39. static int positive_gettext_test (void);
  40. static int negative_gettext_test (void);
  41. static int positive_dgettext_test (const char *domain);
  42. static int positive_dcgettext_test (const char *domain, int category);
  43. static int negative_dcgettext_test (const char *domain, int category);
  44. #define check_setlocale(cat, name) do { \
  45. if (setlocale (cat, name) == NULL) \
  46. { \
  47. printf ("%s:%u: setlocale (%s, \"%s\"): %m\n", \
  48. __FILE__, __LINE__, #cat, name); \
  49. result = 1; \
  50. } \
  51. } while (0)
  52. int
  53. main (int argc, char *argv[])
  54. {
  55. int result = 0;
  56. /* For debugging. */
  57. mtrace ();
  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. /* 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. check_setlocale (LC_CTYPE, "de_DE.UTF-8");
  74. check_setlocale (LC_MESSAGES, "de_DE.UTF-8");
  75. unsetenv ("OUTPUT_CHARSET");
  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. /* Now the same tests with LC_ALL deciding. */
  102. unsetenv ("LANGUAGE");
  103. setenv ("LC_ALL", "existing-locale", 1);
  104. check_setlocale (LC_ALL, "");
  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 LC_ALL deciding");
  119. if (negative_gettext_test () != 0)
  120. {
  121. puts ("FAILED");
  122. result = 1;
  123. }
  124. puts ("test `dgettext' with LC_ALL deciding");
  125. if (positive_dgettext_test ("existing-domain") != 0)
  126. {
  127. puts ("FAILED");
  128. result = 1;
  129. }
  130. /* Now the same tests with LC_MESSAGES deciding. */
  131. unsetenv ("LC_ALL");
  132. setenv ("LC_MESSAGES", "existing-locale", 1);
  133. check_setlocale (LC_MESSAGES, "");
  134. setenv ("LC_TIME", "existing-locale", 1);
  135. check_setlocale (LC_TIME, "");
  136. setenv ("LC_NUMERIC", "non-existing-locale", 1);
  137. char *what = setlocale (LC_NUMERIC, "");
  138. if (what != NULL)
  139. {
  140. printf ("setlocale succeeded (%s), expected failure\n", what);
  141. result = 1;
  142. }
  143. puts ("test `gettext' with LC_MESSAGES set");
  144. /* This is the name of the existing domain with a catalog for the
  145. LC_MESSAGES category. */
  146. textdomain ("existing-domain");
  147. if (positive_gettext_test () != 0)
  148. {
  149. puts ("FAILED");
  150. result = 1;
  151. }
  152. /* This is the name of a non-existing domain with a catalog for the
  153. LC_MESSAGES category. We leave this value set for the `dgettext'
  154. and `dcgettext' tests. */
  155. textdomain ("non-existing-domain");
  156. puts ("test `gettext' with LC_MESSAGES deciding");
  157. if (negative_gettext_test () != 0)
  158. {
  159. puts ("FAILED");
  160. result = 1;
  161. }
  162. puts ("test `dgettext' with LC_MESSAGES deciding");
  163. if (positive_dgettext_test ("existing-domain") != 0)
  164. {
  165. puts ("FAILED");
  166. result = 1;
  167. }
  168. puts ("test `dcgettext' with category == LC_MESSAGES");
  169. if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
  170. {
  171. puts ("FAILED");
  172. result = 1;
  173. }
  174. /* Try a different category. For this we also switch the domain. */
  175. puts ("test `dcgettext' with LANGUAGE == LC_TIME");
  176. if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
  177. {
  178. puts ("FAILED");
  179. result = 1;
  180. }
  181. /* This time use a category for which there is no catalog. */
  182. puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
  183. if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
  184. {
  185. puts ("FAILED");
  186. result = 1;
  187. }
  188. /* Now the same tests with LANG deciding. */
  189. unsetenv ("LC_MESSAGES");
  190. unsetenv ("LC_CTYPE");
  191. unsetenv ("LC_TIME");
  192. unsetenv ("LC_NUMERIC");
  193. setenv ("LANG", "existing-locale", 1);
  194. check_setlocale (LC_ALL, "");
  195. /* This is the name of the existing domain with a catalog for the
  196. LC_MESSAGES category. */
  197. textdomain ("existing-domain");
  198. puts ("test `gettext' with LANG set");
  199. if (positive_gettext_test () != 0)
  200. {
  201. puts ("FAILED");
  202. result = 1;
  203. }
  204. /* This is the name of a non-existing domain with a catalog for the
  205. LC_MESSAGES category. We leave this value set for the `dgettext'
  206. and `dcgettext' tests. */
  207. textdomain ("non-existing-domain");
  208. puts ("test `gettext' with LANG set");
  209. if (negative_gettext_test () != 0)
  210. {
  211. puts ("FAILED");
  212. result = 1;
  213. }
  214. puts ("test `dgettext' with LANG set");
  215. if (positive_dgettext_test ("existing-domain") != 0)
  216. {
  217. puts ("FAILED");
  218. result = 1;
  219. }
  220. check_setlocale (LC_ALL, "C");
  221. return result;
  222. }
  223. static int
  224. positive_gettext_test (void)
  225. {
  226. size_t cnt;
  227. int result = 0;
  228. for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
  229. {
  230. const char *found = gettext (msgs[cnt].msgid);
  231. if (found == NULL
  232. || (msgs[cnt].msgstr[0] != '\0'
  233. && strcmp (found, msgs[cnt].msgstr) != 0))
  234. {
  235. /* Oops, shouldn't happen. */
  236. printf ("\
  237. gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
  238. msgs[cnt].msgid, found, msgs[cnt].msgstr);
  239. result = 1;
  240. }
  241. }
  242. return result;
  243. }
  244. static int
  245. negative_gettext_test (void)
  246. {
  247. size_t cnt;
  248. int result = 0;
  249. for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
  250. {
  251. const char *found = gettext (msgs[cnt].msgid);
  252. if (found != msgs[cnt].msgid)
  253. {
  254. /* Oops, shouldn't happen. */
  255. printf (" gettext (\"%s\") failed\n", msgs[cnt].msgid);
  256. result = 1;
  257. }
  258. }
  259. return result;
  260. }
  261. static int
  262. positive_dgettext_test (const char *domain)
  263. {
  264. size_t cnt;
  265. int result = 0;
  266. for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
  267. {
  268. const char *found = dgettext (domain, msgs[cnt].msgid);
  269. if (found == NULL
  270. || (msgs[cnt].msgstr[0] != '\0'
  271. && strcmp (found, msgs[cnt].msgstr) != 0))
  272. {
  273. /* Oops, shouldn't happen. */
  274. printf ("\
  275. dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
  276. domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
  277. result = 1;
  278. }
  279. }
  280. return result;
  281. }
  282. static int
  283. positive_dcgettext_test (const char *domain, int category)
  284. {
  285. size_t cnt;
  286. int result = 0;
  287. for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
  288. {
  289. const char *found = dcgettext (domain, msgs[cnt].msgid, category);
  290. if (found == NULL
  291. || (msgs[cnt].msgstr[0] != '\0'
  292. && strcmp (found, msgs[cnt].msgstr) != 0))
  293. {
  294. /* Oops, shouldn't happen. */
  295. printf ("\
  296. dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
  297. domain, msgs[cnt].msgid, catname[category], found,
  298. msgs[cnt].msgstr);
  299. result = 1;
  300. }
  301. }
  302. return result;
  303. }
  304. static int
  305. negative_dcgettext_test (const char *domain, int category)
  306. {
  307. size_t cnt;
  308. int result = 0;
  309. for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
  310. {
  311. const char *found = dcgettext (domain, msgs[cnt].msgid, category);
  312. if (found != msgs[cnt].msgid)
  313. {
  314. /* Oops, shouldn't happen. */
  315. printf (" dcgettext (\"%s\", \"%s\", %s) failed\n",
  316. domain, msgs[cnt].msgid, catname[category]);
  317. result = 1;
  318. }
  319. }
  320. return result;
  321. }