tst-C-locale.c 15 KB

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