strip_ut.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "strip.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/charset/wide.h>
  4. Y_UNIT_TEST_SUITE(TStripStringTest) {
  5. struct TStripTest {
  6. TStringBuf Str;
  7. TStringBuf StripLeftRes;
  8. TStringBuf StripRightRes;
  9. TStringBuf StripRes;
  10. };
  11. static constexpr TStripTest StripTests[] = {
  12. {" 012 ", "012 ", " 012", "012"},
  13. {" 012", "012", " 012", "012"},
  14. {"012\t\t", "012\t\t", "012", "012"},
  15. {"\t012\t", "012\t", "\t012", "012"},
  16. {"012", "012", "012", "012"},
  17. {"012\r\n", "012\r\n", "012", "012"},
  18. {"\n012\r", "012\r", "\n012", "012"},
  19. {"\n \t\r", "", "", ""},
  20. {"", "", "", ""},
  21. {"abc", "abc", "abc", "abc"},
  22. {"a c", "a c", "a c", "a c"},
  23. {" long string to avoid SSO \n", "long string to avoid SSO \n", " long string to avoid SSO", "long string to avoid SSO"},
  24. };
  25. Y_UNIT_TEST(TestStrip) {
  26. for (const auto& test : StripTests) {
  27. TString inputStr(test.Str);
  28. TString s;
  29. Strip(inputStr, s);
  30. UNIT_ASSERT_EQUAL(s, test.StripRes);
  31. UNIT_ASSERT_EQUAL(StripString(inputStr), test.StripRes);
  32. UNIT_ASSERT_EQUAL(StripStringLeft(inputStr), test.StripLeftRes);
  33. UNIT_ASSERT_EQUAL(StripStringRight(inputStr), test.StripRightRes);
  34. TStringBuf inputStrBuf(test.Str);
  35. UNIT_ASSERT_EQUAL(StripString(inputStrBuf), test.StripRes);
  36. UNIT_ASSERT_EQUAL(StripStringLeft(inputStrBuf), test.StripLeftRes);
  37. UNIT_ASSERT_EQUAL(StripStringRight(inputStrBuf), test.StripRightRes);
  38. };
  39. }
  40. Y_UNIT_TEST(TestStripInPlace) {
  41. for (const auto& test : StripTests) {
  42. TString str(test.Str);
  43. 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
  44. const void* stringPtrPrior = str.data();
  45. StripInPlace(str);
  46. const void* stringPtrAfter = str.data();
  47. UNIT_ASSERT_VALUES_EQUAL(str, test.StripRes);
  48. if (!test.Str.empty()) {
  49. UNIT_ASSERT_EQUAL_C(stringPtrPrior, stringPtrAfter, TString(test.Str).Quote()); // StripInPlace should reuse buffer of original string
  50. }
  51. }
  52. }
  53. Y_UNIT_TEST(TestCustomStrip) {
  54. struct TTest {
  55. const char* Str;
  56. const char* Result;
  57. };
  58. static const TTest tests[] = {
  59. {"//012//", "012"},
  60. {"//012", "012"},
  61. {"012", "012"},
  62. {"012//", "012"},
  63. };
  64. for (auto test : tests) {
  65. UNIT_ASSERT_EQUAL(
  66. StripString(TString(test.Str), EqualsStripAdapter('/')),
  67. test.Result);
  68. };
  69. }
  70. Y_UNIT_TEST(TestCustomStripLeftRight) {
  71. struct TTest {
  72. const char* Str;
  73. const char* ResultLeft;
  74. const char* ResultRight;
  75. };
  76. static const TTest tests[] = {
  77. {"//012//", "012//", "//012"},
  78. {"//012", "012", "//012"},
  79. {"012", "012", "012"},
  80. {"012//", "012//", "012"},
  81. };
  82. for (const auto& test : tests) {
  83. UNIT_ASSERT_EQUAL(
  84. StripStringLeft(TString(test.Str), EqualsStripAdapter('/')),
  85. test.ResultLeft);
  86. UNIT_ASSERT_EQUAL(
  87. StripStringRight(TString(test.Str), EqualsStripAdapter('/')),
  88. test.ResultRight);
  89. };
  90. }
  91. Y_UNIT_TEST(TestNullStringStrip) {
  92. TStringBuf nullString(nullptr, nullptr);
  93. UNIT_ASSERT_EQUAL(
  94. StripString(nullString),
  95. TString());
  96. }
  97. Y_UNIT_TEST(TestWtrokaStrip) {
  98. UNIT_ASSERT_EQUAL(StripString(TWtringBuf(u" abc ")), u"abc");
  99. UNIT_ASSERT_EQUAL(StripStringLeft(TWtringBuf(u" abc ")), u"abc ");
  100. UNIT_ASSERT_EQUAL(StripStringRight(TWtringBuf(u" abc ")), u" abc");
  101. }
  102. Y_UNIT_TEST(TestWtrokaCustomStrip) {
  103. UNIT_ASSERT_EQUAL(
  104. StripString(
  105. TWtringBuf(u"/abc/"),
  106. EqualsStripAdapter(u'/')),
  107. u"abc");
  108. }
  109. Y_UNIT_TEST(TestCollapseUtf32) {
  110. TUtf32String s;
  111. Collapse(UTF8ToUTF32<true>(" 123 456 "), s, IsWhitespace);
  112. UNIT_ASSERT(s == UTF8ToUTF32<true>(" 123 456 "));
  113. Collapse(UTF8ToUTF32<true>(" 123 456 "), s, IsWhitespace, 10);
  114. UNIT_ASSERT(s == UTF8ToUTF32<true>(" 123 456 "));
  115. s = UTF8ToUTF32<true>(" a b c ");
  116. TUtf32String s2 = s;
  117. CollapseInPlace(s2, IsWhitespace);
  118. UNIT_ASSERT(s == s2);
  119. #ifndef TSTRING_IS_STD_STRING
  120. UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
  121. #endif
  122. }
  123. Y_UNIT_TEST(TestCollapseUtf16) {
  124. TUtf16String s;
  125. Collapse(UTF8ToWide<true>(" 123 456 "), s);
  126. UNIT_ASSERT(s == UTF8ToWide<true>(" 123 456 "));
  127. Collapse(UTF8ToWide<true>(" 123 456 "), s, 10);
  128. UNIT_ASSERT(s == UTF8ToWide<true>(" 123 456 "));
  129. s = UTF8ToWide<true>(" a b c ");
  130. TUtf16String s2 = s;
  131. CollapseInPlace(s2);
  132. UNIT_ASSERT(s == s2);
  133. #ifndef TSTRING_IS_STD_STRING
  134. UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
  135. #endif
  136. }
  137. Y_UNIT_TEST(TestCollapse) {
  138. TString s;
  139. Collapse(TString(" 123 456 "), s);
  140. UNIT_ASSERT(s == " 123 456 ");
  141. Collapse(TString(" 123 456 "), s, 10);
  142. UNIT_ASSERT(s == " 123 456 ");
  143. s = TString(" a b c ");
  144. TString s2 = s;
  145. CollapseInPlace(s2);
  146. UNIT_ASSERT(s == s2);
  147. #ifndef TSTRING_IS_STD_STRING
  148. UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
  149. #endif
  150. }
  151. Y_UNIT_TEST(TestCollapseText) {
  152. TString abs1("Very long description string written in unknown language.");
  153. TString abs2(abs1);
  154. TString abs3(abs1);
  155. CollapseText(abs1, 204);
  156. CollapseText(abs2, 54);
  157. CollapseText(abs3, 49);
  158. UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true);
  159. UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true);
  160. UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true);
  161. }
  162. }