xref: /haiku/src/tests/kits/support/bstring/StringSearchTest.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 #include "StringSearchTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 #include <stdio.h>
5 
6 StringSearchTest::StringSearchTest(std::string name) :
7 		BTestCase(name)
8 {
9 }
10 
11 
12 
13 StringSearchTest::~StringSearchTest()
14 {
15 }
16 
17 
18 void
19 StringSearchTest::PerformTest(void)
20 {
21 	BString *string1, *string2;
22 	int32 i;
23 
24 	//FindFirst(BString&)
25 	NextSubTest();
26 	string1 = new BString("last but not least");
27 	string2 = new BString("st");
28 	i = string1->FindFirst(*string2);
29 	CPPUNIT_ASSERT(i == 2);
30 	delete string1;
31 	delete string2;
32 
33 	NextSubTest();
34 	string1 = new BString;
35 	string2 = new BString("some text");
36 	i = string1->FindFirst(*string2);
37 	CPPUNIT_ASSERT(i == B_ERROR);
38 	delete string1;
39 	delete string2;
40 
41 	//FindFirst(char*)
42 	NextSubTest();
43 	string1 = new BString("last but not least");
44 	i = string1->FindFirst("st");
45 	CPPUNIT_ASSERT(i == 2);
46 	delete string1;
47 
48 	NextSubTest();
49 	string1 = new BString;
50 	i = string1->FindFirst("some text");
51 	CPPUNIT_ASSERT(i == B_ERROR);
52 	delete string1;
53 
54 #ifndef TEST_R5
55 //	Commented, since crashes R5
56 	NextSubTest();
57 	string1 = new BString("string");
58 	i = string1->FindFirst((char*)NULL);
59 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
60 	delete string1;
61 #endif
62 
63 	//FindFirst(BString&, int32)
64 	NextSubTest();
65 	string1 = new BString("abc abc abc");
66 	string2 = new BString("abc");
67 	i = string1->FindFirst(*string2, 5);
68 	CPPUNIT_ASSERT(i == 8);
69 	delete string1;
70 	delete string2;
71 
72 	NextSubTest();
73 	string1 = new BString("abc abc abc");
74 	string2 = new BString("abc");
75 	i = string1->FindFirst(*string2, 200);
76 	CPPUNIT_ASSERT(i == B_ERROR);
77 	delete string1;
78 	delete string2;
79 
80 	NextSubTest();
81 	string1 = new BString("abc abc abc");
82 	string2 = new BString("abc");
83 	i = string1->FindFirst(*string2, -10);
84 	CPPUNIT_ASSERT(i == B_ERROR);
85 	delete string1;
86 	delete string2;
87 
88 	//FindFirst(const char*, int32)
89 	NextSubTest();
90 	string1 = new BString("abc abc abc");
91 	i = string1->FindFirst("abc", 2);
92 	CPPUNIT_ASSERT(i == 4);
93 	delete string1;
94 
95 	NextSubTest();
96 	string1 = new BString("abc abc abc");
97 	i = string1->FindFirst("abc", 200);
98 	CPPUNIT_ASSERT(i == B_ERROR);
99 	delete string1;
100 
101 	NextSubTest();
102 	string1 = new BString("abc abc abc");
103 	i = string1->FindFirst("abc", -10);
104 	CPPUNIT_ASSERT(i == B_ERROR);
105 	delete string1;
106 
107 #ifndef TEST_R5
108 	//Commented since crashes R5
109 	NextSubTest();
110 	string1 = new BString("abc abc abc");
111 	i = string1->FindFirst((char*)NULL, 3);
112 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
113 	delete string1;
114 #endif
115 
116 	//FindFirst(char)
117 	NextSubTest();
118 	string1 = new BString("abcd abcd");
119 	i = string1->FindFirst('c');
120 	CPPUNIT_ASSERT(i == 2);
121 	delete string1;
122 
123 	NextSubTest();
124 	string1 = new BString("abcd abcd");
125 	i = string1->FindFirst('e');
126 	CPPUNIT_ASSERT(i == B_ERROR);
127 	delete string1;
128 
129 	//FindFirst(char, int32)
130 	NextSubTest();
131 	string1 = new BString("abc abc abc");
132 	i = string1->FindFirst("b", 3);
133 	CPPUNIT_ASSERT(i == 5);
134 	delete string1;
135 
136 	NextSubTest();
137 	string1 = new BString("abcd abcd");
138 	i = string1->FindFirst('e', 3);
139 	CPPUNIT_ASSERT(i == B_ERROR);
140 	delete string1;
141 
142 	NextSubTest();
143 	string1 = new BString("abc abc abc");
144 	i = string1->FindFirst("a", 9);
145 	CPPUNIT_ASSERT(i == B_ERROR);
146 	delete string1;
147 
148 	//FindLast(BString&)
149 	NextSubTest();
150 	string1 = new BString("last but not least");
151 	string2 = new BString("st");
152 	i = string1->FindLast(*string2);
153 	CPPUNIT_ASSERT(i == 16);
154 	delete string1;
155 	delete string2;
156 
157 	NextSubTest();
158 	string1 = new BString;
159 	string2 = new BString("some text");
160 	i = string1->FindLast(*string2);
161 	CPPUNIT_ASSERT(i == B_ERROR);
162 	delete string1;
163 	delete string2;
164 
165 	//FindLast(char*)
166 	NextSubTest();
167 	string1 = new BString("last but not least");
168 	i = string1->FindLast("st");
169 	CPPUNIT_ASSERT(i == 16);
170 	delete string1;
171 
172 	NextSubTest();
173 	string1 = new BString;
174 	i = string1->FindLast("some text");
175 	CPPUNIT_ASSERT(i == B_ERROR);
176 	delete string1;
177 
178 #ifndef TEST_R5
179 	//Commented since crashes R5
180 	NextSubTest();
181 	string1 = new BString("string");
182 	i = string1->FindLast((char*)NULL);
183 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
184 	delete string1;
185 #endif
186 
187 	//FindLast(BString&, int32)
188 	NextSubTest();
189 	string1 = new BString("abcabcabc");
190 	string2 = new BString("abc");
191 	i = string1->FindLast(*string2, 7);
192 	CPPUNIT_ASSERT(i == 3);
193 	delete string1;
194 	delete string2;
195 
196 	NextSubTest();
197 	string1 = new BString("abc abc abc");
198 	string2 = new BString("abc");
199 	i = string1->FindLast(*string2, -10);
200 	CPPUNIT_ASSERT(i == B_ERROR);
201 	delete string1;
202 	delete string2;
203 
204 	//FindLast(const char*, int32)
205 	NextSubTest();
206 	string1 = new BString("abc abc abc");
207 	i = string1->FindLast("abc", 9);
208 	CPPUNIT_ASSERT(i == 4);
209 	delete string1;
210 
211 	NextSubTest();
212 	string1 = new BString("abc abc abc");
213 	i = string1->FindLast("abc", -10);
214 	CPPUNIT_ASSERT(i == B_ERROR);
215 	delete string1;
216 
217 #ifndef TEST_R5
218 	//Commented since crashes r5
219 	NextSubTest();
220 	string1 = new BString("abc abc abc");
221 	i = string1->FindLast((char*)NULL, 3);
222 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
223 	delete string1;
224 #endif
225 
226 	//FindLast(char)
227 	NextSubTest();
228 	string1 = new BString("abcd abcd");
229 	i = string1->FindLast('c');
230 	CPPUNIT_ASSERT(i == 7);
231 	delete string1;
232 
233 	NextSubTest();
234 	string1 = new BString("abcd abcd");
235 	i = string1->FindLast('e');
236 	CPPUNIT_ASSERT(i == B_ERROR);
237 	delete string1;
238 
239 	//FindLast(char, int32)
240 	NextSubTest();
241 	string1 = new BString("abc abc abc");
242 	i = string1->FindLast("b", 5);
243 	CPPUNIT_ASSERT(i == 1);
244 	delete string1;
245 
246 	NextSubTest();
247 	string1 = new BString("abcd abcd");
248 	i = string1->FindLast('e', 3);
249 	CPPUNIT_ASSERT(i == B_ERROR);
250 	delete string1;
251 
252 	NextSubTest();
253 	string1 = new BString("abcd abcd");
254 	i = string1->FindLast('b', 6);
255 	CPPUNIT_ASSERT(i == 6);
256 	delete string1;
257 
258 	NextSubTest();
259 	string1 = new BString("abcd abcd");
260 	i = string1->FindLast('b', 5);
261 	CPPUNIT_ASSERT(i == 1);
262 	delete string1;
263 
264 	NextSubTest();
265 	string1 = new BString("abc abc abc");
266 	i = string1->FindLast("a", 0);
267 	CPPUNIT_ASSERT(i == B_ERROR);
268 	delete string1;
269 
270 	//IFindFirst(BString&)
271 	NextSubTest();
272 	string1 = new BString("last but not least");
273 	string2 = new BString("st");
274 	i = string1->IFindFirst(*string2);
275 	CPPUNIT_ASSERT(i == 2);
276 	delete string1;
277 	delete string2;
278 
279 	NextSubTest();
280 	string1 = new BString("last but not least");
281 	string2 = new BString("ST");
282 	i = string1->IFindFirst(*string2);
283 	CPPUNIT_ASSERT(i == 2);
284 	delete string1;
285 	delete string2;
286 
287 	NextSubTest();
288 	string1 = new BString;
289 	string2 = new BString("some text");
290 	i = string1->IFindFirst(*string2);
291 	CPPUNIT_ASSERT(i == B_ERROR);
292 	delete string1;
293 	delete string2;
294 
295 	NextSubTest();
296 	string1 = new BString("string");
297 	string2 = new BString;
298 	i = string1->IFindFirst(*string2);
299 	CPPUNIT_ASSERT(i == 0);
300 	delete string1;
301 	delete string2;
302 
303 	//IFindFirst(const char*)
304 	NextSubTest();
305 	string1 = new BString("last but not least");
306 	i = string1->IFindFirst("st");
307 	CPPUNIT_ASSERT(i == 2);
308 	delete string1;
309 
310 	NextSubTest();
311 	string1 = new BString("LAST BUT NOT least");
312 	i = string1->IFindFirst("st");
313 	CPPUNIT_ASSERT(i == 2);
314 	delete string1;
315 
316 	NextSubTest();
317 	string1 = new BString;
318 	i = string1->IFindFirst("some text");
319 	CPPUNIT_ASSERT(i == B_ERROR);
320 	delete string1;
321 
322 #ifndef TEST_R5
323 	//Commented, since crashes R5
324 	NextSubTest();
325 	string1 = new BString("string");
326 	i = string1->IFindFirst((char*)NULL);
327 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
328 	delete string1;
329 #endif
330 
331 	//IFindFirst(BString&, int32)
332 	NextSubTest();
333 	string1 = new BString("abc abc abc");
334 	string2 = new BString("abc");
335 	i = string1->IFindFirst(*string2, 5);
336 	CPPUNIT_ASSERT(i == 8);
337 	delete string1;
338 	delete string2;
339 
340 	NextSubTest();
341 	string1 = new BString("abc abc abc");
342 	string2 = new BString("AbC");
343 	i = string1->IFindFirst(*string2, 5);
344 	CPPUNIT_ASSERT(i == 8);
345 	delete string1;
346 	delete string2;
347 
348 	NextSubTest();
349 	string1 = new BString("abc abc abc");
350 	string2 = new BString("abc");
351 	i = string1->IFindFirst(*string2, 200);
352 	CPPUNIT_ASSERT(i == B_ERROR);
353 	delete string1;
354 	delete string2;
355 
356 	NextSubTest();
357 	string1 = new BString("abc abc abc");
358 	string2 = new BString("abc");
359 	i = string1->IFindFirst(*string2, -10);
360 	CPPUNIT_ASSERT(i == B_ERROR);
361 	delete string1;
362 	delete string2;
363 
364 	//IFindFirst(const char*, int32)
365 	NextSubTest();
366 	string1 = new BString("abc abc abc");
367 	i = string1->IFindFirst("abc", 2);
368 	CPPUNIT_ASSERT(i == 4);
369 	delete string1;
370 
371 	NextSubTest();
372 	string1 = new BString("AbC ABC abC");
373 	i = string1->IFindFirst("abc", 2);
374 	CPPUNIT_ASSERT(i == 4);
375 	delete string1;
376 
377 	NextSubTest();
378 	string1 = new BString("abc abc abc");
379 	i = string1->IFindFirst("abc", 200);
380 	CPPUNIT_ASSERT(i == B_ERROR);
381 	delete string1;
382 
383 	NextSubTest();
384 	string1 = new BString("abc abc abc");
385 	i = string1->IFindFirst("abc", -10);
386 	CPPUNIT_ASSERT(i == B_ERROR);
387 	delete string1;
388 
389 #ifndef TEST_R5
390 	//IFindLast(BString&)
391 	NextSubTest();
392 	string1 = new BString("last but not least");
393 	string2 = new BString("st");
394 	i = string1->IFindLast(*string2);
395 	CPPUNIT_ASSERT(i == 16);
396 	delete string1;
397 	delete string2;
398 
399 	NextSubTest();
400 	string1 = new BString("laSt but NOT leaSt");
401 	string2 = new BString("sT");
402 	i = string1->IFindLast(*string2);
403 	CPPUNIT_ASSERT(i == 16);
404 	delete string1;
405 	delete string2;
406 #endif
407 
408 	NextSubTest();
409 	string1 = new BString;
410 	string2 = new BString("some text");
411 	i = string1->IFindLast(*string2);
412 	CPPUNIT_ASSERT(i == B_ERROR);
413 	delete string1;
414 	delete string2;
415 
416 	//IFindLast(const char*)
417 	NextSubTest();
418 	string1 = new BString("last but not least");
419 	i = string1->IFindLast("st");
420 	CPPUNIT_ASSERT(i == 16);
421 	delete string1;
422 
423 #ifndef TEST_R5
424 	NextSubTest();
425 	string1 = new BString("laSt but NOT leaSt");
426 	i = string1->IFindLast("ST");
427 	CPPUNIT_ASSERT(i == 16);
428 	delete string1;
429 #endif
430 
431 	NextSubTest();
432 	string1 = new BString;
433 	i = string1->IFindLast("some text");
434 	CPPUNIT_ASSERT(i == B_ERROR);
435 	delete string1;
436 
437 #ifndef TEST_R5
438 	//Commented since crashes R5
439 	NextSubTest();
440 	string1 = new BString("string");
441 	i = string1->IFindLast((char*)NULL);
442 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
443 	delete string1;
444 #endif
445 
446 	//FindLast(BString&, int32)
447 	NextSubTest();
448 	string1 = new BString("abcabcabc");
449 	string2 = new BString("abc");
450 	i = string1->IFindLast(*string2, 7);
451 	CPPUNIT_ASSERT(i == 3);
452 	delete string1;
453 	delete string2;
454 
455 	NextSubTest();
456 	string1 = new BString("abcabcabc");
457 	string2 = new BString("AbC");
458 	i = string1->IFindLast(*string2, 7);
459 	CPPUNIT_ASSERT(i == 3);
460 	delete string1;
461 	delete string2;
462 
463 	NextSubTest();
464 	string1 = new BString("abc abc abc");
465 	string2 = new BString("abc");
466 	i = string1->IFindLast(*string2, -10);
467 	CPPUNIT_ASSERT(i == B_ERROR);
468 	delete string1;
469 	delete string2;
470 
471 	//IFindLast(const char*, int32)
472 //#ifndef TEST_R5
473 	NextSubTest();
474 	string1 = new BString("abc abc abc");
475 	i = string1->IFindLast("abc", 9);
476 	CPPUNIT_ASSERT(i == 4);
477 	delete string1;
478 //#endif
479 #ifndef TEST_R5
480 	NextSubTest();
481 	string1 = new BString("ABc abC aBC");
482 	i = string1->IFindLast("aBc", 9);
483 	CPPUNIT_ASSERT(i == 4);
484 	delete string1;
485 #endif
486 	NextSubTest();
487 	string1 = new BString("abc abc abc");
488 	i = string1->IFindLast("abc", -10);
489 	CPPUNIT_ASSERT(i == B_ERROR);
490 	delete string1;
491 
492 	NextSubTest();
493 	string1 = new BString("abc def ghi");
494 	i = string1->IFindLast("abc",4);
495 	CPPUNIT_ASSERT(i == 0);
496 	delete string1;
497 }
498 
499 
500 CppUnit::Test *StringSearchTest::suite(void)
501 {
502 	typedef CppUnit::TestCaller<StringSearchTest>
503 		StringSearchTestCaller;
504 
505 	return(new StringSearchTestCaller("BString::Search Test", &StringSearchTest::PerformTest));
506 }
507