strip_ut.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Y_UNIT_TEST(TestStrip) {
  6. struct TTest {
  7. const char* Str;
  8. const char* StripLeftRes;
  9. const char* StripRightRes;
  10. const char* StripRes;
  11. };
  12. static const TTest tests[] = {
  13. {" 012 ", "012 ", " 012", "012"},
  14. {" 012", "012", " 012", "012"},
  15. {"012\t\t", "012\t\t", "012", "012"},
  16. {"\t012\t", "012\t", "\t012", "012"},
  17. {"012", "012", "012", "012"},
  18. {"012\r\n", "012\r\n", "012", "012"},
  19. {"\n012\r", "012\r", "\n012", "012"},
  20. {"\n \t\r", "", "", ""},
  21. {"", "", "", ""},
  22. {"abc", "abc", "abc", "abc"},
  23. {"a c", "a c", "a c", "a c"},
  24. };
  25. for (const auto& test : tests) {
  26. TString inputStr(test.Str);
  27. TString s;
  28. Strip(inputStr, s);
  29. UNIT_ASSERT_EQUAL(s, test.StripRes);
  30. UNIT_ASSERT_EQUAL(StripString(inputStr), test.StripRes);
  31. UNIT_ASSERT_EQUAL(StripStringLeft(inputStr), test.StripLeftRes);
  32. UNIT_ASSERT_EQUAL(StripStringRight(inputStr), test.StripRightRes);
  33. TStringBuf inputStrBuf(test.Str);
  34. UNIT_ASSERT_EQUAL(StripString(inputStrBuf), test.StripRes);
  35. UNIT_ASSERT_EQUAL(StripStringLeft(inputStrBuf), test.StripLeftRes);
  36. UNIT_ASSERT_EQUAL(StripStringRight(inputStrBuf), test.StripRightRes);
  37. };
  38. }
  39. Y_UNIT_TEST(TestCustomStrip) {
  40. struct TTest {
  41. const char* Str;
  42. const char* Result;
  43. };
  44. static const TTest tests[] = {
  45. {"//012//", "012"},
  46. {"//012", "012"},
  47. {"012", "012"},
  48. {"012//", "012"},
  49. };
  50. for (auto test : tests) {
  51. UNIT_ASSERT_EQUAL(
  52. StripString(TString(test.Str), EqualsStripAdapter('/')),
  53. test.Result);
  54. };
  55. }
  56. Y_UNIT_TEST(TestCustomStripLeftRight) {
  57. struct TTest {
  58. const char* Str;
  59. const char* ResultLeft;
  60. const char* ResultRight;
  61. };
  62. static const TTest tests[] = {
  63. {"//012//", "012//", "//012"},
  64. {"//012", "012", "//012"},
  65. {"012", "012", "012"},
  66. {"012//", "012//", "012"},
  67. };
  68. for (const auto& test : tests) {
  69. UNIT_ASSERT_EQUAL(
  70. StripStringLeft(TString(test.Str), EqualsStripAdapter('/')),
  71. test.ResultLeft);
  72. UNIT_ASSERT_EQUAL(
  73. StripStringRight(TString(test.Str), EqualsStripAdapter('/')),
  74. test.ResultRight);
  75. };
  76. }
  77. Y_UNIT_TEST(TestNullStringStrip) {
  78. TStringBuf nullString(nullptr, nullptr);
  79. UNIT_ASSERT_EQUAL(
  80. StripString(nullString),
  81. TString());
  82. }
  83. Y_UNIT_TEST(TestWtrokaStrip) {
  84. UNIT_ASSERT_EQUAL(StripString(TWtringBuf(u" abc ")), u"abc");
  85. UNIT_ASSERT_EQUAL(StripStringLeft(TWtringBuf(u" abc ")), u"abc ");
  86. UNIT_ASSERT_EQUAL(StripStringRight(TWtringBuf(u" abc ")), u" abc");
  87. }
  88. Y_UNIT_TEST(TestWtrokaCustomStrip) {
  89. UNIT_ASSERT_EQUAL(
  90. StripString(
  91. TWtringBuf(u"/abc/"),
  92. EqualsStripAdapter(u'/')),
  93. u"abc");
  94. }
  95. Y_UNIT_TEST(TestCollapse) {
  96. TString s;
  97. Collapse(TString(" 123 456 "), s);
  98. UNIT_ASSERT(s == " 123 456 ");
  99. Collapse(TString(" 123 456 "), s, 10);
  100. UNIT_ASSERT(s == " 123 456 ");
  101. s = TString(" a b c ");
  102. TString s2 = s;
  103. CollapseInPlace(s2);
  104. UNIT_ASSERT(s == s2);
  105. #ifndef TSTRING_IS_STD_STRING
  106. UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
  107. #endif
  108. }
  109. Y_UNIT_TEST(TestCollapseText) {
  110. TString abs1("Very long description string written in unknown language.");
  111. TString abs2(abs1);
  112. TString abs3(abs1);
  113. CollapseText(abs1, 204);
  114. CollapseText(abs2, 54);
  115. CollapseText(abs3, 49);
  116. UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true);
  117. UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true);
  118. UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true);
  119. }
  120. }