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();
80 // test if converting s using the given encoding gives ws and vice versa
82 // if either of the first 2 arguments is NULL, the conversion is supposed
84 void DoTestConversion(const char *s
, const wchar_t *w
, wxCSConv
& conv
);
85 #endif // wxUSE_WCHAR_T
87 DECLARE_NO_COPY_CLASS(StringTestCase
)
90 // register in the unnamed registry so that these tests are run by default
91 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
93 // also include in it's own registry so that these tests can be run alone
94 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
96 StringTestCase::StringTestCase()
100 void StringTestCase::String()
108 for (int i
= 0; i
< 2; ++i
)
112 c
= _T("! How'ya doin'?");
115 c
= _T("Hello world! What's up?");
116 CPPUNIT_ASSERT( c
!= a
);
120 void StringTestCase::PChar()
126 for (int i
= 0; i
< 2; ++i
)
128 wxStrcpy (a
, _T("Hello"));
129 wxStrcpy (b
, _T(" world"));
130 wxStrcpy (c
, _T("! How'ya doin'?"));
133 wxStrcpy (c
, _T("Hello world! What's up?"));
134 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
138 void StringTestCase::Format()
141 s1
.Printf(_T("%03d"), 18);
142 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
143 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
144 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
147 void StringTestCase::Constructors()
149 #define TEST_CTOR(args, res) \
152 CPPUNIT_ASSERT( s == res ); \
155 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
156 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
157 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
159 static const wxChar
*s
= _T("?really!");
160 const wxChar
*start
= wxStrchr(s
, _T('r'));
161 const wxChar
*end
= wxStrchr(s
, _T('!'));
162 TEST_CTOR((start
, end
), _T("really"));
166 void StringTestCase::ConstructorsWithConversion()
168 // the string "Déjà" in UTF-8 and wchar_t:
169 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
170 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
171 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
172 const char *utf8
= (char *)utf8Buf
;
173 const char *utf8sub
= (char *)utf8subBuf
;
175 wxString
s1(utf8
, wxConvUTF8
);
176 wxString
s2(wchar
, wxConvUTF8
);
179 CPPUNIT_ASSERT( s1
== wchar
);
180 CPPUNIT_ASSERT( s2
== wchar
);
182 CPPUNIT_ASSERT( s1
== utf8
);
183 CPPUNIT_ASSERT( s2
== utf8
);
186 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
187 wxString
s3(utf8
, wxConvUTF8
, 4);
188 wxString
s4(wchar
, wxConvUTF8
, 3);
190 CPPUNIT_ASSERT( s3
== sub
);
191 CPPUNIT_ASSERT( s4
== sub
);
194 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
195 wxConvUTF8
) == wxT("") ); //should stop at pos 35
199 void StringTestCase::Conversion()
202 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
203 wxCharBuffer theBuffer
= szTheString
.mb_str();
205 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
207 wxString
szTheString2("The\0String", wxConvLocal
, 10);
208 CPPUNIT_ASSERT( szTheString2
.length() == 11 );
209 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
211 wxString
szTheString(wxT("TheString"));
212 szTheString
.insert(3, 1, '\0');
213 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
215 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
217 wxString
szLocalTheString(wxT("TheString"));
218 szLocalTheString
.insert(3, 1, '\0');
219 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
221 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
225 // in case wcscmp is missing
227 static int wx_wcscmp(const wchar_t *s1
, const wchar_t *s2
)
241 StringTestCase::DoTestConversion(const char *s
,
248 wxCharBuffer
buf(wxString(ws
).mb_str(conv
));
250 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
252 #else // wxUSE_UNICODE
255 wxWCharBuffer
wbuf(wxString(s
).wc_str(conv
));
258 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
260 CPPUNIT_ASSERT( !*wbuf
);
262 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
265 struct StringConversionData
271 void StringTestCase::ConversionUTF7()
273 static const StringConversionData utf7data
[] =
277 //\u isn't recognized on MSVC 6
278 #if !defined(_MSC_VER)
279 #if !defined(__GNUC__) || (__GNUC__ >= 3)
280 { "+AKM-", L
"\u00a3" },
283 // Windows accepts invalid UTF-7 strings and so does our UTF-7
284 // conversion code -- this is wrong IMO but the way it is for now
286 // notice that converting "+" still behaves as expected because the
287 // result is just an empty string, i.e. the same as if there were an
288 // error, but converting "a+" results in "a" while it really should
294 wxCSConv
conv(_T("utf-7"));
295 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
297 const StringConversionData
& d
= utf7data
[n
];
298 DoTestConversion(d
.str
, d
.wcs
, conv
);
302 void StringTestCase::ConversionUTF8()
304 static const StringConversionData utf8data
[] =
306 //\u isn't recognized on MSVC 6
307 #if !defined(_MSC_VER)
308 #if !defined(__GNUC__) || (__GNUC__ >= 3)
309 { "\xc2\xa3", L
"\u00a3" },
315 wxCSConv
conv(_T("utf-8"));
316 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
318 const StringConversionData
& d
= utf8data
[n
];
319 DoTestConversion(d
.str
, d
.wcs
, conv
);
323 #endif // wxUSE_WCHAR_T
326 void StringTestCase::Extraction()
328 wxString
s(_T("Hello, world!"));
330 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
331 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
332 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
333 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
334 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
335 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
336 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
340 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
342 ( s.StartsWith( prefix, &rest ) == result ) && \
343 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
346 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
347 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
348 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
349 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
350 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
351 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
352 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
354 #undef TEST_STARTS_WITH
357 void StringTestCase::Find()
359 #define TEST_FIND( str , start , result ) \
360 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
362 TEST_FIND( _T("Well, hello world"), 0, 1 );
363 TEST_FIND( _T("Well, hello world"), 6, 7 );
364 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
369 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
371 wxStringTokenizer
tkz( str
, delims
, mode
);
372 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
374 wxChar
*buf
, *s
= NULL
, *last
;
376 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
378 buf
= new wxChar
[wxStrlen(str
) + 1];
380 s
= wxStrtok(buf
, delims
, &last
);
388 while ( tkz
.HasMoreTokens() )
390 wxString token
= tkz
.GetNextToken();
393 CPPUNIT_ASSERT( token
== s
);
394 s
= wxStrtok(NULL
, delims
, &last
);
399 CPPUNIT_ASSERT( count2
== count
);
406 void StringTestCase::Tokenizer()
408 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
409 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
410 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
411 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
412 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
413 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
414 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
415 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
416 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
417 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
418 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
419 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
420 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
423 // call this with the string to tokenize, delimeters to use and the expected
424 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
425 // terminate positions with 0
426 static void DoTokenizerGetPosition(const wxChar
*s
,
427 const wxChar
*delims
, int pos
, ...)
429 wxStringTokenizer
tkz(s
, delims
);
431 CPPUNIT_ASSERT( tkz
.GetPosition() == 0 );
440 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
446 CPPUNIT_ASSERT( tkz
.GetPosition() == (size_t)pos
);
448 pos
= va_arg(ap
, int);
454 void StringTestCase::TokenizerGetPosition()
456 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
457 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
458 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
461 void StringTestCase::Replace()
463 #define TEST_REPLACE( original , pos , len , replacement , result ) \
465 wxString s = original; \
466 s.replace( pos , len , replacement ); \
467 CPPUNIT_ASSERT( s == result ); \
470 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
471 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
472 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
473 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
474 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
477 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
479 wxString s(o,olen); \
480 s.replace( pos , len , replacement ); \
481 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
484 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
485 _T("null\0dhar"), 9 );
487 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
489 wxString s(o,olen); \
490 s.Replace( olds, news, all ); \
491 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
494 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
495 _T("null\0dehar"), 10 );
497 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
498 _T("null\0char"), 9 );
500 #undef TEST_WXREPLACE
501 #undef TEST_NULLCHARREPLACE
505 void StringTestCase::Match()
507 #define TEST_MATCH( s1 , s2 , result ) \
508 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
510 TEST_MATCH( _T("foobar"), _T("foo*"), true );
511 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
512 TEST_MATCH( _T("foobar"), _T("*bar"), true );
513 TEST_MATCH( _T("foobar"), _T("??????"), true );
514 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
515 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
516 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
517 TEST_MATCH( _T("foobar"), _T("*foo"), false );
518 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
519 TEST_MATCH( _T(""), _T("*"), true );
520 TEST_MATCH( _T(""), _T("?"), false );
526 void StringTestCase::CaseChanges()
528 wxString
s1(_T("Hello!"));
537 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
538 CPPUNIT_ASSERT( s1l
== _T("hello!") );
539 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
540 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
543 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
546 // try upper casing 8bit strings
547 wxString
sUpper("\xdf"),
550 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
551 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
553 #endif // !wxUSE_UNICODE
556 void StringTestCase::Compare()
558 wxString s1
= wxT("AHH");
559 wxString eq
= wxT("AHH");
560 wxString neq1
= wxT("HAH");
561 wxString neq2
= wxT("AH");
562 wxString neq3
= wxT("AHHH");
563 wxString neq4
= wxT("AhH");
565 CPPUNIT_ASSERT( s1
== eq
);
566 CPPUNIT_ASSERT( s1
!= neq1
);
567 CPPUNIT_ASSERT( s1
!= neq2
);
568 CPPUNIT_ASSERT( s1
!= neq3
);
569 CPPUNIT_ASSERT( s1
!= neq4
);
571 // wxString _s1 = wxT("A\0HH");
572 // wxString _eq = wxT("A\0HH");
573 // wxString _neq1 = wxT("H\0AH");
574 // wxString _neq2 = wxT("A\0H");
575 // wxString _neq3 = wxT("A\0HHH");
576 // wxString _neq4 = wxT("A\0hH");
579 neq1
.insert(1,1,'\0');
580 neq2
.insert(1,1,'\0');
581 neq3
.insert(1,1,'\0');
582 neq4
.insert(1,1,'\0');
584 CPPUNIT_ASSERT( s1
== eq
);
585 CPPUNIT_ASSERT( s1
!= neq1
);
586 CPPUNIT_ASSERT( s1
!= neq2
);
587 CPPUNIT_ASSERT( s1
!= neq3
);
588 CPPUNIT_ASSERT( s1
!= neq4
);
591 void StringTestCase::CompareNoCase()
593 wxString s1
= wxT("AHH");
594 wxString eq
= wxT("AHH");
595 wxString eq2
= wxT("AhH");
596 wxString eq3
= wxT("ahh");
597 wxString neq
= wxT("HAH");
598 wxString neq2
= wxT("AH");
599 wxString neq3
= wxT("AHHH");
601 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
602 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
604 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
605 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
606 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
608 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
609 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
610 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
613 // wxString _s1 = wxT("A\0HH");
614 // wxString _eq = wxT("A\0HH");
615 // wxString _eq2 = wxT("A\0hH");
616 // wxString _eq3 = wxT("a\0hh");
617 // wxString _neq = wxT("H\0AH");
618 // wxString _neq2 = wxT("A\0H");
619 // wxString _neq3 = wxT("A\0HHH");
623 eq2
.insert(1,1,'\0');
624 eq3
.insert(1,1,'\0');
625 neq
.insert(1,1,'\0');
626 neq2
.insert(1,1,'\0');
627 neq3
.insert(1,1,'\0');
629 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
630 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
631 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
633 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
634 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
635 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);