diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/deprecated/split/delim_string_iter_ut.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/deprecated/split/delim_string_iter_ut.cpp')
-rw-r--r-- | library/cpp/deprecated/split/delim_string_iter_ut.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/library/cpp/deprecated/split/delim_string_iter_ut.cpp b/library/cpp/deprecated/split/delim_string_iter_ut.cpp new file mode 100644 index 0000000000..18a8b2a160 --- /dev/null +++ b/library/cpp/deprecated/split/delim_string_iter_ut.cpp @@ -0,0 +1,99 @@ +#include "delim_string_iter.h" +#include <util/generic/vector.h> +#include <library/cpp/testing/unittest/registar.h> + +/// Test that TDelimStringIter build on top of given string and delimeter will produce expected sequence +static void AssertStringSplit(const TString& str, const TString& delim, const TVector<TString>& expected) { + TDelimStringIter it(str, delim); + + // test iterator invariants + for (const auto& expectedString : expected) { + UNIT_ASSERT(it.Valid()); + UNIT_ASSERT(bool(it)); + UNIT_ASSERT_STRINGS_EQUAL(it->ToString(), expectedString); + ++it; + } + UNIT_ASSERT(!it.Valid()); +}; + +Y_UNIT_TEST_SUITE(TDelimStrokaIterTestSuite) { + Y_UNIT_TEST(SingleCharacterAsDelimiter) { + AssertStringSplit( + "Hello words!", " ", {"Hello", "words!"}); + } + + Y_UNIT_TEST(MultipleCharactersAsDelimiter) { + AssertStringSplit( + "0, 1, 1, 2, 3, 5, 8, 13, 21, 34", "1, ", {"0, ", "", "2, 3, 5, 8, 13, 2", "34"}); + } + + Y_UNIT_TEST(NoDelimitersPresent) { + AssertStringSplit("This string could be yours", "\t", {"This string could be yours"}); + } + + Y_UNIT_TEST(Cdr) { + TDelimStringIter it("a\tc\t", "\t"); + UNIT_ASSERT_STRINGS_EQUAL(*it, "a"); + UNIT_ASSERT_STRINGS_EQUAL(it.Cdr(), "c\t"); + ++it; + UNIT_ASSERT_STRINGS_EQUAL(it.Cdr(), ""); + } + + Y_UNIT_TEST(ForIter) { + TVector<TStringBuf> expected = {"1", "", "3@4", ""}; + TVector<TStringBuf> got; + + for (TStringBuf x : TDelimStroka("1@@@@3@4@@", "@@")) { + got.push_back(x); + } + + UNIT_ASSERT_EQUAL(got, expected); + } +} + +static void AssertKeyValueStringSplit( + const TStringBuf str, + const TStringBuf delim, + const TVector<std::pair<TStringBuf, TStringBuf>>& expected) { + TKeyValueDelimStringIter it(str, delim); + + for (const auto& expectedKeyValue : expected) { + UNIT_ASSERT(it.Valid()); + UNIT_ASSERT_STRINGS_EQUAL(it.Key(), expectedKeyValue.first); + UNIT_ASSERT_STRINGS_EQUAL(it.Value(), expectedKeyValue.second); + ++it; + } + UNIT_ASSERT(!it.Valid()); +} + +Y_UNIT_TEST_SUITE(TKeyValueDelimStringIterTestSuite) { + Y_UNIT_TEST(SingleCharacterAsDelimiter) { + AssertKeyValueStringSplit( + "abc=123,cde=qwer", ",", + {{"abc", "123"}, + {"cde", "qwer"}}); + } + + Y_UNIT_TEST(MultipleCharactersAsDelimiter) { + AssertKeyValueStringSplit( + "abc=xyz@@qwerty=zxcv", "@@", + {{"abc", "xyz"}, + {"qwerty", "zxcv"}}); + } + + Y_UNIT_TEST(NoDelimiters) { + AssertKeyValueStringSplit( + "abc=zz", ",", + {{"abc", "zz"}}); + } + + Y_UNIT_TEST(EmptyElements) { + AssertKeyValueStringSplit( + "@@abc=zxy@@@@qwerty=y@@", "@@", + {{"", ""}, + {"abc", "zxy"}, + {"", ""}, + {"qwerty", "y"}, + {"", ""}}); + } +} |