tst-digits.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <ctype.h>
  17. #include <langinfo.h>
  18. #include <locale.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <wchar.h>
  24. #include <wctype.h>
  25. #include <sys/types.h>
  26. #define ZERO "\xe2\x82\x80"
  27. #define ONE "\xe2\x82\x81"
  28. #define TWO "\xe2\x82\x82"
  29. #define THREE "\xe2\x82\x83"
  30. #define FOUR "\xe2\x82\x84"
  31. #define FIVE "\xe2\x82\x85"
  32. #define SIX "\xe2\x82\x86"
  33. #define SEVEN "\xe2\x82\x87"
  34. #define EIGHT "\xe2\x82\x88"
  35. #define NINE "\xe2\x82\x89"
  36. static struct printf_int_test
  37. {
  38. int n;
  39. const char *format;
  40. const char *expected;
  41. } printf_int_tests[] =
  42. {
  43. { 0, "%I'10d", " " ZERO },
  44. { 1, "%I'10d", " " ONE },
  45. { 2, "%I'10d", " " TWO },
  46. { 3, "%I'10d", " " THREE },
  47. { 4, "%I'10d", " " FOUR },
  48. { 5, "%I'10d", " " FIVE },
  49. { 6, "%I'10d", " " SIX },
  50. { 7, "%I'10d", " " SEVEN },
  51. { 8, "%I'10d", " " EIGHT },
  52. { 9, "%I'10d", " " NINE },
  53. { 11, "%I'10d", " " ONE ONE },
  54. { 12, "%I'10d", " " ONE TWO },
  55. { 123, "%I10d", " " ONE TWO THREE },
  56. { 123, "%I'10d", " " ONE TWO THREE },
  57. { 1234, "%I10d", ONE TWO THREE FOUR },
  58. { 1234, "%I'10d", ONE "," TWO THREE FOUR },
  59. { 12345, "%I'10d", ONE TWO "," THREE FOUR FIVE },
  60. { 123456, "%I'10d", ONE TWO THREE "," FOUR FIVE SIX },
  61. { 1234567, "%I'10d", ONE "," TWO THREE FOUR "," FIVE SIX SEVEN }
  62. };
  63. #define nprintf_int_tests \
  64. (sizeof (printf_int_tests) / sizeof (printf_int_tests[0]))
  65. #define WZERO L"\x2080"
  66. #define WONE L"\x2081"
  67. #define WTWO L"\x2082"
  68. #define WTHREE L"\x2083"
  69. #define WFOUR L"\x2084"
  70. #define WFIVE L"\x2085"
  71. #define WSIX L"\x2086"
  72. #define WSEVEN L"\x2087"
  73. #define WEIGHT L"\x2088"
  74. #define WNINE L"\x2089"
  75. static struct wprintf_int_test
  76. {
  77. int n;
  78. const wchar_t *format;
  79. const wchar_t *expected;
  80. } wprintf_int_tests[] =
  81. {
  82. { 0, L"%I'10d", L" " WZERO },
  83. { 1, L"%I'10d", L" " WONE },
  84. { 2, L"%I'10d", L" " WTWO },
  85. { 3, L"%I'10d", L" " WTHREE },
  86. { 4, L"%I'10d", L" " WFOUR },
  87. { 5, L"%I'10d", L" " WFIVE },
  88. { 6, L"%I'10d", L" " WSIX },
  89. { 7, L"%I'10d", L" " WSEVEN },
  90. { 8, L"%I'10d", L" " WEIGHT },
  91. { 9, L"%I'10d", L" " WNINE },
  92. { 11, L"%I'10d", L" " WONE WONE },
  93. { 12, L"%I'10d", L" " WONE WTWO },
  94. { 123, L"%I10d", L" " WONE WTWO WTHREE },
  95. { 123, L"%I'10d", L" " WONE WTWO WTHREE },
  96. { 1234, L"%I10d", L" " WONE WTWO WTHREE WFOUR },
  97. { 1234, L"%I'10d", L" " WONE L"," WTWO WTHREE WFOUR },
  98. { 12345, L"%I'10d", L" " WONE WTWO L"," WTHREE WFOUR WFIVE },
  99. { 123456, L"%I'10d", L" " WONE WTWO WTHREE L"," WFOUR WFIVE WSIX },
  100. { 1234567, L"%I'10d", L" " WONE L"," WTWO WTHREE WFOUR L"," WFIVE WSIX WSEVEN }
  101. };
  102. #define nwprintf_int_tests \
  103. (sizeof (wprintf_int_tests) / sizeof (wprintf_int_tests[0]))
  104. int
  105. main (void)
  106. {
  107. int cnt;
  108. int failures;
  109. int status;
  110. if (setlocale (LC_ALL, "test7") == NULL)
  111. {
  112. puts ("cannot set locale `test7'");
  113. exit (1);
  114. }
  115. printf ("CODESET = \"%s\"\n", nl_langinfo (CODESET));
  116. /* First: printf tests. */
  117. failures = 0;
  118. for (cnt = 0; cnt < (int) nprintf_int_tests; ++cnt)
  119. {
  120. char buf[100];
  121. ssize_t n;
  122. n = snprintf (buf, sizeof buf, printf_int_tests[cnt].format,
  123. printf_int_tests[cnt].n);
  124. printf ("%3d: got \"%s\", expected \"%s\"",
  125. cnt, buf, printf_int_tests[cnt].expected);
  126. if (n != (ssize_t) strlen (printf_int_tests[cnt].expected)
  127. || strcmp (buf, printf_int_tests[cnt].expected) != 0)
  128. {
  129. puts (" -> FAILED");
  130. ++failures;
  131. }
  132. else
  133. puts (" -> OK");
  134. }
  135. printf ("%d failures in printf tests\n", failures);
  136. status = failures != 0;
  137. /* wprintf tests. */
  138. failures = 0;
  139. for (cnt = 0; cnt < (int) nwprintf_int_tests; ++cnt)
  140. {
  141. wchar_t buf[100];
  142. ssize_t n;
  143. n = swprintf (buf, sizeof buf / sizeof (buf[0]),
  144. wprintf_int_tests[cnt].format,
  145. wprintf_int_tests[cnt].n);
  146. printf ("%3d: got \"%ls\", expected \"%ls\"",
  147. cnt, buf, wprintf_int_tests[cnt].expected);
  148. if (n != (ssize_t) wcslen (wprintf_int_tests[cnt].expected)
  149. || wcscmp (buf, wprintf_int_tests[cnt].expected) != 0)
  150. {
  151. puts (" -> FAILED");
  152. ++failures;
  153. }
  154. else
  155. puts (" -> OK");
  156. }
  157. printf ("%d failures in wprintf tests\n", failures);
  158. status = failures != 0;
  159. /* ctype tests. This makes sure that the multibyte chracter digit
  160. representations are not handle in this table. */
  161. failures = 0;
  162. for (cnt = 0; cnt < 256; ++cnt)
  163. if (cnt >= '0' && cnt <= '9')
  164. {
  165. if (! isdigit (cnt))
  166. {
  167. printf ("isdigit ('%c') == 0\n", cnt);
  168. ++failures;
  169. }
  170. }
  171. else
  172. {
  173. if (isdigit (cnt))
  174. {
  175. printf ("isdigit (%d) != 0\n", cnt);
  176. ++failures;
  177. }
  178. }
  179. printf ("%d failures in ctype tests\n", failures);
  180. status = failures != 0;
  181. /* wctype tests. This makes sure the second set of digits is also
  182. recorded. */
  183. failures = 0;
  184. for (cnt = 0; cnt < 256; ++cnt)
  185. if (cnt >= '0' && cnt <= '9')
  186. {
  187. if (! iswdigit (cnt))
  188. {
  189. printf ("iswdigit (L'%c') == 0\n", cnt);
  190. ++failures;
  191. }
  192. }
  193. else
  194. {
  195. if (iswdigit (cnt))
  196. {
  197. printf ("iswdigit (%d) != 0\n", cnt);
  198. ++failures;
  199. }
  200. }
  201. for (cnt = 0x2070; cnt < 0x2090; ++cnt)
  202. if (cnt >= 0x2080 && cnt <= 0x2089)
  203. {
  204. if (! iswdigit (cnt))
  205. {
  206. printf ("iswdigit (U%04X) == 0\n", cnt);
  207. ++failures;
  208. }
  209. }
  210. else
  211. {
  212. if (iswdigit (cnt))
  213. {
  214. printf ("iswdigit (U%04X) != 0\n", cnt);
  215. ++failures;
  216. }
  217. }
  218. printf ("%d failures in wctype tests\n", failures);
  219. status = failures != 0;
  220. return status;
  221. }