xref: /haiku/src/tests/kits/support/bstring/StringAssignTest.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
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 
104 
105 CppUnit::Test *StringAssignTest::suite(void)
106 {
107 	typedef CppUnit::TestCaller<StringAssignTest>
108 		StringAssignTestCaller;
109 
110 	return(new StringAssignTestCaller("BString::Assign Test", &StringAssignTest::PerformTest));
111 }
112