xref: /haiku/src/tests/kits/support/bstring/StringReplaceTest.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
1 #include "StringReplaceTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 
5 StringReplaceTest::StringReplaceTest(std::string name) :
6 		BTestCase(name)
7 {
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', 'i', 2);
66 	CPPUNIT_ASSERT(strcmp(str1->String(), "tesi siring") == 0);
67 	delete str1;
68 
69 	//&Replace(char, char, int32, int32)
70 	NextSubTest();
71 	str1 = new BString("she sells sea shells on the sea shore");
72 	str1->Replace('s', 't', 4, 2);
73 	CPPUNIT_ASSERT(strcmp(str1->String(), "she tellt tea thells on the sea shore") == 0);
74 	delete str1;
75 
76 	NextSubTest();
77 	str1 = new BString();
78 	str1->Replace('s', 'x', 12, 32);
79 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
80 	delete str1;
81 
82 	//&ReplaceFirst(const char*, const char*)
83 	NextSubTest();
84 	str1 = new BString("she sells sea shells on the seashore");
85 	str1->ReplaceFirst("sea", "the");
86 	CPPUNIT_ASSERT(strcmp(str1->String(),
87 		"she sells the shells on the seashore") == 0);
88 	delete str1;
89 
90 	NextSubTest();
91 	str1 = new BString("she sells sea shells on the seashore");
92 	str1->ReplaceFirst("tex", "the");
93 	CPPUNIT_ASSERT(strcmp(str1->String(),
94 		"she sells sea shells on the seashore") == 0);
95 	delete str1;
96 
97 	//&ReplaceLast(const char*, const char*)
98 	NextSubTest();
99 	str1 = new BString("she sells sea shells on the seashore");
100 	str1->ReplaceLast("sea", "the");
101 	CPPUNIT_ASSERT(strcmp(str1->String(),
102 		"she sells sea shells on the theshore") == 0);
103 	delete str1;
104 
105 	NextSubTest();
106 	str1 = new BString("she sells sea shells on the seashore");
107 	str1->ReplaceLast("tex", "the");
108 	CPPUNIT_ASSERT(strcmp(str1->String(),
109 		"she sells sea shells on the seashore") == 0);
110 	delete str1;
111 
112 	//&ReplaceAll(const char*, const char*, int32)
113 	NextSubTest();
114 	str1 = new BString("abc abc abc");
115 	str1->ReplaceAll("ab", "abc");
116 	CPPUNIT_ASSERT(strcmp(str1->String(),
117 		"abcc abcc abcc") == 0);
118 	delete str1;
119 
120 	NextSubTest();
121 	str1 = new BString("she sells sea shells on the seashore");
122 	str1->ReplaceAll("tex", "the");
123 	CPPUNIT_ASSERT(strcmp(str1->String(),
124 		"she sells sea shells on the seashore") == 0);
125 	delete str1;
126 
127 	NextSubTest();
128 	str1 = new BString("she sells sea shells on the seashore");
129 	str1->IReplaceAll("sea", "the", 11);
130 	CPPUNIT_ASSERT(strcmp(str1->String(),
131 		"she sells sea shells on the theshore") == 0);
132 	delete str1;
133 
134 	//&IReplaceFirst(char, char);
135 	NextSubTest();
136 	str1 = new BString("test string");
137 	str1->IReplaceFirst('t', 'b');
138 	CPPUNIT_ASSERT(strcmp(str1->String(), "best string") == 0);
139 	delete str1;
140 
141 	NextSubTest();
142 	str1 = new BString("test string");
143 	str1->IReplaceFirst('x', 'b');
144 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
145 	delete str1;
146 
147 	//&IReplaceLast(char, char);
148 	NextSubTest();
149 	str1 = new BString("test string");
150 	str1->IReplaceLast('t', 'w');
151 	CPPUNIT_ASSERT(strcmp(str1->String(), "test swring") == 0);
152 	delete str1;
153 
154 	NextSubTest();
155 	str1 = new BString("test string");
156 	str1->IReplaceLast('x', 'b');
157 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
158 	delete str1;
159 
160 	//&IReplaceAll(char, char, int32);
161 	NextSubTest();
162 	str1 = new BString("TEST string");
163 	str1->IReplaceAll('t', 'i');
164 	CPPUNIT_ASSERT(strcmp(str1->String(), "iESi siring") == 0);
165 	delete str1;
166 
167 	NextSubTest();
168 	str1 = new BString("test string");
169 	str1->IReplaceAll('x', 'b');
170 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
171 	delete str1;
172 
173 	NextSubTest();
174 	str1 = new BString("TEST string");
175 	str1->IReplaceAll('t', 'i', 2);
176 	CPPUNIT_ASSERT(strcmp(str1->String(), "TESi siring") == 0);
177 	delete str1;
178 
179 	//&IReplace(char, char, int32, int32)
180 	NextSubTest();
181 	str1 = new BString("She sells Sea shells on the sea shore");
182 	str1->IReplace('s', 't', 4, 2);
183 	CPPUNIT_ASSERT(strcmp(str1->String(), "She tellt tea thells on the sea shore") == 0);
184 	delete str1;
185 
186 	NextSubTest();
187 	str1 = new BString();
188 	str1->IReplace('s', 'x', 12, 32);
189 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
190 	delete str1;
191 
192 	//&IReplaceFirst(const char*, const char*)
193 	NextSubTest();
194 	str1 = new BString("she sells SeA shells on the seashore");
195 	str1->IReplaceFirst("sea", "the");
196 	CPPUNIT_ASSERT(strcmp(str1->String(),
197 		"she sells the shells on the seashore") == 0);
198 	delete str1;
199 
200 	NextSubTest();
201 	str1 = new BString("she sells sea shells on the seashore");
202 	str1->IReplaceFirst("tex", "the");
203 	CPPUNIT_ASSERT(strcmp(str1->String(),
204 		"she sells sea shells on the seashore") == 0);
205 	delete str1;
206 
207 	//&IReplaceLast(const char*, const char*)
208 #ifndef TEST_R5
209 	NextSubTest();
210 	str1 = new BString("she sells sea shells on the SEashore");
211 	str1->IReplaceLast("sea", "the");
212 	CPPUNIT_ASSERT(strcmp(str1->String(),
213 		"she sells sea shells on the theshore") == 0);
214 	delete str1;
215 #endif
216 	NextSubTest();
217 	str1 = new BString("she sells sea shells on the seashore");
218 	str1->IReplaceLast("tex", "the");
219 	CPPUNIT_ASSERT(strcmp(str1->String(),
220 		"she sells sea shells on the seashore") == 0);
221 	delete str1;
222 
223 	//&IReplaceAll(const char*, const char*, int32)
224 	NextSubTest();
225 	str1 = new BString("abc ABc aBc");
226 	str1->IReplaceAll("ab", "abc");
227 	CPPUNIT_ASSERT(strcmp(str1->String(),
228 		"abcc abcc abcc") == 0);
229 	delete str1;
230 
231 	NextSubTest();
232 	str1 = new BString("she sells sea shells on the seashore");
233 	str1->IReplaceAll("tex", "the");
234 	CPPUNIT_ASSERT(strcmp(str1->String(),
235 		"she sells sea shells on the seashore") == 0);
236 	delete str1;
237 
238 	NextSubTest();
239 	str1 = new BString("she sells SeA shells on the sEashore");
240 	str1->IReplaceAll("sea", "the", 11);
241 	CPPUNIT_ASSERT(strcmp(str1->String(),
242 		"she sells SeA shells on the theshore") == 0);
243 	delete str1;
244 
245 	//ReplaceSet(const char*, char)
246 	NextSubTest();
247 	str1 = new BString("abc abc abc");
248 	str1->ReplaceSet("ab", 'x');
249 	CPPUNIT_ASSERT(strcmp(str1->String(), "xxc xxc xxc") == 0);
250 	delete str1;
251 
252 	NextSubTest();
253 	str1 = new BString("abcabcabcbababc");
254 	str1->ReplaceSet("abc", 'c');
255 	CPPUNIT_ASSERT(strcmp(str1->String(), "ccccccccccccccc") == 0);
256 	delete str1;
257 
258 #ifndef TEST_R5
259 	//ReplaceSet(const char*, const char*)
260 	NextSubTest();
261 	str1 = new BString("abcd abcd abcd");
262 	str1->ReplaceSet("abcd ", "");
263 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
264 	delete str1;
265 #endif
266 
267 #ifndef TEST_R5
268 	//ReplaceSet(const char*, const char*)
269 	NextSubTest();
270 	str1 = new BString("abcd abcd abcd");
271 	str1->ReplaceSet("ad", "da");
272 	CPPUNIT_ASSERT(strcmp(str1->String(), "dabcda dabcda dabcda") == 0);
273 	delete str1;
274 #endif
275 
276 #ifndef TEST_R5
277 	//ReplaceSet(const char*, const char*)
278 	NextSubTest();
279 	str1 = new BString("abcd abcd abcd");
280 	str1->ReplaceSet("ad", "");
281 	CPPUNIT_ASSERT(strcmp(str1->String(), "bc bc bc") == 0);
282 	delete str1;
283 #endif
284 
285 	// we repeat some test, but this time with a bit of data
286 	// to test the performance:
287 
288 	// ReplaceSet(const char*, const char*)
289 	NextSubTest();
290 	str1 = new BString();
291 	buf = str1->LockBuffer(sz);
292 	memset( buf, 'x', sz);
293 	str1->UnlockBuffer( sz);
294 	str1->ReplaceSet("x", "y");
295 	CPPUNIT_ASSERT(str1->Length() == sz);
296 	delete str1;
297 
298 	NextSubTest();
299 	str1 = new BString();
300 	buf = str1->LockBuffer(sz);
301 	memset( buf, 'x', sz);
302 	str1->UnlockBuffer( sz);
303 	str1->ReplaceSet("x", "");
304 	CPPUNIT_ASSERT(str1->Length() == 0);
305 	delete str1;
306 
307 	// ReplaceAll(const char*, const char*)
308 	NextSubTest();
309 	str1 = new BString();
310 	buf = str1->LockBuffer(sz);
311 	memset( buf, 'x', sz);
312 	str1->UnlockBuffer( sz);
313 	str1->ReplaceAll("x", "y");
314 	CPPUNIT_ASSERT(str1->Length() == sz);
315 	delete str1;
316 
317 	NextSubTest();
318 	str1 = new BString();
319 	buf = str1->LockBuffer(sz);
320 	memset( buf, 'x', sz);
321 	str1->UnlockBuffer( sz);
322 	str1->ReplaceAll("xx", "y");
323 	CPPUNIT_ASSERT(str1->Length() == sz/2);
324 	delete str1;
325 
326 	NextSubTest();
327 	str1 = new BString();
328 	buf = str1->LockBuffer(sz);
329 	memset( buf, 'x', sz);
330 	str1->UnlockBuffer( sz);
331 	str1->ReplaceSet("xx", "");
332 	CPPUNIT_ASSERT(str1->Length() == 0);
333 	delete str1;
334 
335 }
336 
337 
338 CppUnit::Test *StringReplaceTest::suite(void)
339 {
340 	typedef CppUnit::TestCaller<StringReplaceTest>
341 		StringReplaceTestCaller;
342 
343 	return(new StringReplaceTestCaller("BString::Replace Test", &StringReplaceTest::PerformTest));
344 }
345