util_ut.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "util.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TStrUtilTest: public TTestBase {
  4. UNIT_TEST_SUITE(TStrUtilTest);
  5. UNIT_TEST(TestSpn);
  6. UNIT_TEST(TestRemoveAll);
  7. UNIT_TEST_SUITE_END();
  8. public:
  9. void TestSpn() {
  10. str_spn rul("a-z", true);
  11. char s[] = "!@#$ab%^&c+-";
  12. UNIT_ASSERT_EQUAL(rul.brk(s), s + 4);
  13. UNIT_ASSERT_EQUAL(rul.brk(s + 4), s + 4);
  14. UNIT_ASSERT_EQUAL(rul.brk(s + 10), s + 12);
  15. char* s1 = s;
  16. UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "!@#$"), 0);
  17. UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), ""), 0);
  18. UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "%^&"), 0);
  19. UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "+-"), 0);
  20. UNIT_ASSERT_EQUAL(rul.sep(s1), nullptr);
  21. }
  22. void TestRemoveAll() {
  23. static const struct T {
  24. const char* Str;
  25. char Ch;
  26. const char* Result;
  27. } tests[] = {
  28. {"", 'x', ""},
  29. {"hello world", 'h', "ello world"},
  30. {"hello world", 'l', "heo word"},
  31. {"hello world", 'x', "hello world"},
  32. };
  33. for (const T* t = tests; t != std::end(tests); ++t) {
  34. TString str(t->Str);
  35. RemoveAll(str, t->Ch);
  36. UNIT_ASSERT_EQUAL(t->Result, str);
  37. }
  38. }
  39. };
  40. UNIT_TEST_SUITE_REGISTRATION(TStrUtilTest);