diff options
Diffstat (limited to 'src/unit/string.unit')
-rw-r--r-- | src/unit/string.unit | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/src/unit/string.unit b/src/unit/string.unit new file mode 100644 index 0000000..f51e4de --- /dev/null +++ b/src/unit/string.unit | |||
@@ -0,0 +1,351 @@ | |||
1 | // vim: syntax=cpp | ||
2 | /* | ||
3 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
4 | * | ||
5 | * This file is part of the libbu++ library and is released under the | ||
6 | * terms of the license contained in the file LICENSE. | ||
7 | */ | ||
8 | |||
9 | #include "bu/string.h" | ||
10 | |||
11 | #include <dirent.h> | ||
12 | |||
13 | suite String | ||
14 | { | ||
15 | test compare1 | ||
16 | { | ||
17 | Bu::String b("Bob"); | ||
18 | unitTest( !(b == "Bobo") ); | ||
19 | unitTest( b == "Bob" ); | ||
20 | } | ||
21 | |||
22 | test compare2 | ||
23 | { | ||
24 | Bu::String b("Bobo"); | ||
25 | unitTest( !(b == "Bob") ); | ||
26 | unitTest( b == "Bobo" ); | ||
27 | } | ||
28 | |||
29 | test appendSingle | ||
30 | { | ||
31 | Bu::String b; | ||
32 | for( char l = 'a'; l < 'g'; l++ ) | ||
33 | b += l; | ||
34 | unitTest( b == "abcdef" ); | ||
35 | unitTest( strcmp( b.getStr(), "abcdef" ) == 0 ); | ||
36 | } | ||
37 | |||
38 | test shared1 | ||
39 | { | ||
40 | Bu::String a("Hey there"); | ||
41 | Bu::String b( a ); | ||
42 | unitTest( a.getConstStr() == b.getConstStr() ); | ||
43 | b += " guy"; | ||
44 | unitTest( a.getConstStr() != b.getConstStr() ); | ||
45 | a = b; | ||
46 | unitTest( a.getConstStr() == b.getConstStr() ); | ||
47 | } | ||
48 | |||
49 | test insert | ||
50 | { | ||
51 | Bu::String a("abcd"); | ||
52 | a.insert( 2, "-!-", 3 ); | ||
53 | unitTest( a == "ab-!-cd" ); | ||
54 | |||
55 | a.insert( 0, "!!", 2 ); | ||
56 | unitTest( a == "!!ab-!-cd" ); | ||
57 | |||
58 | a.insert( -10, "789", 3 ); | ||
59 | unitTest( a == "789!!ab-!-cd" ); | ||
60 | |||
61 | a.insert( 12, "89", 2 ); | ||
62 | unitTest( a == "789!!ab-!-cd89" ); | ||
63 | |||
64 | a.insert( 1203, "12", 2 ); | ||
65 | unitTest( a == "789!!ab-!-cd8912" ); | ||
66 | } | ||
67 | |||
68 | test remove | ||
69 | { | ||
70 | Bu::String a("abHEYcd"); | ||
71 | a.remove( 2, 3 ); | ||
72 | unitTest( a == "abcd" ); | ||
73 | a.remove( 2, 5 ); | ||
74 | unitTest( a == "ab" ); | ||
75 | a += "cdefghijklmnop"; | ||
76 | a.remove( 5, 1 ); | ||
77 | unitTest( a == "abcdeghijklmnop" ); | ||
78 | } | ||
79 | |||
80 | test add1 | ||
81 | { | ||
82 | Bu::String a("hi there"); | ||
83 | Bu::String b(", yeah!"); | ||
84 | Bu::String c = a + b; | ||
85 | |||
86 | unitTest( c == "hi there, yeah!" ); | ||
87 | } | ||
88 | |||
89 | test add2 | ||
90 | { | ||
91 | Bu::String a("hi there"); | ||
92 | Bu::String c = a + ", yeah!"; | ||
93 | |||
94 | unitTest( c == "hi there, yeah!" ); | ||
95 | } | ||
96 | |||
97 | test add3 | ||
98 | { | ||
99 | Bu::String a("hi there"); | ||
100 | Bu::String b(", yeah!"); | ||
101 | Bu::String c = a + ", Mr. Man" + b; | ||
102 | |||
103 | unitTest( c == "hi there, Mr. Man, yeah!" ); | ||
104 | } | ||
105 | |||
106 | test add4 | ||
107 | { | ||
108 | Bu::String b(", yeah!"); | ||
109 | Bu::String c = "hi there" + b; | ||
110 | |||
111 | unitTest( c == "hi there, yeah!" ); | ||
112 | } | ||
113 | |||
114 | test add5 | ||
115 | { | ||
116 | Bu::String b; | ||
117 | Bu::String c = "sup?"; | ||
118 | b += "hey, " + c; | ||
119 | |||
120 | unitTest( b == "hey, sup?" ); | ||
121 | } | ||
122 | |||
123 | test add6 | ||
124 | { | ||
125 | Bu::String a("Hello"); | ||
126 | char b[256] = {"Dude"}; | ||
127 | Bu::String c = a + "/" + b; | ||
128 | |||
129 | unitTest( c == "Hello/Dude" ); | ||
130 | } | ||
131 | |||
132 | test add7 | ||
133 | { | ||
134 | const Bu::String a("hello "); | ||
135 | Bu::String b(" how "); | ||
136 | unitTest( a == "hello " ); | ||
137 | unitTest( a + "dude" == "hello dude" ); | ||
138 | unitTest( a + "dude" + b + "are you?" == "hello dude how are you?" ); | ||
139 | } | ||
140 | |||
141 | test subStr1 | ||
142 | { | ||
143 | Bu::String a("abcdefghijklmnop"); | ||
144 | Bu::String::iterator i = a.find('f'); | ||
145 | unitTest( a.getSubStr( i, Bu::String::iterator() ) == "fghijklmnop" ); | ||
146 | Bu::String::iterator j = i.find('l'); | ||
147 | unitTest( a.getSubStr( i, j ) == "fghijk" ); | ||
148 | } | ||
149 | |||
150 | test compareSub1 | ||
151 | { | ||
152 | Bu::String a("just a string."); | ||
153 | unitTest( a.compareSub("a ", 5, 2) == true ); | ||
154 | unitTest( a.compareSub("string.aoeu", 7, 11 ) == false ); | ||
155 | unitTest( a.compareSub("string.aoeu", 7, 3 ) == true ); | ||
156 | } | ||
157 | |||
158 | test compareSub2 | ||
159 | { | ||
160 | Bu::String a("just a string."); | ||
161 | unitTest( a.compareSub(Bu::String("a "), 5, 2) == true ); | ||
162 | unitTest( a.compareSub(Bu::String("string.aoeu"), 7, 11 ) == false ); | ||
163 | unitTest( a.compareSub(Bu::String("string.aoeu"), 7, 3 ) == true ); | ||
164 | } | ||
165 | |||
166 | test iterator1 | ||
167 | { | ||
168 | Bu::String a("This is a test."); | ||
169 | Bu::String b; | ||
170 | for( Bu::String::iterator i = a.begin(); i; i++ ) | ||
171 | { | ||
172 | b += *i; | ||
173 | } | ||
174 | unitTest( a == b ); | ||
175 | } | ||
176 | |||
177 | test iterator2 | ||
178 | { | ||
179 | Bu::String a("This is a test."); | ||
180 | Bu::String b("--This is a test."); | ||
181 | Bu::String::iterator ai = a.begin(); | ||
182 | Bu::String::iterator bi = b.begin(); | ||
183 | unitTest( ai.compare( bi ) == false ); | ||
184 | unitTest( bi.compare( ai ) == false ); | ||
185 | bi++; bi++; | ||
186 | unitTest( ai.compare( bi ) == true ); | ||
187 | unitTest( bi.compare( ai ) == true ); | ||
188 | } | ||
189 | |||
190 | test iterator3 | ||
191 | { | ||
192 | Bu::String a("1234honour"); | ||
193 | Bu::String b("--1234ueje"); | ||
194 | Bu::String::iterator ai = a.begin(); | ||
195 | Bu::String::iterator bi = b.begin(); | ||
196 | unitTest( ai.compare( bi, 4 ) == false ); | ||
197 | unitTest( bi.compare( ai, 4 ) == false ); | ||
198 | bi++; bi++; | ||
199 | unitTest( ai.compare( bi, 4 ) == true ); | ||
200 | unitTest( bi.compare( ai, 4 ) == true ); | ||
201 | unitTest( ai.compare( bi, 5 ) == false ); | ||
202 | unitTest( bi.compare( ai, 5 ) == false ); | ||
203 | |||
204 | } | ||
205 | |||
206 | test iterator4 | ||
207 | { | ||
208 | Bu::String a("1234aoeu"); | ||
209 | Bu::String::iterator ai = a.begin(); | ||
210 | unitTest( ai.compare("1234") == false ); | ||
211 | unitTest( ai.compare("1234aoeu") == true ); | ||
212 | unitTest( ai.compare("1234aoeuee") == false ); | ||
213 | } | ||
214 | |||
215 | test iterator5 | ||
216 | { | ||
217 | Bu::String a("1234aoeu"); | ||
218 | Bu::String::iterator ai = a.begin(); | ||
219 | unitTest( ai.compare("1234", 4) == true ); | ||
220 | unitTest( ai.compare("1234aoeu", 8) == true ); | ||
221 | unitTest( ai.compare("1234aoeuee", 10) == false ); | ||
222 | } | ||
223 | |||
224 | test iterator6 | ||
225 | { | ||
226 | Bu::String a("just ->this part"); | ||
227 | Bu::String b; | ||
228 | Bu::String::iterator s = a.begin(); | ||
229 | for(; s; s++ ) | ||
230 | { | ||
231 | if( *s == '>' ) | ||
232 | { | ||
233 | s++; | ||
234 | b.set( s ); | ||
235 | break; | ||
236 | } | ||
237 | } | ||
238 | unitTest( b == "this part" ); | ||
239 | |||
240 | b.append( s ); | ||
241 | |||
242 | Bu::String c; | ||
243 | c.set( b.begin() ); | ||
244 | |||
245 | // This is here because the comparison operator used to cause flattening. | ||
246 | unitTest( b == "this partthis part" ); | ||
247 | unitTest( c == b ); | ||
248 | } | ||
249 | |||
250 | test iterator7 | ||
251 | { | ||
252 | Bu::String a("just [this] part"); | ||
253 | Bu::String b; | ||
254 | Bu::String::iterator s = a.begin(); | ||
255 | for(; s; s++ ) | ||
256 | { | ||
257 | if( *s == '[' ) | ||
258 | { | ||
259 | s++; | ||
260 | break; | ||
261 | } | ||
262 | } | ||
263 | Bu::String::iterator e = s; | ||
264 | for(; e; e++ ) | ||
265 | { | ||
266 | if( *e == ']' ) | ||
267 | { | ||
268 | b.set( s, e ); | ||
269 | break; | ||
270 | } | ||
271 | } | ||
272 | unitTest( b == "this" ); | ||
273 | |||
274 | b.append( s, e ); | ||
275 | |||
276 | for( Bu::String::iterator i = b.begin(); i;) | ||
277 | { | ||
278 | Bu::String::iterator k = i; | ||
279 | k++; | ||
280 | if( !k ) | ||
281 | { | ||
282 | b.append( b.begin(), i ); | ||
283 | break; | ||
284 | } | ||
285 | i = k; | ||
286 | } | ||
287 | Bu::String l; | ||
288 | l.set( b.begin() ); | ||
289 | unitTest( l == "thisthisthisthi" ); | ||
290 | for( Bu::String::iterator i = b.begin(); i;) | ||
291 | { | ||
292 | Bu::String::iterator k = i; | ||
293 | k++; | ||
294 | if( !k ) | ||
295 | { | ||
296 | b.append( b.begin(), i ); | ||
297 | break; | ||
298 | } | ||
299 | i = k; | ||
300 | } | ||
301 | l.set( b.begin() ); | ||
302 | unitTest( l == "thisthisthisthithisthisthisth" ); | ||
303 | } | ||
304 | |||
305 | test isSet1 | ||
306 | { | ||
307 | Bu::String bob; | ||
308 | |||
309 | unitTest( bob.isSet() == false ); | ||
310 | bob = "something"; | ||
311 | unitTest( bob.isSet() == true ); | ||
312 | bob = ""; | ||
313 | unitTest( bob.isSet() == false ); | ||
314 | } | ||
315 | |||
316 | test swap1 | ||
317 | { | ||
318 | Bu::String a, b; | ||
319 | a = "Goodbye"; | ||
320 | b = "Hello"; | ||
321 | Bu::swap( a, b ); | ||
322 | unitTest( a == "Hello" ); | ||
323 | unitTest( b == "Goodbye" ); | ||
324 | } | ||
325 | |||
326 | test swap2 | ||
327 | { | ||
328 | Bu::String a, b; | ||
329 | a = "Goodbye"; | ||
330 | b = "Hello"; | ||
331 | std::swap( a, b ); | ||
332 | unitTest( a == "Hello" ); | ||
333 | unitTest( b == "Goodbye" ); | ||
334 | } | ||
335 | |||
336 | test replace1 | ||
337 | { | ||
338 | Bu::String a; | ||
339 | a = "This is a test."; | ||
340 | unitTest( a.replace("i", "ooo") == "Thooos ooos a test." ); | ||
341 | } | ||
342 | |||
343 | test coreDerefBug1 | ||
344 | { | ||
345 | Bu::String a, b; | ||
346 | a = "bob"; | ||
347 | a.setSize( 0 ); | ||
348 | b = a; | ||
349 | b.getStr(); | ||
350 | } | ||
351 | } | ||