]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/stdstrings.cpp
removed src/gtk/eggtrayicon.h
[wxWidgets.git] / tests / strings / stdstrings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/stdstrings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-05-07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/cppunit.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class StdStringTestCase : public CppUnit::TestCase
31 {
32 public:
33 StdStringTestCase();
34
35 private:
36 CPPUNIT_TEST_SUITE( StdStringTestCase );
37 CPPUNIT_TEST( StdConstructors );
38 CPPUNIT_TEST( StdAppend );
39 CPPUNIT_TEST( StdAssign );
40 CPPUNIT_TEST( StdCompare );
41 CPPUNIT_TEST( StdErase );
42 CPPUNIT_TEST( StdFind );
43 CPPUNIT_TEST( StdFindFirst );
44 CPPUNIT_TEST( StdFindLast );
45 CPPUNIT_TEST( StdInsert );
46 CPPUNIT_TEST( StdReplace );
47 CPPUNIT_TEST( StdRFind );
48 CPPUNIT_TEST( StdResize );
49 CPPUNIT_TEST( StdSubstr );
50 CPPUNIT_TEST_SUITE_END();
51
52 void StdConstructors();
53 void StdAppend();
54 void StdAssign();
55 void StdCompare();
56 void StdErase();
57 void StdFind();
58 void StdFindFirst();
59 void StdFindLast();
60 void StdInsert();
61 void StdReplace();
62 void StdRFind();
63 void StdResize();
64 void StdSubstr();
65
66 DECLARE_NO_COPY_CLASS(StdStringTestCase)
67 };
68
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( StdStringTestCase );
71
72 // also include in it's own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStringTestCase, "StdStringTestCase" );
74
75 StdStringTestCase::StdStringTestCase()
76 {
77 }
78
79 void StdStringTestCase::StdConstructors()
80 {
81 wxString s1(_T("abcdefgh")),
82 s2(_T("abcdefghijklm"), 8),
83 s3(_T("abcdefghijklm")),
84 s4(8, _T('a'));
85 wxString s5(s1),
86 s6(s3, 0, 8),
87 s7(s3.begin(), s3.begin() + 8);
88 wxString s8(s1, 4, 8);
89
90 CPPUNIT_ASSERT( s1 == _T("abcdefgh") );
91 CPPUNIT_ASSERT( s2 == s1 );
92 CPPUNIT_ASSERT( s4 == _T("aaaaaaaa") );
93 CPPUNIT_ASSERT( s5 == _T("abcdefgh") );
94 CPPUNIT_ASSERT( s6 == s1 );
95 CPPUNIT_ASSERT( s7 == s1 );
96 CPPUNIT_ASSERT( s8 == _T("efgh") );
97 }
98
99 void StdStringTestCase::StdAppend()
100 {
101 wxString s1, s2, s3, s4, s5, s6, s7, s8;
102
103 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc");
104 s1.append(_T("def"));
105 s2.append(_T("defgh"), 3);
106 s3.append(wxString(_T("abcdef")), 3, 6);
107 s4.append(s1);
108 s5.append(3, _T('a'));
109 s6.append(s1.begin() + 3, s1.end());
110
111 CPPUNIT_ASSERT( s1 == _T("abcdef") );
112 CPPUNIT_ASSERT( s2 == _T("abcdef") );
113 CPPUNIT_ASSERT( s3 == _T("abcdef") );
114 CPPUNIT_ASSERT( s4 == _T("abcabcdef") );
115 CPPUNIT_ASSERT( s5 == _T("abcaaa") );
116 CPPUNIT_ASSERT( s6 == _T("abcdef") );
117 }
118
119 void StdStringTestCase::StdAssign()
120 {
121 wxString s1, s2, s3, s4, s5, s6, s7, s8;
122
123 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc");
124 s1.assign(_T("def"));
125 s2.assign(_T("defgh"), 3);
126 s3.assign(wxString(_T("abcdef")), 3, 6);
127 s4.assign(s1);
128 s5.assign(3, _T('a'));
129 s6.assign(s1.begin() + 1, s1.end());
130
131 CPPUNIT_ASSERT( s1 == _T("def") );
132 CPPUNIT_ASSERT( s2 == _T("def") );
133 CPPUNIT_ASSERT( s3 == _T("def") );
134 CPPUNIT_ASSERT( s4 == _T("def") );
135 CPPUNIT_ASSERT( s5 == _T("aaa") );
136 CPPUNIT_ASSERT( s6 == _T("ef") );
137 }
138
139 void StdStringTestCase::StdCompare()
140 {
141 wxString s1, s2, s3, s4, s5, s6, s7, s8;
142
143 s1 = _T("abcdefgh");
144 s2 = _T("abcdefgh");
145 s3 = _T("abc");
146 s4 = _T("abcdefghi");
147 s5 = _T("aaa");
148 s6 = _T("zzz");
149
150 CPPUNIT_ASSERT( s1.compare(s2) == 0 );
151 CPPUNIT_ASSERT( s1.compare(s3) > 0 );
152 CPPUNIT_ASSERT( s1.compare(s4) < 0 );
153 CPPUNIT_ASSERT( s1.compare(s5) > 0 );
154 CPPUNIT_ASSERT( s1.compare(s6) < 0 );
155 CPPUNIT_ASSERT( s1.compare(1, 12, s1) > 0);
156 CPPUNIT_ASSERT( s1.compare(_T("abcdefgh")) == 0);
157 CPPUNIT_ASSERT( s1.compare(1, 7, _T("bcdefgh")) == 0);
158 CPPUNIT_ASSERT( s1.compare(1, 7, _T("bcdefgh"), 7) == 0);
159 }
160
161 void StdStringTestCase::StdErase()
162 {
163 wxString s1, s2, s3, s4, s5, s6, s7;
164
165 s1 = _T("abcdefgh");
166 s2 = _T("abcdefgh");
167 s3 = _T("abc");
168 s4 = _T("abcdefghi");
169 s5 = _T("aaa");
170 s6 = _T("zzz");
171 s7 = _T("zabcdefg");
172
173 s1.erase(1, 1);
174 s2.erase(4, 12);
175 wxString::iterator it = s3.erase(s3.begin() + 1);
176 wxString::iterator it2 = s4.erase(s4.begin() + 4, s4.begin() + 6);
177 wxString::iterator it3 = s7.erase(s7.begin() + 4, s7.begin() + 8);
178
179 CPPUNIT_ASSERT( s1 == _T("acdefgh") );
180 CPPUNIT_ASSERT( s2 == _T("abcd") );
181 CPPUNIT_ASSERT( s3 == _T("ac") );
182 CPPUNIT_ASSERT( s4 == _T("abcdghi") );
183 CPPUNIT_ASSERT( s7 == _T("zabc") );
184 CPPUNIT_ASSERT( *it == _T('c') );
185 CPPUNIT_ASSERT( *it2 == _T('g') );
186 CPPUNIT_ASSERT( it3 == s7.end() );
187 }
188
189 void StdStringTestCase::StdFind()
190 {
191 // 0 1 2
192 // 01234567890123456789012345
193 wxString s1 = _T("abcdefgABCDEFGabcABCabcABC");
194 wxString s2 = _T("gAB");
195
196 CPPUNIT_ASSERT( s1.find(_T('A')) == 7u );
197 CPPUNIT_ASSERT( s1.find(_T('A'), 7) == 7u );
198 CPPUNIT_ASSERT( s1.find(_T('Z')) == wxString::npos );
199 CPPUNIT_ASSERT( s1.find(_T('C'), 22) == 25u );
200
201 CPPUNIT_ASSERT( s1.find(_T("gAB")) == 6u );
202 CPPUNIT_ASSERT( s1.find(_T("gAB"), 7) == wxString::npos );
203 CPPUNIT_ASSERT( s1.find(_T("gAB"), 6) == 6u );
204
205 CPPUNIT_ASSERT( s1.find(_T("gABZZZ"), 2, 3) == 6u );
206 CPPUNIT_ASSERT( s1.find(_T("gABZZZ"), 7, 3) == wxString::npos );
207
208 CPPUNIT_ASSERT( s1.find(s2) == 6u );
209 CPPUNIT_ASSERT( s1.find(s2, 7) == wxString::npos );
210 CPPUNIT_ASSERT( s1.find(s2, 6) == 6u );
211
212 // 0 1 2
213 // 0123456 78901234567 8901234567
214 //wxString _s1 = _T("abcdefg\0ABCDEFGabc\0ABCabcABC");
215 //wxString _s2 = _T("g\0AB");
216 wxString _s1 = _T("abcdefgABCDEFGabcABCabcABC");
217 wxString _s2 = _T("gAB");
218
219 _s1.insert(7, 1, '\0');
220 _s1.insert(18, 1, '\0');
221 _s2.insert(1, 1, '\0');
222
223 CPPUNIT_ASSERT( _s1.find(_T('A')) == 8u );
224 CPPUNIT_ASSERT( _s1.find(_T('A'), 8) == 8u );
225 CPPUNIT_ASSERT( _s1.find(_T('Z')) == wxString::npos );
226 CPPUNIT_ASSERT( _s1.find(_T('C'), 22) == 27u );
227
228 CPPUNIT_ASSERT( _s1.find(_T("AB")) == 8u );
229 CPPUNIT_ASSERT( _s1.find(_T("AB"), 26) == wxString::npos );
230 CPPUNIT_ASSERT( _s1.find(_T("AB"), 23) == 25u );
231
232 CPPUNIT_ASSERT( _s1.find(_T("ABZZZ"), 2, 2) == 8u );
233 CPPUNIT_ASSERT( _s1.find(_T("ABZZZ"), 26, 2) == wxString::npos );
234
235 CPPUNIT_ASSERT( _s1.find(_s2) == 6u );
236 CPPUNIT_ASSERT( _s1.find(_s2, 7) == wxString::npos );
237 CPPUNIT_ASSERT( _s1.find(_s2, 6) == 6u );
238 }
239
240 void StdStringTestCase::StdFindFirst()
241 {
242 // 0 1 2 3
243 // 01234567890123456789012345678901234
244 wxString s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
245 wxString s2 = _T("aaaaaa");
246
247 CPPUNIT_ASSERT( s1.find_first_not_of(_T('a')) == 6u );
248 CPPUNIT_ASSERT( s1.find_first_not_of(_T('a'), 7) == 7u );
249 CPPUNIT_ASSERT( s2.find_first_not_of(_T('a')) == wxString::npos );
250
251 CPPUNIT_ASSERT( s1.find_first_not_of(_T("abde"), 4) == 7u );
252 CPPUNIT_ASSERT( s1.find_first_not_of(_T("abde"), 7) == 7u );
253 CPPUNIT_ASSERT( s1.find_first_not_of(_T("abcdefghijkl")) == wxString::npos );
254
255 CPPUNIT_ASSERT( s1.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u );
256
257 CPPUNIT_ASSERT( s1.find_first_of(_T('c')) == 7u );
258 CPPUNIT_ASSERT( s1.find_first_of(_T('v')) == wxString::npos );
259 CPPUNIT_ASSERT( s1.find_first_of(_T('c'), 10) == 24u );
260
261 CPPUNIT_ASSERT( s1.find_first_of(_T("ijkl")) == 13u );
262 CPPUNIT_ASSERT( s1.find_first_of(_T("ddcfg"), 17) == 24u );
263 CPPUNIT_ASSERT( s1.find_first_of(_T("ddcfga"), 17, 5) == 24u );
264 }
265
266 void StdStringTestCase::StdFindLast()
267 {
268 // 0 1 2 3
269 // 01234567890123456789012345678901234
270 wxString s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
271 wxString s2 = _T("aaaaaa");
272
273 CPPUNIT_ASSERT( s2.find_last_not_of(_T('a')) == wxString::npos );
274 CPPUNIT_ASSERT( s1.find_last_not_of(_T('d')) == 33u );
275 CPPUNIT_ASSERT( s1.find_last_not_of(_T('d'), 25) == 24u );
276
277 CPPUNIT_ASSERT( s1.find_last_not_of(_T("bcd")) == 22u );
278 CPPUNIT_ASSERT( s1.find_last_not_of(_T("abc"), 24) == 16u );
279
280 CPPUNIT_ASSERT( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
281
282 CPPUNIT_ASSERT( s2.find_last_of(_T('c')) == wxString::npos );
283 CPPUNIT_ASSERT( s1.find_last_of(_T('a')) == 22u );
284 CPPUNIT_ASSERT( s1.find_last_of(_T('b'), 24) == 23u );
285
286 CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 16u );
287 CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 33, 4) == 16u );
288 CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 17) == 17u );
289
290
291
292 // 0 1 2 3
293 // 012345 67890123456789 01234567890123456
294 // wxString s1 = _T("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd");
295 // wxString s2 = _T("aaaaaa\0");
296 s1.insert(6,1,'\0');
297 s1.insert(20,1,'\0');
298 s2.insert(6,1,'\0');
299
300 CPPUNIT_ASSERT( s2.find_last_not_of(_T('a')) == 6u );
301 CPPUNIT_ASSERT( s1.find_last_not_of(_T('d')) == 35u );
302 CPPUNIT_ASSERT( s1.find_last_not_of(_T('d'), 27) == 26u );
303
304 CPPUNIT_ASSERT( s1.find_last_not_of(_T("bcd")) == 24u );
305 CPPUNIT_ASSERT( s1.find_last_not_of(_T("abc"), 26) == 20u );
306
307 CPPUNIT_ASSERT( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 26, 3) == 20u );
308
309 CPPUNIT_ASSERT( s2.find_last_of(_T('c')) == wxString::npos );
310 CPPUNIT_ASSERT( s1.find_last_of(_T('a')) == 24u );
311 CPPUNIT_ASSERT( s1.find_last_of(_T('b'), 26) == 25u );
312
313 CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 17u );
314 CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 35, 4) == 17u );
315 CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 18) == 18u );
316 }
317
318 void StdStringTestCase::StdInsert()
319 {
320 wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
321
322 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("aaaa");
323 s9 = s10 = _T("cdefg");
324
325 s1.insert(1, _T("cc") );
326 s2.insert(2, _T("cdef"), 3);
327 s3.insert(2, s10);
328 s4.insert(2, s10, 3, 7);
329 s5.insert(1, 2, _T('c'));
330 s6.insert(s6.begin() + 3, _T('X'));
331 s7.insert(s7.begin(), s9.begin(), s9.end() - 1);
332 s8.insert(s8.begin(), 2, _T('c'));
333
334 CPPUNIT_ASSERT( s1 == _T("accaaa") );
335 CPPUNIT_ASSERT( s2 == _T("aacdeaa") );
336 CPPUNIT_ASSERT( s3 == _T("aacdefgaa") );
337 CPPUNIT_ASSERT( s4 == _T("aafgaa") );
338 CPPUNIT_ASSERT( s5 == _T("accaaa") );
339 CPPUNIT_ASSERT( s6 == _T("aaaXa") );
340 CPPUNIT_ASSERT( s7 == _T("cdefaaaa") );
341 CPPUNIT_ASSERT( s8 == _T("ccaaaa") );
342
343 s1 = s2 = s3 = _T("aaaa");
344 s1.insert(0, _T("ccc"), 2);
345 s2.insert(4, _T("ccc"), 2);
346
347 CPPUNIT_ASSERT( s1 == _T("ccaaaa") );
348 CPPUNIT_ASSERT( s2 == _T("aaaacc") );
349 }
350
351 void StdStringTestCase::StdReplace()
352 {
353 wxString s1, s2, s3, s4, s5, s6, s7, s8, s9;
354
355 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("QWERTYUIOP");
356 s9 = _T("werty");
357
358 s1.replace(3, 4, _T("rtyu"));
359 s1.replace(8, 7, _T("opopop"));
360 s2.replace(10, 12, _T("WWWW"));
361 s3.replace(1, 5, s9);
362 s4.replace(1, 4, s9, 0, 4);
363 s5.replace(1, 2, s9, 1, 12);
364 s6.replace(0, 123, s9, 0, 123);
365 s7.replace(2, 7, s9);
366
367 CPPUNIT_ASSERT( s1 == _T("QWErtyuIopopop") );
368 CPPUNIT_ASSERT( s2 == _T("QWERTYUIOPWWWW") );
369 CPPUNIT_ASSERT( s3 == _T("QwertyUIOP") );
370 CPPUNIT_ASSERT( s4 == _T("QwertYUIOP") );
371 CPPUNIT_ASSERT( s5 == _T("QertyRTYUIOP") );
372 CPPUNIT_ASSERT( s6 == s9);
373 CPPUNIT_ASSERT( s7 == _T("QWwertyP") );
374 }
375
376 void StdStringTestCase::StdRFind()
377 {
378 // 0 1 2
379 // 01234567890123456789012345
380 wxString s1 = _T("abcdefgABCDEFGabcABCabcABC");
381 wxString s2 = _T("gAB");
382 wxString s3 = _T("ab");
383
384 CPPUNIT_ASSERT( s1.rfind(_T('A')) == 23u );
385 CPPUNIT_ASSERT( s1.rfind(_T('A'), 7) == 7u );
386 CPPUNIT_ASSERT( s1.rfind(_T('Z')) == wxString::npos );
387 CPPUNIT_ASSERT( s1.rfind(_T('C'), 22) == 19u );
388
389 CPPUNIT_ASSERT( s1.rfind(_T("cAB")) == 22u );
390 CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 15) == wxString::npos );
391 CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 21) == 16u );
392
393 CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 7, 3) == 6u );
394 CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 5, 3) == wxString::npos );
395
396 CPPUNIT_ASSERT( s1.rfind(s2) == 6u );
397 CPPUNIT_ASSERT( s1.rfind(s2, 5) == wxString::npos );
398 CPPUNIT_ASSERT( s1.rfind(s2, 6) == 6u );
399 CPPUNIT_ASSERT( s1.rfind(s3, 1) == 0u );
400
401
402 // 0 1 2
403 // 01234 56789012 345678901234567
404 // wxString s1 = _T("abcde\0fgABCDE\0FGabcABCabcABC");
405 // wxString s2 = _T("gAB");
406 // wxString s3 = _T("ab");
407
408 s1.insert(5,1,'\0');
409 s1.insert(13,1,'\0');
410
411 CPPUNIT_ASSERT( s1.rfind(_T('A')) == 25u );
412 CPPUNIT_ASSERT( s1.rfind(_T('A'), 8) == 8u );
413 CPPUNIT_ASSERT( s1.rfind(_T('Z')) == wxString::npos );
414 CPPUNIT_ASSERT( s1.rfind(_T('C'), 22) == 21u );
415
416 CPPUNIT_ASSERT( s1.rfind(_T("cAB")) == 24u );
417 CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 15) == wxString::npos );
418 CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 21) == 18u );
419
420 CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 8, 3) == 7u );
421 CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 5, 3) == wxString::npos );
422 }
423
424 void StdStringTestCase::StdResize()
425 {
426 wxString s1, s2, s3, s4;
427
428 s1 = s2 = s3 = s4 = _T("abcABCdefDEF");
429
430 s1.resize( 12 );
431 s2.resize( 10 );
432 s3.resize( 14, _T(' ') );
433 s4.resize( 14, _T('W') );
434
435 CPPUNIT_ASSERT( s1 == _T("abcABCdefDEF") );
436 CPPUNIT_ASSERT( s2 == _T("abcABCdefD") );
437 CPPUNIT_ASSERT( s3 == _T("abcABCdefDEF ") );
438 CPPUNIT_ASSERT( s4 == _T("abcABCdefDEFWW") );
439 }
440
441 void StdStringTestCase::StdSubstr()
442 {
443 wxString s1 = _T("abcdefgABCDEFG");
444
445 CPPUNIT_ASSERT( s1.substr( 0, 14 ) == s1 );
446 CPPUNIT_ASSERT( s1.substr( 1, 13 ) == _T("bcdefgABCDEFG") );
447 CPPUNIT_ASSERT( s1.substr( 1, 20 ) == _T("bcdefgABCDEFG") );
448 CPPUNIT_ASSERT( s1.substr( 14, 30 ) == _T("") );
449
450
451 // 1
452 // 012 34567 89012 3456
453 // wxString s1 = _T("abc\0defg\0ABCD\0EFG");
454
455 s1.insert(3,1,'\0');
456 s1.insert(8,1,'\0');
457 s1.insert(13,1,'\0');
458
459 wxString s2 = wxT("bcdefgABCDEFG");
460 s2.insert(2,1,'\0');
461 s2.insert(7,1,'\0');
462 s2.insert(12,1,'\0');
463
464 CPPUNIT_ASSERT( s1.substr( 0, 17 ) == s1 );
465 CPPUNIT_ASSERT( s1.substr( 1, 17 ) == s2 );
466 CPPUNIT_ASSERT( s1.substr( 1, 20 ) == s2 );
467 CPPUNIT_ASSERT( s1.substr( 17, 30 ) == _T("") );
468 }
469