split_iterator_ut.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "split_iterator.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TSplitIteratorTest: public TTestBase {
  4. UNIT_TEST_SUITE(TSplitIteratorTest);
  5. UNIT_TEST(TestDelimiters);
  6. UNIT_TEST(TestDelimitersSplit);
  7. UNIT_TEST(TestDelimitersStrictSplit);
  8. UNIT_TEST(TestTail);
  9. UNIT_TEST(TestScreenedDelimitersSplit);
  10. UNIT_TEST(TestSubstringDelimiter);
  11. UNIT_TEST_SUITE_END();
  12. public:
  13. void TestDelimiters();
  14. void TestDelimitersSplit();
  15. void TestDelimitersStrictSplit();
  16. void TestTail();
  17. void TestScreenedDelimitersSplit();
  18. void TestSubstringDelimiter();
  19. };
  20. void TSplitIteratorTest::TestDelimiters() {
  21. TSplitDelimiters delims("@");
  22. for (int i = 0; i < 256; ++i)
  23. if ('@' != i) {
  24. UNIT_ASSERT(!delims.IsDelimiter((ui8)i));
  25. } else {
  26. UNIT_ASSERT(delims.IsDelimiter((ui8)i));
  27. }
  28. }
  29. void TSplitIteratorTest::TestDelimitersSplit() {
  30. {
  31. TString s = "1a3b45cd";
  32. TSplitDelimiters delims("abcd");
  33. TDelimitersSplit split(s, delims);
  34. TSplitTokens tokens;
  35. Split(split, &tokens);
  36. TSplitTokens pattern = {"1", "3", "45"};
  37. UNIT_ASSERT(tokens == pattern);
  38. }
  39. {
  40. TString s = "aaaaaa";
  41. TSplitDelimiters delims("abcd");
  42. TDelimitersSplit split(s, delims);
  43. TSplitTokens tokens;
  44. Split(split, &tokens);
  45. TSplitTokens pattern = {};
  46. UNIT_ASSERT(tokens == pattern);
  47. }
  48. }
  49. void TSplitIteratorTest::TestDelimitersStrictSplit() {
  50. {
  51. TString s = "grp@2";
  52. TSplitDelimiters delims("@");
  53. TDelimitersStrictSplit split(s, delims);
  54. TSplitTokens tokens;
  55. Split(split, &tokens);
  56. TSplitTokens pattern = {"grp", "2"};
  57. UNIT_ASSERT(tokens == pattern);
  58. }
  59. {
  60. TString s = "@grp@2@@";
  61. TSplitDelimiters delims("@");
  62. TDelimitersStrictSplit split(s, delims);
  63. TSplitTokens tokens;
  64. Split(split, &tokens);
  65. TSplitTokens pattern = {"", "grp", "2", ""};
  66. UNIT_ASSERT(tokens == pattern);
  67. }
  68. }
  69. void TSplitIteratorTest::TestTail() {
  70. TString s = "grp@2@4";
  71. TSplitDelimiters delims("@");
  72. TDelimitersSplit split(s, delims);
  73. TDelimitersSplit::TIterator it = split.Iterator();
  74. UNIT_ASSERT_EQUAL(it.GetTail(), "grp@2@4");
  75. it.Next();
  76. UNIT_ASSERT_EQUAL(it.GetTail(), "2@4");
  77. it.Next();
  78. UNIT_ASSERT_EQUAL(it.GetTail(), "4");
  79. it.Next();
  80. UNIT_ASSERT_EQUAL(it.GetTail(), "");
  81. }
  82. void TSplitIteratorTest::TestScreenedDelimitersSplit() {
  83. {
  84. const TString s = "77.88.58.91 - - [28/Aug/2008:00:08:07 +0400] \"GET /export/mordashka.tgz HTTP/1.1\" 304 - \"-\" \"libwww-perl/5.805\" \"news.yandex.ru,80\" \"-\" \"-\" 1219867687 \"0\" 3283 2";
  85. const TSplitDelimiters delims(" ");
  86. const TSplitDelimiters screens("\"[]");
  87. const TScreenedDelimitersSplit splitter(s, delims, screens);
  88. TScreenedDelimitersSplit::TIterator it = splitter.Iterator();
  89. UNIT_ASSERT_EQUAL(it.NextString(), "77.88.58.91");
  90. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  91. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  92. UNIT_ASSERT_EQUAL(it.NextString(), "[28/Aug/2008:00:08:07 +0400]");
  93. UNIT_ASSERT_EQUAL(it.NextString(), "\"GET /export/mordashka.tgz HTTP/1.1\"");
  94. UNIT_ASSERT_EQUAL(it.NextString(), "304");
  95. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  96. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  97. UNIT_ASSERT_EQUAL(it.NextString(), "\"libwww-perl/5.805\"");
  98. UNIT_ASSERT_EQUAL(it.NextString(), "\"news.yandex.ru,80\"");
  99. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  100. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  101. UNIT_ASSERT_EQUAL(it.NextString(), "1219867687");
  102. UNIT_ASSERT_EQUAL(it.NextString(), "\"0\"");
  103. UNIT_ASSERT_EQUAL(it.NextString(), "3283");
  104. UNIT_ASSERT_EQUAL(it.NextString(), "2");
  105. }
  106. {
  107. const TString s = "77.88.58.91 - - [28/Aug/2008:00:08:07 +0400] \"GET /export/mordashka.tgz HTTP/1.1\" 304 - \"-\" \"libwww-perl/5.805\" \"news.yandex.ru,80\" \"-\" \"-\" 1219867687 \"0\" 3283 2";
  108. const TSplitDelimiters delims(" ");
  109. const TSplitDelimiters screens("\"[]");
  110. const TScreenedDelimitersSplit splitter(s.Data(), s.Size(), delims, screens);
  111. TScreenedDelimitersSplit::TIterator it = splitter.Iterator();
  112. UNIT_ASSERT_EQUAL(it.NextString(), "77.88.58.91");
  113. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  114. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  115. UNIT_ASSERT_EQUAL(it.NextString(), "[28/Aug/2008:00:08:07 +0400]");
  116. UNIT_ASSERT_EQUAL(it.NextString(), "\"GET /export/mordashka.tgz HTTP/1.1\"");
  117. UNIT_ASSERT_EQUAL(it.NextString(), "304");
  118. UNIT_ASSERT_EQUAL(it.NextString(), "-");
  119. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  120. UNIT_ASSERT_EQUAL(it.NextString(), "\"libwww-perl/5.805\"");
  121. UNIT_ASSERT_EQUAL(it.NextString(), "\"news.yandex.ru,80\"");
  122. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  123. UNIT_ASSERT_EQUAL(it.NextString(), "\"-\"");
  124. UNIT_ASSERT_EQUAL(it.NextString(), "1219867687");
  125. UNIT_ASSERT_EQUAL(it.NextString(), "\"0\"");
  126. UNIT_ASSERT_EQUAL(it.NextString(), "3283");
  127. UNIT_ASSERT_EQUAL(it.NextString(), "2");
  128. }
  129. }
  130. void TSplitIteratorTest::TestSubstringDelimiter() {
  131. const TString s = "a@@bb@@cc@c.d@@r";
  132. static const TSubstringSplitDelimiter delimiter("@@");
  133. const TSubstringSplit splitter(s, delimiter);
  134. TSubstringSplit::TIterator it = splitter.Iterator();
  135. UNIT_ASSERT_EQUAL(it.NextString(), "a");
  136. UNIT_ASSERT_EQUAL(it.NextString(), "bb");
  137. UNIT_ASSERT_EQUAL(it.NextString(), "cc@c.d");
  138. UNIT_ASSERT_EQUAL(it.NextString(), "r");
  139. UNIT_ASSERT(it.Eof());
  140. }
  141. UNIT_TEST_SUITE_REGISTRATION(TSplitIteratorTest);