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