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 // ----------------------------------------------------------------------------
24 #include "wx/tokenzr.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class StringTestCase
: public CppUnit
::TestCase
36 CPPUNIT_TEST_SUITE( StringTestCase
);
37 CPPUNIT_TEST( String
);
38 CPPUNIT_TEST( PChar
);
39 CPPUNIT_TEST( Format
);
40 CPPUNIT_TEST( Constructors
);
42 CPPUNIT_TEST( ConstructorsWithConversion
);
43 CPPUNIT_TEST( Conversion
);
44 CPPUNIT_TEST( ConversionUTF7
);
45 CPPUNIT_TEST( ConversionUTF8
);
46 #endif // wxUSE_WCHAR_T
47 CPPUNIT_TEST( Extraction
);
49 CPPUNIT_TEST( Tokenizer
);
50 CPPUNIT_TEST( TokenizerGetPosition
);
51 CPPUNIT_TEST( Replace
);
52 CPPUNIT_TEST( Match
);
53 CPPUNIT_TEST( CaseChanges
);
54 CPPUNIT_TEST( Compare
);
55 CPPUNIT_TEST( CompareNoCase
);
56 CPPUNIT_TEST_SUITE_END();
63 void ConstructorsWithConversion();
65 void ConversionUTF7();
66 void ConversionUTF8();
67 #endif // wxUSE_WCHAR_T
70 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
72 void TokenizerGetPosition();
79 DECLARE_NO_COPY_CLASS(StringTestCase
)
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
88 StringTestCase
::StringTestCase()
92 void StringTestCase
::String()
100 for (int i
= 0; i
< 2; ++i
)
104 c
= _T("! How'ya doin'?");
107 c
= _T("Hello world! What's up?");
108 CPPUNIT_ASSERT( c
!= a
);
112 void StringTestCase
::PChar()
118 for (int i
= 0; i
< 2; ++i
)
120 wxStrcpy (a
, _T("Hello"));
121 wxStrcpy (b
, _T(" world"));
122 wxStrcpy (c
, _T("! How'ya doin'?"));
125 wxStrcpy (c
, _T("Hello world! What's up?"));
126 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
130 void StringTestCase
::Format()
133 s1
.Printf(_T("%03d"), 18);
134 CPPUNIT_ASSERT( s1
== wxString
::Format(_T("%03d"), 18) );
135 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
136 CPPUNIT_ASSERT( s2
== wxString
::Format(_T("Number 18: %s\n"), s1
.c_str()) );
139 void StringTestCase
::Constructors()
141 #define TEST_CTOR(args, res) \
144 CPPUNIT_ASSERT( s == res ); \
147 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
148 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
149 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
151 static const wxChar
*s
= _T("?really!");
152 const wxChar
*start
= wxStrchr(s
, _T('r'));
153 const wxChar
*end
= wxStrchr(s
, _T('!'));
154 TEST_CTOR((start
, end
), _T("really"));
158 void StringTestCase
::ConstructorsWithConversion()
160 // the string "Déjà" in UTF-8 and wchar_t:
161 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
162 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
163 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
164 const char *utf8
= (char *)utf8Buf
;
165 const char *utf8sub
= (char *)utf8subBuf
;
167 wxString
s1(utf8
, wxConvUTF8
);
168 wxString
s2(wchar
, wxConvUTF8
);
171 CPPUNIT_ASSERT( s1
== wchar
);
172 CPPUNIT_ASSERT( s2
== wchar
);
174 CPPUNIT_ASSERT( s1
== utf8
);
175 CPPUNIT_ASSERT( s2
== utf8
);
178 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
179 wxString
s3(utf8
, wxConvUTF8
, 4);
180 wxString
s4(wchar
, wxConvUTF8
, 3);
182 CPPUNIT_ASSERT( s3
== sub
);
183 CPPUNIT_ASSERT( s4
== sub
);
186 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
187 wxConvUTF8
) == wxT("") ); //should stop at pos 35
191 void StringTestCase
::Conversion()
194 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
195 wxCharBuffer theBuffer
= szTheString
.mb_str();
197 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
199 wxString
szTheString2("The\0String", wxConvLocal
, 10);
200 CPPUNIT_ASSERT( szTheString2
.length() == 11 );
201 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
203 wxString
szTheString(wxT("TheString"));
204 szTheString
.insert(3, 1, '\0');
205 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
207 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
209 wxString
szLocalTheString(wxT("TheString"));
210 szLocalTheString
.insert(3, 1, '\0');
211 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
213 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
217 void StringTestCase
::ConversionUTF7()
219 const wchar_t data
[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
221 //utf7 and utf7alt are equivelent
222 const char *utf7
= "+AKM-+AKM-+AKM-+AKM-";
223 const char *utf7alt
= "+AKMAowCjAKM-";
228 wxCSConv
conv(_T("utf-7"));
230 wxCharBuffer theBuffer
= str
.mb_str(conv
);
232 CPPUNIT_ASSERT( strcmp(theBuffer
, utf7
) == 0 || strcmp(theBuffer
, utf7alt
) == 0);
236 wxCSConv
conv(_T("utf-7"));
238 wxWCharBuffer theWBuffer
= str
.wc_str(conv
);
240 CPPUNIT_ASSERT( wxWcslen(theWBuffer
) == wxWcslen(data
) );
241 CPPUNIT_ASSERT( memcmp(theWBuffer
, data
, wxWcslen(data
) * sizeof(wchar_t)) == 0 );
243 wxString
stralt(utf7alt
);
245 wxWCharBuffer theWBufferAlt
= stralt
.wc_str(conv
);
247 CPPUNIT_ASSERT( wxWcslen(theWBufferAlt
) == wxWcslen(data
) );
248 CPPUNIT_ASSERT( memcmp(theWBufferAlt
, data
, wxWcslen(data
) * sizeof(wchar_t)) == 0 );
250 #endif // wxUSE_UNICODE
253 void StringTestCase
::ConversionUTF8()
255 const wchar_t wcs
[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
256 const char *utf8
= "\xc2\xa3\xc2\xa3\xc2\xa3\xc2\xa3";
258 wxCSConv
conv(_T("utf-8"));
261 wxCharBuffer
buf(wxString(wcs
).mb_str(conv
));
263 CPPUNIT_ASSERT( strcmp(buf
, utf8
) == 0 );
264 #else // !wxUSE_UNICODE
265 wxWCharBuffer
wbuf(wxString(utf8
).wc_str(conv
));
267 CPPUNIT_ASSERT( wcscmp(wbuf
, wcs
) == 0 );
268 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
271 #endif // wxUSE_WCHAR_T
274 void StringTestCase
::Extraction()
276 wxString
s(_T("Hello, world!"));
278 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
279 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
280 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
281 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
282 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
283 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
284 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
288 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
290 ( s.StartsWith( prefix, &rest ) == result ) && \
291 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
294 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
295 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
296 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
297 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
298 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
299 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
300 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
302 #undef TEST_STARTS_WITH
305 void StringTestCase
::Find()
307 #define TEST_FIND( str , start , result ) \
308 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
310 TEST_FIND( _T("Well, hello world"), 0, 1 );
311 TEST_FIND( _T("Well, hello world"), 6, 7 );
312 TEST_FIND( _T("Well, hello world"), 9, wxString
::npos
);
317 void StringTestCase
::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
319 wxStringTokenizer
tkz( str
, delims
, mode
);
320 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
322 wxChar
*buf
, *s
= NULL
, *last
;
324 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
326 buf
= new wxChar
[wxStrlen(str
) + 1];
328 s
= wxStrtok(buf
, delims
, &last
);
336 while ( tkz
.HasMoreTokens() )
338 wxString token
= tkz
.GetNextToken();
341 CPPUNIT_ASSERT( token
== s
);
342 s
= wxStrtok(NULL
, delims
, &last
);
347 CPPUNIT_ASSERT( count2
== count
);
354 void StringTestCase
::Tokenizer()
356 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
357 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
358 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
359 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
360 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
361 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
362 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
363 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
364 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
365 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
366 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
367 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
368 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
371 // call this with the string to tokenize, delimeters to use and the expected
372 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
373 // terminate positions with 0
375 DoTokenizerGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
377 wxStringTokenizer
tkz(s
, delims
);
379 CPPUNIT_ASSERT( tkz
.GetPosition() == 0 );
388 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
394 CPPUNIT_ASSERT( tkz
.GetPosition() == (size_t)pos
);
396 pos
= va_arg(ap
, int);
402 void StringTestCase
::TokenizerGetPosition()
404 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
405 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
406 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
409 void StringTestCase
::Replace()
411 #define TEST_REPLACE( original , pos , len , replacement , result ) \
413 wxString s = original; \
414 s.replace( pos , len , replacement ); \
415 CPPUNIT_ASSERT( s == result ); \
418 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
419 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
420 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
421 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
422 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
427 void StringTestCase
::Match()
429 #define TEST_MATCH( s1 , s2 , result ) \
430 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
432 TEST_MATCH( _T("foobar"), _T("foo*"), true );
433 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
434 TEST_MATCH( _T("foobar"), _T("*bar"), true );
435 TEST_MATCH( _T("foobar"), _T("??????"), true );
436 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
437 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
438 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
439 TEST_MATCH( _T("foobar"), _T("*foo"), false );
440 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
441 TEST_MATCH( _T(""), _T("*"), true );
442 TEST_MATCH( _T(""), _T("?"), false );
448 void StringTestCase
::CaseChanges()
450 wxString
s1(_T("Hello!"));
459 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
460 CPPUNIT_ASSERT( s1l
== _T("hello!") );
461 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
462 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
465 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
468 // try upper casing 8bit strings
469 wxString
sUpper("\xdf"),
472 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
473 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
475 #endif // !wxUSE_UNICODE
478 void StringTestCase
::Compare()
480 wxString s1
= wxT("AHH");
481 wxString eq
= wxT("AHH");
482 wxString neq1
= wxT("HAH");
483 wxString neq2
= wxT("AH");
484 wxString neq3
= wxT("AHHH");
485 wxString neq4
= wxT("AhH");
487 CPPUNIT_ASSERT( s1
== eq
);
488 CPPUNIT_ASSERT( s1
!= neq1
);
489 CPPUNIT_ASSERT( s1
!= neq2
);
490 CPPUNIT_ASSERT( s1
!= neq3
);
491 CPPUNIT_ASSERT( s1
!= neq4
);
493 // wxString _s1 = wxT("A\0HH");
494 // wxString _eq = wxT("A\0HH");
495 // wxString _neq1 = wxT("H\0AH");
496 // wxString _neq2 = wxT("A\0H");
497 // wxString _neq3 = wxT("A\0HHH");
498 // wxString _neq4 = wxT("A\0hH");
501 neq1
.insert(1,1,'\0');
502 neq2
.insert(1,1,'\0');
503 neq3
.insert(1,1,'\0');
504 neq4
.insert(1,1,'\0');
506 CPPUNIT_ASSERT( s1
== eq
);
507 CPPUNIT_ASSERT( s1
!= neq1
);
508 CPPUNIT_ASSERT( s1
!= neq2
);
509 CPPUNIT_ASSERT( s1
!= neq3
);
510 CPPUNIT_ASSERT( s1
!= neq4
);
513 void StringTestCase
::CompareNoCase()
515 wxString s1
= wxT("AHH");
516 wxString eq
= wxT("AHH");
517 wxString eq2
= wxT("AhH");
518 wxString eq3
= wxT("ahh");
519 wxString neq
= wxT("HAH");
520 wxString neq2
= wxT("AH");
521 wxString neq3
= wxT("AHHH");
523 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
524 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
526 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
527 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
528 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
530 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
531 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
532 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
535 // wxString _s1 = wxT("A\0HH");
536 // wxString _eq = wxT("A\0HH");
537 // wxString _eq2 = wxT("A\0hH");
538 // wxString _eq3 = wxT("a\0hh");
539 // wxString _neq = wxT("H\0AH");
540 // wxString _neq2 = wxT("A\0H");
541 // wxString _neq3 = wxT("A\0HHH");
545 eq2
.insert(1,1,'\0');
546 eq3
.insert(1,1,'\0');
547 neq
.insert(1,1,'\0');
548 neq2
.insert(1,1,'\0');
549 neq3
.insert(1,1,'\0');
551 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
552 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
553 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
555 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
556 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
557 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);