strspn_ut.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "strspn.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TStrSpnTest) {
  4. Y_UNIT_TEST(FindFirstOf) {
  5. const TString s("some text!");
  6. UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.begin(), s.end()), s.begin());
  7. UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.c_str()), s.begin());
  8. UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.begin(), s.end()), s.begin() + 5);
  9. UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.c_str()), s.begin() + 5);
  10. UNIT_ASSERT_EQUAL(TCompactStrSpn(".?!").FindFirstOf(s.begin(), s.end()), s.end() - 1);
  11. UNIT_ASSERT_EQUAL(TCompactStrSpn(".?!").FindFirstOf(s.c_str()), s.end() - 1);
  12. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.begin(), s.end()), s.end());
  13. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.c_str()), s.end());
  14. // Must be const. If not, non-const begin() will clone the shared empty string
  15. // and the next assertion will possibly use invalidated end iterator.
  16. const TString empty;
  17. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.begin(), empty.end()), empty.end());
  18. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.c_str()), empty.end());
  19. }
  20. Y_UNIT_TEST(FindFirstNotOf) {
  21. const TString s("abacabaxyz");
  22. UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.begin(), s.end()), s.begin());
  23. UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.c_str()), s.begin());
  24. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.begin(), s.end()), s.begin() + 7);
  25. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.c_str()), s.begin() + 7);
  26. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxy").FindFirstNotOf(s.begin(), s.end()), s.end() - 1);
  27. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxy").FindFirstNotOf(s.c_str()), s.end() - 1);
  28. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.begin(), s.end()), s.end());
  29. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.c_str()), s.end());
  30. const TString empty;
  31. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.begin(), empty.end()), empty.end());
  32. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.c_str()), empty.end());
  33. }
  34. Y_UNIT_TEST(FindFirstOfReverse) {
  35. TStringBuf s("some text");
  36. UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.rbegin(), s.rend()), s.rbegin());
  37. UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 3);
  38. UNIT_ASSERT_EQUAL(TCompactStrSpn("s").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 1);
  39. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.rbegin(), s.rend()), s.rend());
  40. TStringBuf empty;
  41. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.rbegin(), empty.rend()), empty.rend());
  42. }
  43. Y_UNIT_TEST(FindFirstNotOfReverse) {
  44. TStringBuf s("_abacabaxyz");
  45. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin());
  46. UNIT_ASSERT_EQUAL(TCompactStrSpn("xyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin() + 3);
  47. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rend() - 1);
  48. UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz_").FindFirstNotOf(s.rbegin(), s.rend()), s.rend());
  49. TStringBuf empty;
  50. UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.rbegin(), empty.rend()), empty.rend());
  51. }
  52. }