1#include "StringAppendTest.h"
2#include "cppunit/TestCaller.h"
3#include <String.h>
4
5
6StringAppendTest::StringAppendTest(std::string name)
7		: BTestCase(name)
8{
9}
10
11
12StringAppendTest::~StringAppendTest()
13{
14}
15
16
17void
18StringAppendTest::PerformTest(void)
19{
20	BString *str1, *str2;
21
22	// +=(BString&)
23	NextSubTest();
24	str1 = new BString("BASE");
25	str2 = new BString("APPENDED");
26	*str1 += *str2;
27	CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAPPENDED") == 0);
28	delete str1;
29	delete str2;
30
31	// +=(const char *)
32	NextSubTest();
33	str1 = new BString("Base");
34	*str1 += "APPENDED";
35	CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0);
36	delete str1;
37
38	NextSubTest();
39	str1 = new BString;
40	*str1 += "APPENDEDTONOTHING";
41	CPPUNIT_ASSERT(strcmp(str1->String(), "APPENDEDTONOTHING") == 0);
42	delete str1;
43
44	// char pointer is NULL
45	NextSubTest();
46	char *tmp = NULL;
47	str1 = new BString("Base");
48	*str1 += tmp;
49	CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0);
50	delete str1;
51
52	// +=(char)
53	NextSubTest();
54	str1 = new BString("Base");
55	*str1 += 'C';
56	CPPUNIT_ASSERT(strcmp(str1->String(), "BaseC") == 0);
57	delete str1;
58
59	// Append(BString&)
60	NextSubTest();
61	str1 = new BString("BASE");
62	str2 = new BString("APPENDED");
63	str1->Append(*str2);
64	CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAPPENDED") == 0);
65	delete str1;
66	delete str2;
67
68	// Append(const char*)
69	NextSubTest();
70	str1 = new BString("Base");
71	str1->Append("APPENDED");
72	CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0);
73	delete str1;
74
75	NextSubTest();
76	str1 = new BString;
77	str1->Append("APPENDEDTONOTHING");
78	CPPUNIT_ASSERT(strcmp(str1->String(), "APPENDEDTONOTHING") == 0);
79	delete str1;
80
81	// char ptr is NULL
82	NextSubTest();
83	str1 = new BString("Base");
84	str1->Append(tmp);
85	CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0);
86	delete str1;
87
88	// Append(BString&, int32)
89	NextSubTest();
90	str1 = new BString("BASE");
91	str2 = new BString("APPENDED");
92	str1->Append(*str2, 2);
93	CPPUNIT_ASSERT(strcmp(str1->String(), "BASEAP") == 0);
94	delete str1;
95	delete str2;
96
97	// Append(const char*, int32)
98	NextSubTest();
99	str1 = new BString("Base");
100	str1->Append("APPENDED", 40);
101	CPPUNIT_ASSERT(strcmp(str1->String(), "BaseAPPENDED") == 0);
102	CPPUNIT_ASSERT(str1->Length() == (int32)strlen("BaseAPPENDED"));
103	delete str1;
104
105	// char ptr is NULL
106	NextSubTest();
107	str1 = new BString("BLABLA");
108	str1->Append(tmp, 2);
109	CPPUNIT_ASSERT(strcmp(str1->String(), "BLABLA") == 0);
110	delete str1;
111
112	// Append(char, int32)
113	NextSubTest();
114	str1 = new BString("Base");
115	str1->Append('C', 5);
116	CPPUNIT_ASSERT(strcmp(str1->String(), "BaseCCCCC") == 0);
117	delete str1;
118
119	// TODO: The following test cases only work with hoard2, which will not
120	// allow allocations via malloc() larger than the largest size-class
121	// (see threadHeap::malloc(size_t). Other malloc implementations like
122	// rpmalloc will allow arbitrarily large allocations via create_area().
123	//
124	// This test should be made more robust by breaking the dependency on
125	// the allocator to simulate failures in another way. This may require
126	// a tricky build configuration to avoid breaking the ABI of BString.
127#ifndef TEST_R5
128	const int32 OUT_OF_MEM_VAL = 2 * 1000 * 1000 * 1000;
129	// Append(char, int32) with excessive length:
130	NextSubTest();
131	str1 = new BString("Base");
132	str1->Append('C', OUT_OF_MEM_VAL);
133	CPPUNIT_ASSERT(strcmp(str1->String(), "Base") == 0);
134	delete str1;
135#endif
136
137#ifndef TEST_R5
138	// Append(char*, int32) with excessive length:
139	NextSubTest();
140	str1 = new BString("Base");
141	str1->Append("some more text", OUT_OF_MEM_VAL);
142	CPPUNIT_ASSERT(strcmp(str1->String(), "Basesome more text") == 0);
143	delete str1;
144#endif
145}
146
147
148CppUnit::Test *StringAppendTest::suite(void)
149{
150	typedef CppUnit::TestCaller<StringAppendTest>
151		StringAppendTestCaller;
152
153	return(new StringAppendTestCaller("BString::Append Test",
154		&StringAppendTest::PerformTest));
155}
156