xref: /haiku/src/tests/kits/support/bstring/StringAssignTest.cpp (revision 427edfcf0ddc74fc461c9355484d33fd4b027b70)
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 	// char ptr is NULL
36 	NextSubTest();
37 	char *s = NULL;
38 	str = new BString;
39 	*str = s;
40 	CPPUNIT_ASSERT(strcmp(str->String(), "") == 0);
41 	delete str;
42 
43 	// SetTo(const char *) (NULL)
44 	NextSubTest();
45 	str = new BString;
46 	str->SetTo(s);
47 	CPPUNIT_ASSERT(strcmp(str->String(), "") == 0);
48 	delete str;
49 
50 	NextSubTest();
51 	str = new BString;
52 	str->SetTo("BLA");
53 	CPPUNIT_ASSERT(strcmp(str->String(), "BLA") == 0);
54 	delete str;
55 
56 	// SetTo(BString&)
57 	NextSubTest();
58 	str = new BString;
59 	str->SetTo(string);
60 	CPPUNIT_ASSERT(strcmp(str->String(), string.String()) == 0);
61 	delete str;
62 
63 	// SetTo(char, int32)
64 	NextSubTest();
65 	str = new BString;
66 	str->SetTo('C', 10);
67 	CPPUNIT_ASSERT(strcmp(str->String(), "CCCCCCCCCC") == 0);
68 	delete str;
69 
70 	NextSubTest();
71 	str = new BString("ASDSGAFA");
72 	str->SetTo('C', 0);
73 	CPPUNIT_ASSERT(strcmp(str->String(), "") == 0);
74 	delete str;
75 
76 	// SetTo(const char*, int32)
77 	NextSubTest();
78 	str = new BString;
79 	str->SetTo("ABC", 10);
80 	CPPUNIT_ASSERT(strcmp(str->String(), "ABC") == 0);
81 	delete str;
82 
83 	// Adopt(BString&)
84 	NextSubTest();
85 	const char *oldString2 = string2.String();
86 	str = new BString;
87 	str->Adopt(string2);
88 	CPPUNIT_ASSERT(strcmp(str->String(), oldString2) == 0);
89 	CPPUNIT_ASSERT(strcmp(string2.String(), "") == 0);
90 	delete str;
91 
92 	NextSubTest();
93 	BString newstring("SomethingElseAgain");
94 	str = new BString;
95 	str->Adopt(newstring, 2);
96 	CPPUNIT_ASSERT(strncmp(str->String(), "SomethingElseAgain", 2) == 0);
97 	CPPUNIT_ASSERT(str->Length() == 2);
98 	CPPUNIT_ASSERT(strcmp(newstring.String(), "") == 0);
99 	delete str;
100 
101 #ifndef TEST_R5
102 	const int32 OUT_OF_MEM_VAL = 2 * 1000 * 1000 * 1000;
103 	// SetTo(char, int32) with excessive length:
104 	NextSubTest();
105 	str = new BString("dummy");
106 	str->SetTo('C', OUT_OF_MEM_VAL);
107 	CPPUNIT_ASSERT(strcmp(str->String(), "dummy") == 0);
108 	delete str;
109 
110 	// SetTo(char*, int32) with excessive length:
111 	NextSubTest();
112 	str = new BString("dummy");
113 	str->SetTo("some more text", OUT_OF_MEM_VAL);
114 	CPPUNIT_ASSERT(strcmp(str->String(), "some more text") == 0);
115 	delete str;
116 #endif
117 }
118 
119 
120 CppUnit::Test *StringAssignTest::suite(void)
121 {
122 	typedef CppUnit::TestCaller<StringAssignTest>
123 		StringAssignTestCaller;
124 
125 	return(new StringAssignTestCaller("BString::Assign Test",
126 		&StringAssignTest::PerformTest));
127 }
128