xref: /haiku/src/tests/kits/support/bstring/StringSearchTest.cpp (revision 4afae676ad98b8f1e219f704dfc9c8507bce106e)
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("abc abc abc");
254 	i = string1->FindLast("a", 0);
255 	CPPUNIT_ASSERT(i == B_ERROR);
256 	delete string1;
257 
258 	//IFindFirst(BString&)
259 	NextSubTest();
260 	string1 = new BString("last but not least");
261 	string2 = new BString("st");
262 	i = string1->IFindFirst(*string2);
263 	CPPUNIT_ASSERT(i == 2);
264 	delete string1;
265 	delete string2;
266 
267 	NextSubTest();
268 	string1 = new BString("last but not least");
269 	string2 = new BString("ST");
270 	i = string1->IFindFirst(*string2);
271 	CPPUNIT_ASSERT(i == 2);
272 	delete string1;
273 	delete string2;
274 
275 	NextSubTest();
276 	string1 = new BString;
277 	string2 = new BString("some text");
278 	i = string1->IFindFirst(*string2);
279 	CPPUNIT_ASSERT(i == B_ERROR);
280 	delete string1;
281 	delete string2;
282 
283 	NextSubTest();
284 	string1 = new BString("string");
285 	string2 = new BString;
286 	i = string1->IFindFirst(*string2);
287 	CPPUNIT_ASSERT(i == 0);
288 	delete string1;
289 	delete string2;
290 
291 	//IFindFirst(const char*)
292 	NextSubTest();
293 	string1 = new BString("last but not least");
294 	i = string1->IFindFirst("st");
295 	CPPUNIT_ASSERT(i == 2);
296 	delete string1;
297 
298 	NextSubTest();
299 	string1 = new BString("LAST BUT NOT least");
300 	i = string1->IFindFirst("st");
301 	CPPUNIT_ASSERT(i == 2);
302 	delete string1;
303 
304 	NextSubTest();
305 	string1 = new BString;
306 	i = string1->IFindFirst("some text");
307 	CPPUNIT_ASSERT(i == B_ERROR);
308 	delete string1;
309 
310 #ifndef TEST_R5
311 	//Commented, since crashes R5
312 	NextSubTest();
313 	string1 = new BString("string");
314 	i = string1->IFindFirst((char*)NULL);
315 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
316 	delete string1;
317 #endif
318 
319 	//IFindFirst(BString&, int32)
320 	NextSubTest();
321 	string1 = new BString("abc abc abc");
322 	string2 = new BString("abc");
323 	i = string1->IFindFirst(*string2, 5);
324 	CPPUNIT_ASSERT(i == 8);
325 	delete string1;
326 	delete string2;
327 
328 	NextSubTest();
329 	string1 = new BString("abc abc abc");
330 	string2 = new BString("AbC");
331 	i = string1->IFindFirst(*string2, 5);
332 	CPPUNIT_ASSERT(i == 8);
333 	delete string1;
334 	delete string2;
335 
336 	NextSubTest();
337 	string1 = new BString("abc abc abc");
338 	string2 = new BString("abc");
339 	i = string1->IFindFirst(*string2, 200);
340 	CPPUNIT_ASSERT(i == B_ERROR);
341 	delete string1;
342 	delete string2;
343 
344 	NextSubTest();
345 	string1 = new BString("abc abc abc");
346 	string2 = new BString("abc");
347 	i = string1->IFindFirst(*string2, -10);
348 	CPPUNIT_ASSERT(i == B_ERROR);
349 	delete string1;
350 	delete string2;
351 
352 	//IFindFirst(const char*, int32)
353 	NextSubTest();
354 	string1 = new BString("abc abc abc");
355 	i = string1->IFindFirst("abc", 2);
356 	CPPUNIT_ASSERT(i == 4);
357 	delete string1;
358 
359 	NextSubTest();
360 	string1 = new BString("AbC ABC abC");
361 	i = string1->IFindFirst("abc", 2);
362 	CPPUNIT_ASSERT(i == 4);
363 	delete string1;
364 
365 	NextSubTest();
366 	string1 = new BString("abc abc abc");
367 	i = string1->IFindFirst("abc", 200);
368 	CPPUNIT_ASSERT(i == B_ERROR);
369 	delete string1;
370 
371 	NextSubTest();
372 	string1 = new BString("abc abc abc");
373 	i = string1->IFindFirst("abc", -10);
374 	CPPUNIT_ASSERT(i == B_ERROR);
375 	delete string1;
376 
377 #ifndef TEST_R5
378 	//IFindLast(BString&)
379 	NextSubTest();
380 	string1 = new BString("last but not least");
381 	string2 = new BString("st");
382 	i = string1->IFindLast(*string2);
383 	CPPUNIT_ASSERT(i == 16);
384 	delete string1;
385 	delete string2;
386 
387 	NextSubTest();
388 	string1 = new BString("laSt but NOT leaSt");
389 	string2 = new BString("sT");
390 	i = string1->IFindLast(*string2);
391 	CPPUNIT_ASSERT(i == 16);
392 	delete string1;
393 	delete string2;
394 #endif
395 
396 	NextSubTest();
397 	string1 = new BString;
398 	string2 = new BString("some text");
399 	i = string1->IFindLast(*string2);
400 	CPPUNIT_ASSERT(i == B_ERROR);
401 	delete string1;
402 	delete string2;
403 
404 	//IFindLast(const char*)
405 	NextSubTest();
406 	string1 = new BString("last but not least");
407 	i = string1->IFindLast("st");
408 	CPPUNIT_ASSERT(i == 16);
409 	delete string1;
410 
411 	NextSubTest();
412 	string1 = new BString("laSt but NOT leaSt");
413 	i = string1->IFindLast("ST");
414 	CPPUNIT_ASSERT(i == 16);
415 	delete string1;
416 
417 	NextSubTest();
418 	string1 = new BString;
419 	i = string1->IFindLast("some text");
420 	CPPUNIT_ASSERT(i == B_ERROR);
421 	delete string1;
422 
423 #ifndef TEST_R5
424 	//Commented since crashes R5
425 	NextSubTest();
426 	string1 = new BString("string");
427 	i = string1->IFindLast((char*)NULL);
428 	CPPUNIT_ASSERT(i == B_BAD_VALUE);
429 	delete string1;
430 #endif
431 
432 	//FindLast(BString&, int32)
433 	NextSubTest();
434 	string1 = new BString("abcabcabc");
435 	string2 = new BString("abc");
436 	i = string1->IFindLast(*string2, 7);
437 	CPPUNIT_ASSERT(i == 3);
438 	delete string1;
439 	delete string2;
440 
441 	NextSubTest();
442 	string1 = new BString("abcabcabc");
443 	string2 = new BString("AbC");
444 	i = string1->IFindLast(*string2, 7);
445 	CPPUNIT_ASSERT(i == 3);
446 	delete string1;
447 	delete string2;
448 
449 	NextSubTest();
450 	string1 = new BString("abc abc abc");
451 	string2 = new BString("abc");
452 	i = string1->IFindLast(*string2, -10);
453 	CPPUNIT_ASSERT(i == B_ERROR);
454 	delete string1;
455 	delete string2;
456 
457 	//IFindLast(const char*, int32)
458 	NextSubTest();
459 	string1 = new BString("abc abc abc");
460 	i = string1->IFindLast("abc", 9);
461 	CPPUNIT_ASSERT(i == 4);
462 	delete string1;
463 
464 	NextSubTest();
465 	string1 = new BString("ABc abC aBC");
466 	i = string1->IFindLast("aBc", 9);
467 	CPPUNIT_ASSERT(i == 4);
468 	delete string1;
469 
470 	NextSubTest();
471 	string1 = new BString("abc abc abc");
472 	i = string1->IFindLast("abc", -10);
473 	CPPUNIT_ASSERT(i == B_ERROR);
474 	delete string1;
475 }
476 
477 
478 CppUnit::Test *StringSearchTest::suite(void)
479 {
480 	typedef CppUnit::TestCaller<StringSearchTest>
481 		StringSearchTestCaller;
482 
483 	return(new StringSearchTestCaller("BString::Search Test", &StringSearchTest::PerformTest));
484 }
485