delim_string_iter_ut.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "delim_string_iter.h"
  2. #include <util/generic/vector.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. /// Test that TDelimStringIter build on top of given string and delimeter will produce expected sequence
  5. static void AssertStringSplit(const TString& str, const TString& delim, const TVector<TString>& expected) {
  6. TDelimStringIter it(str, delim);
  7. // test iterator invariants
  8. for (const auto& expectedString : expected) {
  9. UNIT_ASSERT(it.Valid());
  10. UNIT_ASSERT(bool(it));
  11. UNIT_ASSERT_STRINGS_EQUAL(*it, expectedString);
  12. ++it;
  13. }
  14. UNIT_ASSERT(!it.Valid());
  15. }
  16. Y_UNIT_TEST_SUITE(TDelimStrokaIterTestSuite) {
  17. Y_UNIT_TEST(SingleCharacterAsDelimiter) {
  18. AssertStringSplit(
  19. "Hello words!", " ", {"Hello", "words!"});
  20. }
  21. Y_UNIT_TEST(MultipleCharactersAsDelimiter) {
  22. AssertStringSplit(
  23. "0, 1, 1, 2, 3, 5, 8, 13, 21, 34", "1, ", {"0, ", "", "2, 3, 5, 8, 13, 2", "34"});
  24. }
  25. Y_UNIT_TEST(NoDelimitersPresent) {
  26. AssertStringSplit("This string could be yours", "\t", {"This string could be yours"});
  27. }
  28. Y_UNIT_TEST(Cdr) {
  29. TDelimStringIter it("a\tc\t", "\t");
  30. UNIT_ASSERT_STRINGS_EQUAL(*it, "a");
  31. UNIT_ASSERT_STRINGS_EQUAL(it.Cdr(), "c\t");
  32. ++it;
  33. UNIT_ASSERT_STRINGS_EQUAL(it.Cdr(), "");
  34. }
  35. Y_UNIT_TEST(ForIter) {
  36. TVector<TStringBuf> expected = {"1", "", "3@4", ""};
  37. TVector<TStringBuf> got;
  38. for (TStringBuf x : TDelimStroka("1@@@@3@4@@", "@@")) {
  39. got.push_back(x);
  40. }
  41. UNIT_ASSERT_EQUAL(got, expected);
  42. }
  43. }
  44. static void AssertKeyValueStringSplit(
  45. const TStringBuf str,
  46. const TStringBuf delim,
  47. const TVector<std::pair<TStringBuf, TStringBuf>>& expected) {
  48. TKeyValueDelimStringIter it(str, delim);
  49. for (const auto& expectedKeyValue : expected) {
  50. UNIT_ASSERT(it.Valid());
  51. UNIT_ASSERT_STRINGS_EQUAL(it.Key(), expectedKeyValue.first);
  52. UNIT_ASSERT_STRINGS_EQUAL(it.Value(), expectedKeyValue.second);
  53. ++it;
  54. }
  55. UNIT_ASSERT(!it.Valid());
  56. }
  57. Y_UNIT_TEST_SUITE(TKeyValueDelimStringIterTestSuite) {
  58. Y_UNIT_TEST(SingleCharacterAsDelimiter) {
  59. AssertKeyValueStringSplit(
  60. "abc=123,cde=qwer", ",",
  61. {{"abc", "123"},
  62. {"cde", "qwer"}});
  63. }
  64. Y_UNIT_TEST(MultipleCharactersAsDelimiter) {
  65. AssertKeyValueStringSplit(
  66. "abc=xyz@@qwerty=zxcv", "@@",
  67. {{"abc", "xyz"},
  68. {"qwerty", "zxcv"}});
  69. }
  70. Y_UNIT_TEST(NoDelimiters) {
  71. AssertKeyValueStringSplit(
  72. "abc=zz", ",",
  73. {{"abc", "zz"}});
  74. }
  75. Y_UNIT_TEST(EmptyElements) {
  76. AssertKeyValueStringSplit(
  77. "@@abc=zxy@@@@qwerty=y@@", "@@",
  78. {{"", ""},
  79. {"abc", "zxy"},
  80. {"", ""},
  81. {"qwerty", "y"},
  82. {"", ""}});
  83. }
  84. }