1 #include "StringAppendTest.h" 2 #include "cppunit/TestCaller.h" 3 #include <String.h> 4 5 6 StringAppendTest::StringAppendTest(std::string name) 7 : BTestCase(name) 8 { 9 } 10 11 12 StringAppendTest::~StringAppendTest() 13 { 14 } 15 16 17 void 18 StringAppendTest::PerformTest(void) 19 { 20 BString *str1, *str2; 21 22 // +=(BString&) 23 NextSubTest(); 24 str1 = new BString("BASE"); 25 str2 = new BString("APPENDED"); 26 *str1 += *str2; 27 CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAPPENDED") == 0); 28 delete str1; 29 delete str2; 30 31 // +=(const char *) 32 NextSubTest(); 33 str1 = new BString("Base"); 34 *str1 += "APPENDED"; 35 CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0); 36 delete str1; 37 38 NextSubTest(); 39 str1 = new BString; 40 *str1 += "APPENDEDTONOTHING"; 41 CPPUNIT_ASSERT(strcmp(str1->String(), "APPENDEDTONOTHING") == 0); 42 delete str1; 43 44 // char pointer is NULL 45 NextSubTest(); 46 char *tmp = NULL; 47 str1 = new BString("Base"); 48 *str1 += tmp; 49 CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0); 50 delete str1; 51 52 // +=(char) 53 NextSubTest(); 54 str1 = new BString("Base"); 55 *str1 += 'C'; 56 CPPUNIT_ASSERT(strcmp(str1->String(), "BaseC") == 0); 57 delete str1; 58 59 // Append(BString&) 60 NextSubTest(); 61 str1 = new BString("BASE"); 62 str2 = new BString("APPENDED"); 63 str1->Append(*str2); 64 CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAPPENDED") == 0); 65 delete str1; 66 delete str2; 67 68 // Append(const char*) 69 NextSubTest(); 70 str1 = new BString("Base"); 71 str1->Append("APPENDED"); 72 CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0); 73 delete str1; 74 75 NextSubTest(); 76 str1 = new BString; 77 str1->Append("APPENDEDTONOTHING"); 78 CPPUNIT_ASSERT(strcmp(str1->String(), "APPENDEDTONOTHING") == 0); 79 delete str1; 80 81 // char ptr is NULL 82 NextSubTest(); 83 str1 = new BString("Base"); 84 str1->Append(tmp); 85 CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0); 86 delete str1; 87 88 // Append(BString&, int32) 89 NextSubTest(); 90 str1 = new BString("BASE"); 91 str2 = new BString("APPENDED"); 92 str1->Append(*str2, 2); 93 CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAP") == 0); 94 delete str1; 95 delete str2; 96 97 // Append(const char*, int32) 98 NextSubTest(); 99 str1 = new BString("Base"); 100 str1->Append("APPENDED", 40); 101 CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0); 102 CPPUNIT_ASSERT(str1->Length() == (int32)strlen("BaseAPPENDED")); 103 delete str1; 104 105 // char ptr is NULL 106 NextSubTest(); 107 str1 = new BString("BLABLA"); 108 str1->Append(tmp, 2); 109 CPPUNIT_ASSERT(strcmp(str1->String(), "BLABLA") == 0); 110 delete str1; 111 112 // Append(char, int32) 113 NextSubTest(); 114 str1 = new BString("Base"); 115 str1->Append('C', 5); 116 CPPUNIT_ASSERT(strcmp(str1->String(), "BaseCCCCC") == 0); 117 delete str1; 118 119 120 #ifndef TEST_R5 121 const int32 OUT_OF_MEM_VAL = 2 * 1000 * 1000 * 1000; 122 // Append(char, int32) with excessive length: 123 NextSubTest(); 124 str1 = new BString("Base"); 125 str1->Append('C', OUT_OF_MEM_VAL); 126 CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0); 127 delete str1; 128 #endif 129 130 #ifndef TEST_R5 131 // Append(char*, int32) with excessive length: 132 NextSubTest(); 133 str1 = new BString("Base"); 134 str1->Append("some more text", OUT_OF_MEM_VAL); 135 CPPUNIT_ASSERT(strcmp(str1->String(), "Basesome more text") == 0); 136 delete str1; 137 #endif 138 } 139 140 141 CppUnit::Test *StringAppendTest::suite(void) 142 { 143 typedef CppUnit::TestCaller<StringAppendTest> 144 StringAppendTestCaller; 145 146 return(new StringAppendTestCaller("BString::Append Test", 147 &StringAppendTest::PerformTest)); 148 } 149