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