xref: /haiku/src/tests/kits/support/bstring/StringReplaceTest.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
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, *str2;
21 
22 	//&ReplaceFirst(char, char);
23 	NextSubTest();
24 	str1 = new BString("test string");
25 	str1->ReplaceFirst('t', 'b');
26 	CPPUNIT_ASSERT(strcmp(str1->String(), "best string") == 0);
27 	delete str1;
28 
29 	NextSubTest();
30 	str1 = new BString("test string");
31 	str1->ReplaceFirst('x', 'b');
32 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
33 	delete str1;
34 
35 	//&ReplaceLast(char, char);
36 	NextSubTest();
37 	str1 = new BString("test string");
38 	str1->ReplaceLast('t', 'w');
39 	CPPUNIT_ASSERT(strcmp(str1->String(), "test swring") == 0);
40 	delete str1;
41 
42 	NextSubTest();
43 	str1 = new BString("test string");
44 	str1->ReplaceLast('x', 'b');
45 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
46 	delete str1;
47 
48 	//&ReplaceAll(char, char, int32);
49 	NextSubTest();
50 	str1 = new BString("test string");
51 	str1->ReplaceAll('t', 'i');
52 	CPPUNIT_ASSERT(strcmp(str1->String(), "iesi siring") == 0);
53 	delete str1;
54 
55 	NextSubTest();
56 	str1 = new BString("test string");
57 	str1->ReplaceAll('x', 'b');
58 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
59 	delete str1;
60 
61 	NextSubTest();
62 	str1 = new BString("test string");
63 	str1->ReplaceAll('t', 'i', 2);
64 	CPPUNIT_ASSERT(strcmp(str1->String(), "tesi siring") == 0);
65 	delete str1;
66 
67 	//&Replace(char, char, int32, int32)
68 	NextSubTest();
69 	str1 = new BString("she sells sea shells on the sea shore");
70 	str1->Replace('s', 't', 4, 2);
71 	CPPUNIT_ASSERT(strcmp(str1->String(), "she tellt tea thells on the sea shore") == 0);
72 	delete str1;
73 
74 	NextSubTest();
75 	str1 = new BString();
76 	str1->Replace('s', 'x', 12, 32);
77 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
78 	delete str1;
79 
80 	//&ReplaceFirst(const char*, const char*)
81 	NextSubTest();
82 	str1 = new BString("she sells sea shells on the seashore");
83 	str1->ReplaceFirst("sea", "the");
84 	CPPUNIT_ASSERT(strcmp(str1->String(),
85 		"she sells the shells on the seashore") == 0);
86 	delete str1;
87 
88 	NextSubTest();
89 	str1 = new BString("she sells sea shells on the seashore");
90 	str1->ReplaceFirst("tex", "the");
91 	CPPUNIT_ASSERT(strcmp(str1->String(),
92 		"she sells sea shells on the seashore") == 0);
93 	delete str1;
94 
95 	//&ReplaceLast(const char*, const char*)
96 	NextSubTest();
97 	str1 = new BString("she sells sea shells on the seashore");
98 	str1->ReplaceLast("sea", "the");
99 	CPPUNIT_ASSERT(strcmp(str1->String(),
100 		"she sells sea shells on the theshore") == 0);
101 	delete str1;
102 
103 	NextSubTest();
104 	str1 = new BString("she sells sea shells on the seashore");
105 	str1->ReplaceLast("tex", "the");
106 	CPPUNIT_ASSERT(strcmp(str1->String(),
107 		"she sells sea shells on the seashore") == 0);
108 	delete str1;
109 
110 	//&ReplaceAll(const char*, const char*, int32)
111 	NextSubTest();
112 	str1 = new BString("abc abc abc");
113 	str1->ReplaceAll("ab", "abc");
114 	CPPUNIT_ASSERT(strcmp(str1->String(),
115 		"abcc abcc abcc") == 0);
116 	delete str1;
117 
118 	NextSubTest();
119 	str1 = new BString("she sells sea shells on the seashore");
120 	str1->ReplaceAll("tex", "the");
121 	CPPUNIT_ASSERT(strcmp(str1->String(),
122 		"she sells sea shells on the seashore") == 0);
123 	delete str1;
124 
125 	NextSubTest();
126 	str1 = new BString("she sells sea shells on the seashore");
127 	str1->IReplaceAll("sea", "the", 11);
128 	CPPUNIT_ASSERT(strcmp(str1->String(),
129 		"she sells sea shells on the theshore") == 0);
130 	delete str1;
131 
132 	//&IReplaceFirst(char, char);
133 	NextSubTest();
134 	str1 = new BString("test string");
135 	str1->IReplaceFirst('t', 'b');
136 	CPPUNIT_ASSERT(strcmp(str1->String(), "best string") == 0);
137 	delete str1;
138 
139 	NextSubTest();
140 	str1 = new BString("test string");
141 	str1->IReplaceFirst('x', 'b');
142 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
143 	delete str1;
144 
145 	//&IReplaceLast(char, char);
146 	NextSubTest();
147 	str1 = new BString("test string");
148 	str1->IReplaceLast('t', 'w');
149 	CPPUNIT_ASSERT(strcmp(str1->String(), "test swring") == 0);
150 	delete str1;
151 
152 	NextSubTest();
153 	str1 = new BString("test string");
154 	str1->IReplaceLast('x', 'b');
155 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
156 	delete str1;
157 
158 	//&IReplaceAll(char, char, int32);
159 	NextSubTest();
160 	str1 = new BString("TEST string");
161 	str1->IReplaceAll('t', 'i');
162 	CPPUNIT_ASSERT(strcmp(str1->String(), "iESi siring") == 0);
163 	delete str1;
164 
165 	NextSubTest();
166 	str1 = new BString("test string");
167 	str1->IReplaceAll('x', 'b');
168 	CPPUNIT_ASSERT(strcmp(str1->String(), "test string") == 0);
169 	delete str1;
170 
171 	NextSubTest();
172 	str1 = new BString("TEST string");
173 	str1->IReplaceAll('t', 'i', 2);
174 	CPPUNIT_ASSERT(strcmp(str1->String(), "TESi siring") == 0);
175 	delete str1;
176 
177 	//&IReplace(char, char, int32, int32)
178 	NextSubTest();
179 	str1 = new BString("She sells Sea shells on the sea shore");
180 	str1->IReplace('s', 't', 4, 2);
181 	CPPUNIT_ASSERT(strcmp(str1->String(), "She tellt tea thells on the sea shore") == 0);
182 	delete str1;
183 
184 	NextSubTest();
185 	str1 = new BString();
186 	str1->IReplace('s', 'x', 12, 32);
187 	CPPUNIT_ASSERT(strcmp(str1->String(), "") == 0);
188 	delete str1;
189 
190 	//&IReplaceFirst(const char*, const char*)
191 	NextSubTest();
192 	str1 = new BString("she sells SeA shells on the seashore");
193 	str1->IReplaceFirst("sea", "the");
194 	CPPUNIT_ASSERT(strcmp(str1->String(),
195 		"she sells the shells on the seashore") == 0);
196 	delete str1;
197 
198 	NextSubTest();
199 	str1 = new BString("she sells sea shells on the seashore");
200 	str1->IReplaceFirst("tex", "the");
201 	CPPUNIT_ASSERT(strcmp(str1->String(),
202 		"she sells sea shells on the seashore") == 0);
203 	delete str1;
204 
205 	//&IReplaceLast(const char*, const char*)
206 #ifndef TEST_R5
207 	NextSubTest();
208 	str1 = new BString("she sells sea shells on the SEashore");
209 	str1->IReplaceLast("sea", "the");
210 	CPPUNIT_ASSERT(strcmp(str1->String(),
211 		"she sells sea shells on the theshore") == 0);
212 	delete str1;
213 #endif
214 	NextSubTest();
215 	str1 = new BString("she sells sea shells on the seashore");
216 	str1->IReplaceLast("tex", "the");
217 	CPPUNIT_ASSERT(strcmp(str1->String(),
218 		"she sells sea shells on the seashore") == 0);
219 	delete str1;
220 
221 	//&IReplaceAll(const char*, const char*, int32)
222 	NextSubTest();
223 	str1 = new BString("abc ABc aBc");
224 	str1->IReplaceAll("ab", "abc");
225 	CPPUNIT_ASSERT(strcmp(str1->String(),
226 		"abcc abcc abcc") == 0);
227 	delete str1;
228 
229 	NextSubTest();
230 	str1 = new BString("she sells sea shells on the seashore");
231 	str1->IReplaceAll("tex", "the");
232 	CPPUNIT_ASSERT(strcmp(str1->String(),
233 		"she sells sea shells on the seashore") == 0);
234 	delete str1;
235 
236 	NextSubTest();
237 	str1 = new BString("she sells SeA shells on the sEashore");
238 	str1->IReplaceAll("sea", "the", 11);
239 	CPPUNIT_ASSERT(strcmp(str1->String(),
240 		"she sells SeA shells on the theshore") == 0);
241 	delete str1;
242 
243 	//ReplaceSet(const char*, char)
244 	NextSubTest();
245 	str1 = new BString("abc abc abc");
246 	str1->ReplaceSet("ab", 'x');
247 	CPPUNIT_ASSERT(strcmp(str1->String(), "xxc xxc xxc") == 0);
248 	delete str1;
249 
250 	NextSubTest();
251 	str1 = new BString("abcabcabcbababc");
252 	str1->ReplaceSet("abc", 'c');
253 	CPPUNIT_ASSERT(strcmp(str1->String(), "ccccccccccccccc") == 0);
254 	delete str1;
255 
256 #ifndef TEST_R5
257 	//ReplaceSet(const char*, const char*)
258 	NextSubTest();
259 	str1 = new BString("abcd abcd abcd");
260 	str1->ReplaceSet("ad", "da");
261 	CPPUNIT_ASSERT(strcmp(str1->String(), "dabcda dabcda dabcda") == 0);
262 	delete str1;
263 #endif
264 }
265 
266 
267 CppUnit::Test *StringReplaceTest::suite(void)
268 {
269 	typedef CppUnit::TestCaller<StringReplaceTest>
270 		StringReplaceTestCaller;
271 
272 	return(new StringReplaceTestCaller("BString::Replace Test", &StringReplaceTest::PerformTest));
273 }
274