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 StringAssignTest::~StringAssignTest() 13 { 14 } 15 16 17 void 18 StringAssignTest::PerformTest(void) 19 { 20 // =(BString&) 21 NextSubTest(); 22 BString string; 23 BString string2("Something"); 24 string = string2; 25 CPPUNIT_ASSERT(strcmp(string.String(), string2.String()) == 0); 26 CPPUNIT_ASSERT(strcmp(string.String(), "Something") == 0); 27 28 // =(const char*) 29 NextSubTest(); 30 BString *str = new BString(); 31 *str = "Something Else"; 32 CPPUNIT_ASSERT(strcmp(str->String(), "Something Else") == 0); 33 delete str; 34 35 // =(BString&&) 36 #if __cplusplus >= 201103L 37 NextSubTest(); 38 BString movableString("Something movable"); 39 str = new BString(); 40 *str = std::move(movableString); 41 CPPUNIT_ASSERT(strcmp(str->String(), "Something movable") == 0); 42 CPPUNIT_ASSERT(strcmp(movableString.String(), "") == 0); 43 delete str; 44 #endif 45 46 // char ptr is NULL 47 NextSubTest(); 48 char *s = NULL; 49 str = new BString; 50 *str = s; 51 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 52 delete str; 53 54 // SetTo(const char *) (NULL) 55 NextSubTest(); 56 str = new BString; 57 str->SetTo(s); 58 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 59 delete str; 60 61 NextSubTest(); 62 str = new BString; 63 str->SetTo("BLA"); 64 CPPUNIT_ASSERT(strcmp(str->String(), "BLA") == 0); 65 delete str; 66 67 // SetTo(BString&) 68 NextSubTest(); 69 str = new BString; 70 str->SetTo(string); 71 CPPUNIT_ASSERT(strcmp(str->String(), string.String()) == 0); 72 delete str; 73 74 // SetTo(char, int32) 75 NextSubTest(); 76 str = new BString; 77 str->SetTo('C', 10); 78 CPPUNIT_ASSERT(strcmp(str->String(), "CCCCCCCCCC") == 0); 79 delete str; 80 81 NextSubTest(); 82 str = new BString("ASDSGAFA"); 83 str->SetTo('C', 0); 84 CPPUNIT_ASSERT(strcmp(str->String(), "") == 0); 85 delete str; 86 87 // SetTo(const char*, int32) 88 NextSubTest(); 89 str = new BString; 90 str->SetTo("ABC", 10); 91 CPPUNIT_ASSERT(strcmp(str->String(), "ABC") == 0); 92 delete str; 93 94 // Adopt(BString&) 95 NextSubTest(); 96 const char *oldString2 = string2.String(); 97 str = new BString; 98 str->Adopt(string2); 99 CPPUNIT_ASSERT(strcmp(str->String(), oldString2) == 0); 100 CPPUNIT_ASSERT(strcmp(string2.String(), "") == 0); 101 delete str; 102 103 NextSubTest(); 104 BString newstring("SomethingElseAgain"); 105 str = new BString; 106 str->Adopt(newstring, 2); 107 CPPUNIT_ASSERT(strncmp(str->String(), "SomethingElseAgain", 2) == 0); 108 CPPUNIT_ASSERT(str->Length() == 2); 109 CPPUNIT_ASSERT(strcmp(newstring.String(), "") == 0); 110 delete str; 111 112 #ifndef TEST_R5 113 // TODO: The following test cases only work with hoard2, which will not 114 // allow allocations via malloc() larger than the largest size-class 115 // (see threadHeap::malloc(size_t). Other malloc implementations like 116 // rpmalloc will allow arbitrarily large allocations via create_area(). 117 // 118 // This test should be made more robust by breaking the dependency on 119 // the allocator to simulate failures in another way. This may require 120 // a tricky build configuration to avoid breaking the ABI of BString. 121 const int32 OUT_OF_MEM_VAL = 2 * 1000 * 1000 * 1000; 122 // SetTo(char, int32) with excessive length: 123 NextSubTest(); 124 str = new BString("dummy"); 125 str->SetTo('C', OUT_OF_MEM_VAL); 126 CPPUNIT_ASSERT(strcmp(str->String(), "dummy") == 0); 127 delete str; 128 129 // SetTo(char*, int32) with excessive length: 130 NextSubTest(); 131 str = new BString("dummy"); 132 str->SetTo("some more text", OUT_OF_MEM_VAL); 133 CPPUNIT_ASSERT(strcmp(str->String(), "some more text") == 0); 134 delete str; 135 #endif 136 } 137 138 139 CppUnit::Test *StringAssignTest::suite(void) 140 { 141 typedef CppUnit::TestCaller<StringAssignTest> 142 StringAssignTestCaller; 143 144 return(new StringAssignTestCaller("BString::Assign Test", 145 &StringAssignTest::PerformTest)); 146 } 147