2.5.3 - cleanups, fixes, etc. etc. -
[wxWidgets.git] / tests / strings / strings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-04-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/tokenzr.h"
25
26 #include "wx/cppunit.h"
27
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class StringTestCase : public CppUnit::TestCase
33 {
34 public:
35 StringTestCase();
36
37 private:
38 CPPUNIT_TEST_SUITE( StringTestCase );
39 CPPUNIT_TEST( String );
40 CPPUNIT_TEST( PChar );
41 CPPUNIT_TEST( Format );
42 CPPUNIT_TEST( Constructors );
43 #if wxUSE_WCHAR_T
44 CPPUNIT_TEST( ConstructorsWithConversion );
45 #endif
46 CPPUNIT_TEST( Extraction );
47 CPPUNIT_TEST( Find );
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();
55
56 void String();
57 void PChar();
58 void Format();
59 void Constructors();
60 #if wxUSE_WCHAR_T
61 void ConstructorsWithConversion();
62 #endif
63 void Extraction();
64 void Find();
65 void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
66 void Tokenizer();
67 void Replace();
68 void Match();
69 void CaseChanges();
70 void Compare();
71 void CompareNoCase();
72
73 DECLARE_NO_COPY_CLASS(StringTestCase)
74 };
75
76 // register in the unnamed registry so that these tests are run by default
77 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
78
79 // also include in it's own registry so that these tests can be run alone
80 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
81
82 StringTestCase::StringTestCase()
83 {
84 }
85
86 void StringTestCase::String()
87 {
88 wxString a, b, c;
89
90 a.reserve (128);
91 b.reserve (128);
92 c.reserve (128);
93
94 for (int i = 0; i < 2; ++i)
95 {
96 a = _T("Hello");
97 b = _T(" world");
98 c = _T("! How'ya doin'?");
99 a += b;
100 a += c;
101 c = _T("Hello world! What's up?");
102 CPPUNIT_ASSERT( c != a );
103 }
104 }
105
106 void StringTestCase::PChar()
107 {
108 wxChar a [128];
109 wxChar b [128];
110 wxChar c [128];
111
112 for (int i = 0; i < 2; ++i)
113 {
114 wxStrcpy (a, _T("Hello"));
115 wxStrcpy (b, _T(" world"));
116 wxStrcpy (c, _T("! How'ya doin'?"));
117 wxStrcat (a, b);
118 wxStrcat (a, c);
119 wxStrcpy (c, _T("Hello world! What's up?"));
120 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
121 }
122 }
123
124 void StringTestCase::Format()
125 {
126 wxString s1,s2;
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()) );
131 }
132
133 void StringTestCase::Constructors()
134 {
135 #define TEST_CTOR(args, res) \
136 { \
137 wxString s args ; \
138 CPPUNIT_ASSERT( s == res ); \
139 }
140
141 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
142 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
143 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
144
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"));
149 }
150
151 #if wxUSE_WCHAR_T
152 void StringTestCase::ConstructorsWithConversion()
153 {
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"
158
159 wxString s1(utf8, wxConvUTF8);
160 wxString s2(wchar, wxConvUTF8);
161
162 #if wxUSE_UNICODE
163 CPPUNIT_ASSERT( s1 == wchar );
164 CPPUNIT_ASSERT( s2 == wchar );
165 #else
166 CPPUNIT_ASSERT( s1 == utf8 );
167 CPPUNIT_ASSERT( s2 == utf8 );
168 #endif
169
170 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
171 wxString s3(utf8, wxConvUTF8, 4);
172 wxString s4(wchar, wxConvUTF8, 3);
173
174 CPPUNIT_ASSERT( s3 == sub );
175 CPPUNIT_ASSERT( s4 == sub );
176 }
177 #endif
178
179 void StringTestCase::Extraction()
180 {
181 wxString s(_T("Hello, world!"));
182
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 );
190
191 wxString rest;
192
193 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
194 CPPUNIT_ASSERT( \
195 ( s.StartsWith( prefix, &rest ) == result ) && \
196 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
197 )
198
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 );
206
207 #undef TEST_STARTS_WITH
208 }
209
210 void StringTestCase::Find()
211 {
212 #define TEST_FIND( str , start , result ) \
213 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
214
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 );
218
219 #undef TEST_FIND
220 }
221
222 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
223 {
224 wxStringTokenizer tkz( str, delims, mode);
225 CPPUNIT_ASSERT( tkz.CountTokens() == count );
226
227 wxChar *buf, *s = NULL, *last;
228
229 if ( tkz.GetMode() == wxTOKEN_STRTOK )
230 {
231 buf = new wxChar[wxStrlen(str) + 1];
232 wxStrcpy(buf, str);
233 s = wxStrtok(buf, delims, &last);
234 }
235 else
236 {
237 buf = NULL;
238 }
239
240 size_t count2 = 0;
241 while ( tkz.HasMoreTokens() )
242 {
243 wxString token = tkz.GetNextToken();
244 if ( buf )
245 {
246 CPPUNIT_ASSERT( token == s );
247 s = wxStrtok(NULL, delims, &last);
248 }
249 count2++;
250 }
251
252 CPPUNIT_ASSERT( count2 == count );
253 if ( buf )
254 {
255 delete [] buf;
256 }
257 }
258
259 void StringTestCase::Tokenizer()
260 {
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 );
274 }
275
276 void StringTestCase::Replace()
277 {
278 #define TEST_REPLACE( original , pos , len , replacement , result ) \
279 { \
280 wxString s = original; \
281 s.replace( pos , len , replacement ); \
282 CPPUNIT_ASSERT( s == result ); \
283 }
284
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") );
290
291 #undef TEST_REPLACE
292 }
293
294 void StringTestCase::Match()
295 {
296 #define TEST_MATCH( s1 , s2 , result ) \
297 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
298
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 );
310
311 #undef TEST_MATCH
312 }
313
314
315 void StringTestCase::CaseChanges()
316 {
317 wxString s1(_T("Hello!"));
318 wxString s1u(s1);
319 wxString s1l(s1);
320 s1u.MakeUpper();
321 s1l.MakeLower();
322 wxString s2u, s2l;
323 s2u.MakeUpper();
324 s2l.MakeLower();
325
326 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
327 CPPUNIT_ASSERT( s1l == _T("hello!") );
328 CPPUNIT_ASSERT( s2u == wxEmptyString );
329 CPPUNIT_ASSERT( s2l == wxEmptyString );
330
331 #if !wxUSE_UNICODE
332 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
333 if ( locRu.IsOk() )
334 {
335 // try upper casing 8bit strings
336 wxString sUpper("\xdf"),
337 sLower("\xff");
338
339 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
340 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
341 }
342 #endif // !wxUSE_UNICODE
343 }
344
345 void StringTestCase::Compare()
346 {
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");
353
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 );
359
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");
366 s1.insert(1,1,'\0');
367 eq.insert(1,1,'\0');
368 neq1.insert(1,1,'\0');
369 neq2.insert(1,1,'\0');
370 neq3.insert(1,1,'\0');
371 neq4.insert(1,1,'\0');
372
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 );
378 }
379
380 void StringTestCase::CompareNoCase()
381 {
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");
389
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)
392
393 CPPUNIT_CNCEQ_ASSERT( s1, eq );
394 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
395 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
396
397 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
398 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
399 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
400
401
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");
409
410 s1.insert(1,1,'\0');
411 eq.insert(1,1,'\0');
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');
417
418 CPPUNIT_CNCEQ_ASSERT( s1, eq );
419 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
420 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
421
422 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
423 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
424 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
425 }