]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/stdstrings.cpp
Add wxListCtrlBase common base class for port-specific wxListCtrl.
[wxWidgets.git] / tests / strings / stdstrings.cpp
CommitLineData
7978bc72 1//////////////////////////////////////////////////////////////////////////////
0a199299
WS
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 );
39c20230 36 CPPUNIT_TEST( StdIterators );
bdb40fa6 37 CPPUNIT_TEST( StdIteratorsCmp );
0a199299
WS
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 );
968f291b 49 CPPUNIT_TEST( StdRiter );
0a199299 50 CPPUNIT_TEST( StdSubstr );
59953bf4
VS
51#if wxUSE_STD_STRING
52 CPPUNIT_TEST( StdConversion );
53#endif
0a199299
WS
54 CPPUNIT_TEST_SUITE_END();
55
56 void StdConstructors();
39c20230 57 void StdIterators();
bdb40fa6 58 void StdIteratorsCmp();
0a199299
WS
59 void StdAppend();
60 void StdAssign();
61 void StdCompare();
62 void StdErase();
63 void StdFind();
64 void StdFindFirst();
65 void StdFindLast();
66 void StdInsert();
67 void StdReplace();
68 void StdRFind();
69 void StdResize();
968f291b 70 void StdRiter();
0a199299 71 void StdSubstr();
59953bf4
VS
72#if wxUSE_STD_STRING
73 void StdConversion();
74#endif
0a199299
WS
75
76 DECLARE_NO_COPY_CLASS(StdStringTestCase)
77};
78
79// register in the unnamed registry so that these tests are run by default
80CPPUNIT_TEST_SUITE_REGISTRATION( StdStringTestCase );
81
e3778b4d 82// also include in its own registry so that these tests can be run alone
0a199299
WS
83CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStringTestCase, "StdStringTestCase" );
84
85StdStringTestCase::StdStringTestCase()
86{
87}
88
89void StdStringTestCase::StdConstructors()
90{
9a83f860
VZ
91 wxString s1(wxT("abcdefgh")),
92 s2(wxT("abcdefghijklm"), 8),
93 s3(wxT("abcdefghijklm")),
94 s4(8, wxT('a'));
0a199299
WS
95 wxString s5(s1),
96 s6(s3, 0, 8),
97 s7(s3.begin(), s3.begin() + 8);
98 wxString s8(s1, 4, 8);
99
9a83f860 100 CPPUNIT_ASSERT_EQUAL( wxT("abcdefgh"), s1 );
339e08c7 101 CPPUNIT_ASSERT_EQUAL( s1, s2 );
9a83f860
VZ
102 CPPUNIT_ASSERT_EQUAL( wxT("aaaaaaaa"), s4 );
103 CPPUNIT_ASSERT_EQUAL( wxT("abcdefgh"), s5 );
339e08c7
VZ
104 CPPUNIT_ASSERT_EQUAL( s1, s6 );
105 CPPUNIT_ASSERT_EQUAL( s1, s7 );
9a83f860 106 CPPUNIT_ASSERT_EQUAL( wxT("efgh"), s8 );
f2a1b1bd 107
7978bc72 108 const char *pc = s1.c_str();
1de532f5 109 CPPUNIT_ASSERT_EQUAL( "bcd", wxString(pc + 1, pc + 4) );
f2a1b1bd
VZ
110
111 const wchar_t *pw = s2.c_str();
1de532f5 112 CPPUNIT_ASSERT_EQUAL( "a", wxString(pw, pw + 1) );
0a199299
WS
113}
114
39c20230
VS
115void StdStringTestCase::StdIterators()
116{
117 // test compilation of default iterators ctors:
118 wxString::iterator i1;
119 wxString::const_iterator i2;
120 wxString::reverse_iterator i3;
121 wxString::const_reverse_iterator i4;
122}
123
bdb40fa6
VZ
124void StdStringTestCase::StdIteratorsCmp()
125{
126 wxString s("foobar");
127 wxString::iterator i = s.begin();
128 wxString::const_iterator ci = s.begin();
129
130 CPPUNIT_ASSERT( i == ci );
131 CPPUNIT_ASSERT( i >= ci );
132 CPPUNIT_ASSERT( i <= ci );
133 CPPUNIT_ASSERT( ci == i );
134 CPPUNIT_ASSERT( ci >= i );
135 CPPUNIT_ASSERT( ci <= i );
136
137 ci++;
138
139 CPPUNIT_ASSERT( i != ci );
140 CPPUNIT_ASSERT( i < ci );
141 CPPUNIT_ASSERT( !(i > ci) );
142
143 CPPUNIT_ASSERT( ci != i );
144 CPPUNIT_ASSERT( ci > i );
145 CPPUNIT_ASSERT( !(ci < i) );
146}
147
0a199299
WS
148void StdStringTestCase::StdAppend()
149{
150 wxString s1, s2, s3, s4, s5, s6, s7, s8;
151
9a83f860
VZ
152 s1 = s2 = s3 = s4 = s5 = s6 = wxT("abc");
153 s1.append(wxT("def"));
154 s2.append(wxT("defgh"), 3);
155 s3.append(wxString(wxT("abcdef")), 3, 6);
0a199299 156 s4.append(s1);
9a83f860 157 s5.append(3, wxT('a'));
d483da3c
VS
158 s5.append(2, 'x');
159 s5.append(1, (unsigned char)'y');
0a199299
WS
160 s6.append(s1.begin() + 3, s1.end());
161
9a83f860
VZ
162 CPPUNIT_ASSERT_EQUAL( wxT("abcdef"), s1 );
163 CPPUNIT_ASSERT_EQUAL( wxT("abcdef"), s2 );
164 CPPUNIT_ASSERT_EQUAL( wxT("abcdef"), s3 );
165 CPPUNIT_ASSERT_EQUAL( wxT("abcabcdef"), s4 );
166 CPPUNIT_ASSERT_EQUAL( wxT("abcaaaxxy"), s5 );
167 CPPUNIT_ASSERT_EQUAL( wxT("abcdef"), s6 );
7634e443 168
f2a1b1bd
VZ
169 const char *pc = s1.c_str() + 2;
170 s7.append(pc, pc + 4);
1de532f5 171 CPPUNIT_ASSERT_EQUAL( "cdef", s7 );
f2a1b1bd
VZ
172
173 const wchar_t *pw = s2.c_str() + 2;
174 s8.append(pw, pw + 4);
1de532f5 175 CPPUNIT_ASSERT_EQUAL( "cdef", s8 );
f2a1b1bd 176
9a83f860 177 s7 = s8 = wxString(wxT("null\0time"), 9);
7634e443 178
9a83f860
VZ
179 s7.append(wxT("def"));
180 s8.append(wxT("defgh"), 3);
7634e443 181
9a83f860
VZ
182 CPPUNIT_ASSERT_EQUAL( wxString(wxT("null\0timedef"), 12), s7 );
183 CPPUNIT_ASSERT_EQUAL( wxString(wxT("null\0timedef"), 12), s8 );
0a199299
WS
184}
185
186void StdStringTestCase::StdAssign()
187{
188 wxString s1, s2, s3, s4, s5, s6, s7, s8;
189
9a83f860
VZ
190 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = wxT("abc");
191 s1.assign(wxT("def"));
192 s2.assign(wxT("defgh"), 3);
193 s3.assign(wxString(wxT("abcdef")), 3, 6);
0a199299 194 s4.assign(s1);
9a83f860 195 s5.assign(3, wxT('a'));
0a199299
WS
196 s6.assign(s1.begin() + 1, s1.end());
197
9a83f860
VZ
198 CPPUNIT_ASSERT_EQUAL( wxT("def"), s1 );
199 CPPUNIT_ASSERT_EQUAL( wxT("def"), s2 );
200 CPPUNIT_ASSERT_EQUAL( wxT("def"), s3 );
201 CPPUNIT_ASSERT_EQUAL( wxT("def"), s4 );
202 CPPUNIT_ASSERT_EQUAL( wxT("aaa"), s5 );
203 CPPUNIT_ASSERT_EQUAL( wxT("ef"), s6 );
f2a1b1bd 204
7978bc72 205 const char *pc = s1.c_str();
f2a1b1bd 206 s7.assign(pc, pc + 2);
1de532f5 207 CPPUNIT_ASSERT_EQUAL( "de", s7 );
f2a1b1bd
VZ
208
209 const wchar_t *pw = s1.c_str();
210 s8.assign(pw + 2, pw + 3);
1de532f5 211 CPPUNIT_ASSERT_EQUAL( "f", s8 );
74374c9e
VS
212
213 s1.assign(s1, 1, 1);
1de532f5 214 CPPUNIT_ASSERT_EQUAL("e", s1);
0a199299
WS
215}
216
217void StdStringTestCase::StdCompare()
218{
219 wxString s1, s2, s3, s4, s5, s6, s7, s8;
220
9a83f860
VZ
221 s1 = wxT("abcdefgh");
222 s2 = wxT("abcdefgh");
223 s3 = wxT("abc");
224 s4 = wxT("abcdefghi");
225 s5 = wxT("aaa");
226 s6 = wxT("zzz");
0a199299
WS
227
228 CPPUNIT_ASSERT( s1.compare(s2) == 0 );
229 CPPUNIT_ASSERT( s1.compare(s3) > 0 );
230 CPPUNIT_ASSERT( s1.compare(s4) < 0 );
231 CPPUNIT_ASSERT( s1.compare(s5) > 0 );
232 CPPUNIT_ASSERT( s1.compare(s6) < 0 );
233 CPPUNIT_ASSERT( s1.compare(1, 12, s1) > 0);
9a83f860
VZ
234 CPPUNIT_ASSERT( s1.compare(wxT("abcdefgh")) == 0);
235 CPPUNIT_ASSERT( s1.compare(1, 7, wxT("bcdefgh")) == 0);
236 CPPUNIT_ASSERT( s1.compare(1, 7, wxT("bcdefgh"), 7) == 0);
0a199299
WS
237}
238
239void StdStringTestCase::StdErase()
240{
241 wxString s1, s2, s3, s4, s5, s6, s7;
242
9a83f860
VZ
243 s1 = wxT("abcdefgh");
244 s2 = wxT("abcdefgh");
245 s3 = wxT("abc");
246 s4 = wxT("abcdefghi");
247 s5 = wxT("aaa");
248 s6 = wxT("zzz");
249 s7 = wxT("zabcdefg");
0a199299
WS
250
251 s1.erase(1, 1);
252 s2.erase(4, 12);
253 wxString::iterator it = s3.erase(s3.begin() + 1);
254 wxString::iterator it2 = s4.erase(s4.begin() + 4, s4.begin() + 6);
255 wxString::iterator it3 = s7.erase(s7.begin() + 4, s7.begin() + 8);
256
9a83f860
VZ
257 CPPUNIT_ASSERT_EQUAL( wxT("acdefgh"), s1 );
258 CPPUNIT_ASSERT_EQUAL( wxT("abcd"), s2 );
259 CPPUNIT_ASSERT_EQUAL( wxT("ac"), s3 );
260 CPPUNIT_ASSERT_EQUAL( wxT("abcdghi"), s4 );
261 CPPUNIT_ASSERT_EQUAL( wxT("zabc"), s7 );
262 CPPUNIT_ASSERT( *it == wxT('c') );
263 CPPUNIT_ASSERT( *it2 == wxT('g') );
0a199299
WS
264 CPPUNIT_ASSERT( it3 == s7.end() );
265}
266
267void StdStringTestCase::StdFind()
268{
269 // 0 1 2
270 // 01234567890123456789012345
9a83f860
VZ
271 wxString s1 = wxT("abcdefgABCDEFGabcABCabcABC");
272 wxString s2 = wxT("gAB");
0a199299 273
9a83f860
VZ
274 CPPUNIT_ASSERT( s1.find(wxT('A')) == 7u );
275 CPPUNIT_ASSERT( s1.find(wxT('A'), 7) == 7u );
276 CPPUNIT_ASSERT( s1.find(wxT('Z')) == wxString::npos );
277 CPPUNIT_ASSERT( s1.find(wxT('C'), 22) == 25u );
0a199299 278
9a83f860
VZ
279 CPPUNIT_ASSERT( s1.find(wxT("gAB")) == 6u );
280 CPPUNIT_ASSERT( s1.find(wxT("gAB"), 7) == wxString::npos );
281 CPPUNIT_ASSERT( s1.find(wxT("gAB"), 6) == 6u );
0a199299 282
9a83f860
VZ
283 CPPUNIT_ASSERT( s1.find(wxT("gABZZZ"), 2, 3) == 6u );
284 CPPUNIT_ASSERT( s1.find(wxT("gABZZZ"), 7, 3) == wxString::npos );
0a199299
WS
285
286 CPPUNIT_ASSERT( s1.find(s2) == 6u );
287 CPPUNIT_ASSERT( s1.find(s2, 7) == wxString::npos );
288 CPPUNIT_ASSERT( s1.find(s2, 6) == 6u );
968f291b 289
dcb68102
RN
290 // 0 1 2
291 // 0123456 78901234567 8901234567
9a83f860
VZ
292 //wxString _s1 = wxT("abcdefg\0ABCDEFGabc\0ABCabcABC");
293 //wxString _s2 = wxT("g\0AB");
294 wxString _s1 = wxT("abcdefgABCDEFGabcABCabcABC");
295 wxString _s2 = wxT("gAB");
968f291b 296
dcb68102
RN
297 _s1.insert(7, 1, '\0');
298 _s1.insert(18, 1, '\0');
299 _s2.insert(1, 1, '\0');
968f291b 300
9a83f860
VZ
301 CPPUNIT_ASSERT( _s1.find(wxT('A')) == 8u );
302 CPPUNIT_ASSERT( _s1.find(wxT('A'), 8) == 8u );
303 CPPUNIT_ASSERT( _s1.find(wxT('Z')) == wxString::npos );
304 CPPUNIT_ASSERT( _s1.find(wxT('C'), 22) == 27u );
dcb68102 305
9a83f860
VZ
306 CPPUNIT_ASSERT( _s1.find(wxT("AB")) == 8u );
307 CPPUNIT_ASSERT( _s1.find(wxT("AB"), 26) == wxString::npos );
308 CPPUNIT_ASSERT( _s1.find(wxT("AB"), 23) == 25u );
dcb68102 309
9a83f860
VZ
310 CPPUNIT_ASSERT( _s1.find(wxT("ABZZZ"), 2, 2) == 8u );
311 CPPUNIT_ASSERT( _s1.find(wxT("ABZZZ"), 26, 2) == wxString::npos );
dcb68102
RN
312
313 CPPUNIT_ASSERT( _s1.find(_s2) == 6u );
314 CPPUNIT_ASSERT( _s1.find(_s2, 7) == wxString::npos );
315 CPPUNIT_ASSERT( _s1.find(_s2, 6) == 6u );
0a199299
WS
316}
317
318void StdStringTestCase::StdFindFirst()
319{
320 // 0 1 2 3
321 // 01234567890123456789012345678901234
9a83f860
VZ
322 wxString s1 = wxT("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
323 wxString s2 = wxT("aaaaaa");
0a199299 324
9a83f860
VZ
325 CPPUNIT_ASSERT( s1.find_first_not_of(wxT('a')) == 6u );
326 CPPUNIT_ASSERT( s1.find_first_not_of(wxT('a'), 7) == 7u );
327 CPPUNIT_ASSERT( s2.find_first_not_of(wxT('a')) == wxString::npos );
0a199299 328
9a83f860
VZ
329 CPPUNIT_ASSERT( s1.find_first_not_of(wxT("abde"), 4) == 7u );
330 CPPUNIT_ASSERT( s1.find_first_not_of(wxT("abde"), 7) == 7u );
331 CPPUNIT_ASSERT( s1.find_first_not_of(wxT("abcdefghijkl")) == wxString::npos );
0a199299 332
9a83f860 333 CPPUNIT_ASSERT( s1.find_first_not_of(wxT("abcdefghi"), 0, 4) == 9u );
0a199299 334
9a83f860
VZ
335 CPPUNIT_ASSERT( s1.find_first_of(wxT('c')) == 7u );
336 CPPUNIT_ASSERT( s1.find_first_of(wxT('v')) == wxString::npos );
337 CPPUNIT_ASSERT( s1.find_first_of(wxT('c'), 10) == 24u );
0a199299 338
9a83f860
VZ
339 CPPUNIT_ASSERT( s1.find_first_of(wxT("ijkl")) == 13u );
340 CPPUNIT_ASSERT( s1.find_first_of(wxT("ddcfg"), 17) == 24u );
341 CPPUNIT_ASSERT( s1.find_first_of(wxT("ddcfga"), 17, 5) == 24u );
0a199299
WS
342}
343
344void StdStringTestCase::StdFindLast()
345{
346 // 0 1 2 3
347 // 01234567890123456789012345678901234
9a83f860
VZ
348 wxString s1 = wxT("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
349 wxString s2 = wxT("aaaaaa");
0a199299 350
9a83f860
VZ
351 CPPUNIT_ASSERT( s2.find_last_not_of(wxT('a')) == wxString::npos );
352 CPPUNIT_ASSERT( s1.find_last_not_of(wxT('d')) == 33u );
353 CPPUNIT_ASSERT( s1.find_last_not_of(wxT('d'), 25) == 24u );
0a199299 354
9a83f860
VZ
355 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("bcd")) == 22u );
356 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("abc"), 24) == 16u );
0a199299 357
9a83f860 358 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
0a199299 359
9a83f860
VZ
360 CPPUNIT_ASSERT( s2.find_last_of(wxT('c')) == wxString::npos );
361 CPPUNIT_ASSERT( s1.find_last_of(wxT('a')) == 22u );
362 CPPUNIT_ASSERT( s1.find_last_of(wxT('b'), 24) == 23u );
0a199299 363
9a83f860
VZ
364 CPPUNIT_ASSERT( s1.find_last_of(wxT("ijklm")) == 16u );
365 CPPUNIT_ASSERT( s1.find_last_of(wxT("ijklma"), 33, 4) == 16u );
366 CPPUNIT_ASSERT( s1.find_last_of(wxT("a"), 17) == 17u );
422d306c
RN
367
368
369
370 // 0 1 2 3
371 // 012345 67890123456789 01234567890123456
9a83f860
VZ
372// wxString s1 = wxT("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd");
373// wxString s2 = wxT("aaaaaa\0");
422d306c
RN
374 s1.insert(6,1,'\0');
375 s1.insert(20,1,'\0');
376 s2.insert(6,1,'\0');
377
9a83f860
VZ
378 CPPUNIT_ASSERT( s2.find_last_not_of(wxT('a')) == 6u );
379 CPPUNIT_ASSERT( s1.find_last_not_of(wxT('d')) == 35u );
380 CPPUNIT_ASSERT( s1.find_last_not_of(wxT('d'), 27) == 26u );
422d306c 381
9a83f860
VZ
382 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("bcd")) == 24u );
383 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("abc"), 26) == 20u );
422d306c 384
9a83f860 385 CPPUNIT_ASSERT( s1.find_last_not_of(wxT("abcdefghijklmnopqrstuv"), 26, 3) == 20u );
422d306c 386
9a83f860
VZ
387 CPPUNIT_ASSERT( s2.find_last_of(wxT('c')) == wxString::npos );
388 CPPUNIT_ASSERT( s1.find_last_of(wxT('a')) == 24u );
389 CPPUNIT_ASSERT( s1.find_last_of(wxT('b'), 26) == 25u );
422d306c 390
9a83f860
VZ
391 CPPUNIT_ASSERT( s1.find_last_of(wxT("ijklm")) == 17u );
392 CPPUNIT_ASSERT( s1.find_last_of(wxT("ijklma"), 35, 4) == 17u );
393 CPPUNIT_ASSERT( s1.find_last_of(wxT("a"), 18) == 18u );
0a199299
WS
394}
395
396void StdStringTestCase::StdInsert()
397{
398 wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
399
9a83f860
VZ
400 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = wxT("aaaa");
401 s9 = s10 = wxT("cdefg");
0a199299 402
9a83f860
VZ
403 s1.insert(1, wxT("cc") );
404 s2.insert(2, wxT("cdef"), 3);
0a199299
WS
405 s3.insert(2, s10);
406 s4.insert(2, s10, 3, 7);
9a83f860
VZ
407 s5.insert(1, 2, wxT('c'));
408 s6.insert(s6.begin() + 3, wxT('X'));
0a199299 409 s7.insert(s7.begin(), s9.begin(), s9.end() - 1);
9a83f860
VZ
410 s8.insert(s8.begin(), 2, wxT('c'));
411
412 CPPUNIT_ASSERT_EQUAL( wxT("accaaa") , s1 );
413 CPPUNIT_ASSERT_EQUAL( wxT("aacdeaa") , s2 );
414 CPPUNIT_ASSERT_EQUAL( wxT("aacdefgaa"), s3 );
415 CPPUNIT_ASSERT_EQUAL( wxT("aafgaa") , s4 );
416 CPPUNIT_ASSERT_EQUAL( wxT("accaaa") , s5 );
417 CPPUNIT_ASSERT_EQUAL( wxT("aaaXa") , s6 );
418 CPPUNIT_ASSERT_EQUAL( wxT("cdefaaaa") , s7 );
419 CPPUNIT_ASSERT_EQUAL( wxT("ccaaaa") , s8 );
420
421 s1 = s2 = s3 = wxT("aaaa");
422 s1.insert(0, wxT("ccc"), 2);
423 s2.insert(4, wxT("ccc"), 2);
424
425 CPPUNIT_ASSERT_EQUAL( wxT("ccaaaa"), s1 );
426 CPPUNIT_ASSERT_EQUAL( wxT("aaaacc"), s2 );
0a199299
WS
427}
428
429void StdStringTestCase::StdReplace()
430{
431 wxString s1, s2, s3, s4, s5, s6, s7, s8, s9;
432
9a83f860
VZ
433 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = wxT("QWERTYUIOP");
434 s9 = wxT("werty");
0a199299 435
9a83f860
VZ
436 s1.replace(3, 4, wxT("rtyu"));
437 s1.replace(8, 7, wxT("opopop"));
438 s2.replace(10, 12, wxT("WWWW"));
0a199299
WS
439 s3.replace(1, 5, s9);
440 s4.replace(1, 4, s9, 0, 4);
441 s5.replace(1, 2, s9, 1, 12);
442 s6.replace(0, 123, s9, 0, 123);
443 s7.replace(2, 7, s9);
444
9a83f860
VZ
445 CPPUNIT_ASSERT_EQUAL( wxT("QWErtyuIopopop"), s1 );
446 CPPUNIT_ASSERT_EQUAL( wxT("QWERTYUIOPWWWW"), s2 );
447 CPPUNIT_ASSERT_EQUAL( wxT("QwertyUIOP") , s3 );
448 CPPUNIT_ASSERT_EQUAL( wxT("QwertYUIOP") , s4 );
449 CPPUNIT_ASSERT_EQUAL( wxT("QertyRTYUIOP") , s5 );
339e08c7 450 CPPUNIT_ASSERT_EQUAL( s9, s6 );
9a83f860 451 CPPUNIT_ASSERT_EQUAL( wxT("QWwertyP"), s7 );
0a199299
WS
452}
453
454void StdStringTestCase::StdRFind()
455{
456 // 0 1 2
457 // 01234567890123456789012345
9a83f860
VZ
458 wxString s1 = wxT("abcdefgABCDEFGabcABCabcABC");
459 wxString s2 = wxT("gAB");
460 wxString s3 = wxT("ab");
0a199299 461
9a83f860
VZ
462 CPPUNIT_ASSERT( s1.rfind(wxT('A')) == 23u );
463 CPPUNIT_ASSERT( s1.rfind(wxT('A'), 7) == 7u );
464 CPPUNIT_ASSERT( s1.rfind(wxT('Z')) == wxString::npos );
465 CPPUNIT_ASSERT( s1.rfind(wxT('C'), 22) == 19u );
0a199299 466
9a83f860
VZ
467 CPPUNIT_ASSERT( s1.rfind(wxT("cAB")) == 22u );
468 CPPUNIT_ASSERT( s1.rfind(wxT("cAB"), 15) == wxString::npos );
469 CPPUNIT_ASSERT( s1.rfind(wxT("cAB"), 21) == 16u );
0a199299 470
9a83f860
VZ
471 CPPUNIT_ASSERT( s1.rfind(wxT("gABZZZ"), 7, 3) == 6u );
472 CPPUNIT_ASSERT( s1.rfind(wxT("gABZZZ"), 5, 3) == wxString::npos );
0a199299
WS
473
474 CPPUNIT_ASSERT( s1.rfind(s2) == 6u );
475 CPPUNIT_ASSERT( s1.rfind(s2, 5) == wxString::npos );
476 CPPUNIT_ASSERT( s1.rfind(s2, 6) == 6u );
477 CPPUNIT_ASSERT( s1.rfind(s3, 1) == 0u );
dcb68102
RN
478
479
480 // 0 1 2
481 // 01234 56789012 345678901234567
9a83f860
VZ
482// wxString s1 = wxT("abcde\0fgABCDE\0FGabcABCabcABC");
483// wxString s2 = wxT("gAB");
484// wxString s3 = wxT("ab");
968f291b 485
dcb68102
RN
486 s1.insert(5,1,'\0');
487 s1.insert(13,1,'\0');
488
9a83f860
VZ
489 CPPUNIT_ASSERT( s1.rfind(wxT('A')) == 25u );
490 CPPUNIT_ASSERT( s1.rfind(wxT('A'), 8) == 8u );
491 CPPUNIT_ASSERT( s1.rfind(wxT('Z')) == wxString::npos );
492 CPPUNIT_ASSERT( s1.rfind(wxT('C'), 22) == 21u );
dcb68102 493
9a83f860
VZ
494 CPPUNIT_ASSERT( s1.rfind(wxT("cAB")) == 24u );
495 CPPUNIT_ASSERT( s1.rfind(wxT("cAB"), 15) == wxString::npos );
496 CPPUNIT_ASSERT( s1.rfind(wxT("cAB"), 21) == 18u );
dcb68102 497
9a83f860
VZ
498 CPPUNIT_ASSERT( s1.rfind(wxT("gABZZZ"), 8, 3) == 7u );
499 CPPUNIT_ASSERT( s1.rfind(wxT("gABZZZ"), 5, 3) == wxString::npos );
0a199299
WS
500}
501
502void StdStringTestCase::StdResize()
503{
504 wxString s1, s2, s3, s4;
505
9a83f860 506 s1 = s2 = s3 = s4 = wxT("abcABCdefDEF");
0a199299
WS
507
508 s1.resize( 12 );
509 s2.resize( 10 );
9a83f860
VZ
510 s3.resize( 14, wxT(' ') );
511 s4.resize( 14, wxT('W') );
0a199299 512
9a83f860
VZ
513 CPPUNIT_ASSERT_EQUAL( wxT("abcABCdefDEF"), s1 );
514 CPPUNIT_ASSERT_EQUAL( wxT("abcABCdefD"), s2 );
515 CPPUNIT_ASSERT_EQUAL( wxT("abcABCdefDEF "), s3 );
516 CPPUNIT_ASSERT_EQUAL( wxT("abcABCdefDEFWW"), s4 );
fe1b98f5 517
87f528f1 518#if wxUSE_UNICODE
fe1b98f5
VZ
519 wxString s =
520 wxString::FromUTF8("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
521 s.resize(3);
27dd021d 522 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8("\xd0\x9f\xd1\x80\xd0\xb8"), s);
87f528f1 523#endif // wxUSE_UNICODE
0a199299
WS
524}
525
968f291b
VZ
526void StdStringTestCase::StdRiter()
527{
9a83f860 528 const wxString s(wxT("fozbar"));
968f291b
VZ
529
530 wxString::const_reverse_iterator ri(s.rbegin());
9a83f860
VZ
531 CPPUNIT_ASSERT( wxT('r') == *ri );
532 CPPUNIT_ASSERT( wxT('a') == *++ri );
533 CPPUNIT_ASSERT( wxT('r') == *--ri );
968f291b
VZ
534
535 ri = s.rend();
536 ri--;
9a83f860 537 CPPUNIT_ASSERT( wxT('f') == *ri );
968f291b
VZ
538
539 --ri;
9a83f860 540 CPPUNIT_ASSERT( wxT('o') == *ri );
968f291b
VZ
541
542 wxString::const_iterator i = ri.base();
9a83f860 543 CPPUNIT_ASSERT( wxT('z') == *i );
968f291b
VZ
544}
545
0a199299
WS
546void StdStringTestCase::StdSubstr()
547{
9a83f860 548 wxString s1 = wxT("abcdefgABCDEFG");
0a199299
WS
549
550 CPPUNIT_ASSERT( s1.substr( 0, 14 ) == s1 );
9a83f860
VZ
551 CPPUNIT_ASSERT( s1.substr( 1, 13 ) == wxT("bcdefgABCDEFG") );
552 CPPUNIT_ASSERT( s1.substr( 1, 20 ) == wxT("bcdefgABCDEFG") );
553 CPPUNIT_ASSERT( s1.substr( 14, 30 ) == wxT("") );
dcb68102 554
dcb68102
RN
555 s1.insert(3,1,'\0');
556 s1.insert(8,1,'\0');
557 s1.insert(13,1,'\0');
558
559 wxString s2 = wxT("bcdefgABCDEFG");
560 s2.insert(2,1,'\0');
561 s2.insert(7,1,'\0');
562 s2.insert(12,1,'\0');
968f291b 563
dcb68102
RN
564 CPPUNIT_ASSERT( s1.substr( 0, 17 ) == s1 );
565 CPPUNIT_ASSERT( s1.substr( 1, 17 ) == s2 );
566 CPPUNIT_ASSERT( s1.substr( 1, 20 ) == s2 );
9a83f860 567 CPPUNIT_ASSERT( s1.substr( 17, 30 ) == wxT("") );
0a199299
WS
568}
569
59953bf4
VS
570#if wxUSE_STD_STRING
571void StdStringTestCase::StdConversion()
572{
573 std::string strStd("std::string value");
574 wxStdWideString strStdWide(L"std::wstring value");
575
576 wxString s1(strStd);
1de532f5 577 CPPUNIT_ASSERT_EQUAL( "std::string value", s1 );
59953bf4
VS
578
579 wxString s2(strStdWide);
1de532f5 580 CPPUNIT_ASSERT_EQUAL( "std::wstring value", s2 );
59953bf4
VS
581
582 wxString s3;
583 s3 = strStd;
1de532f5 584 CPPUNIT_ASSERT_EQUAL( "std::string value", s3 );
59953bf4 585 s3 = strStdWide;
1de532f5 586 CPPUNIT_ASSERT_EQUAL( "std::wstring value", s3 );
59953bf4
VS
587
588 wxString s4("hello");
7978bc72 589
e3ab6952
VZ
590 // notice that implicit wxString -> std::string conversion is only
591 // available in wxUSE_STL case, because it conflicts with conversion to
592 // const char*/wchar_t*
7978bc72 593#if wxUSE_STL
59953bf4 594 std::string s5 = s4;
e3ab6952
VZ
595#else
596 std::string s5 = s4.ToStdString();
597#endif
1de532f5 598 CPPUNIT_ASSERT_EQUAL( "hello", s5 );
59953bf4 599
e3ab6952 600#if wxUSE_STL
59953bf4 601 wxStdWideString s6 = s4;
e3ab6952
VZ
602#else
603 wxStdWideString s6 = s4.ToStdWstring();
7978bc72 604#endif
e3ab6952 605 CPPUNIT_ASSERT_EQUAL( "hello", s6 );
7978bc72
VS
606
607 std::string s7(s4);
608 CPPUNIT_ASSERT( s7 == "hello" );
609
610 wxStdWideString s8(s4);
611 CPPUNIT_ASSERT( s8 == "hello" );
59953bf4
VS
612}
613#endif // wxUSE_STD_STRING