xref: /haiku/src/tests/kits/support/bstring/StringRemoveTest.cpp (revision 31f5d420edd1625a5c211fceee69e943677593d2)
1 #include "StringRemoveTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 #include <stdio.h>
5 
6 StringRemoveTest::StringRemoveTest(std::string name) :
7 		BTestCase(name)
8 {
9 }
10 
11 
12 
13 StringRemoveTest::~StringRemoveTest()
14 {
15 }
16 
17 
18 void
19 StringRemoveTest::PerformTest(void)
20 {
21 	BString *string1, *string2;
22 
23 	//Truncate(int32 newLength, bool lazy);
24 	//lazy = true
25 	NextSubTest();
26 	string1 = new BString("This is a long string");
27 	string1->Truncate(14, true);
28 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long") == 0);
29 	CPPUNIT_ASSERT(string1->Length() == 14);
30 	delete string1;
31 
32 	//lazy = false
33 	NextSubTest();
34 	string1 = new BString("This is a long string");
35 	string1->Truncate(14, false);
36 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long") == 0);
37 	CPPUNIT_ASSERT(string1->Length() == 14);
38 	delete string1;
39 
40 #ifndef TEST_R5
41 	//new length is < 0
42 	//it crashes r5 implementation, but ours works fine here,
43 	//in this case, we just truncate to 0
44 	NextSubTest();
45 	string1 = new BString("This is a long string");
46 	string1->Truncate(-3);
47 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
48 	CPPUNIT_ASSERT(string1->Length() == 0);
49 	delete string1;
50 #endif
51 
52 	//new length is > old length
53 	NextSubTest();
54 	string1 = new BString("This is a long string");
55 	string1->Truncate(45);
56 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long string") == 0);
57 	CPPUNIT_ASSERT(string1->Length() == 21);
58 	delete string1;
59 
60 	//String was empty
61 	NextSubTest();
62 	string1 = new BString;
63 	string1->Truncate(0);
64 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
65 	CPPUNIT_ASSERT(string1->Length() == 0);
66 	delete string1;
67 
68 	//Remove(int32 from, int32 length)
69 	NextSubTest();
70 	string1 = new BString("a String");
71 	string1->Remove(2, 2);
72 	CPPUNIT_ASSERT(strcmp(string1->String(), "a ring") == 0);
73 	delete string1;
74 
75 	//String was empty
76 	NextSubTest();
77 	string1 = new BString;
78 	string1->Remove(2, 1);
79 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
80 	delete string1;
81 
82 	//from is beyond the end of the string
83 	NextSubTest();
84 	string1 = new BString("a String");
85 	string1->Remove(20, 2);
86 	CPPUNIT_ASSERT(strcmp(string1->String(), "a String") == 0);
87 	delete string1;
88 
89 	//from + length is > Length()
90 	NextSubTest();
91 	string1 = new BString("a String");
92 	string1->Remove(4, 30);
93 	CPPUNIT_ASSERT(strcmp(string1->String(), "a String") == 0);
94 	delete string1;
95 
96 	NextSubTest();
97 	string1 = new BString("a String");
98 	string1->Remove(-3, 5);
99 	CPPUNIT_ASSERT(strcmp(string1->String(), "ing") == 0);
100 	delete string1;
101 
102 	//RemoveFirst(BString&)
103 	NextSubTest();
104 	string1 = new BString("first second first");
105 	string2 = new BString("first");
106 	string1->RemoveFirst(*string2);
107 	CPPUNIT_ASSERT(strcmp(string1->String(), " second first") == 0);
108 	delete string1;
109 	delete string2;
110 
111 	NextSubTest();
112 	string1 = new BString("first second first");
113 	string2 = new BString("noway");
114 	string1->RemoveFirst(*string2);
115 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
116 	delete string1;
117 	delete string2;
118 
119 	//RemoveLast(Bstring&)
120 	NextSubTest();
121 	string1 = new BString("first second first");
122 	string2 = new BString("first");
123 	string1->RemoveLast(*string2);
124 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second ") == 0);
125 	delete string1;
126 	delete string2;
127 
128 	NextSubTest();
129 	string1 = new BString("first second first");
130 	string2 = new BString("noway");
131 	string1->RemoveLast(*string2);
132 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
133 	delete string1;
134 	delete string2;
135 
136 	//RemoveAll(BString&)
137 	NextSubTest();
138 	string1 = new BString("first second first");
139 	string2 = new BString("first");
140 	string1->RemoveAll(*string2);
141 	CPPUNIT_ASSERT(strcmp(string1->String(), " second ") == 0);
142 	delete string1;
143 	delete string2;
144 
145 	NextSubTest();
146 	string1 = new BString("first second first");
147 	string2 = new BString("noway");
148 	string1->RemoveAll(*string2);
149 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
150 	delete string1;
151 	delete string2;
152 
153 	//RemoveFirst(const char*)
154 	NextSubTest();
155 	string1 = new BString("first second first");
156 	string1->RemoveFirst("first");
157 	CPPUNIT_ASSERT(strcmp(string1->String(), " second first") == 0);
158 	delete string1;
159 
160 	NextSubTest();
161 	string1 = new BString("first second first");
162 	string1->RemoveFirst("noway");
163 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
164 	delete string1;
165 
166 	NextSubTest();
167 	string1 = new BString("first second first");
168 	string1->RemoveFirst((char*)NULL);
169 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
170 	delete string1;
171 
172 	//RemoveLast(const char*)
173 	NextSubTest();
174 	string1 = new BString("first second first");
175 	string1->RemoveLast("first");
176 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second ") == 0);
177 	delete string1;
178 
179 	NextSubTest();
180 	string1 = new BString("first second first");
181 	string1->RemoveLast("noway");
182 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
183 	delete string1;
184 
185 	//RemoveAll(const char*)
186 	NextSubTest();
187 	string1 = new BString("first second first");
188 	string1->RemoveAll("first");
189 	CPPUNIT_ASSERT(strcmp(string1->String(), " second ") == 0);
190 	delete string1;
191 
192 	NextSubTest();
193 	string1 = new BString("first second first");
194 	string1->RemoveAll("noway");
195 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
196 	delete string1;
197 
198 	//RemoveSet(const char*)
199 	NextSubTest();
200 	string1 = new BString("a sentence with (3) (642) numbers (2) in it");
201 	string1->RemoveSet("()3624 ");
202 	CPPUNIT_ASSERT(strcmp(string1->String(), "asentencewithnumbersinit") == 0);
203 	delete string1;
204 
205 	NextSubTest();
206 	string1 = new BString("a string");
207 	string1->RemoveSet("1345");
208 	CPPUNIT_ASSERT(strcmp(string1->String(), "a string") == 0);
209 	delete string1;
210 
211 	//MoveInto(BString &into, int32, int32)
212 	NextSubTest();
213 	string1 = new BString("some text");
214 	string2 = new BString("string");
215 	string2->MoveInto(*string1, 3, 2);
216 	CPPUNIT_ASSERT(strcmp(string1->String(), "in") == 0);
217 	CPPUNIT_ASSERT(strcmp(string2->String(), "strg") == 0);
218 	delete string1;
219 	delete string2;
220 
221 	NextSubTest();
222 	string1 = new BString("some text");
223 	string2 = new BString("string");
224 	string2->MoveInto(*string1, 0, 200);
225 	CPPUNIT_ASSERT(strcmp(string1->String(), "string") == 0);
226 	CPPUNIT_ASSERT(strcmp(string2->String(), "string") == 0);
227 	delete string1;
228 	delete string2;
229 
230 	//MoveInto(char *, int32, int32)
231 	NextSubTest();
232 	char dest[100];
233 	memset(dest, 0, 100);
234 	string1 = new BString("some text");
235 	string1->MoveInto(dest, 3, 2);
236 	CPPUNIT_ASSERT(strcmp(dest, "e ") == 0);
237 	CPPUNIT_ASSERT(strcmp(string1->String(), "somtext") == 0);
238 	delete string1;
239 
240 	NextSubTest();
241 	string1 = new BString("some text");
242 	memset(dest, 0, 100);
243 	string1->MoveInto(dest, 0, 50);
244 	CPPUNIT_ASSERT(strcmp(dest, "some text") == 0);
245 	CPPUNIT_ASSERT(strcmp(string1->String(), "some text") == 0);
246 	delete string1;
247 }
248 
249 
250 CppUnit::Test *StringRemoveTest::suite(void)
251 {
252 	typedef CppUnit::TestCaller<StringRemoveTest>
253 		StringRemoveTestCaller;
254 
255 	return(new StringRemoveTestCaller("BString::Remove Test", &StringRemoveTest::PerformTest));
256 }
257