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 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
191 wxConvUTF8
) == wxT("") ); //Pos 35 (funky e) is invalid UTF8
193 CPPUNIT_ASSERT ( wxString(L
"\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
194 wxConvUTF8
) == wxT("") ); //Pos 35 (funky e) is invalid UTF8
198 void StringTestCase
::Conversion()
201 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
202 wxCharBuffer theBuffer
= szTheString
.mb_str();
204 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
206 wxString
szTheString2("The\0String", wxConvLocal
, 10);
207 CPPUNIT_ASSERT( wxMemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
209 wxString
szTheString(wxT("TheString"));
210 szTheString
.insert(3, 1, '\0');
211 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
213 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
215 wxString
szLocalTheString(wxT("TheString"));
216 szLocalTheString
.insert(3, 1, '\0');
217 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
219 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
222 #endif // wxUSE_WCHAR_T
225 void StringTestCase
::ConversionUTF7()
227 const wxChar wdata
[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
228 const char *utf7
= "+AKM-+AKM-+AKM-+AKM-";
231 wxCSConv
conv(_T("utf-7"));
232 CPPUNIT_ASSERT( strcmp(str
.mb_str(conv
), utf7
) == 0 );
234 #endif // wxUSE_UNICODE
236 void StringTestCase
::Extraction()
238 wxString
s(_T("Hello, world!"));
240 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
241 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
242 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
243 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
244 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
245 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
246 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
250 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
252 ( s.StartsWith( prefix, &rest ) == result ) && \
253 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
256 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
257 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
258 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
259 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
260 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
261 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
262 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
264 #undef TEST_STARTS_WITH
267 void StringTestCase
::Find()
269 #define TEST_FIND( str , start , result ) \
270 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
272 TEST_FIND( _T("Well, hello world"), 0, 1 );
273 TEST_FIND( _T("Well, hello world"), 6, 7 );
274 TEST_FIND( _T("Well, hello world"), 9, wxString
::npos
);
279 void StringTestCase
::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
281 wxStringTokenizer
tkz( str
, delims
, mode
);
282 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
284 wxChar
*buf
, *s
= NULL
, *last
;
286 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
288 buf
= new wxChar
[wxStrlen(str
) + 1];
290 s
= wxStrtok(buf
, delims
, &last
);
298 while ( tkz
.HasMoreTokens() )
300 wxString token
= tkz
.GetNextToken();
303 CPPUNIT_ASSERT( token
== s
);
304 s
= wxStrtok(NULL
, delims
, &last
);
309 CPPUNIT_ASSERT( count2
== count
);
316 void StringTestCase
::Tokenizer()
318 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
319 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
320 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
321 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
322 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
323 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
324 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
325 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
326 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
327 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
328 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
329 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
330 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
333 // call this with the string to tokenize, delimeters to use and the expected
334 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
335 // terminate positions with 0
337 DoTokenizerGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
339 wxStringTokenizer
tkz(s
, delims
);
341 CPPUNIT_ASSERT( tkz
.GetPosition() == 0 );
350 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
356 CPPUNIT_ASSERT( tkz
.GetPosition() == (size_t)pos
);
358 pos
= va_arg(ap
, int);
364 void StringTestCase
::TokenizerGetPosition()
366 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
367 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
368 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
371 void StringTestCase
::Replace()
373 #define TEST_REPLACE( original , pos , len , replacement , result ) \
375 wxString s = original; \
376 s.replace( pos , len , replacement ); \
377 CPPUNIT_ASSERT( s == result ); \
380 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
381 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
382 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
383 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
384 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
389 void StringTestCase
::Match()
391 #define TEST_MATCH( s1 , s2 , result ) \
392 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
394 TEST_MATCH( _T("foobar"), _T("foo*"), true );
395 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
396 TEST_MATCH( _T("foobar"), _T("*bar"), true );
397 TEST_MATCH( _T("foobar"), _T("??????"), true );
398 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
399 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
400 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
401 TEST_MATCH( _T("foobar"), _T("*foo"), false );
402 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
403 TEST_MATCH( _T(""), _T("*"), true );
404 TEST_MATCH( _T(""), _T("?"), false );
410 void StringTestCase
::CaseChanges()
412 wxString
s1(_T("Hello!"));
421 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
422 CPPUNIT_ASSERT( s1l
== _T("hello!") );
423 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
424 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
427 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
430 // try upper casing 8bit strings
431 wxString
sUpper("\xdf"),
434 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
435 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
437 #endif // !wxUSE_UNICODE
440 void StringTestCase
::Compare()
442 wxString s1
= wxT("AHH");
443 wxString eq
= wxT("AHH");
444 wxString neq1
= wxT("HAH");
445 wxString neq2
= wxT("AH");
446 wxString neq3
= wxT("AHHH");
447 wxString neq4
= wxT("AhH");
449 CPPUNIT_ASSERT( s1
== eq
);
450 CPPUNIT_ASSERT( s1
!= neq1
);
451 CPPUNIT_ASSERT( s1
!= neq2
);
452 CPPUNIT_ASSERT( s1
!= neq3
);
453 CPPUNIT_ASSERT( s1
!= neq4
);
455 // wxString _s1 = wxT("A\0HH");
456 // wxString _eq = wxT("A\0HH");
457 // wxString _neq1 = wxT("H\0AH");
458 // wxString _neq2 = wxT("A\0H");
459 // wxString _neq3 = wxT("A\0HHH");
460 // wxString _neq4 = wxT("A\0hH");
463 neq1
.insert(1,1,'\0');
464 neq2
.insert(1,1,'\0');
465 neq3
.insert(1,1,'\0');
466 neq4
.insert(1,1,'\0');
468 CPPUNIT_ASSERT( s1
== eq
);
469 CPPUNIT_ASSERT( s1
!= neq1
);
470 CPPUNIT_ASSERT( s1
!= neq2
);
471 CPPUNIT_ASSERT( s1
!= neq3
);
472 CPPUNIT_ASSERT( s1
!= neq4
);
475 void StringTestCase
::CompareNoCase()
477 wxString s1
= wxT("AHH");
478 wxString eq
= wxT("AHH");
479 wxString eq2
= wxT("AhH");
480 wxString eq3
= wxT("ahh");
481 wxString neq
= wxT("HAH");
482 wxString neq2
= wxT("AH");
483 wxString neq3
= wxT("AHHH");
485 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
486 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
488 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
489 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
490 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
492 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
493 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
494 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
497 // wxString _s1 = wxT("A\0HH");
498 // wxString _eq = wxT("A\0HH");
499 // wxString _eq2 = wxT("A\0hH");
500 // wxString _eq3 = wxT("a\0hh");
501 // wxString _neq = wxT("H\0AH");
502 // wxString _neq2 = wxT("A\0H");
503 // wxString _neq3 = wxT("A\0HHH");
507 eq2
.insert(1,1,'\0');
508 eq3
.insert(1,1,'\0');
509 neq
.insert(1,1,'\0');
510 neq2
.insert(1,1,'\0');
511 neq3
.insert(1,1,'\0');
513 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
514 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
515 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
517 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
518 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
519 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);