xref: /haiku/src/tests/kits/support/bstring/StringReplaceTest.cpp (revision f960e7e9a2b236d50e202d5c46b4d759d46b233a)
1 #include "StringReplaceTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 
5 
6 StringReplaceTest::StringReplaceTest(std::string name)
7 		: BTestCase(name)
8 {
9 }
10 
11 
12 StringReplaceTest::~StringReplaceTest()
13 {
14 }
15 
16 
17 void
18 StringReplaceTest::PerformTest(void)
19 {
20 	BString *str1;
21 	const int32 sz = 1024 * 50;
22 	char* buf;
23 
24 	// &ReplaceFirst(char, char);
25 	NextSubTest();
26 	str1 = new BString("test string");
27 	str1->ReplaceFirst('t', 'b');
28 	CPPUNIT_ASSERT(strcmp(str1->String(), "best string") == 0);
29 	delete str1;
30 
31 	NextSubTest();
32 	str1 = new BString("test string");
33 	str1->ReplaceFirst('x', 'b');
34 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
35 	delete str1;
36 
37 	// &ReplaceLast(char, char);
38 	NextSubTest();
39 	str1 = new BString("test string");
40 	str1->ReplaceLast('t', 'w');
41 	CPPUNIT_ASSERT(strcmp(str1->String(), "test swring") == 0);
42 	delete str1;
43 
44 	NextSubTest();
45 	str1 = new BString("test string");
46 	str1->ReplaceLast('x', 'b');
47 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
48 	delete str1;
49 
50 	// &ReplaceAll(char, char, int32);
51 	NextSubTest();
52 	str1 = new BString("test string");
53 	str1->ReplaceAll('t', 'i');
54 	CPPUNIT_ASSERT(strcmp(str1->String(), "iesi siring") == 0);
55 	delete str1;
56 
57 	NextSubTest();
58 	str1 = new BString("test string");
59 	str1->ReplaceAll('x', 'b');
60 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
61 	delete str1;
62 
63 	NextSubTest();
64 	str1 = new BString("test string");
65 	str1->ReplaceAll('t', 't');
66 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
67 	delete str1;
68 
69 	NextSubTest();
70 	str1 = new BString("test string");
71 	str1->ReplaceAll('t', 'i', 2);
72 	CPPUNIT_ASSERT(strcmp(str1->String(), "tesi siring") == 0);
73 	delete str1;
74 
75 	// &Replace(char, char, int32, int32)
76 	NextSubTest();
77 	str1 = new BString("she sells sea shells on the sea shore");
78 	str1->Replace('s', 't', 4, 2);
79 	CPPUNIT_ASSERT(strcmp(str1->String(),
80 		"she tellt tea thells on the sea shore") == 0);
81 	delete str1;
82 
83 	NextSubTest();
84 	str1 = new BString("she sells sea shells on the sea shore");
85 	str1->Replace('s', 's', 4, 2);
86 	CPPUNIT_ASSERT(strcmp(str1->String(),
87 		"she sells sea shells on the sea shore") == 0);
88 	delete str1;
89 
90 	NextSubTest();
91 	str1 = new BString();
92 	str1->Replace('s', 'x', 12, 32);
93 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
94 	delete str1;
95 
96 	// &ReplaceFirst(const char*, const char*)
97 	NextSubTest();
98 	str1 = new BString("she sells sea shells on the seashore");
99 	str1->ReplaceFirst("sea", "the");
100 	CPPUNIT_ASSERT(strcmp(str1->String(),
101 		"she sells the shells on the seashore") == 0);
102 	delete str1;
103 
104 	NextSubTest();
105 	str1 = new BString("she sells sea shells on the seashore");
106 	str1->ReplaceFirst("tex", "the");
107 	CPPUNIT_ASSERT(strcmp(str1->String(),
108 		"she sells sea shells on the seashore") == 0);
109 	delete str1;
110 
111 	// &ReplaceLast(const char*, const char*)
112 	NextSubTest();
113 	str1 = new BString("she sells sea shells on the seashore");
114 	str1->ReplaceLast("sea", "the");
115 	CPPUNIT_ASSERT(strcmp(str1->String(),
116 		"she sells sea shells on the theshore") == 0);
117 	delete str1;
118 
119 	NextSubTest();
120 	str1 = new BString("she sells sea shells on the seashore");
121 	str1->ReplaceLast("tex", "the");
122 	CPPUNIT_ASSERT(strcmp(str1->String(),
123 		"she sells sea shells on the seashore") == 0);
124 	delete str1;
125 
126 	// &ReplaceAll(const char*, const char*, int32)
127 	NextSubTest();
128 	str1 = new BString("abc abc abc");
129 	str1->ReplaceAll("ab", "abc");
130 	CPPUNIT_ASSERT(strcmp(str1->String(), "abcc abcc abcc") == 0);
131 	delete str1;
132 
133 	NextSubTest();
134 	str1 = new BString("abc abc abc");
135 	str1->ReplaceAll("abc", "abc");
136 	CPPUNIT_ASSERT(strcmp(str1->String(), "abc abc abc") == 0);
137 	delete str1;
138 
139 	NextSubTest();
140 	str1 = new BString("she sells sea shells on the seashore");
141 	str1->ReplaceAll("tex", "the");
142 	CPPUNIT_ASSERT(strcmp(str1->String(),
143 		"she sells sea shells on the seashore") == 0);
144 	delete str1;
145 
146 	NextSubTest();
147 	str1 = new BString("she sells sea shells on the seashore");
148 	str1->IReplaceAll("sea", "the", 11);
149 	CPPUNIT_ASSERT(strcmp(str1->String(),
150 		"she sells sea shells on the theshore") == 0);
151 	delete str1;
152 
153 	NextSubTest();
154 	str1 = new BString("she sells sea shells on the seashore");
155 	str1->IReplaceAll("sea", "sea", 11);
156 	CPPUNIT_ASSERT(strcmp(str1->String(),
157 		"she sells sea shells on the seashore") == 0);
158 	delete str1;
159 
160 	// &IReplaceFirst(char, char);
161 	NextSubTest();
162 	str1 = new BString("test string");
163 	str1->IReplaceFirst('t', 'b');
164 	CPPUNIT_ASSERT(strcmp(str1->String(), "best string") == 0);
165 	delete str1;
166 
167 	NextSubTest();
168 	str1 = new BString("test string");
169 	str1->IReplaceFirst('x', 'b');
170 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
171 	delete str1;
172 
173 	// &IReplaceLast(char, char);
174 	NextSubTest();
175 	str1 = new BString("test string");
176 	str1->IReplaceLast('t', 'w');
177 	CPPUNIT_ASSERT(strcmp(str1->String(), "test swring") == 0);
178 	delete str1;
179 
180 	NextSubTest();
181 	str1 = new BString("test string");
182 	str1->IReplaceLast('x', 'b');
183 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
184 	delete str1;
185 
186 	// &IReplaceAll(char, char, int32);
187 	NextSubTest();
188 	str1 = new BString("TEST string");
189 	str1->IReplaceAll('t', 'i');
190 	CPPUNIT_ASSERT(strcmp(str1->String(), "iESi siring") == 0);
191 	delete str1;
192 
193 	NextSubTest();
194 	str1 = new BString("TEST string");
195 	str1->IReplaceAll('t', 'T');
196 	CPPUNIT_ASSERT(strcmp(str1->String(), "TEST sTring") == 0);
197 	delete str1;
198 
199 	NextSubTest();
200 	str1 = new BString("test string");
201 	str1->IReplaceAll('x', 'b');
202 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
203 	delete str1;
204 
205 	NextSubTest();
206 	str1 = new BString("TEST string");
207 	str1->IReplaceAll('t', 'i', 2);
208 	CPPUNIT_ASSERT(strcmp(str1->String(), "TESi siring") == 0);
209 	delete str1;
210 
211 	// &IReplace(char, char, int32, int32)
212 	NextSubTest();
213 	str1 = new BString("She sells Sea shells on the sea shore");
214 	str1->IReplace('s', 't', 4, 2);
215 	CPPUNIT_ASSERT(strcmp(str1->String(),
216 		"She tellt tea thells on the sea shore") == 0);
217 	delete str1;
218 
219 	NextSubTest();
220 	str1 = new BString("She sells Sea shells on the sea shore");
221 	str1->IReplace('s', 's', 4, 2);
222 	CPPUNIT_ASSERT(strcmp(str1->String(),
223 		"She sells sea shells on the sea shore") == 0);
224 	delete str1;
225 
226 	NextSubTest();
227 	str1 = new BString();
228 	str1->IReplace('s', 'x', 12, 32);
229 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
230 	delete str1;
231 
232 	// &IReplaceFirst(const char*, const char*)
233 	NextSubTest();
234 	str1 = new BString("she sells SeA shells on the seashore");
235 	str1->IReplaceFirst("sea", "the");
236 	CPPUNIT_ASSERT(strcmp(str1->String(),
237 		"she sells the shells on the seashore") == 0);
238 	delete str1;
239 
240 	NextSubTest();
241 	str1 = new BString("she sells sea shells on the seashore");
242 	str1->IReplaceFirst("tex", "the");
243 	CPPUNIT_ASSERT(strcmp(str1->String(),
244 		"she sells sea shells on the seashore") == 0);
245 	delete str1;
246 
247 	// &IReplaceLast(const char*, const char*)
248 #ifndef TEST_R5
249 	NextSubTest();
250 	str1 = new BString("she sells sea shells on the SEashore");
251 	str1->IReplaceLast("sea", "the");
252 	CPPUNIT_ASSERT(strcmp(str1->String(),
253 		"she sells sea shells on the theshore") == 0);
254 	delete str1;
255 #endif
256 	NextSubTest();
257 	str1 = new BString("she sells sea shells on the seashore");
258 	str1->IReplaceLast("tex", "the");
259 	CPPUNIT_ASSERT(strcmp(str1->String(),
260 		"she sells sea shells on the seashore") == 0);
261 	delete str1;
262 
263 	// &IReplaceAll(const char*, const char*, int32)
264 	NextSubTest();
265 	str1 = new BString("abc ABc aBc");
266 	str1->IReplaceAll("ab", "abc");
267 	CPPUNIT_ASSERT(strcmp(str1->String(),
268 		"abcc abcc abcc") == 0);
269 	delete str1;
270 
271 	NextSubTest();
272 	str1 = new BString("she sells sea shells on the seashore");
273 	str1->IReplaceAll("tex", "the");
274 	CPPUNIT_ASSERT(strcmp(str1->String(),
275 		"she sells sea shells on the seashore") == 0);
276 	delete str1;
277 
278 	NextSubTest();
279 	str1 = new BString("she sells SeA shells on the sEashore");
280 	str1->IReplaceAll("sea", "the", 11);
281 	CPPUNIT_ASSERT(strcmp(str1->String(),
282 		"she sells SeA shells on the theshore") == 0);
283 	delete str1;
284 
285 	// ReplaceSet(const char*, char)
286 	NextSubTest();
287 	str1 = new BString("abc abc abc");
288 	str1->ReplaceSet("ab", 'x');
289 	CPPUNIT_ASSERT(strcmp(str1->String(), "xxc xxc xxc") == 0);
290 	delete str1;
291 
292 	NextSubTest();
293 	str1 = new BString("abcabcabcbababc");
294 	str1->ReplaceSet("abc", 'c');
295 	CPPUNIT_ASSERT(strcmp(str1->String(), "ccccccccccccccc") == 0);
296 	delete str1;
297 
298 	NextSubTest();
299 	str1 = new BString("abcabcabcbababc");
300 	str1->ReplaceSet("c", 'c');
301 	CPPUNIT_ASSERT(strcmp(str1->String(), "abcabcabcbababc") == 0);
302 	delete str1;
303 
304 #ifndef TEST_R5
305 	// ReplaceSet(const char*, const char*)
306 	NextSubTest();
307 	str1 = new BString("abcd abcd abcd");
308 	str1->ReplaceSet("abcd ", "");
309 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
310 	delete str1;
311 #endif
312 
313 #ifndef TEST_R5
314 	// ReplaceSet(const char*, const char*)
315 	NextSubTest();
316 	str1 = new BString("abcd abcd abcd");
317 	str1->ReplaceSet("ad", "da");
318 	CPPUNIT_ASSERT(strcmp(str1->String(), "dabcda dabcda dabcda") == 0);
319 	delete str1;
320 #endif
321 
322 #ifndef TEST_R5
323 	// ReplaceSet(const char*, const char*)
324 	NextSubTest();
325 	str1 = new BString("abcd abcd abcd");
326 	str1->ReplaceSet("ad", "");
327 	CPPUNIT_ASSERT(strcmp(str1->String(), "bc bc bc") == 0);
328 	delete str1;
329 #endif
330 
331 	// we repeat some test, but this time with a bit of data
332 	// to test the performance:
333 
334 	// ReplaceSet(const char*, const char*)
335 	NextSubTest();
336 	str1 = new BString();
337 	buf = str1->LockBuffer(sz);
338 	memset( buf, 'x', sz);
339 	str1->UnlockBuffer( sz);
340 	str1->ReplaceSet("x", "y");
341 	CPPUNIT_ASSERT(str1->Length() == sz);
342 	delete str1;
343 
344 	NextSubTest();
345 	str1 = new BString();
346 	buf = str1->LockBuffer(sz);
347 	memset( buf, 'x', sz);
348 	str1->UnlockBuffer( sz);
349 	str1->ReplaceSet("x", "");
350 	CPPUNIT_ASSERT(str1->Length() == 0);
351 	delete str1;
352 
353 	// ReplaceAll(const char*, const char*)
354 	NextSubTest();
355 	str1 = new BString();
356 	buf = str1->LockBuffer(sz);
357 	memset( buf, 'x', sz);
358 	str1->UnlockBuffer( sz);
359 	str1->ReplaceAll("x", "y");
360 	CPPUNIT_ASSERT(str1->Length() == sz);
361 	delete str1;
362 
363 	NextSubTest();
364 	str1 = new BString();
365 	buf = str1->LockBuffer(sz);
366 	memset( buf, 'x', sz);
367 	str1->UnlockBuffer( sz);
368 	str1->ReplaceAll("xx", "y");
369 	CPPUNIT_ASSERT(str1->Length() == sz / 2);
370 	delete str1;
371 
372 	NextSubTest();
373 	str1 = new BString();
374 	buf = str1->LockBuffer(sz);
375 	memset( buf, 'x', sz);
376 	str1->UnlockBuffer( sz);
377 	str1->ReplaceSet("xx", "");
378 	CPPUNIT_ASSERT(str1->Length() == 0);
379 	delete str1;
380 
381 }
382 
383 
384 CppUnit::Test *StringReplaceTest::suite(void)
385 {
386 	typedef CppUnit::TestCaller<StringReplaceTest>
387 		StringReplaceTestCaller;
388 
389 	return(new StringReplaceTestCaller("BString::Replace Test",
390 		&StringReplaceTest::PerformTest));
391 }
392