]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/stdstrings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/stdstrings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class StdStringTestCase
: public CppUnit::TestCase
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();
50 void StdConstructors();
64 DECLARE_NO_COPY_CLASS(StdStringTestCase
)
67 // register in the unnamed registry so that these tests are run by default
68 CPPUNIT_TEST_SUITE_REGISTRATION( StdStringTestCase
);
70 // also include in it's own registry so that these tests can be run alone
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStringTestCase
, "StdStringTestCase" );
73 StdStringTestCase::StdStringTestCase()
77 void StdStringTestCase::StdConstructors()
79 wxString
s1(_T("abcdefgh")),
80 s2(_T("abcdefghijklm"), 8),
81 s3(_T("abcdefghijklm")),
85 s7(s3
.begin(), s3
.begin() + 8);
86 wxString
s8(s1
, 4, 8);
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") );
97 void StdStringTestCase::StdAppend()
99 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
101 s1
= s2
= s3
= s4
= s5
= s6
= _T("abc");
102 s1
.append(_T("def"));
103 s2
.append(_T("defgh"), 3);
104 s3
.append(wxString(_T("abcdef")), 3, 6);
106 s5
.append(3, _T('a'));
107 s6
.append(s1
.begin() + 3, s1
.end());
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") );
116 s7
= s8
= wxString(_T("null\0time"), 9);
118 s7
.append(_T("def"));
119 s8
.append(_T("defgh"), 3);
121 CPPUNIT_ASSERT( s7
== wxString(_T("null\0timedef"), 12) );
122 CPPUNIT_ASSERT( s8
== wxString(_T("null\0timedef"), 12) );
125 void StdStringTestCase::StdAssign()
127 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
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);
134 s5
.assign(3, _T('a'));
135 s6
.assign(s1
.begin() + 1, s1
.end());
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") );
145 void StdStringTestCase::StdCompare()
147 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
152 s4
= _T("abcdefghi");
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);
167 void StdStringTestCase::StdErase()
169 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
;
174 s4
= _T("abcdefghi");
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);
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() );
195 void StdStringTestCase::StdFind()
198 // 01234567890123456789012345
199 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
200 wxString s2
= _T("gAB");
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 );
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 );
211 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 2, 3) == 6u );
212 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 7, 3) == wxString::npos
);
214 CPPUNIT_ASSERT( s1
.find(s2
) == 6u );
215 CPPUNIT_ASSERT( s1
.find(s2
, 7) == wxString::npos
);
216 CPPUNIT_ASSERT( s1
.find(s2
, 6) == 6u );
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");
225 _s1
.insert(7, 1, '\0');
226 _s1
.insert(18, 1, '\0');
227 _s2
.insert(1, 1, '\0');
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 );
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 );
238 CPPUNIT_ASSERT( _s1
.find(_T("ABZZZ"), 2, 2) == 8u );
239 CPPUNIT_ASSERT( _s1
.find(_T("ABZZZ"), 26, 2) == wxString::npos
);
241 CPPUNIT_ASSERT( _s1
.find(_s2
) == 6u );
242 CPPUNIT_ASSERT( _s1
.find(_s2
, 7) == wxString::npos
);
243 CPPUNIT_ASSERT( _s1
.find(_s2
, 6) == 6u );
246 void StdStringTestCase::StdFindFirst()
249 // 01234567890123456789012345678901234
250 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
251 wxString s2
= _T("aaaaaa");
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
);
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
);
261 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u );
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 );
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 );
272 void StdStringTestCase::StdFindLast()
275 // 01234567890123456789012345678901234
276 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
277 wxString s2
= _T("aaaaaa");
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 );
283 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 22u );
284 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 24) == 16u );
286 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
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 );
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 );
299 // 012345 67890123456789 01234567890123456
300 // wxString s1 = _T("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd");
301 // wxString s2 = _T("aaaaaa\0");
303 s1
.insert(20,1,'\0');
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 );
310 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 24u );
311 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 26) == 20u );
313 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 26, 3) == 20u );
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 );
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 );
324 void StdStringTestCase::StdInsert()
326 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
, s10
;
328 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
329 s9
= s10
= _T("cdefg");
331 s1
.insert(1, _T("cc") );
332 s2
.insert(2, _T("cdef"), 3);
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'));
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") );
349 s1
= s2
= s3
= _T("aaaa");
350 s1
.insert(0, _T("ccc"), 2);
351 s2
.insert(4, _T("ccc"), 2);
353 CPPUNIT_ASSERT( s1
== _T("ccaaaa") );
354 CPPUNIT_ASSERT( s2
== _T("aaaacc") );
357 void StdStringTestCase::StdReplace()
359 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
;
361 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
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
);
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") );
382 void StdStringTestCase::StdRFind()
385 // 01234567890123456789012345
386 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
387 wxString s2
= _T("gAB");
388 wxString s3
= _T("ab");
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 );
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 );
399 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 7, 3) == 6u );
400 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
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 );
409 // 01234 56789012 345678901234567
410 // wxString s1 = _T("abcde\0fgABCDE\0FGabcABCabcABC");
411 // wxString s2 = _T("gAB");
412 // wxString s3 = _T("ab");
415 s1
.insert(13,1,'\0');
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 );
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 );
426 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 8, 3) == 7u );
427 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
430 void StdStringTestCase::StdResize()
432 wxString s1
, s2
, s3
, s4
;
434 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
438 s3
.resize( 14, _T(' ') );
439 s4
.resize( 14, _T('W') );
441 CPPUNIT_ASSERT( s1
== _T("abcABCdefDEF") );
442 CPPUNIT_ASSERT( s2
== _T("abcABCdefD") );
443 CPPUNIT_ASSERT( s3
== _T("abcABCdefDEF ") );
444 CPPUNIT_ASSERT( s4
== _T("abcABCdefDEFWW") );
447 void StdStringTestCase::StdSubstr()
449 wxString s1
= _T("abcdefgABCDEFG");
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("") );
458 // 012 34567 89012 3456
459 // wxString s1 = _T("abc\0defg\0ABCD\0EFG");
463 s1
.insert(13,1,'\0');
465 wxString s2
= wxT("bcdefgABCDEFG");
468 s2
.insert(12,1,'\0');
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("") );