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