TestCharacterTypes.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/CharacterTypes.h>
  8. #include <ctype.h>
  9. #define ASCII 0x80
  10. #define UNICODE 0x10FFFF + 100
  11. void compare_bool_output_over(u32 range, auto& old_function, auto& new_function)
  12. {
  13. bool result1 = false;
  14. bool result2 = false;
  15. for (u32 i = 0; i < range; ++i) {
  16. EXPECT_EQ(result1 = (old_function(i) > 0), result2 = (new_function(i) > 0));
  17. if (result1 != result2)
  18. dbgln("Function input value was {}.", i);
  19. }
  20. }
  21. void compare_value_output_over(u32 range, auto& old_function, auto& new_function)
  22. {
  23. i64 result1 = 0;
  24. i64 result2 = 0;
  25. for (u32 i = 0; i < range; ++i) {
  26. EXPECT_EQ(result1 = old_function(i), result2 = new_function(i));
  27. if (result1 != result2)
  28. dbgln("Function input value was {}.", i);
  29. }
  30. }
  31. TEST_CASE(is_ascii)
  32. {
  33. compare_bool_output_over(UNICODE, isascii, is_ascii);
  34. }
  35. TEST_CASE(is_ascii_alphanumeric)
  36. {
  37. compare_bool_output_over(ASCII, isalnum, is_ascii_alphanumeric);
  38. }
  39. TEST_CASE(is_ascii_blank)
  40. {
  41. compare_bool_output_over(ASCII, isblank, is_ascii_blank);
  42. }
  43. TEST_CASE(is_ascii_c0_control)
  44. {
  45. compare_bool_output_over(ASCII - 1, iscntrl, is_ascii_c0_control);
  46. }
  47. TEST_CASE(is_ascii_control)
  48. {
  49. compare_bool_output_over(ASCII, iscntrl, is_ascii_control);
  50. }
  51. TEST_CASE(is_ascii_digit)
  52. {
  53. compare_bool_output_over(ASCII, isdigit, is_ascii_digit);
  54. }
  55. TEST_CASE(is_ascii_graphical)
  56. {
  57. compare_bool_output_over(ASCII, isgraph, is_ascii_graphical);
  58. }
  59. TEST_CASE(is_ascii_hex_digit)
  60. {
  61. compare_bool_output_over(ASCII, isxdigit, is_ascii_hex_digit);
  62. }
  63. TEST_CASE(is_ascii_lower_alpha)
  64. {
  65. compare_bool_output_over(ASCII, islower, is_ascii_lower_alpha);
  66. }
  67. TEST_CASE(is_ascii_printable)
  68. {
  69. compare_bool_output_over(ASCII, isprint, is_ascii_printable);
  70. }
  71. TEST_CASE(is_ascii_punctuation)
  72. {
  73. compare_bool_output_over(ASCII, ispunct, is_ascii_punctuation);
  74. }
  75. TEST_CASE(is_ascii_space)
  76. {
  77. compare_bool_output_over(ASCII, isspace, is_ascii_space);
  78. }
  79. TEST_CASE(is_ascii_upper_alpha)
  80. {
  81. compare_bool_output_over(ASCII, isupper, is_ascii_upper_alpha);
  82. }
  83. TEST_CASE(to_ascii_lowercase)
  84. {
  85. compare_value_output_over(UNICODE, tolower, to_ascii_lowercase);
  86. }
  87. TEST_CASE(to_ascii_uppercase)
  88. {
  89. compare_value_output_over(UNICODE, toupper, to_ascii_uppercase);
  90. }
  91. TEST_CASE(parse_ascii_digit)
  92. {
  93. EXPECT_EQ(parse_ascii_digit('0'), 0u);
  94. EXPECT_EQ(parse_ascii_digit('9'), 9u);
  95. EXPECT_CRASH("parsing invalid ASCII digit", [] {
  96. parse_ascii_digit('a');
  97. return Test::Crash::Failure::DidNotCrash;
  98. });
  99. EXPECT_CRASH("parsing invalid unicode digit", [] {
  100. parse_ascii_digit(0x00A9);
  101. return Test::Crash::Failure::DidNotCrash;
  102. });
  103. }
  104. TEST_CASE(parse_ascii_hex_digit)
  105. {
  106. EXPECT_EQ(parse_ascii_hex_digit('0'), 0u);
  107. EXPECT_EQ(parse_ascii_hex_digit('F'), 15u);
  108. EXPECT_EQ(parse_ascii_hex_digit('f'), 15u);
  109. EXPECT_CRASH("parsing invalid ASCII hex digit", [] {
  110. parse_ascii_hex_digit('g');
  111. return Test::Crash::Failure::DidNotCrash;
  112. });
  113. }