]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
24 #include "wx/tokenzr.h"
26 #include "wx/cppunit.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class StringTestCase
: public CppUnit::TestCase
38 CPPUNIT_TEST_SUITE( StringTestCase
);
39 CPPUNIT_TEST( String
);
40 CPPUNIT_TEST( PChar
);
41 CPPUNIT_TEST( Format
);
42 CPPUNIT_TEST( Constructors
);
43 CPPUNIT_TEST( Extraction
);
45 CPPUNIT_TEST( Tokenizer
);
46 CPPUNIT_TEST( Replace
);
47 CPPUNIT_TEST( Match
);
49 CPPUNIT_TEST_SUITE_END();
62 DECLARE_NO_COPY_CLASS(StringTestCase
)
65 // register in the unnamed registry so that these tests are run by default
66 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
68 // also include in it's own registry so that these tests can be run alone
69 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
71 StringTestCase::StringTestCase()
75 void StringTestCase::String()
83 for (int i
= 0; i
< 2; ++i
)
87 c
= _T("! How'ya doin'?");
90 c
= _T("Hello world! What's up?");
91 CPPUNIT_ASSERT( c
!= a
);
95 void StringTestCase::PChar()
101 for (int i
= 0; i
< 2; ++i
)
103 wxStrcpy (a
, _T("Hello"));
104 wxStrcpy (b
, _T(" world"));
105 wxStrcpy (c
, _T("! How'ya doin'?"));
108 wxStrcpy (c
, _T("Hello world! What's up?"));
109 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
113 void StringTestCase::Format()
116 s1
.Printf(_T("%03d"), 18);
117 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
118 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
119 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
122 void StringTestCase::Constructors()
124 #define TEST_CTOR(args, res) \
127 CPPUNIT_ASSERT( s == res ); \
130 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
131 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
132 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
134 static const wxChar
*s
= _T("?really!");
135 const wxChar
*start
= wxStrchr(s
, _T('r'));
136 const wxChar
*end
= wxStrchr(s
, _T('!'));
137 TEST_CTOR((start
, end
), _T("really"));
140 void StringTestCase::Extraction()
142 wxString
s(_T("Hello, world!"));
144 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
145 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
146 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
147 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
148 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
149 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
150 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
154 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
156 ( s.StartsWith( prefix, &rest ) == result ) && \
157 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
160 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
161 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
162 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
163 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
164 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
165 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
166 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
168 #undef TEST_STARTS_WITH
171 void StringTestCase::Find()
173 #define TEST_FIND( str , start , result ) \
174 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
176 TEST_FIND( _T("Well, hello world"), 0, 1 );
177 TEST_FIND( _T("Well, hello world"), 6, 7 );
178 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
183 void StringTestCase::Tokenizer()
185 #define TEST_TOKENIZER( str , delims, count , mode ) \
187 wxStringTokenizer tkz( str, delims, mode); \
188 CPPUNIT_ASSERT( tkz.CountTokens() == count ); \
190 wxChar *buf, *s = NULL, *last; \
192 if ( tkz.GetMode() == wxTOKEN_STRTOK ) \
194 buf = new wxChar[wxStrlen(str) + 1]; \
195 wxStrcpy(buf, str); \
196 s = wxStrtok(buf, delims, &last); \
204 while ( tkz.HasMoreTokens() ) \
206 wxString token = tkz.GetNextToken(); \
209 CPPUNIT_ASSERT( token == s ); \
210 s = wxStrtok(NULL, delims, &last); \
215 CPPUNIT_ASSERT( count2 == count ); \
222 TEST_TOKENIZER( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
223 TEST_TOKENIZER( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
224 TEST_TOKENIZER( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
225 TEST_TOKENIZER( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
226 TEST_TOKENIZER( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
227 TEST_TOKENIZER( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
228 TEST_TOKENIZER( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
229 TEST_TOKENIZER( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
230 TEST_TOKENIZER( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
231 TEST_TOKENIZER( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
232 TEST_TOKENIZER( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
233 TEST_TOKENIZER( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
234 TEST_TOKENIZER( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
236 #undef TEST_TOKENIZER
239 void StringTestCase::Replace()
241 #define TEST_REPLACE( original , pos , len , replacement , result ) \
243 wxString s = original; \
244 s.replace( pos , len , replacement ); \
245 CPPUNIT_ASSERT( s == result ); \
248 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
249 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
250 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
251 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
252 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
257 void StringTestCase::Match()
259 #define TEST_MATCH( s1 , s2 , result ) \
260 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
262 TEST_MATCH( _T("foobar"), _T("foo*"), true );
263 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
264 TEST_MATCH( _T("foobar"), _T("*bar"), true );
265 TEST_MATCH( _T("foobar"), _T("??????"), true );
266 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
267 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
268 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
269 TEST_MATCH( _T("foobar"), _T("*foo"), false );
270 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
271 TEST_MATCH( _T(""), _T("*"), true );
272 TEST_MATCH( _T(""), _T("?"), false );
277 void StringTestCase::Std()
279 wxString
s1(_T("abcdefgh")),
280 s2(_T("abcdefghijklm"), 8),
281 s3(_T("abcdefghijklm")),
285 s7(s3
.begin(), s3
.begin() + 8);
286 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
288 CPPUNIT_ASSERT( s1
== _T("abcdefgh") );
289 CPPUNIT_ASSERT( s2
== s1
);
290 CPPUNIT_ASSERT( s4
== _T("aaaaaaaa") );
291 CPPUNIT_ASSERT( s5
== _T("abcdefgh") );
292 CPPUNIT_ASSERT( s6
== s1
);
293 CPPUNIT_ASSERT( s7
== s1
);
294 CPPUNIT_ASSERT( s8
== _T("efgh") );
297 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
298 s1
.append(_T("def"));
299 s2
.append(_T("defgh"), 3);
300 s3
.append(wxString(_T("abcdef")), 3, 6);
302 s5
.append(3, _T('a'));
303 s6
.append(s1
.begin() + 3, s1
.end());
305 CPPUNIT_ASSERT( s1
== _T("abcdef") );
306 CPPUNIT_ASSERT( s2
== _T("abcdef") );
307 CPPUNIT_ASSERT( s3
== _T("abcdef") );
308 CPPUNIT_ASSERT( s4
== _T("abcabcdef") );
309 CPPUNIT_ASSERT( s5
== _T("abcaaa") );
310 CPPUNIT_ASSERT( s6
== _T("abcdef") );
313 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
314 s1
.assign(_T("def"));
315 s2
.assign(_T("defgh"), 3);
316 s3
.assign(wxString(_T("abcdef")), 3, 6);
318 s5
.assign(3, _T('a'));
319 s6
.assign(s1
.begin() + 1, s1
.end());
321 CPPUNIT_ASSERT( s1
== _T("def") );
322 CPPUNIT_ASSERT( s2
== _T("def") );
323 CPPUNIT_ASSERT( s3
== _T("def") );
324 CPPUNIT_ASSERT( s4
== _T("def") );
325 CPPUNIT_ASSERT( s5
== _T("aaa") );
326 CPPUNIT_ASSERT( s6
== _T("ef") );
332 s4
= _T("abcdefghi");
337 CPPUNIT_ASSERT( s1
.compare(s2
) == 0 );
338 CPPUNIT_ASSERT( s1
.compare(s3
) > 0 );
339 CPPUNIT_ASSERT( s1
.compare(s4
) < 0 );
340 CPPUNIT_ASSERT( s1
.compare(s5
) > 0 );
341 CPPUNIT_ASSERT( s1
.compare(s6
) < 0 );
342 CPPUNIT_ASSERT( s1
.compare(1, 12, s1
) > 0);
343 CPPUNIT_ASSERT( s1
.compare(_T("abcdefgh")) == 0);
344 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh")) == 0);
345 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
350 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
351 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
352 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
354 CPPUNIT_ASSERT( s1
== _T("acdefgh") );
355 CPPUNIT_ASSERT( s2
== _T("abcd") );
356 CPPUNIT_ASSERT( s3
== _T("ac") );
357 CPPUNIT_ASSERT( s4
== _T("abcdghi") );
358 CPPUNIT_ASSERT( s7
== _T("zabc") );
359 CPPUNIT_ASSERT( *it
== _T('c') );
360 CPPUNIT_ASSERT( *it2
== _T('g') );
361 CPPUNIT_ASSERT( it3
== s7
.end() );
365 // 01234567890123456789012345
366 s1
= _T("abcdefgABCDEFGabcABCabcABC");
369 CPPUNIT_ASSERT( s1
.find(_T('A')) == 7u );
370 CPPUNIT_ASSERT( s1
.find(_T('A'), 7) == 7u );
371 CPPUNIT_ASSERT( s1
.find(_T('Z')) == wxString::npos
);
372 CPPUNIT_ASSERT( s1
.find(_T('C'), 22) == 25u );
374 CPPUNIT_ASSERT( s1
.find(_T("gAB")) == 6u );
375 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 7) == wxString::npos
);
376 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 6) == 6u );
378 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 2, 3) == 6u );
379 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 7, 3) == wxString::npos
);
381 CPPUNIT_ASSERT( s1
.find(s2
) == 6u );
382 CPPUNIT_ASSERT( s1
.find(s2
, 7) == wxString::npos
);
383 CPPUNIT_ASSERT( s1
.find(s2
, 6) == 6u );
387 // 01234567890123456789012345678901234
388 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
391 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a')) == 6u );
392 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a'), 7) == 7u );
393 CPPUNIT_ASSERT( s2
.find_first_not_of(_T('a')) == wxString::npos
);
395 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 4) == 7u );
396 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 7) == 7u );
397 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghijkl")) == wxString::npos
);
399 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u );
402 CPPUNIT_ASSERT( s1
.find_first_of(_T('c')) == 7u );
403 CPPUNIT_ASSERT( s1
.find_first_of(_T('v')) == wxString::npos
);
404 CPPUNIT_ASSERT( s1
.find_first_of(_T('c'), 10) == 24u );
406 CPPUNIT_ASSERT( s1
.find_first_of(_T("ijkl")) == 13u );
407 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfg"), 17) == 24u );
408 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfga"), 17, 5) == 24u );
412 // 01234567890123456789012345678901234
413 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
416 CPPUNIT_ASSERT( s2
.find_last_not_of(_T('a')) == wxString::npos
);
417 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d')) == 33u );
418 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d'), 25) == 24u );
420 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 22u );
421 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 24) == 16u );
423 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
426 CPPUNIT_ASSERT( s2
.find_last_of(_T('c')) == wxString::npos
);
427 CPPUNIT_ASSERT( s1
.find_last_of(_T('a')) == 22u );
428 CPPUNIT_ASSERT( s1
.find_last_of(_T('b'), 24) == 23u );
430 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklm")) == 16u );
431 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklma"), 33, 4) == 16u );
432 CPPUNIT_ASSERT( s1
.find_last_of(_T("a"), 17) == 17u );
435 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
436 s9
= s10
= _T("cdefg");
438 s1
.insert(1, _T("cc") );
439 s2
.insert(2, _T("cdef"), 3);
441 s4
.insert(2, s10
, 3, 7);
442 s5
.insert(1, 2, _T('c'));
443 it
= s6
.insert(s6
.begin() + 3, _T('X'));
444 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
445 s8
.insert(s8
.begin(), 2, _T('c'));
447 CPPUNIT_ASSERT( s1
== _T("accaaa") );
448 CPPUNIT_ASSERT( s2
== _T("aacdeaa") );
449 CPPUNIT_ASSERT( s3
== _T("aacdefgaa") );
450 CPPUNIT_ASSERT( s4
== _T("aafgaa") );
451 CPPUNIT_ASSERT( s5
== _T("accaaa") );
452 CPPUNIT_ASSERT( s6
== _T("aaaXa") );
453 CPPUNIT_ASSERT( s7
== _T("cdefaaaa") );
454 CPPUNIT_ASSERT( s8
== _T("ccaaaa") );
456 s1
= s2
= s3
= _T("aaaa");
457 s1
.insert(0, _T("ccc"), 2);
458 s2
.insert(4, _T("ccc"), 2);
460 CPPUNIT_ASSERT( s1
== _T("ccaaaa") );
461 CPPUNIT_ASSERT( s2
== _T("aaaacc") );
464 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
465 s9
= s10
= _T("werty");
467 s1
.replace(3, 4, _T("rtyu"));
468 s1
.replace(8, 7, _T("opopop"));
469 s2
.replace(10, 12, _T("WWWW"));
470 s3
.replace(1, 5, s9
);
471 s4
.replace(1, 4, s9
, 0, 4);
472 s5
.replace(1, 2, s9
, 1, 12);
473 s6
.replace(0, 123, s9
, 0, 123);
474 s7
.replace(2, 7, s9
);
476 CPPUNIT_ASSERT( s1
== _T("QWErtyuIopopop") );
477 CPPUNIT_ASSERT( s2
== _T("QWERTYUIOPWWWW") );
478 CPPUNIT_ASSERT( s3
== _T("QwertyUIOP") );
479 CPPUNIT_ASSERT( s4
== _T("QwertYUIOP") );
480 CPPUNIT_ASSERT( s5
== _T("QertyRTYUIOP") );
481 CPPUNIT_ASSERT( s6
== s9
);
482 CPPUNIT_ASSERT( s7
== _T("QWwertyP") );
486 // 01234567890123456789012345
487 s1
= _T("abcdefgABCDEFGabcABCabcABC");
491 CPPUNIT_ASSERT( s1
.rfind(_T('A')) == 23u );
492 CPPUNIT_ASSERT( s1
.rfind(_T('A'), 7) == 7u );
493 CPPUNIT_ASSERT( s1
.rfind(_T('Z')) == wxString::npos
);
494 CPPUNIT_ASSERT( s1
.rfind(_T('C'), 22) == 19u );
496 CPPUNIT_ASSERT( s1
.rfind(_T("cAB")) == 22u );
497 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 15) == wxString::npos
);
498 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 21) == 16u );
500 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 7, 3) == 6u );
501 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
503 CPPUNIT_ASSERT( s1
.rfind(s2
) == 6u );
504 CPPUNIT_ASSERT( s1
.rfind(s2
, 5) == wxString::npos
);
505 CPPUNIT_ASSERT( s1
.rfind(s2
, 6) == 6u );
506 CPPUNIT_ASSERT( s1
.rfind(s3
, 1) == 0u );
509 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
513 s3
.resize( 14, _T(' ') );
514 s4
.resize( 14, _T('W') );
516 CPPUNIT_ASSERT( s1
== _T("abcABCdefDEF") );
517 CPPUNIT_ASSERT( s2
== _T("abcABCdefD") );
518 CPPUNIT_ASSERT( s3
== _T("abcABCdefDEF ") );
519 CPPUNIT_ASSERT( s4
== _T("abcABCdefDEFWW") );
522 s1
= _T("abcdefgABCDEFG");
524 CPPUNIT_ASSERT( s1
.substr( 0, 14 ) == s1
);
525 CPPUNIT_ASSERT( s1
.substr( 1, 13 ) == _T("bcdefgABCDEFG") );
526 CPPUNIT_ASSERT( s1
.substr( 1, 20 ) == _T("bcdefgABCDEFG") );
527 CPPUNIT_ASSERT( s1
.substr( 14, 30 ) == _T("") );