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_SUITE_END();
59 void ConstructorsWithConversion();
63 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
69 DECLARE_NO_COPY_CLASS(StringTestCase
)
72 // register in the unnamed registry so that these tests are run by default
73 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
75 // also include in it's own registry so that these tests can be run alone
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
78 StringTestCase::StringTestCase()
82 void StringTestCase::String()
90 for (int i
= 0; i
< 2; ++i
)
94 c
= _T("! How'ya doin'?");
97 c
= _T("Hello world! What's up?");
98 CPPUNIT_ASSERT( c
!= a
);
102 void StringTestCase::PChar()
108 for (int i
= 0; i
< 2; ++i
)
110 wxStrcpy (a
, _T("Hello"));
111 wxStrcpy (b
, _T(" world"));
112 wxStrcpy (c
, _T("! How'ya doin'?"));
115 wxStrcpy (c
, _T("Hello world! What's up?"));
116 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
120 void StringTestCase::Format()
123 s1
.Printf(_T("%03d"), 18);
124 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
125 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
126 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
129 void StringTestCase::Constructors()
131 #define TEST_CTOR(args, res) \
134 CPPUNIT_ASSERT( s == res ); \
137 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
138 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
139 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
141 static const wxChar
*s
= _T("?really!");
142 const wxChar
*start
= wxStrchr(s
, _T('r'));
143 const wxChar
*end
= wxStrchr(s
, _T('!'));
144 TEST_CTOR((start
, end
), _T("really"));
148 void StringTestCase::ConstructorsWithConversion()
150 // Déj`a in UTF-8 and wchar_t:
151 const char utf8
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
152 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
153 const char utf8sub
[] = {0x44,0xC3,0xA9,0x6A,0}; // "Dej"
155 wxString
s1(utf8
, wxConvUTF8
);
156 wxString
s2(wchar
, wxConvUTF8
);
159 CPPUNIT_ASSERT( s1
== wchar
);
160 CPPUNIT_ASSERT( s2
== wchar
);
162 CPPUNIT_ASSERT( s1
== utf8
);
163 CPPUNIT_ASSERT( s2
== utf8
);
166 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
167 wxString
s3(utf8
, wxConvUTF8
, 4);
168 wxString
s4(wchar
, wxConvUTF8
, 3);
170 CPPUNIT_ASSERT( s3
== sub
);
171 CPPUNIT_ASSERT( s4
== sub
);
175 void StringTestCase::Extraction()
177 wxString
s(_T("Hello, world!"));
179 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
180 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
181 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
182 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
183 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
184 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
185 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
189 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
191 ( s.StartsWith( prefix, &rest ) == result ) && \
192 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
195 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
196 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
197 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
198 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
199 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
200 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
201 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
203 #undef TEST_STARTS_WITH
206 void StringTestCase::Find()
208 #define TEST_FIND( str , start , result ) \
209 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
211 TEST_FIND( _T("Well, hello world"), 0, 1 );
212 TEST_FIND( _T("Well, hello world"), 6, 7 );
213 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
218 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
220 wxStringTokenizer
tkz( str
, delims
, mode
);
221 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
223 wxChar
*buf
, *s
= NULL
, *last
;
225 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
227 buf
= new wxChar
[wxStrlen(str
) + 1];
229 s
= wxStrtok(buf
, delims
, &last
);
237 while ( tkz
.HasMoreTokens() )
239 wxString token
= tkz
.GetNextToken();
242 CPPUNIT_ASSERT( token
== s
);
243 s
= wxStrtok(NULL
, delims
, &last
);
248 CPPUNIT_ASSERT( count2
== count
);
255 void StringTestCase::Tokenizer()
257 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
258 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
259 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
260 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
261 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
262 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
263 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
264 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
265 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
266 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
267 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
268 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
269 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
272 void StringTestCase::Replace()
274 #define TEST_REPLACE( original , pos , len , replacement , result ) \
276 wxString s = original; \
277 s.replace( pos , len , replacement ); \
278 CPPUNIT_ASSERT( s == result ); \
281 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
282 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
283 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
284 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
285 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
290 void StringTestCase::Match()
292 #define TEST_MATCH( s1 , s2 , result ) \
293 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
295 TEST_MATCH( _T("foobar"), _T("foo*"), true );
296 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
297 TEST_MATCH( _T("foobar"), _T("*bar"), true );
298 TEST_MATCH( _T("foobar"), _T("??????"), true );
299 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
300 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
301 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
302 TEST_MATCH( _T("foobar"), _T("*foo"), false );
303 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
304 TEST_MATCH( _T(""), _T("*"), true );
305 TEST_MATCH( _T(""), _T("?"), false );
311 void StringTestCase::CaseChanges()
313 wxString
s1(_T("Hello!"));
322 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
323 CPPUNIT_ASSERT( s1l
== _T("hello!") );
324 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
325 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
328 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
331 // try upper casing 8bit strings
332 wxString
sUpper("\xdf"),
335 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
336 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
338 #endif // !wxUSE_UNICODE