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