1 #include "StringAccessTest.h" 2 #include "cppunit/TestCaller.h" 3 #include <stdio.h> 4 #include <String.h> 5 #include <UTF8.h> 6 7 StringAccessTest::StringAccessTest(std::string name) : 8 BTestCase(name) 9 { 10 } 11 12 13 14 StringAccessTest::~StringAccessTest() 15 { 16 } 17 18 19 void 20 StringAccessTest::PerformTest(void) 21 { 22 //CountChars(), Length(), String() 23 NextSubTest(); 24 BString string("Something"B_UTF8_ELLIPSIS); 25 CPPUNIT_ASSERT(string.CountChars() == 10); 26 CPPUNIT_ASSERT((unsigned)string.Length() == strlen(string.String())); 27 28 NextSubTest(); 29 BString string2("ABCD"); 30 CPPUNIT_ASSERT(string2.CountChars() == 4); 31 CPPUNIT_ASSERT((unsigned)string2.Length() == strlen(string2.String())); 32 33 NextSubTest(); 34 static char s[64]; 35 strcpy(s, B_UTF8_ELLIPSIS); 36 strcat(s, B_UTF8_SMILING_FACE); 37 BString string3(s); 38 CPPUNIT_ASSERT(string3.CountChars() == 2); 39 CPPUNIT_ASSERT((unsigned)string3.Length() == strlen(string3.String())); 40 41 //An empty string 42 NextSubTest(); 43 BString empty; 44 CPPUNIT_ASSERT(strcmp(empty.String(), "") == 0); 45 CPPUNIT_ASSERT(empty.Length() == 0); 46 CPPUNIT_ASSERT(empty.CountChars() == 0); 47 48 //Truncate the string at end so we are left with an invalid 49 //UTF8 character 50 NextSubTest(); 51 BString invalid("some text with utf8 characters"B_UTF8_ELLIPSIS); 52 invalid.Truncate(invalid.Length() -1); 53 CPPUNIT_ASSERT(invalid.CountChars() == 31); 54 55 //LockBuffer(int32) and UnlockBuffer(int32) 56 NextSubTest(); 57 BString locked("a string"); 58 char *ptrstr = locked.LockBuffer(20); 59 CPPUNIT_ASSERT(strcmp(ptrstr, "a string") == 0); 60 strcat(ptrstr, " to be locked"); 61 locked.UnlockBuffer(); 62 CPPUNIT_ASSERT(strcmp(ptrstr, "a string to be locked") == 0); 63 64 NextSubTest(); 65 BString locked2("some text"); 66 char *ptr = locked2.LockBuffer(3); 67 CPPUNIT_ASSERT(strcmp(ptr, "some text") == 0); 68 locked2.UnlockBuffer(4); 69 CPPUNIT_ASSERT(strcmp(locked2.String(), "some") == 0); 70 CPPUNIT_ASSERT(locked2.Length() == 4); 71 72 NextSubTest(); 73 BString emptylocked; 74 ptr = emptylocked.LockBuffer(10); 75 CPPUNIT_ASSERT(strcmp(ptr, "") == 0); 76 strcat(ptr, "pippo"); 77 emptylocked.UnlockBuffer(); 78 CPPUNIT_ASSERT(strcmp(emptylocked.String(), "pippo") == 0); 79 80 // LockBuffer(0) and UnlockBuffer(-1) on a zero lenght string 81 #ifndef TEST_R5 82 NextSubTest(); 83 BString crashesR5; 84 ptr = crashesR5.LockBuffer(0); 85 crashesR5.UnlockBuffer(-1); 86 CPPUNIT_ASSERT(strcmp(crashesR5.String(), "") == 0); 87 #endif 88 } 89 90 91 CppUnit::Test *StringAccessTest::suite(void) 92 { 93 typedef CppUnit::TestCaller<StringAccessTest> 94 StringAccessTestCaller; 95 96 return(new StringAccessTestCaller("BString::Access Test", &StringAccessTest::PerformTest)); 97 } 98