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
);
44 CPPUNIT_TEST( ConstructorsWithConversion
);
45 CPPUNIT_TEST( Conversion
);
46 CPPUNIT_TEST( ConversionUTF7
);
48 CPPUNIT_TEST( Extraction
);
50 CPPUNIT_TEST( Tokenizer
);
51 CPPUNIT_TEST( TokenizerGetPosition
);
52 CPPUNIT_TEST( Replace
);
53 CPPUNIT_TEST( Match
);
54 CPPUNIT_TEST( CaseChanges
);
55 CPPUNIT_TEST( Compare
);
56 CPPUNIT_TEST( CompareNoCase
);
57 CPPUNIT_TEST_SUITE_END();
64 void ConstructorsWithConversion();
66 void ConversionUTF7();
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("") ); //Pos 35 (funky e) is invalid UTF8
190 // CPPUNIT_ASSERT ( wxString(L"\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
191 // wxConvUTF8) == wxT("") ); //Pos 35 (funky e) is invalid UTF8
195 void StringTestCase::Conversion()
198 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
199 wxCharBuffer theBuffer
= szTheString
.mb_str();
201 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
203 wxString
szTheString2("The\0String", wxConvLocal
, 10);
204 CPPUNIT_ASSERT( wxMemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
206 wxString
szTheString(wxT("TheString"));
207 szTheString
.insert(3, 1, '\0');
208 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
210 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
212 wxString
szLocalTheString(wxT("TheString"));
213 szLocalTheString
.insert(3, 1, '\0');
214 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
216 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
220 void StringTestCase::ConversionUTF7()
222 const wchar_t data
[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
223 const char *utf7
= "+AKM-+AKM-+AKM-+AKM-";
228 wxCSConv
conv(_T("utf-7"));
230 wxCharBuffer theBuffer
= str
.mb_str(conv
);
232 if (theBuffer
.data()[0u])
233 CPPUNIT_ASSERT( strcmp(theBuffer
, utf7
) == 0 );
237 wxCSConv
conv(_T("utf-7"));
239 wxWCharBuffer theWBuffer
= str
.wc_str(conv
);
241 if (theWBuffer
.data()[0u])
243 CPPUNIT_ASSERT( wxWcslen(theWBuffer
) == wxWcslen(data
) );
244 CPPUNIT_ASSERT( memcmp(theWBuffer
, data
, wxWcslen(data
) * sizeof(wchar_t)) == 0 );
246 #endif // wxUSE_UNICODE
249 #endif // wxUSE_WCHAR_T
252 void StringTestCase::Extraction()
254 wxString
s(_T("Hello, world!"));
256 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
257 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
258 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
259 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
260 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
261 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
262 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
266 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
268 ( s.StartsWith( prefix, &rest ) == result ) && \
269 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
272 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
273 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
274 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
275 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
276 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
277 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
278 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
280 #undef TEST_STARTS_WITH
283 void StringTestCase::Find()
285 #define TEST_FIND( str , start , result ) \
286 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
288 TEST_FIND( _T("Well, hello world"), 0, 1 );
289 TEST_FIND( _T("Well, hello world"), 6, 7 );
290 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
295 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
297 wxStringTokenizer
tkz( str
, delims
, mode
);
298 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
300 wxChar
*buf
, *s
= NULL
, *last
;
302 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
304 buf
= new wxChar
[wxStrlen(str
) + 1];
306 s
= wxStrtok(buf
, delims
, &last
);
314 while ( tkz
.HasMoreTokens() )
316 wxString token
= tkz
.GetNextToken();
319 CPPUNIT_ASSERT( token
== s
);
320 s
= wxStrtok(NULL
, delims
, &last
);
325 CPPUNIT_ASSERT( count2
== count
);
332 void StringTestCase::Tokenizer()
334 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
335 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
336 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
337 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
338 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
339 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
340 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
341 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
342 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
343 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
344 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
345 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
346 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
349 // call this with the string to tokenize, delimeters to use and the expected
350 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
351 // terminate positions with 0
353 DoTokenizerGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
355 wxStringTokenizer
tkz(s
, delims
);
357 CPPUNIT_ASSERT( tkz
.GetPosition() == 0 );
366 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
372 CPPUNIT_ASSERT( tkz
.GetPosition() == (size_t)pos
);
374 pos
= va_arg(ap
, int);
380 void StringTestCase::TokenizerGetPosition()
382 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
383 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
384 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
387 void StringTestCase::Replace()
389 #define TEST_REPLACE( original , pos , len , replacement , result ) \
391 wxString s = original; \
392 s.replace( pos , len , replacement ); \
393 CPPUNIT_ASSERT( s == result ); \
396 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
397 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
398 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
399 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
400 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
405 void StringTestCase::Match()
407 #define TEST_MATCH( s1 , s2 , result ) \
408 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
410 TEST_MATCH( _T("foobar"), _T("foo*"), true );
411 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
412 TEST_MATCH( _T("foobar"), _T("*bar"), true );
413 TEST_MATCH( _T("foobar"), _T("??????"), true );
414 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
415 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
416 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
417 TEST_MATCH( _T("foobar"), _T("*foo"), false );
418 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
419 TEST_MATCH( _T(""), _T("*"), true );
420 TEST_MATCH( _T(""), _T("?"), false );
426 void StringTestCase::CaseChanges()
428 wxString
s1(_T("Hello!"));
437 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
438 CPPUNIT_ASSERT( s1l
== _T("hello!") );
439 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
440 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
443 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
446 // try upper casing 8bit strings
447 wxString
sUpper("\xdf"),
450 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
451 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
453 #endif // !wxUSE_UNICODE
456 void StringTestCase::Compare()
458 wxString s1
= wxT("AHH");
459 wxString eq
= wxT("AHH");
460 wxString neq1
= wxT("HAH");
461 wxString neq2
= wxT("AH");
462 wxString neq3
= wxT("AHHH");
463 wxString neq4
= wxT("AhH");
465 CPPUNIT_ASSERT( s1
== eq
);
466 CPPUNIT_ASSERT( s1
!= neq1
);
467 CPPUNIT_ASSERT( s1
!= neq2
);
468 CPPUNIT_ASSERT( s1
!= neq3
);
469 CPPUNIT_ASSERT( s1
!= neq4
);
471 // wxString _s1 = wxT("A\0HH");
472 // wxString _eq = wxT("A\0HH");
473 // wxString _neq1 = wxT("H\0AH");
474 // wxString _neq2 = wxT("A\0H");
475 // wxString _neq3 = wxT("A\0HHH");
476 // wxString _neq4 = wxT("A\0hH");
479 neq1
.insert(1,1,'\0');
480 neq2
.insert(1,1,'\0');
481 neq3
.insert(1,1,'\0');
482 neq4
.insert(1,1,'\0');
484 CPPUNIT_ASSERT( s1
== eq
);
485 CPPUNIT_ASSERT( s1
!= neq1
);
486 CPPUNIT_ASSERT( s1
!= neq2
);
487 CPPUNIT_ASSERT( s1
!= neq3
);
488 CPPUNIT_ASSERT( s1
!= neq4
);
491 void StringTestCase::CompareNoCase()
493 wxString s1
= wxT("AHH");
494 wxString eq
= wxT("AHH");
495 wxString eq2
= wxT("AhH");
496 wxString eq3
= wxT("ahh");
497 wxString neq
= wxT("HAH");
498 wxString neq2
= wxT("AH");
499 wxString neq3
= wxT("AHHH");
501 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
502 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
504 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
505 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
506 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
508 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
509 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
510 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
513 // wxString _s1 = wxT("A\0HH");
514 // wxString _eq = wxT("A\0HH");
515 // wxString _eq2 = wxT("A\0hH");
516 // wxString _eq3 = wxT("a\0hh");
517 // wxString _neq = wxT("H\0AH");
518 // wxString _neq2 = wxT("A\0H");
519 // wxString _neq3 = wxT("A\0HHH");
523 eq2
.insert(1,1,'\0');
524 eq3
.insert(1,1,'\0');
525 neq
.insert(1,1,'\0');
526 neq2
.insert(1,1,'\0');
527 neq3
.insert(1,1,'\0');
529 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
530 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
531 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
533 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
534 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
535 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);