diff options
author | swarmer <swarmer@yandex-team.ru> | 2022-05-24 01:42:42 +0300 |
---|---|---|
committer | swarmer <swarmer@yandex-team.ru> | 2022-05-24 01:42:42 +0300 |
commit | 9e6590dab63a1a07e738a246030af9b888333214 (patch) | |
tree | 038ab3bdf14231ab80bcfa12922f2371ea0c0a31 /util/string/strip_ut.cpp | |
parent | cc9b48149be0bca6e6c350983ace4fc660902b80 (diff) | |
download | ydb-9e6590dab63a1a07e738a246030af9b888333214.tar.gz |
[util] StripInPlace should modify string in-place
ref:2aec19528c2378a0994534ec032550413a095389
Diffstat (limited to 'util/string/strip_ut.cpp')
-rw-r--r-- | util/string/strip_ut.cpp | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp index df4f9bc57d..0b1687ffa7 100644 --- a/util/string/strip_ut.cpp +++ b/util/string/strip_ut.cpp @@ -5,28 +5,29 @@ #include <util/charset/wide.h> Y_UNIT_TEST_SUITE(TStripStringTest) { - Y_UNIT_TEST(TestStrip) { - struct TTest { - const char* Str; - const char* StripLeftRes; - const char* StripRightRes; - const char* StripRes; - }; - static const TTest tests[] = { - {" 012 ", "012 ", " 012", "012"}, - {" 012", "012", " 012", "012"}, - {"012\t\t", "012\t\t", "012", "012"}, - {"\t012\t", "012\t", "\t012", "012"}, - {"012", "012", "012", "012"}, - {"012\r\n", "012\r\n", "012", "012"}, - {"\n012\r", "012\r", "\n012", "012"}, - {"\n \t\r", "", "", ""}, - {"", "", "", ""}, - {"abc", "abc", "abc", "abc"}, - {"a c", "a c", "a c", "a c"}, - }; + struct TStripTest { + TStringBuf Str; + TStringBuf StripLeftRes; + TStringBuf StripRightRes; + TStringBuf StripRes; + }; + static constexpr TStripTest StripTests[] = { + {" 012 ", "012 ", " 012", "012"}, + {" 012", "012", " 012", "012"}, + {"012\t\t", "012\t\t", "012", "012"}, + {"\t012\t", "012\t", "\t012", "012"}, + {"012", "012", "012", "012"}, + {"012\r\n", "012\r\n", "012", "012"}, + {"\n012\r", "012\r", "\n012", "012"}, + {"\n \t\r", "", "", ""}, + {"", "", "", ""}, + {"abc", "abc", "abc", "abc"}, + {"a c", "a c", "a c", "a c"}, + {" long string to avoid SSO \n", "long string to avoid SSO \n", " long string to avoid SSO", "long string to avoid SSO"}, + }; - for (const auto& test : tests) { + Y_UNIT_TEST(TestStrip) { + for (const auto& test : StripTests) { TString inputStr(test.Str); TString s; @@ -44,6 +45,20 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { }; } + Y_UNIT_TEST(TestStripInPlace) { + for (const auto& test : StripTests) { + TString str(test.Str); + Y_ASSERT(str.IsDetached() || str.empty()); // prerequisite of the test; check that we don't try to modify shared COW-string in-place by accident + const void* stringPtrPrior = str.data(); + StripInPlace(str); + const void* stringPtrAfter = str.data(); + UNIT_ASSERT_VALUES_EQUAL(str, test.StripRes); + if (!test.Str.empty()) { + UNIT_ASSERT_EQUAL_C(stringPtrPrior, stringPtrAfter, TString(test.Str).Quote()); // StripInPlace should reuse buffer of original string + } + } + } + Y_UNIT_TEST(TestCustomStrip) { struct TTest { const char* Str; |