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
);
46 CPPUNIT_TEST( Extraction
);
48 CPPUNIT_TEST( Tokenizer
);
49 CPPUNIT_TEST( Replace
);
50 CPPUNIT_TEST( Match
);
51 CPPUNIT_TEST( CaseChanges
);
52 CPPUNIT_TEST( Compare
);
53 CPPUNIT_TEST( CompareNoCase
);
54 CPPUNIT_TEST_SUITE_END();
61 void ConstructorsWithConversion();
65 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
73 DECLARE_NO_COPY_CLASS(StringTestCase
)
76 // register in the unnamed registry so that these tests are run by default
77 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
79 // also include in it's own registry so that these tests can be run alone
80 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
82 StringTestCase::StringTestCase()
86 void StringTestCase::String()
94 for (int i
= 0; i
< 2; ++i
)
98 c
= _T("! How'ya doin'?");
101 c
= _T("Hello world! What's up?");
102 CPPUNIT_ASSERT( c
!= a
);
106 void StringTestCase::PChar()
112 for (int i
= 0; i
< 2; ++i
)
114 wxStrcpy (a
, _T("Hello"));
115 wxStrcpy (b
, _T(" world"));
116 wxStrcpy (c
, _T("! How'ya doin'?"));
119 wxStrcpy (c
, _T("Hello world! What's up?"));
120 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
124 void StringTestCase::Format()
127 s1
.Printf(_T("%03d"), 18);
128 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
129 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
130 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
133 void StringTestCase::Constructors()
135 #define TEST_CTOR(args, res) \
138 CPPUNIT_ASSERT( s == res ); \
141 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
142 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
143 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
145 static const wxChar
*s
= _T("?really!");
146 const wxChar
*start
= wxStrchr(s
, _T('r'));
147 const wxChar
*end
= wxStrchr(s
, _T('!'));
148 TEST_CTOR((start
, end
), _T("really"));
152 void StringTestCase::ConstructorsWithConversion()
154 // Déj`a in UTF-8 and wchar_t:
155 const char utf8
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
156 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
157 const char utf8sub
[] = {0x44,0xC3,0xA9,0x6A,0}; // "Dej"
159 wxString
s1(utf8
, wxConvUTF8
);
160 wxString
s2(wchar
, wxConvUTF8
);
163 CPPUNIT_ASSERT( s1
== wchar
);
164 CPPUNIT_ASSERT( s2
== wchar
);
166 CPPUNIT_ASSERT( s1
== utf8
);
167 CPPUNIT_ASSERT( s2
== utf8
);
170 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
171 wxString
s3(utf8
, wxConvUTF8
, 4);
172 wxString
s4(wchar
, wxConvUTF8
, 3);
174 CPPUNIT_ASSERT( s3
== sub
);
175 CPPUNIT_ASSERT( s4
== sub
);
179 void StringTestCase::Extraction()
181 wxString
s(_T("Hello, world!"));
183 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
184 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
185 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
186 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
187 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
188 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
189 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
193 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
195 ( s.StartsWith( prefix, &rest ) == result ) && \
196 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
199 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
200 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
201 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
202 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
203 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
204 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
205 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
207 #undef TEST_STARTS_WITH
210 void StringTestCase::Find()
212 #define TEST_FIND( str , start , result ) \
213 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
215 TEST_FIND( _T("Well, hello world"), 0, 1 );
216 TEST_FIND( _T("Well, hello world"), 6, 7 );
217 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
222 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
224 wxStringTokenizer
tkz( str
, delims
, mode
);
225 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
227 wxChar
*buf
, *s
= NULL
, *last
;
229 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
231 buf
= new wxChar
[wxStrlen(str
) + 1];
233 s
= wxStrtok(buf
, delims
, &last
);
241 while ( tkz
.HasMoreTokens() )
243 wxString token
= tkz
.GetNextToken();
246 CPPUNIT_ASSERT( token
== s
);
247 s
= wxStrtok(NULL
, delims
, &last
);
252 CPPUNIT_ASSERT( count2
== count
);
259 void StringTestCase::Tokenizer()
261 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
262 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
263 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
264 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
265 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
266 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
267 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
268 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
269 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
270 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
271 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
272 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
273 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
276 void StringTestCase::Replace()
278 #define TEST_REPLACE( original , pos , len , replacement , result ) \
280 wxString s = original; \
281 s.replace( pos , len , replacement ); \
282 CPPUNIT_ASSERT( s == result ); \
285 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
286 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
287 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
288 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
289 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
294 void StringTestCase::Match()
296 #define TEST_MATCH( s1 , s2 , result ) \
297 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
299 TEST_MATCH( _T("foobar"), _T("foo*"), true );
300 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
301 TEST_MATCH( _T("foobar"), _T("*bar"), true );
302 TEST_MATCH( _T("foobar"), _T("??????"), true );
303 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
304 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
305 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
306 TEST_MATCH( _T("foobar"), _T("*foo"), false );
307 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
308 TEST_MATCH( _T(""), _T("*"), true );
309 TEST_MATCH( _T(""), _T("?"), false );
315 void StringTestCase::CaseChanges()
317 wxString
s1(_T("Hello!"));
326 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
327 CPPUNIT_ASSERT( s1l
== _T("hello!") );
328 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
329 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
332 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
335 // try upper casing 8bit strings
336 wxString
sUpper("\xdf"),
339 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
340 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
342 #endif // !wxUSE_UNICODE
345 void StringTestCase::Compare()
347 wxString s1
= wxT("AHH");
348 wxString eq
= wxT("AHH");
349 wxString neq1
= wxT("HAH");
350 wxString neq2
= wxT("AH");
351 wxString neq3
= wxT("AHHH");
352 wxString neq4
= wxT("AhH");
354 CPPUNIT_ASSERT( s1
== eq
);
355 CPPUNIT_ASSERT( s1
!= neq1
);
356 CPPUNIT_ASSERT( s1
!= neq2
);
357 CPPUNIT_ASSERT( s1
!= neq3
);
358 CPPUNIT_ASSERT( s1
!= neq4
);
360 // wxString _s1 = wxT("A\0HH");
361 // wxString _eq = wxT("A\0HH");
362 // wxString _neq1 = wxT("H\0AH");
363 // wxString _neq2 = wxT("A\0H");
364 // wxString _neq3 = wxT("A\0HHH");
365 // wxString _neq4 = wxT("A\0hH");
368 neq1
.insert(1,1,'\0');
369 neq2
.insert(1,1,'\0');
370 neq3
.insert(1,1,'\0');
371 neq4
.insert(1,1,'\0');
373 CPPUNIT_ASSERT( s1
== eq
);
374 CPPUNIT_ASSERT( s1
!= neq1
);
375 CPPUNIT_ASSERT( s1
!= neq2
);
376 CPPUNIT_ASSERT( s1
!= neq3
);
377 CPPUNIT_ASSERT( s1
!= neq4
);
380 void StringTestCase::CompareNoCase()
382 wxString s1
= wxT("AHH");
383 wxString eq
= wxT("AHH");
384 wxString eq2
= wxT("AhH");
385 wxString eq3
= wxT("ahh");
386 wxString neq
= wxT("HAH");
387 wxString neq2
= wxT("AH");
388 wxString neq3
= wxT("AHHH");
390 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
391 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
393 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
394 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
395 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
397 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
398 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
399 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
402 // wxString _s1 = wxT("A\0HH");
403 // wxString _eq = wxT("A\0HH");
404 // wxString _eq2 = wxT("A\0hH");
405 // wxString _eq3 = wxT("a\0hh");
406 // wxString _neq = wxT("H\0AH");
407 // wxString _neq2 = wxT("A\0H");
408 // wxString _neq3 = wxT("A\0HHH");
412 eq2
.insert(1,1,'\0');
413 eq3
.insert(1,1,'\0');
414 neq
.insert(1,1,'\0');
415 neq2
.insert(1,1,'\0');
416 neq3
.insert(1,1,'\0');
418 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
419 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
420 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
422 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
423 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
424 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);