tst-langinfo.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Test program for nl_langinfo() function.
  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>.
  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 <langinfo.h>
  17. #include <locale.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. struct map
  21. {
  22. const char *str;
  23. int val;
  24. } map[] =
  25. {
  26. #define VAL(name) { #name, name }
  27. VAL (ABDAY_1),
  28. VAL (ABDAY_2),
  29. VAL (ABDAY_3),
  30. VAL (ABDAY_4),
  31. VAL (ABDAY_5),
  32. VAL (ABDAY_6),
  33. VAL (ABDAY_7),
  34. VAL (ABMON_1),
  35. VAL (ABMON_10),
  36. VAL (ABMON_11),
  37. VAL (ABMON_12),
  38. VAL (ABMON_2),
  39. VAL (ABMON_3),
  40. VAL (ABMON_4),
  41. VAL (ABMON_5),
  42. VAL (ABMON_6),
  43. VAL (ABMON_7),
  44. VAL (ABMON_8),
  45. VAL (ABMON_9),
  46. VAL (ALT_DIGITS),
  47. VAL (AM_STR),
  48. VAL (CRNCYSTR),
  49. VAL (CURRENCY_SYMBOL),
  50. VAL (DAY_1),
  51. VAL (DAY_2),
  52. VAL (DAY_3),
  53. VAL (DAY_4),
  54. VAL (DAY_5),
  55. VAL (DAY_6),
  56. VAL (DAY_7),
  57. VAL (DECIMAL_POINT),
  58. VAL (D_FMT),
  59. VAL (D_T_FMT),
  60. VAL (ERA),
  61. VAL (ERA_D_FMT),
  62. VAL (ERA_D_T_FMT),
  63. VAL (ERA_T_FMT),
  64. VAL (ERA_YEAR),
  65. VAL (FRAC_DIGITS),
  66. VAL (GROUPING),
  67. VAL (INT_CURR_SYMBOL),
  68. VAL (INT_FRAC_DIGITS),
  69. VAL (MON_1),
  70. VAL (MON_10),
  71. VAL (MON_11),
  72. VAL (MON_12),
  73. VAL (MON_2),
  74. VAL (MON_3),
  75. VAL (MON_4),
  76. VAL (MON_5),
  77. VAL (MON_6),
  78. VAL (MON_7),
  79. VAL (MON_8),
  80. VAL (MON_9),
  81. VAL (MON_DECIMAL_POINT),
  82. VAL (MON_GROUPING),
  83. VAL (MON_THOUSANDS_SEP),
  84. VAL (NEGATIVE_SIGN),
  85. VAL (NOEXPR),
  86. VAL (NOSTR),
  87. VAL (N_CS_PRECEDES),
  88. VAL (N_SEP_BY_SPACE),
  89. VAL (N_SIGN_POSN),
  90. VAL (PM_STR),
  91. VAL (POSITIVE_SIGN),
  92. VAL (P_CS_PRECEDES),
  93. VAL (P_SEP_BY_SPACE),
  94. VAL (P_SIGN_POSN),
  95. VAL (RADIXCHAR),
  96. VAL (THOUSANDS_SEP),
  97. VAL (THOUSEP),
  98. VAL (T_FMT),
  99. VAL (T_FMT_AMPM),
  100. VAL (YESEXPR),
  101. VAL (YESSTR)
  102. };
  103. static int
  104. map_paramstr (const char *str)
  105. {
  106. int low = 0;
  107. int high = sizeof (map) / sizeof (map[0]);
  108. while (low < high)
  109. {
  110. int med = (low + high) / 2;
  111. int cmpres;
  112. cmpres = strcmp (str, map[med].str);
  113. if (cmpres == 0)
  114. return map[med].val;
  115. else if (cmpres > 0)
  116. low = med + 1;
  117. else
  118. high = med;
  119. }
  120. return -1;
  121. }
  122. #ifdef DEBUG
  123. # define REASON(str) printf ("\"%s\" ignored: %s\n", buf, str)
  124. #else
  125. # define REASON(str)
  126. #endif
  127. int
  128. main (void)
  129. {
  130. int result = 0;
  131. while (! feof (stdin))
  132. {
  133. char buf[1000];
  134. char *rp;
  135. char *locale;
  136. char *paramstr;
  137. char *expected;
  138. char *actual;
  139. int param;
  140. if (fgets (buf, sizeof (buf), stdin) == NULL)
  141. break;
  142. /* Split the fields. There are three is them:
  143. 1. locale
  144. 2. langinfo() parameter
  145. 3. expected result; this can be a string with white space etc.
  146. */
  147. rp = buf;
  148. while (*rp == ' ' || *rp == '\t')
  149. ++rp;
  150. if (*rp == '#')
  151. {
  152. /* It's a comment line. Ignore it. */
  153. REASON ("comment");
  154. continue;
  155. }
  156. locale = rp;
  157. while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
  158. ++rp;
  159. if (*rp == '\0' || *rp == '\n')
  160. {
  161. /* Incomplete line. */
  162. REASON ("incomplete line");
  163. continue;
  164. }
  165. *rp++ = '\0';
  166. while (*rp == ' ' || *rp == '\t')
  167. ++rp;
  168. paramstr = rp;
  169. while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
  170. ++rp;
  171. if (*rp == '\0' || *rp == '\n')
  172. {
  173. /* Incomplete line. */
  174. REASON ("incomplete line");
  175. continue;
  176. }
  177. *rp++ = '\0';
  178. while (*rp == ' ' || *rp == '\t')
  179. ++rp;
  180. if (*rp == '"')
  181. {
  182. char *wp;
  183. expected = wp = ++rp;
  184. while (*rp != '"' && *rp != '\n' && *rp != '\0')
  185. {
  186. if (*rp == '\\')
  187. {
  188. ++rp;
  189. if (*rp == '\0')
  190. break;
  191. if (*rp >= '0' && *rp <= '9')
  192. {
  193. int val = *rp - '0';
  194. if (rp[1] >= '0' && rp[1] <= '9')
  195. {
  196. ++rp;
  197. val *= 10;
  198. val += *rp - '0';
  199. if (rp[1] >= '0' && rp[1] <= '9')
  200. {
  201. ++rp;
  202. val *= 10;
  203. val += *rp - '0';
  204. }
  205. }
  206. *rp = val;
  207. }
  208. }
  209. *wp++ = *rp++;
  210. }
  211. if (*rp != '"')
  212. {
  213. REASON ("missing '\"'");
  214. continue;
  215. }
  216. *wp = '\0';
  217. }
  218. else
  219. {
  220. expected = rp;
  221. while (*rp != '\0' && *rp != '\n')
  222. ++rp;
  223. *rp = '\0';
  224. }
  225. param = map_paramstr (paramstr);
  226. if (param == -1)
  227. {
  228. /* Invalid parameter. */
  229. REASON ("invalid parameter");
  230. continue;
  231. }
  232. /* Set the locale and check whether it worked. */
  233. printf ("LC_ALL=%s nl_langinfo(%s)", locale, paramstr);
  234. setlocale (LC_ALL, locale);
  235. if (strcmp (locale, setlocale (LC_ALL, NULL)) != 0)
  236. {
  237. puts (": failed to set locale");
  238. result = 1;
  239. continue;
  240. }
  241. actual = nl_langinfo (param);
  242. printf (" = \"%s\", ", actual);
  243. if (strcmp (actual, expected) == 0)
  244. puts ("OK");
  245. else
  246. {
  247. printf ("FAILED (expected: %s)\n", expected);
  248. result = 1;
  249. }
  250. }
  251. return result;
  252. }