ascii_ut.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "ascii.h"
  2. #include <ctype.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. Y_UNIT_TEST_SUITE(TAsciiTest) {
  5. Y_UNIT_TEST(TestAscii) {
  6. UNIT_ASSERT(IsAsciiDigit('3'));
  7. UNIT_ASSERT(!IsAsciiDigit('x'));
  8. UNIT_ASSERT(IsAsciiAlpha('r'));
  9. UNIT_ASSERT(IsAsciiAlpha('R'));
  10. UNIT_ASSERT(!IsAsciiAlpha('3'));
  11. UNIT_ASSERT_EQUAL(AsciiToLower('3'), '3');
  12. UNIT_ASSERT_EQUAL(AsciiToLower('A'), 'a');
  13. UNIT_ASSERT_EQUAL(AsciiToLower('a'), 'a');
  14. UNIT_ASSERT_EQUAL(AsciiToUpper('3'), '3');
  15. UNIT_ASSERT_EQUAL(AsciiToUpper('A'), 'A');
  16. UNIT_ASSERT_EQUAL(AsciiToUpper('a'), 'A');
  17. UNIT_ASSERT(IsAscii('a'));
  18. UNIT_ASSERT(!IsAscii(-100));
  19. UNIT_ASSERT(!IsAscii(+200));
  20. UNIT_ASSERT(!IsAscii(int('a') + 256));
  21. for (int i = 0; i < 128; ++i) {
  22. UNIT_ASSERT_VALUES_EQUAL((bool)isxdigit(i), IsAsciiHex(i));
  23. UNIT_ASSERT_VALUES_EQUAL((bool)isspace(i), IsAsciiSpace((char)i));
  24. UNIT_ASSERT_VALUES_EQUAL((bool)isspace(i), IsAsciiSpace((char)i));
  25. UNIT_ASSERT_VALUES_EQUAL((bool)isalnum(i), IsAsciiAlnum((char)i));
  26. UNIT_ASSERT_VALUES_EQUAL((bool)isalpha(i), IsAsciiAlpha((char)i));
  27. UNIT_ASSERT_VALUES_EQUAL((bool)isupper(i), IsAsciiUpper((char)i));
  28. UNIT_ASSERT_VALUES_EQUAL((bool)islower(i), IsAsciiLower((char)i));
  29. UNIT_ASSERT_VALUES_EQUAL((bool)isdigit(i), IsAsciiDigit((char)i));
  30. UNIT_ASSERT_VALUES_EQUAL((bool)ispunct(i), IsAsciiPunct((char)i));
  31. }
  32. }
  33. Y_UNIT_TEST(Test1) {
  34. for (int i = 128; i < 1000; ++i) {
  35. UNIT_ASSERT(!IsAsciiHex(i));
  36. UNIT_ASSERT(!IsAsciiSpace(i));
  37. UNIT_ASSERT(!IsAsciiAlnum(i));
  38. UNIT_ASSERT(!IsAsciiAlpha(i));
  39. UNIT_ASSERT(!IsAsciiUpper(i));
  40. UNIT_ASSERT(!IsAsciiLower(i));
  41. UNIT_ASSERT(!IsAsciiDigit(i));
  42. UNIT_ASSERT(!IsAsciiPunct(i));
  43. }
  44. for (int i = -1000; i < 0; ++i) {
  45. UNIT_ASSERT(!IsAsciiHex(i));
  46. UNIT_ASSERT(!IsAsciiSpace(i));
  47. UNIT_ASSERT(!IsAsciiAlnum(i));
  48. UNIT_ASSERT(!IsAsciiAlpha(i));
  49. UNIT_ASSERT(!IsAsciiUpper(i));
  50. UNIT_ASSERT(!IsAsciiLower(i));
  51. UNIT_ASSERT(!IsAsciiDigit(i));
  52. UNIT_ASSERT(!IsAsciiPunct(i));
  53. }
  54. }
  55. Y_UNIT_TEST(CompareTest) {
  56. UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", "qQq"));
  57. UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", TStringBuf("qQq")));
  58. TString qq = "qq";
  59. TString qQ = "qQ";
  60. UNIT_ASSERT(AsciiEqualsIgnoreCase(qq, qQ));
  61. TString x = "qqqA";
  62. TString y = "qQqB";
  63. TString z = "qQnB";
  64. TString zz = "qQqq";
  65. TString zzz = "qQqqq";
  66. TStringBuf xs = TStringBuf(x.data(), 3);
  67. TStringBuf ys = TStringBuf(y.data(), 3);
  68. TStringBuf zs = TStringBuf(z.data(), 3);
  69. UNIT_ASSERT(AsciiCompareIgnoreCase(xs, ys) == 0);
  70. UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zs) > 0);
  71. UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zz) < 0);
  72. UNIT_ASSERT(AsciiCompareIgnoreCase(zzz, zz) > 0);
  73. UNIT_ASSERT(AsciiCompareIgnoreCase("qqQ", "qq") > 0);
  74. UNIT_ASSERT(AsciiCompareIgnoreCase("qq", "qq") == 0);
  75. UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qwe"), true);
  76. UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qWe"), false);
  77. UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "eWq"), false);
  78. UNIT_ASSERT_EQUAL(AsciiHasPrefixIgnoreCase("qweasd", "qWe"), true);
  79. UNIT_ASSERT_EQUAL(AsciiHasPrefixIgnoreCase("qweasd", "eWq"), false);
  80. UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "asD"), true);
  81. UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "ast"), false);
  82. }
  83. }