1 | // |
2 | // StringTokenizerTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | #include <iostream> |
10 | |
11 | #include "StringTokenizerTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/StringTokenizer.h" |
15 | #include "Poco/Exception.h" |
16 | |
17 | |
18 | using Poco::StringTokenizer; |
19 | using Poco::RangeException; |
20 | using Poco::NotFoundException; |
21 | |
22 | |
23 | StringTokenizerTest::StringTokenizerTest(const std::string& rName): CppUnit::TestCase(rName) |
24 | { |
25 | } |
26 | |
27 | |
28 | StringTokenizerTest::~StringTokenizerTest() |
29 | { |
30 | } |
31 | |
32 | |
33 | void StringTokenizerTest::testStringTokenizer() |
34 | { |
35 | { |
36 | StringTokenizer st("" , "" ); |
37 | assertTrue (st.begin() == st.end()); |
38 | } |
39 | { |
40 | StringTokenizer st("" , "" , StringTokenizer::TOK_IGNORE_EMPTY); |
41 | assertTrue (st.begin() == st.end()); |
42 | } |
43 | { |
44 | StringTokenizer st("" , "" , StringTokenizer::TOK_TRIM); |
45 | assertTrue (st.begin() == st.end()); |
46 | } |
47 | { |
48 | StringTokenizer st("" , "" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
49 | assertTrue (st.begin() == st.end()); |
50 | } |
51 | { |
52 | StringTokenizer st("abc" , "" ); |
53 | StringTokenizer::Iterator it = st.begin(); |
54 | assertTrue (st.find("abc" ) == 0); |
55 | assertTrue (it != st.end()); |
56 | assertTrue (*it++ == "abc" ); |
57 | assertTrue (it == st.end()); |
58 | } |
59 | { |
60 | StringTokenizer st("abc " , "" , StringTokenizer::TOK_TRIM); |
61 | StringTokenizer::Iterator it = st.begin(); |
62 | assertTrue (st.find("abc" ) == 0); |
63 | assertTrue (it != st.end()); |
64 | assertTrue (*it++ == "abc" ); |
65 | assertTrue (it == st.end()); |
66 | } |
67 | { |
68 | StringTokenizer st(" abc " , "" , StringTokenizer::TOK_TRIM); |
69 | StringTokenizer::Iterator it = st.begin(); |
70 | assertTrue (st.find("abc" ) == 0); |
71 | assertTrue (it != st.end()); |
72 | assertTrue (*it++ == "abc" ); |
73 | assertTrue (it == st.end()); |
74 | } |
75 | { |
76 | StringTokenizer st(" abc" , "" , StringTokenizer::TOK_TRIM); |
77 | StringTokenizer::Iterator it = st.begin(); |
78 | assertTrue (st.find("abc" ) == 0); |
79 | assertTrue (it != st.end()); |
80 | assertTrue (*it++ == "abc" ); |
81 | assertTrue (it == st.end()); |
82 | } |
83 | { |
84 | StringTokenizer st("abc" , "b" ); |
85 | StringTokenizer::Iterator it = st.begin(); |
86 | assertTrue (st.find("a" ) == 0); |
87 | assertTrue (st.find("c" ) == 1); |
88 | assertTrue (it != st.end()); |
89 | assertTrue (*it++ == "a" ); |
90 | assertTrue (it != st.end()); |
91 | assertTrue (*it++ == "c" ); |
92 | assertTrue (it == st.end()); |
93 | } |
94 | { |
95 | StringTokenizer st("abc" , "b" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
96 | StringTokenizer::Iterator it = st.begin(); |
97 | assertTrue (st.find("a" ) == 0); |
98 | assertTrue (st.find("c" ) == 1); |
99 | assertTrue (it != st.end()); |
100 | assertTrue (*it++ == "a" ); |
101 | assertTrue (it != st.end()); |
102 | assertTrue (*it++ == "c" ); |
103 | assertTrue (it == st.end()); |
104 | } |
105 | { |
106 | StringTokenizer st("abc" , "bc" ); |
107 | StringTokenizer::Iterator it = st.begin(); |
108 | assertTrue (st.find("a" ) == 0); |
109 | assertTrue (st.find("" ) == 1); |
110 | assertTrue (it != st.end()); |
111 | assertTrue (*it++ == "a" ); |
112 | assertTrue (it != st.end()); |
113 | assertTrue (*it++ == "" ); |
114 | assertTrue (it != st.end()); |
115 | assertTrue (*it++ == "" ); |
116 | assertTrue (it == st.end()); |
117 | } |
118 | { |
119 | StringTokenizer st("abc" , "bc" , StringTokenizer::TOK_TRIM); |
120 | StringTokenizer::Iterator it = st.begin(); |
121 | assertTrue (st.find("a" ) == 0); |
122 | assertTrue (st.find("" ) == 1); |
123 | assertTrue (it != st.end()); |
124 | assertTrue (*it++ == "a" ); |
125 | assertTrue (it != st.end()); |
126 | assertTrue (*it++ == "" ); |
127 | assertTrue (it != st.end()); |
128 | assertTrue (*it++ == "" ); |
129 | assertTrue (it == st.end()); |
130 | } |
131 | { |
132 | StringTokenizer st("abc" , "bc" , StringTokenizer::TOK_IGNORE_EMPTY); |
133 | StringTokenizer::Iterator it = st.begin(); |
134 | assertTrue (st.find("a" ) == 0); |
135 | assertTrue (it != st.end()); |
136 | assertTrue (*it++ == "a" ); |
137 | assertTrue (it == st.end()); |
138 | } |
139 | { |
140 | StringTokenizer st("abc" , "bc" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
141 | StringTokenizer::Iterator it = st.begin(); |
142 | assertTrue (st.find("a" ) == 0); |
143 | assertTrue (it != st.end()); |
144 | assertTrue (*it++ == "a" ); |
145 | assertTrue (it == st.end()); |
146 | } |
147 | { |
148 | StringTokenizer st("abc" , "bc" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
149 | StringTokenizer::Iterator it = st.begin(); |
150 | assertTrue (st.find("a" ) == 0); |
151 | assertTrue (it != st.end()); |
152 | assertTrue (*it++ == "a" ); |
153 | assertTrue (it == st.end()); |
154 | } |
155 | { |
156 | StringTokenizer st("a a,c c" , "," ); |
157 | StringTokenizer::Iterator it = st.begin(); |
158 | assertTrue (st.find("a a" ) == 0); |
159 | assertTrue (st.find("c c" ) == 1); |
160 | assertTrue (it != st.end()); |
161 | assertTrue (*it++ == "a a" ); |
162 | assertTrue (it != st.end()); |
163 | assertTrue (*it++ == "c c" ); |
164 | assertTrue (it == st.end()); |
165 | } |
166 | { |
167 | StringTokenizer st("a a,c c" , "," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
168 | StringTokenizer::Iterator it = st.begin(); |
169 | assertTrue (st.find("a a" ) == 0); |
170 | assertTrue (st.find("c c" ) == 1); |
171 | assertTrue (it != st.end()); |
172 | assertTrue (*it++ == "a a" ); |
173 | assertTrue (it != st.end()); |
174 | assertTrue (*it++ == "c c" ); |
175 | assertTrue (it == st.end()); |
176 | } |
177 | { |
178 | StringTokenizer st(" a a , , c c " , "," ); |
179 | StringTokenizer::Iterator it = st.begin(); |
180 | assertTrue (st.find(" a a " ) == 0); |
181 | assertTrue (st.find(" " ) == 1); |
182 | assertTrue (st.find(" c c " ) == 2); |
183 | assertTrue (it != st.end()); |
184 | assertTrue (*it++ == " a a " ); |
185 | assertTrue (it != st.end()); |
186 | assertTrue (*it++ == " " ); |
187 | assertTrue (it != st.end()); |
188 | assertTrue (*it++ == " c c " ); |
189 | assertTrue (it == st.end()); |
190 | } |
191 | { |
192 | StringTokenizer st(" a a , , c c " , "," , StringTokenizer::TOK_TRIM); |
193 | StringTokenizer::Iterator it = st.begin(); |
194 | assertTrue (st.find("a a" ) == 0); |
195 | assertTrue (st.find("" ) == 1); |
196 | assertTrue (st.find("c c" ) == 2); |
197 | assertTrue (it != st.end()); |
198 | assertTrue (*it++ == "a a" ); |
199 | assertTrue (it != st.end()); |
200 | assertTrue (*it++ == "" ); |
201 | assertTrue (it != st.end()); |
202 | assertTrue (*it++ == "c c" ); |
203 | assertTrue (it == st.end()); |
204 | } |
205 | { |
206 | StringTokenizer st(" a a , , c c " , "," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
207 | StringTokenizer::Iterator it = st.begin(); |
208 | assertTrue (st.find("a a" ) == 0); |
209 | assertTrue (st.find("c c" ) == 1); |
210 | assertTrue (it != st.end()); |
211 | assertTrue (*it++ == "a a" ); |
212 | assertTrue (it != st.end()); |
213 | assertTrue (*it++ == "c c" ); |
214 | assertTrue (it == st.end()); |
215 | } |
216 | { |
217 | StringTokenizer st("abc,def,,ghi , jk, l " , "," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
218 | StringTokenizer::Iterator it = st.begin(); |
219 | assertTrue (st.find("abc" ) == 0); |
220 | assertTrue (st.find("def" ) == 1); |
221 | assertTrue (st.find("ghi" ) == 2); |
222 | assertTrue (st.find("jk" ) == 3); |
223 | assertTrue (st.find("l" ) == 4); |
224 | assertTrue (it != st.end()); |
225 | assertTrue (*it++ == "abc" ); |
226 | assertTrue (it != st.end()); |
227 | assertTrue (*it++ == "def" ); |
228 | assertTrue (it != st.end()); |
229 | assertTrue (*it++ == "ghi" ); |
230 | assertTrue (it != st.end()); |
231 | assertTrue (*it++ == "jk" ); |
232 | assertTrue (it != st.end()); |
233 | assertTrue (*it++ == "l" ); |
234 | assertTrue (it == st.end()); |
235 | } |
236 | { |
237 | StringTokenizer st("abc,def,,ghi // jk, l " , ",/" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
238 | StringTokenizer::Iterator it = st.begin(); |
239 | assertTrue (st.find("abc" ) == 0); |
240 | assertTrue (st.find("def" ) == 1); |
241 | assertTrue (st.find("ghi" ) == 2); |
242 | assertTrue (st.find("jk" ) == 3); |
243 | assertTrue (st.find("l" ) == 4); |
244 | assertTrue (it != st.end()); |
245 | assertTrue (*it++ == "abc" ); |
246 | assertTrue (it != st.end()); |
247 | assertTrue (*it++ == "def" ); |
248 | assertTrue (it != st.end()); |
249 | assertTrue (*it++ == "ghi" ); |
250 | assertTrue (it != st.end()); |
251 | assertTrue (*it++ == "jk" ); |
252 | assertTrue (it != st.end()); |
253 | assertTrue (*it++ == "l" ); |
254 | assertTrue (it == st.end()); |
255 | } |
256 | { |
257 | StringTokenizer st("a/bc,def,,ghi // jk, l " , ",/" , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
258 | StringTokenizer::Iterator it = st.begin(); |
259 | assertTrue (st.find("a" ) == 0); |
260 | assertTrue (st.find("bc" ) == 1); |
261 | assertTrue (st.find("def" ) == 2); |
262 | assertTrue (st.find("ghi" ) == 3); |
263 | assertTrue (st.find("jk" ) == 4); |
264 | assertTrue (st.find("l" ) == 5); |
265 | assertTrue (it != st.end()); |
266 | assertTrue (*it++ == "a" ); |
267 | assertTrue (it != st.end()); |
268 | assertTrue (*it++ == "bc" ); |
269 | assertTrue (it != st.end()); |
270 | assertTrue (*it++ == "def" ); |
271 | assertTrue (it != st.end()); |
272 | assertTrue (*it++ == "ghi" ); |
273 | assertTrue (it != st.end()); |
274 | assertTrue (*it++ == "jk" ); |
275 | assertTrue (it != st.end()); |
276 | assertTrue (*it++ == "l" ); |
277 | assertTrue (it == st.end()); |
278 | } |
279 | { |
280 | StringTokenizer st(",ab,cd," , "," ); |
281 | StringTokenizer::Iterator it = st.begin(); |
282 | assertTrue (st.find("" ) == 0); |
283 | assertTrue (st.find("ab" ) == 1); |
284 | assertTrue (st.find("cd" ) == 2); |
285 | assertTrue (it != st.end()); |
286 | assertTrue (*it++ == "" ); |
287 | assertTrue (it != st.end()); |
288 | assertTrue (*it++ == "ab" ); |
289 | assertTrue (it != st.end()); |
290 | assertTrue (*it++ == "cd" ); |
291 | assertTrue (it != st.end()); |
292 | assertTrue (*it++ == "" ); |
293 | assertTrue (it == st.end()); |
294 | } |
295 | { |
296 | StringTokenizer st(",ab,cd," , "," , StringTokenizer::TOK_IGNORE_EMPTY); |
297 | StringTokenizer::Iterator it = st.begin(); |
298 | assertTrue (st.find("ab" ) == 0); |
299 | assertTrue (st.find("cd" ) == 1); |
300 | assertTrue (it != st.end()); |
301 | assertTrue (*it++ == "ab" ); |
302 | assertTrue (it != st.end()); |
303 | assertTrue (*it++ == "cd" ); |
304 | assertTrue (it == st.end()); |
305 | } |
306 | { |
307 | StringTokenizer st(" , ab , cd , " , "," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
308 | StringTokenizer::Iterator it = st.begin(); |
309 | assertTrue (st.find("ab" ) == 0); |
310 | assertTrue (st.find("cd" ) == 1); |
311 | assertTrue (it != st.end()); |
312 | assertTrue (*it++ == "ab" ); |
313 | assertTrue (it != st.end()); |
314 | assertTrue (*it++ == "cd" ); |
315 | assertTrue (it == st.end()); |
316 | } |
317 | { |
318 | StringTokenizer st("1 : 2 , : 3 " , ":," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
319 | assertTrue (st.count() == 3); |
320 | assertTrue (st[0] == "1" ); |
321 | assertTrue (st[1] == "2" ); |
322 | assertTrue (st[2] == "3" ); |
323 | assertTrue (st.find("1" ) == 0); |
324 | assertTrue (st.find("2" ) == 1); |
325 | assertTrue (st.find("3" ) == 2); |
326 | } |
327 | |
328 | { |
329 | Poco::StringTokenizer st(" 2- " ,"-" , Poco::StringTokenizer::TOK_TRIM); |
330 | assertTrue (st.count() == 2); |
331 | assertTrue (st[0] == "2" ); |
332 | assertTrue (st[1] == "" ); |
333 | } |
334 | } |
335 | |
336 | |
337 | void StringTokenizerTest::testFind() |
338 | { |
339 | StringTokenizer st("0,1,2,3,3,2,1,0" , "," , StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); |
340 | |
341 | assertTrue (st.count() == 8); |
342 | assertTrue (2 == st.count("0" )); |
343 | assertTrue (2 == st.count("1" )); |
344 | assertTrue (2 == st.count("2" )); |
345 | assertTrue (2 == st.count("3" )); |
346 | assertTrue (0 == st.count("4" )); |
347 | assertTrue (0 == st.count("5" )); |
348 | assertTrue (0 == st.count("6" )); |
349 | assertTrue (0 == st.count("7" )); |
350 | |
351 | assertTrue (st[0] == "0" ); |
352 | assertTrue (st[1] == "1" ); |
353 | assertTrue (st[2] == "2" ); |
354 | assertTrue (st[3] == "3" ); |
355 | assertTrue (st[4] == "3" ); |
356 | assertTrue (st[5] == "2" ); |
357 | assertTrue (st[6] == "1" ); |
358 | assertTrue (st[7] == "0" ); |
359 | |
360 | assertTrue (st.has("0" )); |
361 | assertTrue (st.has("1" )); |
362 | assertTrue (st.has("2" )); |
363 | assertTrue (st.has("3" )); |
364 | assertTrue (!st.has("4" )); |
365 | assertTrue (!st.has("5" )); |
366 | assertTrue (!st.has("6" )); |
367 | assertTrue (!st.has("7" )); |
368 | |
369 | assertTrue (st.find("0" ) == 0); |
370 | assertTrue (st.find("1" ) == 1); |
371 | assertTrue (st.find("2" ) == 2); |
372 | assertTrue (st.find("3" ) == 3); |
373 | |
374 | assertTrue (st.find("0" , 1) == 7); |
375 | assertTrue (st.find("1" , 2) == 6); |
376 | assertTrue (st.find("2" , 3) == 5); |
377 | assertTrue (st.find("3" , 4) == 4); |
378 | |
379 | try |
380 | { |
381 | std::size_t p = st.find("4" ); |
382 | fail ("must fail" ); |
383 | } |
384 | catch (NotFoundException&) { } |
385 | |
386 | try |
387 | { |
388 | std::string s = st[8]; |
389 | fail ("must fail" ); |
390 | } |
391 | catch (RangeException&) { } |
392 | |
393 | st[0] = "1" ; |
394 | st[7] = "1" ; |
395 | assertTrue (st[0] == "1" ); |
396 | assertTrue (st[7] == "1" ); |
397 | assertTrue (0 == st.count("0" )); |
398 | assertTrue (4 == st.count("1" )); |
399 | |
400 | st.replace("2" , "5" ); |
401 | assertTrue (0 == st.count("2" )); |
402 | assertTrue (2 == st.count("5" )); |
403 | |
404 | st.replace("3" , "6" , 4); |
405 | assertTrue (1 == st.count("3" )); |
406 | assertTrue (1 == st.count("6" )); |
407 | } |
408 | |
409 | |
410 | void StringTokenizerTest::setUp() |
411 | { |
412 | } |
413 | |
414 | |
415 | void StringTokenizerTest::tearDown() |
416 | { |
417 | } |
418 | |
419 | |
420 | CppUnit::Test* StringTokenizerTest::suite() |
421 | { |
422 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StringTokenizerTest" ); |
423 | |
424 | CppUnit_addTest(pSuite, StringTokenizerTest, testStringTokenizer); |
425 | CppUnit_addTest(pSuite, StringTokenizerTest, testFind); |
426 | |
427 | return pSuite; |
428 | } |
429 | |