tst-langinfo.c 5.4 KB

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