tst-C-locale.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* Tests of C and POSIX locale contents.
  2. Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <ctype.h>
  17. #include <langinfo.h>
  18. #include <limits.h>
  19. #include <locale.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
  24. static int
  25. run_test (const char *locname)
  26. {
  27. struct lconv *lc;
  28. const char *str;
  29. const wchar_t *wstr;
  30. int result = 0;
  31. locale_t loc;
  32. /* ISO C stuff. */
  33. lc = localeconv ();
  34. if (lc == NULL)
  35. {
  36. printf ("localeconv failed for locale %s\n", locname);
  37. result = 1;
  38. }
  39. else
  40. {
  41. #define STRTEST(name, exp) \
  42. do \
  43. if (strcmp (lc->name, exp) != 0) \
  44. { \
  45. printf (#name " in locale %s wrong (is \"%s\", should be \"%s\")\n",\
  46. locname, lc->name, exp); \
  47. result = 1; \
  48. } \
  49. while (0)
  50. STRTEST (decimal_point, ".");
  51. STRTEST (thousands_sep, "");
  52. STRTEST (grouping, "");
  53. STRTEST (mon_decimal_point, "");
  54. STRTEST (mon_thousands_sep, "");
  55. STRTEST (mon_grouping, "");
  56. STRTEST (positive_sign, "");
  57. STRTEST (negative_sign, "");
  58. STRTEST (currency_symbol, "");
  59. STRTEST (int_curr_symbol, "");
  60. #define CHARTEST(name, exp) \
  61. do \
  62. if (lc->name != exp) \
  63. { \
  64. printf (#name " in locale %s wrong (is %d, should be %d)\n", \
  65. locname, lc->name, CHAR_MAX); \
  66. result = 1; \
  67. } \
  68. while (0)
  69. CHARTEST (frac_digits, CHAR_MAX);
  70. CHARTEST (p_cs_precedes, CHAR_MAX);
  71. CHARTEST (n_cs_precedes, CHAR_MAX);
  72. CHARTEST (p_sep_by_space, CHAR_MAX);
  73. CHARTEST (n_sep_by_space, CHAR_MAX);
  74. CHARTEST (p_sign_posn, CHAR_MAX);
  75. CHARTEST (n_sign_posn, CHAR_MAX);
  76. CHARTEST (int_frac_digits, CHAR_MAX);
  77. CHARTEST (int_p_cs_precedes, CHAR_MAX);
  78. CHARTEST (int_n_cs_precedes, CHAR_MAX);
  79. CHARTEST (int_p_sep_by_space, CHAR_MAX);
  80. CHARTEST (int_n_sep_by_space, CHAR_MAX);
  81. CHARTEST (int_p_sign_posn, CHAR_MAX);
  82. CHARTEST (int_n_sign_posn, CHAR_MAX);
  83. }
  84. #undef STRTEST
  85. #define STRTEST(name, exp) \
  86. str = nl_langinfo (name); \
  87. if (strcmp (str, exp) != 0) \
  88. { \
  89. printf ("nl_langinfo(" #name ") in locale %s wrong " \
  90. "(is \"%s\", should be \"%s\")\n", locname, str, exp); \
  91. result = 1; \
  92. }
  93. #define WSTRTEST(name, exp) \
  94. wstr = (wchar_t *) nl_langinfo (name); \
  95. if (wcscmp (wstr, exp) != 0) \
  96. { \
  97. printf ("nl_langinfo(" #name ") in locale %s wrong " \
  98. "(is \"%S\", should be \"%S\")\n", locname, wstr, exp); \
  99. result = 1; \
  100. }
  101. /* Unix stuff. */
  102. STRTEST (ABDAY_1, "Sun");
  103. STRTEST (ABDAY_2, "Mon");
  104. STRTEST (ABDAY_3, "Tue");
  105. STRTEST (ABDAY_4, "Wed");
  106. STRTEST (ABDAY_5, "Thu");
  107. STRTEST (ABDAY_6, "Fri");
  108. STRTEST (ABDAY_7, "Sat");
  109. STRTEST (DAY_1, "Sunday");
  110. STRTEST (DAY_2, "Monday");
  111. STRTEST (DAY_3, "Tuesday");
  112. STRTEST (DAY_4, "Wednesday");
  113. STRTEST (DAY_5, "Thursday");
  114. STRTEST (DAY_6, "Friday");
  115. STRTEST (DAY_7, "Saturday");
  116. STRTEST (ABMON_1, "Jan");
  117. STRTEST (ABMON_2, "Feb");
  118. STRTEST (ABMON_3, "Mar");
  119. STRTEST (ABMON_4, "Apr");
  120. STRTEST (ABMON_5, "May");
  121. STRTEST (ABMON_6, "Jun");
  122. STRTEST (ABMON_7, "Jul");
  123. STRTEST (ABMON_8, "Aug");
  124. STRTEST (ABMON_9, "Sep");
  125. STRTEST (ABMON_10, "Oct");
  126. STRTEST (ABMON_11, "Nov");
  127. STRTEST (ABMON_12, "Dec");
  128. STRTEST (MON_1, "January");
  129. STRTEST (MON_2, "February");
  130. STRTEST (MON_3, "March");
  131. STRTEST (MON_4, "April");
  132. STRTEST (MON_5, "May");
  133. STRTEST (MON_6, "June");
  134. STRTEST (MON_7, "July");
  135. STRTEST (MON_8, "August");
  136. STRTEST (MON_9, "September");
  137. STRTEST (MON_10, "October");
  138. STRTEST (MON_11, "November");
  139. STRTEST (MON_12, "December");
  140. STRTEST (AM_STR, "AM");
  141. STRTEST (PM_STR, "PM");
  142. STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
  143. STRTEST (D_FMT, "%m/%d/%y");
  144. STRTEST (T_FMT, "%H:%M:%S");
  145. STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
  146. STRTEST (ERA, "");
  147. STRTEST (ERA_D_FMT, "");
  148. STRTEST (ERA_T_FMT, "");
  149. STRTEST (ERA_D_T_FMT, "");
  150. STRTEST (ALT_DIGITS, "");
  151. STRTEST (RADIXCHAR, ".");
  152. STRTEST (THOUSEP, "");
  153. STRTEST (YESEXPR, "^[yY]");
  154. STRTEST (NOEXPR, "^[nN]");
  155. /* Extensions. */
  156. WSTRTEST (_NL_WABDAY_1, L"Sun");
  157. WSTRTEST (_NL_WABDAY_2, L"Mon");
  158. WSTRTEST (_NL_WABDAY_3, L"Tue");
  159. WSTRTEST (_NL_WABDAY_4, L"Wed");
  160. WSTRTEST (_NL_WABDAY_5, L"Thu");
  161. WSTRTEST (_NL_WABDAY_6, L"Fri");
  162. WSTRTEST (_NL_WABDAY_7, L"Sat");
  163. WSTRTEST (_NL_WDAY_1, L"Sunday");
  164. WSTRTEST (_NL_WDAY_2, L"Monday");
  165. WSTRTEST (_NL_WDAY_3, L"Tuesday");
  166. WSTRTEST (_NL_WDAY_4, L"Wednesday");
  167. WSTRTEST (_NL_WDAY_5, L"Thursday");
  168. WSTRTEST (_NL_WDAY_6, L"Friday");
  169. WSTRTEST (_NL_WDAY_7, L"Saturday");
  170. WSTRTEST (_NL_WABMON_1, L"Jan");
  171. WSTRTEST (_NL_WABMON_2, L"Feb");
  172. WSTRTEST (_NL_WABMON_3, L"Mar");
  173. WSTRTEST (_NL_WABMON_4, L"Apr");
  174. WSTRTEST (_NL_WABMON_5, L"May");
  175. WSTRTEST (_NL_WABMON_6, L"Jun");
  176. WSTRTEST (_NL_WABMON_7, L"Jul");
  177. WSTRTEST (_NL_WABMON_8, L"Aug");
  178. WSTRTEST (_NL_WABMON_9, L"Sep");
  179. WSTRTEST (_NL_WABMON_10, L"Oct");
  180. WSTRTEST (_NL_WABMON_11, L"Nov");
  181. WSTRTEST (_NL_WABMON_12, L"Dec");
  182. WSTRTEST (_NL_WMON_1, L"January");
  183. WSTRTEST (_NL_WMON_2, L"February");
  184. WSTRTEST (_NL_WMON_3, L"March");
  185. WSTRTEST (_NL_WMON_4, L"April");
  186. WSTRTEST (_NL_WMON_5, L"May");
  187. WSTRTEST (_NL_WMON_6, L"June");
  188. WSTRTEST (_NL_WMON_7, L"July");
  189. WSTRTEST (_NL_WMON_8, L"August");
  190. WSTRTEST (_NL_WMON_9, L"September");
  191. WSTRTEST (_NL_WMON_10, L"October");
  192. WSTRTEST (_NL_WMON_11, L"November");
  193. WSTRTEST (_NL_WMON_12, L"December");
  194. WSTRTEST (_NL_WAM_STR, L"AM");
  195. WSTRTEST (_NL_WPM_STR, L"PM");
  196. WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
  197. WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
  198. WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
  199. WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
  200. WSTRTEST (_NL_WERA_D_FMT, L"");
  201. WSTRTEST (_NL_WERA_T_FMT, L"");
  202. WSTRTEST (_NL_WERA_D_T_FMT, L"");
  203. WSTRTEST (_NL_WALT_DIGITS, L"");
  204. STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
  205. WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
  206. STRTEST (INT_CURR_SYMBOL, "");
  207. STRTEST (CURRENCY_SYMBOL, "");
  208. STRTEST (MON_DECIMAL_POINT, "");
  209. STRTEST (MON_THOUSANDS_SEP, "");
  210. STRTEST (MON_GROUPING, "");
  211. STRTEST (POSITIVE_SIGN, "");
  212. STRTEST (NEGATIVE_SIGN, "");
  213. STRTEST (GROUPING, "");
  214. STRTEST (YESSTR, "");
  215. STRTEST (NOSTR, "");
  216. /* Test the new locale mechanisms. */
  217. loc = newlocale (LC_ALL_MASK, locname, NULL);
  218. if (loc == NULL)
  219. {
  220. printf ("cannot create locale object for locale %s\n", locname);
  221. result = 1;
  222. }
  223. else
  224. {
  225. int c;
  226. #undef STRTEST
  227. #define STRTEST(name, exp) \
  228. str = nl_langinfo_l (name, loc); \
  229. if (strcmp (str, exp) != 0) \
  230. { \
  231. printf ("nl_langinfo_l(" #name ") in locale %s wrong " \
  232. "(is \"%s\", should be \"%s\")\n", locname, str, exp); \
  233. result = 1; \
  234. }
  235. #undef WSTRTEST
  236. #define WSTRTEST(name, exp) \
  237. wstr = (wchar_t *) nl_langinfo_l (name, loc); \
  238. if (wcscmp (wstr, exp) != 0) \
  239. { \
  240. printf ("nl_langinfo_l(" #name ") in locale %s wrong " \
  241. "(is \"%S\", should be \"%S\")\n", locname, wstr, exp); \
  242. result = 1; \
  243. }
  244. /* Unix stuff. */
  245. STRTEST (ABDAY_1, "Sun");
  246. STRTEST (ABDAY_2, "Mon");
  247. STRTEST (ABDAY_3, "Tue");
  248. STRTEST (ABDAY_4, "Wed");
  249. STRTEST (ABDAY_5, "Thu");
  250. STRTEST (ABDAY_6, "Fri");
  251. STRTEST (ABDAY_7, "Sat");
  252. STRTEST (DAY_1, "Sunday");
  253. STRTEST (DAY_2, "Monday");
  254. STRTEST (DAY_3, "Tuesday");
  255. STRTEST (DAY_4, "Wednesday");
  256. STRTEST (DAY_5, "Thursday");
  257. STRTEST (DAY_6, "Friday");
  258. STRTEST (DAY_7, "Saturday");
  259. STRTEST (ABMON_1, "Jan");
  260. STRTEST (ABMON_2, "Feb");
  261. STRTEST (ABMON_3, "Mar");
  262. STRTEST (ABMON_4, "Apr");
  263. STRTEST (ABMON_5, "May");
  264. STRTEST (ABMON_6, "Jun");
  265. STRTEST (ABMON_7, "Jul");
  266. STRTEST (ABMON_8, "Aug");
  267. STRTEST (ABMON_9, "Sep");
  268. STRTEST (ABMON_10, "Oct");
  269. STRTEST (ABMON_11, "Nov");
  270. STRTEST (ABMON_12, "Dec");
  271. STRTEST (MON_1, "January");
  272. STRTEST (MON_2, "February");
  273. STRTEST (MON_3, "March");
  274. STRTEST (MON_4, "April");
  275. STRTEST (MON_5, "May");
  276. STRTEST (MON_6, "June");
  277. STRTEST (MON_7, "July");
  278. STRTEST (MON_8, "August");
  279. STRTEST (MON_9, "September");
  280. STRTEST (MON_10, "October");
  281. STRTEST (MON_11, "November");
  282. STRTEST (MON_12, "December");
  283. STRTEST (AM_STR, "AM");
  284. STRTEST (PM_STR, "PM");
  285. STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
  286. STRTEST (D_FMT, "%m/%d/%y");
  287. STRTEST (T_FMT, "%H:%M:%S");
  288. STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
  289. STRTEST (ERA, "");
  290. STRTEST (ERA_D_FMT, "");
  291. STRTEST (ERA_T_FMT, "");
  292. STRTEST (ERA_D_T_FMT, "");
  293. STRTEST (ALT_DIGITS, "");
  294. STRTEST (RADIXCHAR, ".");
  295. STRTEST (THOUSEP, "");
  296. STRTEST (YESEXPR, "^[yY]");
  297. STRTEST (NOEXPR, "^[nN]");
  298. /* Extensions. */
  299. WSTRTEST (_NL_WABDAY_1, L"Sun");
  300. WSTRTEST (_NL_WABDAY_2, L"Mon");
  301. WSTRTEST (_NL_WABDAY_3, L"Tue");
  302. WSTRTEST (_NL_WABDAY_4, L"Wed");
  303. WSTRTEST (_NL_WABDAY_5, L"Thu");
  304. WSTRTEST (_NL_WABDAY_6, L"Fri");
  305. WSTRTEST (_NL_WABDAY_7, L"Sat");
  306. WSTRTEST (_NL_WDAY_1, L"Sunday");
  307. WSTRTEST (_NL_WDAY_2, L"Monday");
  308. WSTRTEST (_NL_WDAY_3, L"Tuesday");
  309. WSTRTEST (_NL_WDAY_4, L"Wednesday");
  310. WSTRTEST (_NL_WDAY_5, L"Thursday");
  311. WSTRTEST (_NL_WDAY_6, L"Friday");
  312. WSTRTEST (_NL_WDAY_7, L"Saturday");
  313. WSTRTEST (_NL_WABMON_1, L"Jan");
  314. WSTRTEST (_NL_WABMON_2, L"Feb");
  315. WSTRTEST (_NL_WABMON_3, L"Mar");
  316. WSTRTEST (_NL_WABMON_4, L"Apr");
  317. WSTRTEST (_NL_WABMON_5, L"May");
  318. WSTRTEST (_NL_WABMON_6, L"Jun");
  319. WSTRTEST (_NL_WABMON_7, L"Jul");
  320. WSTRTEST (_NL_WABMON_8, L"Aug");
  321. WSTRTEST (_NL_WABMON_9, L"Sep");
  322. WSTRTEST (_NL_WABMON_10, L"Oct");
  323. WSTRTEST (_NL_WABMON_11, L"Nov");
  324. WSTRTEST (_NL_WABMON_12, L"Dec");
  325. WSTRTEST (_NL_WMON_1, L"January");
  326. WSTRTEST (_NL_WMON_2, L"February");
  327. WSTRTEST (_NL_WMON_3, L"March");
  328. WSTRTEST (_NL_WMON_4, L"April");
  329. WSTRTEST (_NL_WMON_5, L"May");
  330. WSTRTEST (_NL_WMON_6, L"June");
  331. WSTRTEST (_NL_WMON_7, L"July");
  332. WSTRTEST (_NL_WMON_8, L"August");
  333. WSTRTEST (_NL_WMON_9, L"September");
  334. WSTRTEST (_NL_WMON_10, L"October");
  335. WSTRTEST (_NL_WMON_11, L"November");
  336. WSTRTEST (_NL_WMON_12, L"December");
  337. WSTRTEST (_NL_WAM_STR, L"AM");
  338. WSTRTEST (_NL_WPM_STR, L"PM");
  339. WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
  340. WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
  341. WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
  342. WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
  343. WSTRTEST (_NL_WERA_D_FMT, L"");
  344. WSTRTEST (_NL_WERA_T_FMT, L"");
  345. WSTRTEST (_NL_WERA_D_T_FMT, L"");
  346. WSTRTEST (_NL_WALT_DIGITS, L"");
  347. STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
  348. WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
  349. STRTEST (INT_CURR_SYMBOL, "");
  350. STRTEST (CURRENCY_SYMBOL, "");
  351. STRTEST (MON_DECIMAL_POINT, "");
  352. STRTEST (MON_THOUSANDS_SEP, "");
  353. STRTEST (MON_GROUPING, "");
  354. STRTEST (POSITIVE_SIGN, "");
  355. STRTEST (NEGATIVE_SIGN, "");
  356. STRTEST (GROUPING, "");
  357. STRTEST (YESSTR, "");
  358. STRTEST (NOSTR, "");
  359. /* Character class tests. */
  360. for (c = 0; c < 128; ++c)
  361. {
  362. #define CLASSTEST(name) \
  363. if (is##name (c) != is##name##_l (c, loc)) \
  364. { \
  365. printf ("is%s('\\%o') != is%s_l('\\%o')\n", \
  366. #name, c, #name, c); \
  367. result = 1; \
  368. }
  369. CLASSTEST (alnum);
  370. CLASSTEST (alpha);
  371. CLASSTEST (blank);
  372. CLASSTEST (cntrl);
  373. CLASSTEST (digit);
  374. CLASSTEST (lower);
  375. CLASSTEST (graph);
  376. CLASSTEST (print);
  377. CLASSTEST (punct);
  378. CLASSTEST (space);
  379. CLASSTEST (upper);
  380. CLASSTEST (xdigit);
  381. /* Character mapping tests. */
  382. #define MAPTEST(name) \
  383. if (to##name (c) != to##name##_l (c, loc)) \
  384. { \
  385. printf ("to%s('\\%o') != to%s_l('\\%o'): '\\%o' vs '\\%o'\n", \
  386. #name, c, #name, c, \
  387. to##name (c), to##name##_l (c, loc)); \
  388. result = 1; \
  389. }
  390. MAPTEST (lower);
  391. MAPTEST (upper);
  392. }
  393. /* Character class tests, this time for wide characters. Note that
  394. this only works because we know that the internal encoding is
  395. UCS4. */
  396. for (c = 0; c < 128; ++c)
  397. {
  398. #undef CLASSTEST
  399. #define CLASSTEST(name) \
  400. if (isw##name (c) != isw##name##_l (c, loc)) \
  401. { \
  402. printf ("isw%s('\\%o') != isw%s_l('\\%o')\n", \
  403. #name, c, #name, c); \
  404. result = 1; \
  405. }
  406. CLASSTEST (alnum);
  407. CLASSTEST (alpha);
  408. CLASSTEST (blank);
  409. CLASSTEST (cntrl);
  410. CLASSTEST (digit);
  411. CLASSTEST (lower);
  412. CLASSTEST (graph);
  413. CLASSTEST (print);
  414. CLASSTEST (punct);
  415. CLASSTEST (space);
  416. CLASSTEST (upper);
  417. CLASSTEST (xdigit);
  418. /* Character mapping tests. Note that
  419. this only works because we know that the internal encoding is
  420. UCS4. */
  421. #undef MAPTEST
  422. #define MAPTEST(name) \
  423. if (tow##name (c) != tow##name##_l (c, loc)) \
  424. { \
  425. printf ("tow%s('\\%o') != tow%s_l('\\%o'): '\\%o' vs '\\%o'\n",\
  426. #name, c, #name, c, \
  427. tow##name (c), tow##name##_l (c, loc)); \
  428. result = 1; \
  429. }
  430. MAPTEST (lower);
  431. MAPTEST (upper);
  432. }
  433. freelocale (loc);
  434. }
  435. return result;
  436. }
  437. static int
  438. do_test (void)
  439. {
  440. int result;
  441. /* First use the name "C". */
  442. if (setlocale (LC_ALL, "C") == NULL)
  443. {
  444. puts ("cannot set C locale");
  445. result = 1;
  446. }
  447. else
  448. result = run_test ("C");
  449. /* Then the name "POSIX". */
  450. if (setlocale (LC_ALL, "POSIX") == NULL)
  451. {
  452. puts ("cannot set POSIX locale");
  453. result = 1;
  454. }
  455. else
  456. result |= run_test ("POSIX");
  457. return result;
  458. }
  459. #define TEST_FUNCTION do_test ()
  460. #include "../test-skeleton.c"