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