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