1 #include "StringAssignTest.h" 2 #include "cppunit/TestCaller.h" 3 #include <String.h> 4 5 6 StringAssignTest::StringAssignTest(std::string name) : 7 BTestCase(name) 8 { 9 } 10 11 12 13 StringAssignTest::~StringAssignTest() 14 { 15 } 16 17 18 void 19 StringAssignTest::PerformTest(void) 20 { 21 //=(BString&) 22 NextSubTest(); 23 BString string; 24 BString string2("Something"); 25 string = string2; 26 CPPUNIT_ASSERT(strcmp(string.String(), string2.String()) == 0); 27 CPPUNIT_ASSERT(strcmp(string.String(), "Something") == 0); 28 29 //=(const char*) 30 NextSubTest(); 31 BString *str = new BString(); 32 *str = "Something Else"; 33 CPPUNIT_ASSERT(strcmp(str->String(), "Something Else") == 0); 34 delete str; 35 36 //char ptr is NULL 37 NextSubTest(); 38 char *s = NULL; 39 str = new BString; 40 *str = s; 41 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 42 delete str; 43 44 //SetTo(const char *) (NULL) 45 NextSubTest(); 46 str = new BString; 47 str->SetTo(s); 48 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 49 delete str; 50 51 NextSubTest(); 52 str = new BString; 53 str->SetTo("BLA"); 54 CPPUNIT_ASSERT(strcmp(str->String(), "BLA") == 0); 55 delete str; 56 57 //SetTo(BString&) 58 NextSubTest(); 59 str = new BString; 60 str->SetTo(string); 61 CPPUNIT_ASSERT(strcmp(str->String(), string.String()) == 0); 62 delete str; 63 64 //SetTo(char, int32) 65 NextSubTest(); 66 str = new BString; 67 str->SetTo('C', 10); 68 CPPUNIT_ASSERT(strcmp(str->String(), "CCCCCCCCCC") == 0); 69 delete str; 70 71 NextSubTest(); 72 str = new BString("ASDSGAFA"); 73 str->SetTo('C', 0); 74 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 75 delete str; 76 77 //SetTo(const char*, int32) 78 NextSubTest(); 79 str = new BString; 80 str->SetTo("ABC", 10); 81 CPPUNIT_ASSERT(strcmp(str->String(), "ABC") == 0); 82 delete str; 83 84 //Adopt(BString&) 85 NextSubTest(); 86 const char *oldString2 = string2.String(); 87 str = new BString; 88 str->Adopt(string2); 89 CPPUNIT_ASSERT(strcmp(str->String(), oldString2) == 0); 90 CPPUNIT_ASSERT(strcmp(string2.String(), "") == 0); 91 delete str; 92 93 NextSubTest(); 94 BString newstring("SomethingElseAgain"); 95 const char *oldString = newstring.String(); 96 str = new BString; 97 str->Adopt(newstring, 2); 98 CPPUNIT_ASSERT(strncmp(str->String(), oldString, 2) == 0); 99 CPPUNIT_ASSERT(str->Length() == 2); 100 CPPUNIT_ASSERT(strcmp(newstring.String(), "") == 0); 101 delete str; 102 103 #ifndef TEST_R5 104 const int32 OUT_OF_MEM_VAL = 2*1000*1000*1000; 105 //SetTo(char, int32) with excessive length: 106 NextSubTest(); 107 str = new BString("dummy"); 108 str->SetTo('C', OUT_OF_MEM_VAL); 109 CPPUNIT_ASSERT(strcmp(str->String(), "dummy") == 0); 110 delete str; 111 112 //SetTo(char*, int32) with excessive length: 113 NextSubTest(); 114 str = new BString("dummy"); 115 str->SetTo("some more text", OUT_OF_MEM_VAL); 116 CPPUNIT_ASSERT(strcmp(str->String(), "some more text") == 0); 117 delete str; 118 #endif 119 } 120 121 122 CppUnit::Test *StringAssignTest::suite(void) 123 { 124 typedef CppUnit::TestCaller<StringAssignTest> 125 StringAssignTestCaller; 126 127 return(new StringAssignTestCaller("BString::Assign Test", &StringAssignTest::PerformTest)); 128 } 129