xref: /haiku/src/tests/kits/support/bstring/StringSearchTest.cpp (revision bddcee2a27042b4d8d6b0142b466f30abc886648)
1 #include "StringSearchTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 #include <stdio.h>
5 
6 
7 StringSearchTest::StringSearchTest(std::string name)
8 		: BTestCase(name)
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 #ifndef TEST_R5
149 	// StartsWith(BString&)
150 	NextSubTest();
151 	string1 = new BString("last but not least");
152 	string2 = new BString("last");
153 	i = (int32)string1->StartsWith(*string2);
154 	CPPUNIT_ASSERT(i != 0);
155 	delete string1;
156 	delete string2;
157 
158 	// StartsWith(const char*)
159 	NextSubTest();
160 	string1 = new BString("last but not least");
161 	i = (int32)string1->StartsWith("last");
162 	CPPUNIT_ASSERT(i != 0);
163 	delete string1;
164 
165 	// StartsWith(const char*, int32)
166 	NextSubTest();
167 	string1 = new BString("last but not least");
168 	i = (int32)string1->StartsWith("last", 4);
169 	CPPUNIT_ASSERT(i != 0);
170 	delete string1;
171 #endif
172 
173 	// FindLast(BString&)
174 	NextSubTest();
175 	string1 = new BString("last but not least");
176 	string2 = new BString("st");
177 	i = string1->FindLast(*string2);
178 	CPPUNIT_ASSERT(i == 16);
179 	delete string1;
180 	delete string2;
181 
182 	NextSubTest();
183 	string1 = new BString;
184 	string2 = new BString("some text");
185 	i = string1->FindLast(*string2);
186 	CPPUNIT_ASSERT(i == B_ERROR);
187 	delete string1;
188 	delete string2;
189 
190 	// FindLast(char*)
191 	NextSubTest();
192 	string1 = new BString("last but not least");
193 	i = string1->FindLast("st");
194 	CPPUNIT_ASSERT(i == 16);
195 	delete string1;
196 
197 	NextSubTest();
198 	string1 = new BString;
199 	i = string1->FindLast("some text");
200 	CPPUNIT_ASSERT(i == B_ERROR);
201 	delete string1;
202 
203 #ifndef TEST_R5
204 	// Commented since crashes R5
205 	NextSubTest();
206 	string1 = new BString("string");
207 	i = string1->FindLast((char*)NULL);
208 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
209 	delete string1;
210 #endif
211 
212 	// FindLast(BString&, int32)
213 	NextSubTest();
214 	string1 = new BString("abcabcabc");
215 	string2 = new BString("abc");
216 	i = string1->FindLast(*string2, 7);
217 	CPPUNIT_ASSERT(i == 3);
218 	delete string1;
219 	delete string2;
220 
221 	NextSubTest();
222 	string1 = new BString("abc abc abc");
223 	string2 = new BString("abc");
224 	i = string1->FindLast(*string2, -10);
225 	CPPUNIT_ASSERT(i == B_ERROR);
226 	delete string1;
227 	delete string2;
228 
229 	// FindLast(const char*, int32)
230 	NextSubTest();
231 	string1 = new BString("abc abc abc");
232 	i = string1->FindLast("abc", 9);
233 	CPPUNIT_ASSERT(i == 4);
234 	delete string1;
235 
236 	NextSubTest();
237 	string1 = new BString("abc abc abc");
238 	i = string1->FindLast("abc", -10);
239 	CPPUNIT_ASSERT(i == B_ERROR);
240 	delete string1;
241 
242 #ifndef TEST_R5
243 	// Commented since crashes r5
244 	NextSubTest();
245 	string1 = new BString("abc abc abc");
246 	i = string1->FindLast((char*)NULL, 3);
247 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
248 	delete string1;
249 #endif
250 
251 	// FindLast(char)
252 	NextSubTest();
253 	string1 = new BString("abcd abcd");
254 	i = string1->FindLast('c');
255 	CPPUNIT_ASSERT(i == 7);
256 	delete string1;
257 
258 	NextSubTest();
259 	string1 = new BString("abcd abcd");
260 	i = string1->FindLast('e');
261 	CPPUNIT_ASSERT(i == B_ERROR);
262 	delete string1;
263 
264 	// FindLast(char, int32)
265 	NextSubTest();
266 	string1 = new BString("abc abc abc");
267 	i = string1->FindLast("b", 5);
268 	CPPUNIT_ASSERT(i == 1);
269 	delete string1;
270 
271 	NextSubTest();
272 	string1 = new BString("abcd abcd");
273 	i = string1->FindLast('e', 3);
274 	CPPUNIT_ASSERT(i == B_ERROR);
275 	delete string1;
276 
277 	NextSubTest();
278 	string1 = new BString("abcd abcd");
279 	i = string1->FindLast('b', 6);
280 	CPPUNIT_ASSERT(i == 6);
281 	delete string1;
282 
283 	NextSubTest();
284 	string1 = new BString("abcd abcd");
285 	i = string1->FindLast('b', 5);
286 	CPPUNIT_ASSERT(i == 1);
287 	delete string1;
288 
289 	NextSubTest();
290 	string1 = new BString("abc abc abc");
291 	i = string1->FindLast("a", 0);
292 	CPPUNIT_ASSERT(i == B_ERROR);
293 	delete string1;
294 
295 	// IFindFirst(BString&)
296 	NextSubTest();
297 	string1 = new BString("last but not least");
298 	string2 = new BString("st");
299 	i = string1->IFindFirst(*string2);
300 	CPPUNIT_ASSERT(i == 2);
301 	delete string1;
302 	delete string2;
303 
304 	NextSubTest();
305 	string1 = new BString("last but not least");
306 	string2 = new BString("ST");
307 	i = string1->IFindFirst(*string2);
308 	CPPUNIT_ASSERT(i == 2);
309 	delete string1;
310 	delete string2;
311 
312 	NextSubTest();
313 	string1 = new BString;
314 	string2 = new BString("some text");
315 	i = string1->IFindFirst(*string2);
316 	CPPUNIT_ASSERT(i == B_ERROR);
317 	delete string1;
318 	delete string2;
319 
320 	NextSubTest();
321 	string1 = new BString("string");
322 	string2 = new BString;
323 	i = string1->IFindFirst(*string2);
324 	CPPUNIT_ASSERT(i == 0);
325 	delete string1;
326 	delete string2;
327 
328 	// IFindFirst(const char*)
329 	NextSubTest();
330 	string1 = new BString("last but not least");
331 	i = string1->IFindFirst("st");
332 	CPPUNIT_ASSERT(i == 2);
333 	delete string1;
334 
335 	NextSubTest();
336 	string1 = new BString("LAST BUT NOT least");
337 	i = string1->IFindFirst("st");
338 	CPPUNIT_ASSERT(i == 2);
339 	delete string1;
340 
341 	NextSubTest();
342 	string1 = new BString;
343 	i = string1->IFindFirst("some text");
344 	CPPUNIT_ASSERT(i == B_ERROR);
345 	delete string1;
346 
347 #ifndef TEST_R5
348 	// Commented, since crashes R5
349 	NextSubTest();
350 	string1 = new BString("string");
351 	i = string1->IFindFirst((char*)NULL);
352 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
353 	delete string1;
354 #endif
355 
356 	// IFindFirst(BString&, int32)
357 	NextSubTest();
358 	string1 = new BString("abc abc abc");
359 	string2 = new BString("abc");
360 	i = string1->IFindFirst(*string2, 5);
361 	CPPUNIT_ASSERT(i == 8);
362 	delete string1;
363 	delete string2;
364 
365 	NextSubTest();
366 	string1 = new BString("abc abc abc");
367 	string2 = new BString("AbC");
368 	i = string1->IFindFirst(*string2, 5);
369 	CPPUNIT_ASSERT(i == 8);
370 	delete string1;
371 	delete string2;
372 
373 	NextSubTest();
374 	string1 = new BString("abc abc abc");
375 	string2 = new BString("abc");
376 	i = string1->IFindFirst(*string2, 200);
377 	CPPUNIT_ASSERT(i == B_ERROR);
378 	delete string1;
379 	delete string2;
380 
381 	NextSubTest();
382 	string1 = new BString("abc abc abc");
383 	string2 = new BString("abc");
384 	i = string1->IFindFirst(*string2, -10);
385 	CPPUNIT_ASSERT(i == B_ERROR);
386 	delete string1;
387 	delete string2;
388 
389 	// IFindFirst(const char*, int32)
390 	NextSubTest();
391 	string1 = new BString("abc abc abc");
392 	i = string1->IFindFirst("abc", 2);
393 	CPPUNIT_ASSERT(i == 4);
394 	delete string1;
395 
396 	NextSubTest();
397 	string1 = new BString("AbC ABC abC");
398 	i = string1->IFindFirst("abc", 2);
399 	CPPUNIT_ASSERT(i == 4);
400 	delete string1;
401 
402 	NextSubTest();
403 	string1 = new BString("abc abc abc");
404 	i = string1->IFindFirst("abc", 200);
405 	CPPUNIT_ASSERT(i == B_ERROR);
406 	delete string1;
407 
408 	NextSubTest();
409 	string1 = new BString("abc abc abc");
410 	i = string1->IFindFirst("abc", -10);
411 	CPPUNIT_ASSERT(i == B_ERROR);
412 	delete string1;
413 
414 #ifndef TEST_R5
415 	// IStartsWith(BString&)
416 	NextSubTest();
417 	string1 = new BString("last but not least");
418 	string2 = new BString("lAsT");
419 	i = (int32)string1->IStartsWith(*string2);
420 	CPPUNIT_ASSERT(i != 0);
421 	delete string1;
422 	delete string2;
423 
424 	// IStartsWith(const char*)
425 	NextSubTest();
426 	string1 = new BString("last but not least");
427 	i = (int32)string1->IStartsWith("lAsT");
428 	CPPUNIT_ASSERT(i != 0);
429 	delete string1;
430 
431 	// IStartsWith(const char*, int32)
432 	NextSubTest();
433 	string1 = new BString("last but not least");
434 	i = (int32)string1->IStartsWith("lAsT", 4);
435 	CPPUNIT_ASSERT(i != 0);
436 	delete string1;
437 
438 	// IFindLast(BString&)
439 	NextSubTest();
440 	string1 = new BString("last but not least");
441 	string2 = new BString("st");
442 	i = string1->IFindLast(*string2);
443 	CPPUNIT_ASSERT(i == 16);
444 	delete string1;
445 	delete string2;
446 
447 	NextSubTest();
448 	string1 = new BString("laSt but NOT leaSt");
449 	string2 = new BString("sT");
450 	i = string1->IFindLast(*string2);
451 	CPPUNIT_ASSERT(i == 16);
452 	delete string1;
453 	delete string2;
454 
455 	// EndsWith(BString&)
456 	NextSubTest();
457 	string1 = new BString("last but not least");
458 	string2 = new BString("st");
459 	i = (int32)string1->EndsWith(*string2);
460 	CPPUNIT_ASSERT(i != 0);
461 	delete string1;
462 	delete string2;
463 
464 	NextSubTest();
465 	string1 = new BString("laSt but NOT leaSt");
466 	string2 = new BString("sT");
467 	i = (int32)string1->EndsWith(*string2);
468 	CPPUNIT_ASSERT(i == 0);
469 	delete string1;
470 	delete string2;
471 
472 	// EndsWith(const char*)
473 	NextSubTest();
474 	string1 = new BString("last but not least");
475 	i = (int32)string1->EndsWith("least");
476 	CPPUNIT_ASSERT(i != 0);
477 	delete string1;
478 
479 	NextSubTest();
480 	string1 = new BString("laSt but NOT leaSt");
481 	i = (int32)string1->EndsWith("least");
482 	CPPUNIT_ASSERT(i == 0);
483 	delete string1;
484 
485 	// EndsWith(const char*, int32)
486 	NextSubTest();
487 	string1 = new BString("last but not least");
488 	i = (int32)string1->EndsWith("st", 2);
489 	CPPUNIT_ASSERT(i != 0);
490 	delete string1;
491 
492 	NextSubTest();
493 	string1 = new BString("laSt but NOT leaSt");
494 	i = (int32)string1->EndsWith("sT", 2);
495 	CPPUNIT_ASSERT(i == 0);
496 	delete string1;
497 
498 	// IEndsWith(BString&)
499 	NextSubTest();
500 	string1 = new BString("last but not least");
501 	string2 = new BString("st");
502 	i = (int32)string1->IEndsWith(*string2);
503 	CPPUNIT_ASSERT(i != 0);
504 	delete string1;
505 	delete string2;
506 
507 	NextSubTest();
508 	string1 = new BString("laSt but NOT leaSt");
509 	string2 = new BString("sT");
510 	i = (int32)string1->IEndsWith(*string2);
511 	CPPUNIT_ASSERT(i != 0);
512 	delete string1;
513 	delete string2;
514 
515 	// IEndsWith(const char*)
516 	NextSubTest();
517 	string1 = new BString("last but not least");
518 	i = (int32)string1->IEndsWith("st");
519 	CPPUNIT_ASSERT(i != 0);
520 	delete string1;
521 
522 	NextSubTest();
523 	string1 = new BString("laSt but NOT leaSt");
524 	i = (int32)string1->IEndsWith("sT");
525 	CPPUNIT_ASSERT(i != 0);
526 	delete string1;
527 
528 	// IEndsWith(const char*, int32)
529 	NextSubTest();
530 	string1 = new BString("last but not least");
531 	i = (int32)string1->IEndsWith("st", 2);
532 	CPPUNIT_ASSERT(i != 0);
533 	delete string1;
534 
535 	NextSubTest();
536 	string1 = new BString("laSt but NOT leaSt");
537 	i = (int32)string1->IEndsWith("sT", 2);
538 	CPPUNIT_ASSERT(i != 0);
539 	delete string1;
540 #endif
541 
542 	NextSubTest();
543 	string1 = new BString;
544 	string2 = new BString("some text");
545 	i = string1->IFindLast(*string2);
546 	CPPUNIT_ASSERT(i == B_ERROR);
547 	delete string1;
548 	delete string2;
549 
550 	// IFindLast(const char*)
551 	NextSubTest();
552 	string1 = new BString("last but not least");
553 	i = string1->IFindLast("st");
554 	CPPUNIT_ASSERT(i == 16);
555 	delete string1;
556 
557 #ifndef TEST_R5
558 	NextSubTest();
559 	string1 = new BString("laSt but NOT leaSt");
560 	i = string1->IFindLast("ST");
561 	CPPUNIT_ASSERT(i == 16);
562 	delete string1;
563 #endif
564 
565 	NextSubTest();
566 	string1 = new BString;
567 	i = string1->IFindLast("some text");
568 	CPPUNIT_ASSERT(i == B_ERROR);
569 	delete string1;
570 
571 #ifndef TEST_R5
572 	// Commented since crashes R5
573 	NextSubTest();
574 	string1 = new BString("string");
575 	i = string1->IFindLast((char*)NULL);
576 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
577 	delete string1;
578 #endif
579 
580 	// FindLast(BString&, int32)
581 	NextSubTest();
582 	string1 = new BString("abcabcabc");
583 	string2 = new BString("abc");
584 	i = string1->IFindLast(*string2, 7);
585 	CPPUNIT_ASSERT(i == 3);
586 	delete string1;
587 	delete string2;
588 
589 	NextSubTest();
590 	string1 = new BString("abcabcabc");
591 	string2 = new BString("AbC");
592 	i = string1->IFindLast(*string2, 7);
593 	CPPUNIT_ASSERT(i == 3);
594 	delete string1;
595 	delete string2;
596 
597 	NextSubTest();
598 	string1 = new BString("abc abc abc");
599 	string2 = new BString("abc");
600 	i = string1->IFindLast(*string2, -10);
601 	CPPUNIT_ASSERT(i == B_ERROR);
602 	delete string1;
603 	delete string2;
604 
605 	// IFindLast(const char*, int32)
606 // #ifndef TEST_R5
607 	NextSubTest();
608 	string1 = new BString("abc abc abc");
609 	i = string1->IFindLast("abc", 9);
610 	CPPUNIT_ASSERT(i == 4);
611 	delete string1;
612 // #endif
613 #ifndef TEST_R5
614 	NextSubTest();
615 	string1 = new BString("ABc abC aBC");
616 	i = string1->IFindLast("aBc", 9);
617 	CPPUNIT_ASSERT(i == 4);
618 	delete string1;
619 #endif
620 	NextSubTest();
621 	string1 = new BString("abc abc abc");
622 	i = string1->IFindLast("abc", -10);
623 	CPPUNIT_ASSERT(i == B_ERROR);
624 	delete string1;
625 
626 	NextSubTest();
627 	string1 = new BString("abc def ghi");
628 	i = string1->IFindLast("abc",4);
629 	CPPUNIT_ASSERT(i == 0);
630 	delete string1;
631 }
632 
633 
634 CppUnit::Test *StringSearchTest::suite(void)
635 {
636 	typedef CppUnit::TestCaller<StringSearchTest>
637 		StringSearchTestCaller;
638 
639 	return(new StringSearchTestCaller("BString::Search Test",
640 		&StringSearchTest::PerformTest));
641 }
642