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
);
48 CPPUNIT_TEST( ConversionUTF7
);
50 CPPUNIT_TEST( Extraction
);
52 CPPUNIT_TEST( Tokenizer
);
53 CPPUNIT_TEST( TokenizerGetPosition
);
54 CPPUNIT_TEST( Replace
);
55 CPPUNIT_TEST( Match
);
56 CPPUNIT_TEST( CaseChanges
);
57 CPPUNIT_TEST( Compare
);
58 CPPUNIT_TEST( CompareNoCase
);
59 CPPUNIT_TEST_SUITE_END();
66 void ConstructorsWithConversion();
70 void ConversionUTF7();
74 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
76 void TokenizerGetPosition();
83 DECLARE_NO_COPY_CLASS(StringTestCase
)
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
92 StringTestCase::StringTestCase()
96 void StringTestCase::String()
104 for (int i
= 0; i
< 2; ++i
)
108 c
= _T("! How'ya doin'?");
111 c
= _T("Hello world! What's up?");
112 CPPUNIT_ASSERT( c
!= a
);
116 void StringTestCase::PChar()
122 for (int i
= 0; i
< 2; ++i
)
124 wxStrcpy (a
, _T("Hello"));
125 wxStrcpy (b
, _T(" world"));
126 wxStrcpy (c
, _T("! How'ya doin'?"));
129 wxStrcpy (c
, _T("Hello world! What's up?"));
130 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
134 void StringTestCase::Format()
137 s1
.Printf(_T("%03d"), 18);
138 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
139 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
140 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
143 void StringTestCase::Constructors()
145 #define TEST_CTOR(args, res) \
148 CPPUNIT_ASSERT( s == res ); \
151 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
152 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
153 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
155 static const wxChar
*s
= _T("?really!");
156 const wxChar
*start
= wxStrchr(s
, _T('r'));
157 const wxChar
*end
= wxStrchr(s
, _T('!'));
158 TEST_CTOR((start
, end
), _T("really"));
162 void StringTestCase::ConstructorsWithConversion()
164 // the string "Déjà" in UTF-8 and wchar_t:
165 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
166 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
167 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
168 const char *utf8
= (char *)utf8Buf
;
169 const char *utf8sub
= (char *)utf8subBuf
;
171 wxString
s1(utf8
, wxConvUTF8
);
172 wxString
s2(wchar
, wxConvUTF8
);
175 CPPUNIT_ASSERT( s1
== wchar
);
176 CPPUNIT_ASSERT( s2
== wchar
);
178 CPPUNIT_ASSERT( s1
== utf8
);
179 CPPUNIT_ASSERT( s2
== utf8
);
182 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
183 wxString
s3(utf8
, wxConvUTF8
, 4);
184 wxString
s4(wchar
, wxConvUTF8
, 3);
186 CPPUNIT_ASSERT( s3
== sub
);
187 CPPUNIT_ASSERT( s4
== sub
);
190 void StringTestCase::Conversion()
193 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
194 wxCharBuffer theBuffer
= szTheString
.mb_str();
196 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
198 wxString
szTheString2("The\0String", wxConvLocal
, 10);
199 CPPUNIT_ASSERT( wxMemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
201 wxString
szTheString(wxT("TheString"));
202 szTheString
.insert(3, 1, '\0');
203 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
205 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
207 wxString
szLocalTheString(wxT("TheString"));
208 szLocalTheString
.insert(3, 1, '\0');
209 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
211 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
214 #endif // wxUSE_WCHAR_T
217 void StringTestCase::ConversionUTF7()
219 const wxChar wdata
[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
220 const char *utf7
= "+AKM-+AKM-+AKM-+AKM-";
223 wxCSConv
conv(_T("utf-7"));
224 CPPUNIT_ASSERT( strcmp(str
.mb_str(conv
), utf7
) == 0 );
226 #endif // wxUSE_UNICODE
228 void StringTestCase::Extraction()
230 wxString
s(_T("Hello, world!"));
232 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
233 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
234 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
235 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
236 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
237 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
238 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
242 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
244 ( s.StartsWith( prefix, &rest ) == result ) && \
245 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
248 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
249 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
250 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
251 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
252 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
253 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
254 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
256 #undef TEST_STARTS_WITH
259 void StringTestCase::Find()
261 #define TEST_FIND( str , start , result ) \
262 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
264 TEST_FIND( _T("Well, hello world"), 0, 1 );
265 TEST_FIND( _T("Well, hello world"), 6, 7 );
266 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
271 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
273 wxStringTokenizer
tkz( str
, delims
, mode
);
274 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
276 wxChar
*buf
, *s
= NULL
, *last
;
278 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
280 buf
= new wxChar
[wxStrlen(str
) + 1];
282 s
= wxStrtok(buf
, delims
, &last
);
290 while ( tkz
.HasMoreTokens() )
292 wxString token
= tkz
.GetNextToken();
295 CPPUNIT_ASSERT( token
== s
);
296 s
= wxStrtok(NULL
, delims
, &last
);
301 CPPUNIT_ASSERT( count2
== count
);
308 void StringTestCase::Tokenizer()
310 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
311 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
312 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
313 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
314 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
315 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
316 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
317 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
318 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
319 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
320 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
321 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
322 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
325 // call this with the string to tokenize, delimeters to use and the expected
326 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
327 // terminate positions with 0
329 DoTokenizerGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
331 wxStringTokenizer
tkz(s
, delims
);
333 CPPUNIT_ASSERT( tkz
.GetPosition() == 0 );
342 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
348 CPPUNIT_ASSERT( tkz
.GetPosition() == (size_t)pos
);
350 pos
= va_arg(ap
, int);
356 void StringTestCase::TokenizerGetPosition()
358 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
359 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
360 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
363 void StringTestCase::Replace()
365 #define TEST_REPLACE( original , pos , len , replacement , result ) \
367 wxString s = original; \
368 s.replace( pos , len , replacement ); \
369 CPPUNIT_ASSERT( s == result ); \
372 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
373 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
374 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
375 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
376 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
381 void StringTestCase::Match()
383 #define TEST_MATCH( s1 , s2 , result ) \
384 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
386 TEST_MATCH( _T("foobar"), _T("foo*"), true );
387 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
388 TEST_MATCH( _T("foobar"), _T("*bar"), true );
389 TEST_MATCH( _T("foobar"), _T("??????"), true );
390 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
391 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
392 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
393 TEST_MATCH( _T("foobar"), _T("*foo"), false );
394 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
395 TEST_MATCH( _T(""), _T("*"), true );
396 TEST_MATCH( _T(""), _T("?"), false );
402 void StringTestCase::CaseChanges()
404 wxString
s1(_T("Hello!"));
413 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
414 CPPUNIT_ASSERT( s1l
== _T("hello!") );
415 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
416 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
419 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
422 // try upper casing 8bit strings
423 wxString
sUpper("\xdf"),
426 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
427 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
429 #endif // !wxUSE_UNICODE
432 void StringTestCase::Compare()
434 wxString s1
= wxT("AHH");
435 wxString eq
= wxT("AHH");
436 wxString neq1
= wxT("HAH");
437 wxString neq2
= wxT("AH");
438 wxString neq3
= wxT("AHHH");
439 wxString neq4
= wxT("AhH");
441 CPPUNIT_ASSERT( s1
== eq
);
442 CPPUNIT_ASSERT( s1
!= neq1
);
443 CPPUNIT_ASSERT( s1
!= neq2
);
444 CPPUNIT_ASSERT( s1
!= neq3
);
445 CPPUNIT_ASSERT( s1
!= neq4
);
447 // wxString _s1 = wxT("A\0HH");
448 // wxString _eq = wxT("A\0HH");
449 // wxString _neq1 = wxT("H\0AH");
450 // wxString _neq2 = wxT("A\0H");
451 // wxString _neq3 = wxT("A\0HHH");
452 // wxString _neq4 = wxT("A\0hH");
455 neq1
.insert(1,1,'\0');
456 neq2
.insert(1,1,'\0');
457 neq3
.insert(1,1,'\0');
458 neq4
.insert(1,1,'\0');
460 CPPUNIT_ASSERT( s1
== eq
);
461 CPPUNIT_ASSERT( s1
!= neq1
);
462 CPPUNIT_ASSERT( s1
!= neq2
);
463 CPPUNIT_ASSERT( s1
!= neq3
);
464 CPPUNIT_ASSERT( s1
!= neq4
);
467 void StringTestCase::CompareNoCase()
469 wxString s1
= wxT("AHH");
470 wxString eq
= wxT("AHH");
471 wxString eq2
= wxT("AhH");
472 wxString eq3
= wxT("ahh");
473 wxString neq
= wxT("HAH");
474 wxString neq2
= wxT("AH");
475 wxString neq3
= wxT("AHHH");
477 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
478 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
480 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
481 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
482 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
484 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
485 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
486 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
489 // wxString _s1 = wxT("A\0HH");
490 // wxString _eq = wxT("A\0HH");
491 // wxString _eq2 = wxT("A\0hH");
492 // wxString _eq3 = wxT("a\0hh");
493 // wxString _neq = wxT("H\0AH");
494 // wxString _neq2 = wxT("A\0H");
495 // wxString _neq3 = wxT("A\0HHH");
499 eq2
.insert(1,1,'\0');
500 eq3
.insert(1,1,'\0');
501 neq
.insert(1,1,'\0');
502 neq2
.insert(1,1,'\0');
503 neq3
.insert(1,1,'\0');
505 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
506 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
507 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
509 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
510 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
511 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);