wctype.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if defined __STDC_VERSION__
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <wctype.h>
  5. #include "_wctype.h"
  6. #include "test.h"
  7. wint_t test_wint;
  8. wctrans_t test_wctrans;
  9. wctype_t test_wctype;
  10. #define UPPER L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11. #define LOWER L"abcdefghijklmnopqrstuvwxyz"
  12. #define DIGIT L"0123456789"
  13. #define HEX DIGIT L"ABCDEFabcdef"
  14. #define CNTRL L"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
  15. L"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f"
  16. #define PUNCT L"`~!@#$%^&*()_+-=[]\\{}|;':\",./<>?"
  17. #define GRAPH UPPER LOWER DIGIT PUNCT
  18. #define PRINT GRAPH " "
  19. #define SPACE L" \f\n\r\t\v"
  20. #define test_ctype_function(fn, ex) test_ctype_function_imp(#fn, fn, ex, sizeof(ex))
  21. #define test_ctype_conversion(fn, from, to) test_ctype_conversion_imp(#fn, fn, from, to)
  22. static int is_expected(int n, const wchar_t *expected, size_t len)
  23. {
  24. size_t i;
  25. for (i = 0; i < len - 1; i++) {
  26. if (expected[i] == n) {
  27. return 1;
  28. }
  29. }
  30. return 0;
  31. }
  32. static void test_ctype_function_imp(const char *fnname, int (*fn)(int), const wchar_t *expected, size_t len)
  33. {
  34. char expression[64];
  35. int i;
  36. for (i = 0; i < UCHAR_MAX; i++) {
  37. sprintf(expression, "%s(%d)", fnname, i);
  38. test_bool_imp(expression, fn(i), is_expected(i, expected, len));
  39. }
  40. sprintf(expression, "%s(EOF)", fnname);
  41. test_bool_imp(expression, fn(EOF), 0);
  42. }
  43. static int convert(const wchar_t *from, const wchar_t *to, int n)
  44. {
  45. size_t i;
  46. for (i = 0; from[i] != '\0'; i++) {
  47. if (from[i] == n) {
  48. return to[i];
  49. }
  50. }
  51. return n;
  52. }
  53. static void test_ctype_conversion_imp(const char *fnname, int (*fn)(int), const wchar_t *from, const wchar_t *to)
  54. {
  55. char expression[64];
  56. int i;
  57. for (i = 0; i < UCHAR_MAX; i++) {
  58. int j = convert(from, to, i);
  59. sprintf(expression, "%s(%d)", fnname, i);
  60. test_int_equals_imp(expression, fn(i), j);
  61. }
  62. sprintf(expression, "%s(EOF)", fnname);
  63. test_int_equals_imp(expression, fn(EOF), EOF);
  64. }
  65. void test_wctype_h(void)
  66. {
  67. testing_header("wctype.h");
  68. test_ctype_function(iswalnum, UPPER LOWER DIGIT);
  69. test_ctype_function(iswalpha, UPPER LOWER);
  70. test_ctype_function(iswcntrl, CNTRL);
  71. test_ctype_function(iswdigit, DIGIT);
  72. test_ctype_function(iswgraph, GRAPH);
  73. test_ctype_function(iswlower, LOWER);
  74. test_ctype_function(iswprint, PRINT);
  75. test_ctype_function(iswpunct, PUNCT);
  76. test_ctype_function(iswspace, SPACE);
  77. test_ctype_function(iswupper, UPPER);
  78. test_ctype_function(iswxdigit, HEX);
  79. /* TODO: wctype() */
  80. /* TODO: iswctype() */
  81. test_ctype_conversion(towlower, UPPER, LOWER);
  82. test_ctype_conversion(towupper, LOWER, UPPER);
  83. /* TODO: wctrans() */
  84. /* TODO: towctrans() */
  85. testing_end();
  86. }
  87. #else
  88. void test_wctype_h(void)
  89. {
  90. }
  91. #endif