tst-digits.c 6.6 KB

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