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 NextSubTest(); 207 str1 = new BString("she sells sea shells on the SEashore"); 208 str1->IReplaceLast("sea", "the"); 209 CPPUNIT_ASSERT(strcmp(str1->String(), 210 "she sells sea shells on the theshore") == 0); 211 delete str1; 212 213 NextSubTest(); 214 str1 = new BString("she sells sea shells on the seashore"); 215 str1->IReplaceLast("tex", "the"); 216 CPPUNIT_ASSERT(strcmp(str1->String(), 217 "she sells sea shells on the seashore") == 0); 218 delete str1; 219 220 //&IReplaceAll(const char*, const char*, int32) 221 NextSubTest(); 222 str1 = new BString("abc ABc aBc"); 223 str1->IReplaceAll("ab", "abc"); 224 CPPUNIT_ASSERT(strcmp(str1->String(), 225 "abcc abcc abcc") == 0); 226 delete str1; 227 228 NextSubTest(); 229 str1 = new BString("she sells sea shells on the seashore"); 230 str1->IReplaceAll("tex", "the"); 231 CPPUNIT_ASSERT(strcmp(str1->String(), 232 "she sells sea shells on the seashore") == 0); 233 delete str1; 234 235 NextSubTest(); 236 str1 = new BString("she sells SeA shells on the sEashore"); 237 str1->IReplaceAll("sea", "the", 11); 238 CPPUNIT_ASSERT(strcmp(str1->String(), 239 "she sells SeA shells on the theshore") == 0); 240 delete str1; 241 242 //ReplaceSet(const char*, char) 243 NextSubTest(); 244 str1 = new BString("abc abc abc"); 245 str1->ReplaceSet("ab", 'x'); 246 CPPUNIT_ASSERT(strcmp(str1->String(), "xxc xxc xxc") == 0); 247 delete str1; 248 249 NextSubTest(); 250 str1 = new BString("abcabcabcbababc"); 251 str1->ReplaceSet("abc", 'c'); 252 CPPUNIT_ASSERT(strcmp(str1->String(), "ccccccccccccccc") == 0); 253 delete str1; 254 255 //ReplaceSet(const char*, const char*) 256 NextSubTest(); 257 str1 = new BString("abcd abcd abcd"); 258 str1->ReplaceSet("ad", "da"); 259 CPPUNIT_ASSERT(strcmp(str1->String(), "dabcda dabcda dabcda") == 0); 260 delete str1; 261 } 262 263 264 CppUnit::Test *StringReplaceTest::suite(void) 265 { 266 typedef CppUnit::TestCaller<StringReplaceTest> 267 StringReplaceTestCaller; 268 269 return(new StringReplaceTestCaller("BString::Replace Test", &StringReplaceTest::PerformTest)); 270 } 271