]>
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
= s7
= s8
= _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") );
117 void StdStringTestCase::StdAssign()
119 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
121 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
122 s1
.assign(_T("def"));
123 s2
.assign(_T("defgh"), 3);
124 s3
.assign(wxString(_T("abcdef")), 3, 6);
126 s5
.assign(3, _T('a'));
127 s6
.assign(s1
.begin() + 1, s1
.end());
129 CPPUNIT_ASSERT( s1
== _T("def") );
130 CPPUNIT_ASSERT( s2
== _T("def") );
131 CPPUNIT_ASSERT( s3
== _T("def") );
132 CPPUNIT_ASSERT( s4
== _T("def") );
133 CPPUNIT_ASSERT( s5
== _T("aaa") );
134 CPPUNIT_ASSERT( s6
== _T("ef") );
137 void StdStringTestCase::StdCompare()
139 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
144 s4
= _T("abcdefghi");
148 CPPUNIT_ASSERT( s1
.compare(s2
) == 0 );
149 CPPUNIT_ASSERT( s1
.compare(s3
) > 0 );
150 CPPUNIT_ASSERT( s1
.compare(s4
) < 0 );
151 CPPUNIT_ASSERT( s1
.compare(s5
) > 0 );
152 CPPUNIT_ASSERT( s1
.compare(s6
) < 0 );
153 CPPUNIT_ASSERT( s1
.compare(1, 12, s1
) > 0);
154 CPPUNIT_ASSERT( s1
.compare(_T("abcdefgh")) == 0);
155 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh")) == 0);
156 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
159 void StdStringTestCase::StdErase()
161 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
;
166 s4
= _T("abcdefghi");
173 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
174 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
175 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
177 CPPUNIT_ASSERT( s1
== _T("acdefgh") );
178 CPPUNIT_ASSERT( s2
== _T("abcd") );
179 CPPUNIT_ASSERT( s3
== _T("ac") );
180 CPPUNIT_ASSERT( s4
== _T("abcdghi") );
181 CPPUNIT_ASSERT( s7
== _T("zabc") );
182 CPPUNIT_ASSERT( *it
== _T('c') );
183 CPPUNIT_ASSERT( *it2
== _T('g') );
184 CPPUNIT_ASSERT( it3
== s7
.end() );
187 void StdStringTestCase::StdFind()
190 // 01234567890123456789012345
191 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
192 wxString s2
= _T("gAB");
194 CPPUNIT_ASSERT( s1
.find(_T('A')) == 7u );
195 CPPUNIT_ASSERT( s1
.find(_T('A'), 7) == 7u );
196 CPPUNIT_ASSERT( s1
.find(_T('Z')) == wxString::npos
);
197 CPPUNIT_ASSERT( s1
.find(_T('C'), 22) == 25u );
199 CPPUNIT_ASSERT( s1
.find(_T("gAB")) == 6u );
200 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 7) == wxString::npos
);
201 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 6) == 6u );
203 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 2, 3) == 6u );
204 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 7, 3) == wxString::npos
);
206 CPPUNIT_ASSERT( s1
.find(s2
) == 6u );
207 CPPUNIT_ASSERT( s1
.find(s2
, 7) == wxString::npos
);
208 CPPUNIT_ASSERT( s1
.find(s2
, 6) == 6u );
211 // 0123456 78901234567 8901234567
212 //wxString _s1 = _T("abcdefg\0ABCDEFGabc\0ABCabcABC");
213 //wxString _s2 = _T("g\0AB");
214 wxString _s1
= _T("abcdefgABCDEFGabcABCabcABC");
215 wxString _s2
= _T("gAB");
217 _s1
.insert(7, 1, '\0');
218 _s1
.insert(18, 1, '\0');
219 _s2
.insert(1, 1, '\0');
221 CPPUNIT_ASSERT( _s1
.find(_T('A')) == 8u );
222 CPPUNIT_ASSERT( _s1
.find(_T('A'), 8) == 8u );
223 CPPUNIT_ASSERT( _s1
.find(_T('Z')) == wxString::npos
);
224 CPPUNIT_ASSERT( _s1
.find(_T('C'), 22) == 27u );
226 CPPUNIT_ASSERT( _s1
.find(_T("AB")) == 8u );
227 CPPUNIT_ASSERT( _s1
.find(_T("AB"), 26) == wxString::npos
);
228 CPPUNIT_ASSERT( _s1
.find(_T("AB"), 23) == 25u );
230 CPPUNIT_ASSERT( _s1
.find(_T("ABZZZ"), 2, 2) == 8u );
231 CPPUNIT_ASSERT( _s1
.find(_T("ABZZZ"), 26, 2) == wxString::npos
);
233 CPPUNIT_ASSERT( _s1
.find(_s2
) == 6u );
234 CPPUNIT_ASSERT( _s1
.find(_s2
, 7) == wxString::npos
);
235 CPPUNIT_ASSERT( _s1
.find(_s2
, 6) == 6u );
238 void StdStringTestCase::StdFindFirst()
241 // 01234567890123456789012345678901234
242 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
243 wxString s2
= _T("aaaaaa");
245 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a')) == 6u );
246 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a'), 7) == 7u );
247 CPPUNIT_ASSERT( s2
.find_first_not_of(_T('a')) == wxString::npos
);
249 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 4) == 7u );
250 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 7) == 7u );
251 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghijkl")) == wxString::npos
);
253 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u );
255 CPPUNIT_ASSERT( s1
.find_first_of(_T('c')) == 7u );
256 CPPUNIT_ASSERT( s1
.find_first_of(_T('v')) == wxString::npos
);
257 CPPUNIT_ASSERT( s1
.find_first_of(_T('c'), 10) == 24u );
259 CPPUNIT_ASSERT( s1
.find_first_of(_T("ijkl")) == 13u );
260 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfg"), 17) == 24u );
261 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfga"), 17, 5) == 24u );
264 void StdStringTestCase::StdFindLast()
267 // 01234567890123456789012345678901234
268 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
269 wxString s2
= _T("aaaaaa");
271 CPPUNIT_ASSERT( s2
.find_last_not_of(_T('a')) == wxString::npos
);
272 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d')) == 33u );
273 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d'), 25) == 24u );
275 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 22u );
276 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 24) == 16u );
278 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
280 CPPUNIT_ASSERT( s2
.find_last_of(_T('c')) == wxString::npos
);
281 CPPUNIT_ASSERT( s1
.find_last_of(_T('a')) == 22u );
282 CPPUNIT_ASSERT( s1
.find_last_of(_T('b'), 24) == 23u );
284 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklm")) == 16u );
285 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklma"), 33, 4) == 16u );
286 CPPUNIT_ASSERT( s1
.find_last_of(_T("a"), 17) == 17u );
291 // 012345 67890123456789 01234567890123456
292 // wxString s1 = _T("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd");
293 // wxString s2 = _T("aaaaaa\0");
295 s1
.insert(20,1,'\0');
298 CPPUNIT_ASSERT( s2
.find_last_not_of(_T('a')) == 6u );
299 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d')) == 35u );
300 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d'), 27) == 26u );
302 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 24u );
303 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 26) == 20u );
305 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 26, 3) == 20u );
307 CPPUNIT_ASSERT( s2
.find_last_of(_T('c')) == wxString::npos
);
308 CPPUNIT_ASSERT( s1
.find_last_of(_T('a')) == 24u );
309 CPPUNIT_ASSERT( s1
.find_last_of(_T('b'), 26) == 25u );
311 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklm")) == 17u );
312 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklma"), 35, 4) == 17u );
313 CPPUNIT_ASSERT( s1
.find_last_of(_T("a"), 18) == 18u );
316 void StdStringTestCase::StdInsert()
318 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
, s10
;
320 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
321 s9
= s10
= _T("cdefg");
323 s1
.insert(1, _T("cc") );
324 s2
.insert(2, _T("cdef"), 3);
326 s4
.insert(2, s10
, 3, 7);
327 s5
.insert(1, 2, _T('c'));
328 s6
.insert(s6
.begin() + 3, _T('X'));
329 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
330 s8
.insert(s8
.begin(), 2, _T('c'));
332 CPPUNIT_ASSERT( s1
== _T("accaaa") );
333 CPPUNIT_ASSERT( s2
== _T("aacdeaa") );
334 CPPUNIT_ASSERT( s3
== _T("aacdefgaa") );
335 CPPUNIT_ASSERT( s4
== _T("aafgaa") );
336 CPPUNIT_ASSERT( s5
== _T("accaaa") );
337 CPPUNIT_ASSERT( s6
== _T("aaaXa") );
338 CPPUNIT_ASSERT( s7
== _T("cdefaaaa") );
339 CPPUNIT_ASSERT( s8
== _T("ccaaaa") );
341 s1
= s2
= s3
= _T("aaaa");
342 s1
.insert(0, _T("ccc"), 2);
343 s2
.insert(4, _T("ccc"), 2);
345 CPPUNIT_ASSERT( s1
== _T("ccaaaa") );
346 CPPUNIT_ASSERT( s2
== _T("aaaacc") );
349 void StdStringTestCase::StdReplace()
351 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
;
353 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
356 s1
.replace(3, 4, _T("rtyu"));
357 s1
.replace(8, 7, _T("opopop"));
358 s2
.replace(10, 12, _T("WWWW"));
359 s3
.replace(1, 5, s9
);
360 s4
.replace(1, 4, s9
, 0, 4);
361 s5
.replace(1, 2, s9
, 1, 12);
362 s6
.replace(0, 123, s9
, 0, 123);
363 s7
.replace(2, 7, s9
);
365 CPPUNIT_ASSERT( s1
== _T("QWErtyuIopopop") );
366 CPPUNIT_ASSERT( s2
== _T("QWERTYUIOPWWWW") );
367 CPPUNIT_ASSERT( s3
== _T("QwertyUIOP") );
368 CPPUNIT_ASSERT( s4
== _T("QwertYUIOP") );
369 CPPUNIT_ASSERT( s5
== _T("QertyRTYUIOP") );
370 CPPUNIT_ASSERT( s6
== s9
);
371 CPPUNIT_ASSERT( s7
== _T("QWwertyP") );
374 void StdStringTestCase::StdRFind()
377 // 01234567890123456789012345
378 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
379 wxString s2
= _T("gAB");
380 wxString s3
= _T("ab");
382 CPPUNIT_ASSERT( s1
.rfind(_T('A')) == 23u );
383 CPPUNIT_ASSERT( s1
.rfind(_T('A'), 7) == 7u );
384 CPPUNIT_ASSERT( s1
.rfind(_T('Z')) == wxString::npos
);
385 CPPUNIT_ASSERT( s1
.rfind(_T('C'), 22) == 19u );
387 CPPUNIT_ASSERT( s1
.rfind(_T("cAB")) == 22u );
388 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 15) == wxString::npos
);
389 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 21) == 16u );
391 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 7, 3) == 6u );
392 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
394 CPPUNIT_ASSERT( s1
.rfind(s2
) == 6u );
395 CPPUNIT_ASSERT( s1
.rfind(s2
, 5) == wxString::npos
);
396 CPPUNIT_ASSERT( s1
.rfind(s2
, 6) == 6u );
397 CPPUNIT_ASSERT( s1
.rfind(s3
, 1) == 0u );
401 // 01234 56789012 345678901234567
402 // wxString s1 = _T("abcde\0fgABCDE\0FGabcABCabcABC");
403 // wxString s2 = _T("gAB");
404 // wxString s3 = _T("ab");
407 s1
.insert(13,1,'\0');
409 CPPUNIT_ASSERT( s1
.rfind(_T('A')) == 25u );
410 CPPUNIT_ASSERT( s1
.rfind(_T('A'), 8) == 8u );
411 CPPUNIT_ASSERT( s1
.rfind(_T('Z')) == wxString::npos
);
412 CPPUNIT_ASSERT( s1
.rfind(_T('C'), 22) == 21u );
414 CPPUNIT_ASSERT( s1
.rfind(_T("cAB")) == 24u );
415 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 15) == wxString::npos
);
416 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 21) == 18u );
418 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 8, 3) == 7u );
419 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
422 void StdStringTestCase::StdResize()
424 wxString s1
, s2
, s3
, s4
;
426 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
430 s3
.resize( 14, _T(' ') );
431 s4
.resize( 14, _T('W') );
433 CPPUNIT_ASSERT( s1
== _T("abcABCdefDEF") );
434 CPPUNIT_ASSERT( s2
== _T("abcABCdefD") );
435 CPPUNIT_ASSERT( s3
== _T("abcABCdefDEF ") );
436 CPPUNIT_ASSERT( s4
== _T("abcABCdefDEFWW") );
439 void StdStringTestCase::StdSubstr()
441 wxString s1
= _T("abcdefgABCDEFG");
443 CPPUNIT_ASSERT( s1
.substr( 0, 14 ) == s1
);
444 CPPUNIT_ASSERT( s1
.substr( 1, 13 ) == _T("bcdefgABCDEFG") );
445 CPPUNIT_ASSERT( s1
.substr( 1, 20 ) == _T("bcdefgABCDEFG") );
446 CPPUNIT_ASSERT( s1
.substr( 14, 30 ) == _T("") );
450 // 012 34567 89012 3456
451 // wxString s1 = _T("abc\0defg\0ABCD\0EFG");
455 s1
.insert(13,1,'\0');
457 wxString s2
= wxT("bcdefgABCDEFG");
460 s2
.insert(12,1,'\0');
462 CPPUNIT_ASSERT( s1
.substr( 0, 17 ) == s1
);
463 CPPUNIT_ASSERT( s1
.substr( 1, 17 ) == s2
);
464 CPPUNIT_ASSERT( s1
.substr( 1, 20 ) == s2
);
465 CPPUNIT_ASSERT( s1
.substr( 17, 30 ) == _T("") );