TestWctype.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <wctype.h>
  8. TEST_CASE(wctype)
  9. {
  10. // Test that existing properties return non-zero wctypes.
  11. EXPECT(wctype("alnum") != 0);
  12. EXPECT(wctype("alpha") != 0);
  13. EXPECT(wctype("blank") != 0);
  14. EXPECT(wctype("cntrl") != 0);
  15. EXPECT(wctype("digit") != 0);
  16. EXPECT(wctype("graph") != 0);
  17. EXPECT(wctype("lower") != 0);
  18. EXPECT(wctype("print") != 0);
  19. EXPECT(wctype("punct") != 0);
  20. EXPECT(wctype("space") != 0);
  21. EXPECT(wctype("upper") != 0);
  22. EXPECT(wctype("xdigit") != 0);
  23. // Test that invalid properties return the "invalid" wctype (0).
  24. EXPECT(wctype("") == 0);
  25. EXPECT(wctype("abc") == 0);
  26. }
  27. TEST_CASE(wctrans)
  28. {
  29. // Test that existing character mappings return non-zero wctrans values.
  30. EXPECT(wctrans("tolower") != 0);
  31. EXPECT(wctrans("toupper") != 0);
  32. // Test that invalid character mappings return the "invalid" wctrans value (0).
  33. EXPECT(wctrans("") == 0);
  34. EXPECT(wctrans("abc") == 0);
  35. }
  36. TEST_CASE(iswctype)
  37. {
  38. const wint_t test_chars[] = { L'A', L'a', L'F', L'f', L'Z', L'z', L'0', L'\n', L'.', L'\x00' };
  39. // Test that valid properties are wired to the correct implementation.
  40. for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
  41. EXPECT(iswctype(test_chars[i], wctype("alnum")) == iswalnum(test_chars[i]));
  42. EXPECT(iswctype(test_chars[i], wctype("alpha")) == iswalpha(test_chars[i]));
  43. EXPECT(iswctype(test_chars[i], wctype("blank")) == iswblank(test_chars[i]));
  44. EXPECT(iswctype(test_chars[i], wctype("cntrl")) == iswcntrl(test_chars[i]));
  45. EXPECT(iswctype(test_chars[i], wctype("digit")) == iswdigit(test_chars[i]));
  46. EXPECT(iswctype(test_chars[i], wctype("graph")) == iswgraph(test_chars[i]));
  47. EXPECT(iswctype(test_chars[i], wctype("lower")) == iswlower(test_chars[i]));
  48. EXPECT(iswctype(test_chars[i], wctype("print")) == iswprint(test_chars[i]));
  49. EXPECT(iswctype(test_chars[i], wctype("punct")) == iswpunct(test_chars[i]));
  50. EXPECT(iswctype(test_chars[i], wctype("space")) == iswspace(test_chars[i]));
  51. EXPECT(iswctype(test_chars[i], wctype("upper")) == iswupper(test_chars[i]));
  52. EXPECT(iswctype(test_chars[i], wctype("xdigit")) == iswxdigit(test_chars[i]));
  53. }
  54. // Test that invalid properties always return zero.
  55. for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
  56. EXPECT(iswctype(test_chars[i], 0) == 0);
  57. EXPECT(iswctype(test_chars[i], -1) == 0);
  58. }
  59. }
  60. TEST_CASE(towctrans)
  61. {
  62. const wint_t test_chars[] = { L'A', L'a', L'F', L'f', L'Z', L'z', L'0', L'\n', L'.', L'\x00' };
  63. // Test that valid mappings are wired to the correct implementation.
  64. for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
  65. EXPECT(towctrans(test_chars[i], wctrans("tolower")) == towlower(test_chars[i]));
  66. EXPECT(towctrans(test_chars[i], wctrans("toupper")) == towupper(test_chars[i]));
  67. }
  68. // Test that invalid mappings always return the character unchanged.
  69. for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
  70. EXPECT(towctrans(test_chars[i], 0) == test_chars[i]);
  71. EXPECT(towctrans(test_chars[i], -1) == test_chars[i]);
  72. }
  73. }