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