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->ReplaceAll("sea", "the", 11); 128 CPPUNIT_ASSERT(strcmp(str1->String(), 129 "she sells sea shells on the theshore") == 0); 130 delete str1; 131 } 132 133 134 CppUnit::Test *StringReplaceTest::suite(void) 135 { 136 typedef CppUnit::TestCaller<StringReplaceTest> 137 StringReplaceTestCaller; 138 139 return(new StringReplaceTestCaller("BString::Replace Test", &StringReplaceTest::PerformTest)); 140 } 141