]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
only define wxUSE_NATIVE_STATUSBAR for the ports which have it (MSW and Mac)
[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 "testprec.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 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class StringTestCase : public CppUnit::TestCase
31 {
32 public:
33 StringTestCase();
34
35 private:
36 CPPUNIT_TEST_SUITE( StringTestCase );
37 CPPUNIT_TEST( String );
38 CPPUNIT_TEST( PChar );
39 CPPUNIT_TEST( Format );
40 CPPUNIT_TEST( Constructors );
41 #if wxUSE_WCHAR_T
42 CPPUNIT_TEST( ConstructorsWithConversion );
43 CPPUNIT_TEST( Conversion );
44 CPPUNIT_TEST( ConversionUTF7 );
45 #endif
46 CPPUNIT_TEST( Extraction );
47 CPPUNIT_TEST( Find );
48 CPPUNIT_TEST( Tokenizer );
49 CPPUNIT_TEST( TokenizerGetPosition );
50 CPPUNIT_TEST( Replace );
51 CPPUNIT_TEST( Match );
52 CPPUNIT_TEST( CaseChanges );
53 CPPUNIT_TEST( Compare );
54 CPPUNIT_TEST( CompareNoCase );
55 CPPUNIT_TEST_SUITE_END();
56
57 void String();
58 void PChar();
59 void Format();
60 void Constructors();
61 #if wxUSE_WCHAR_T
62 void ConstructorsWithConversion();
63 void Conversion();
64 void ConversionUTF7();
65 #endif
66 void Extraction();
67 void Find();
68 void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
69 void Tokenizer();
70 void TokenizerGetPosition();
71 void Replace();
72 void Match();
73 void CaseChanges();
74 void Compare();
75 void CompareNoCase();
76
77 DECLARE_NO_COPY_CLASS(StringTestCase)
78 };
79
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
82
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
85
86 StringTestCase::StringTestCase()
87 {
88 }
89
90 void StringTestCase::String()
91 {
92 wxString a, b, c;
93
94 a.reserve (128);
95 b.reserve (128);
96 c.reserve (128);
97
98 for (int i = 0; i < 2; ++i)
99 {
100 a = _T("Hello");
101 b = _T(" world");
102 c = _T("! How'ya doin'?");
103 a += b;
104 a += c;
105 c = _T("Hello world! What's up?");
106 CPPUNIT_ASSERT( c != a );
107 }
108 }
109
110 void StringTestCase::PChar()
111 {
112 wxChar a [128];
113 wxChar b [128];
114 wxChar c [128];
115
116 for (int i = 0; i < 2; ++i)
117 {
118 wxStrcpy (a, _T("Hello"));
119 wxStrcpy (b, _T(" world"));
120 wxStrcpy (c, _T("! How'ya doin'?"));
121 wxStrcat (a, b);
122 wxStrcat (a, c);
123 wxStrcpy (c, _T("Hello world! What's up?"));
124 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
125 }
126 }
127
128 void StringTestCase::Format()
129 {
130 wxString s1,s2;
131 s1.Printf(_T("%03d"), 18);
132 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
133 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
134 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
135 }
136
137 void StringTestCase::Constructors()
138 {
139 #define TEST_CTOR(args, res) \
140 { \
141 wxString s args ; \
142 CPPUNIT_ASSERT( s == res ); \
143 }
144
145 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
146 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
147 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
148
149 static const wxChar *s = _T("?really!");
150 const wxChar *start = wxStrchr(s, _T('r'));
151 const wxChar *end = wxStrchr(s, _T('!'));
152 TEST_CTOR((start, end), _T("really"));
153 }
154
155 #if wxUSE_WCHAR_T
156 void StringTestCase::ConstructorsWithConversion()
157 {
158 // the string "Déjà" in UTF-8 and wchar_t:
159 const unsigned char utf8Buf[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
160 const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
161 const unsigned char utf8subBuf[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
162 const char *utf8 = (char *)utf8Buf;
163 const char *utf8sub = (char *)utf8subBuf;
164
165 wxString s1(utf8, wxConvUTF8);
166 wxString s2(wchar, wxConvUTF8);
167
168 #if wxUSE_UNICODE
169 CPPUNIT_ASSERT( s1 == wchar );
170 CPPUNIT_ASSERT( s2 == wchar );
171 #else
172 CPPUNIT_ASSERT( s1 == utf8 );
173 CPPUNIT_ASSERT( s2 == utf8 );
174 #endif
175
176 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
177 wxString s3(utf8, wxConvUTF8, 4);
178 wxString s4(wchar, wxConvUTF8, 3);
179
180 CPPUNIT_ASSERT( s3 == sub );
181 CPPUNIT_ASSERT( s4 == sub );
182
183 #if wxUSE_UNICODE
184 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
185 wxConvUTF8) == wxT("") ); //should stop at pos 35
186 #endif
187 }
188
189 void StringTestCase::Conversion()
190 {
191 #if wxUSE_UNICODE
192 wxString szTheString(L"The\0String", wxConvLibc, 10);
193 wxCharBuffer theBuffer = szTheString.mb_str();
194
195 CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String", 11) == 0 );
196
197 wxString szTheString2("The\0String", wxConvLocal, 10);
198 CPPUNIT_ASSERT( szTheString2.length() == 11 );
199 CPPUNIT_ASSERT( wxTmemcmp(szTheString2.c_str(), L"The\0String", 11) == 0 );
200 #else
201 wxString szTheString(wxT("TheString"));
202 szTheString.insert(3, 1, '\0');
203 wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
204
205 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
206
207 wxString szLocalTheString(wxT("TheString"));
208 szLocalTheString.insert(3, 1, '\0');
209 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
210
211 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
212 #endif
213 }
214
215 void StringTestCase::ConversionUTF7()
216 {
217 const wchar_t data[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
218
219 //utf7 and utf7alt are equivelent
220 const char *utf7 = "+AKM-+AKM-+AKM-+AKM-";
221 const char *utf7alt = "+AKMAowCjAKM-";
222
223 #if wxUSE_UNICODE
224 wxString str(data);
225
226 wxCSConv conv(_T("utf-7"));
227
228 wxCharBuffer theBuffer = str.mb_str(conv);
229
230 CPPUNIT_ASSERT( strcmp(theBuffer, utf7) == 0 || strcmp(theBuffer, utf7alt) == 0);
231 #else //ANSI
232 wxString str(utf7);
233
234 wxCSConv conv(_T("utf-7"));
235
236 wxWCharBuffer theWBuffer = str.wc_str(conv);
237
238 CPPUNIT_ASSERT( wxWcslen(theWBuffer) == wxWcslen(data) );
239 CPPUNIT_ASSERT( memcmp(theWBuffer, data, wxWcslen(data) * sizeof(wchar_t)) == 0 );
240
241 wxString stralt(utf7alt);
242
243 wxWCharBuffer theWBufferAlt = stralt.wc_str(conv);
244
245 CPPUNIT_ASSERT( wxWcslen(theWBufferAlt) == wxWcslen(data) );
246 CPPUNIT_ASSERT( memcmp(theWBufferAlt, data, wxWcslen(data) * sizeof(wchar_t)) == 0 );
247
248 #endif // wxUSE_UNICODE
249 }
250
251 #endif // wxUSE_WCHAR_T
252
253
254 void StringTestCase::Extraction()
255 {
256 wxString s(_T("Hello, world!"));
257
258 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
259 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
260 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
261 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
262 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
263 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
264 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
265
266 wxString rest;
267
268 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
269 CPPUNIT_ASSERT( \
270 ( s.StartsWith( prefix, &rest ) == result ) && \
271 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
272 )
273
274 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
275 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
276 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
277 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
278 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
279 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
280 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
281
282 #undef TEST_STARTS_WITH
283 }
284
285 void StringTestCase::Find()
286 {
287 #define TEST_FIND( str , start , result ) \
288 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
289
290 TEST_FIND( _T("Well, hello world"), 0, 1 );
291 TEST_FIND( _T("Well, hello world"), 6, 7 );
292 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
293
294 #undef TEST_FIND
295 }
296
297 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
298 {
299 wxStringTokenizer tkz( str, delims, mode);
300 CPPUNIT_ASSERT( tkz.CountTokens() == count );
301
302 wxChar *buf, *s = NULL, *last;
303
304 if ( tkz.GetMode() == wxTOKEN_STRTOK )
305 {
306 buf = new wxChar[wxStrlen(str) + 1];
307 wxStrcpy(buf, str);
308 s = wxStrtok(buf, delims, &last);
309 }
310 else
311 {
312 buf = NULL;
313 }
314
315 size_t count2 = 0;
316 while ( tkz.HasMoreTokens() )
317 {
318 wxString token = tkz.GetNextToken();
319 if ( buf )
320 {
321 CPPUNIT_ASSERT( token == s );
322 s = wxStrtok(NULL, delims, &last);
323 }
324 count2++;
325 }
326
327 CPPUNIT_ASSERT( count2 == count );
328 if ( buf )
329 {
330 delete [] buf;
331 }
332 }
333
334 void StringTestCase::Tokenizer()
335 {
336 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT );
337 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT );
338 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT );
339 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT );
340 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT );
341 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT );
342 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL );
343 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT );
344 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT );
345 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY );
346 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL );
347 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT );
348 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS );
349 }
350
351 // call this with the string to tokenize, delimeters to use and the expected
352 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
353 // terminate positions with 0
354 static void
355 DoTokenizerGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
356 {
357 wxStringTokenizer tkz(s, delims);
358
359 CPPUNIT_ASSERT( tkz.GetPosition() == 0 );
360
361 va_list ap;
362 va_start(ap, pos);
363
364 for ( ;; )
365 {
366 if ( !pos )
367 {
368 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
369 break;
370 }
371
372 tkz.GetNextToken();
373
374 CPPUNIT_ASSERT( tkz.GetPosition() == (size_t)pos );
375
376 pos = va_arg(ap, int);
377 }
378
379 va_end(ap);
380 }
381
382 void StringTestCase::TokenizerGetPosition()
383 {
384 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
385 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
386 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
387 }
388
389 void StringTestCase::Replace()
390 {
391 #define TEST_REPLACE( original , pos , len , replacement , result ) \
392 { \
393 wxString s = original; \
394 s.replace( pos , len , replacement ); \
395 CPPUNIT_ASSERT( s == result ); \
396 }
397
398 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
399 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
400 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
401 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
402 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
403
404 #undef TEST_REPLACE
405 }
406
407 void StringTestCase::Match()
408 {
409 #define TEST_MATCH( s1 , s2 , result ) \
410 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
411
412 TEST_MATCH( _T("foobar"), _T("foo*"), true );
413 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
414 TEST_MATCH( _T("foobar"), _T("*bar"), true );
415 TEST_MATCH( _T("foobar"), _T("??????"), true );
416 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
417 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
418 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
419 TEST_MATCH( _T("foobar"), _T("*foo"), false );
420 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
421 TEST_MATCH( _T(""), _T("*"), true );
422 TEST_MATCH( _T(""), _T("?"), false );
423
424 #undef TEST_MATCH
425 }
426
427
428 void StringTestCase::CaseChanges()
429 {
430 wxString s1(_T("Hello!"));
431 wxString s1u(s1);
432 wxString s1l(s1);
433 s1u.MakeUpper();
434 s1l.MakeLower();
435 wxString s2u, s2l;
436 s2u.MakeUpper();
437 s2l.MakeLower();
438
439 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
440 CPPUNIT_ASSERT( s1l == _T("hello!") );
441 CPPUNIT_ASSERT( s2u == wxEmptyString );
442 CPPUNIT_ASSERT( s2l == wxEmptyString );
443
444 #if !wxUSE_UNICODE
445 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
446 if ( locRu.IsOk() )
447 {
448 // try upper casing 8bit strings
449 wxString sUpper("\xdf"),
450 sLower("\xff");
451
452 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
453 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
454 }
455 #endif // !wxUSE_UNICODE
456 }
457
458 void StringTestCase::Compare()
459 {
460 wxString s1 = wxT("AHH");
461 wxString eq = wxT("AHH");
462 wxString neq1 = wxT("HAH");
463 wxString neq2 = wxT("AH");
464 wxString neq3 = wxT("AHHH");
465 wxString neq4 = wxT("AhH");
466
467 CPPUNIT_ASSERT( s1 == eq );
468 CPPUNIT_ASSERT( s1 != neq1 );
469 CPPUNIT_ASSERT( s1 != neq2 );
470 CPPUNIT_ASSERT( s1 != neq3 );
471 CPPUNIT_ASSERT( s1 != neq4 );
472
473 // wxString _s1 = wxT("A\0HH");
474 // wxString _eq = wxT("A\0HH");
475 // wxString _neq1 = wxT("H\0AH");
476 // wxString _neq2 = wxT("A\0H");
477 // wxString _neq3 = wxT("A\0HHH");
478 // wxString _neq4 = wxT("A\0hH");
479 s1.insert(1,1,'\0');
480 eq.insert(1,1,'\0');
481 neq1.insert(1,1,'\0');
482 neq2.insert(1,1,'\0');
483 neq3.insert(1,1,'\0');
484 neq4.insert(1,1,'\0');
485
486 CPPUNIT_ASSERT( s1 == eq );
487 CPPUNIT_ASSERT( s1 != neq1 );
488 CPPUNIT_ASSERT( s1 != neq2 );
489 CPPUNIT_ASSERT( s1 != neq3 );
490 CPPUNIT_ASSERT( s1 != neq4 );
491 }
492
493 void StringTestCase::CompareNoCase()
494 {
495 wxString s1 = wxT("AHH");
496 wxString eq = wxT("AHH");
497 wxString eq2 = wxT("AhH");
498 wxString eq3 = wxT("ahh");
499 wxString neq = wxT("HAH");
500 wxString neq2 = wxT("AH");
501 wxString neq3 = wxT("AHHH");
502
503 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
504 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
505
506 CPPUNIT_CNCEQ_ASSERT( s1, eq );
507 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
508 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
509
510 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
511 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
512 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
513
514
515 // wxString _s1 = wxT("A\0HH");
516 // wxString _eq = wxT("A\0HH");
517 // wxString _eq2 = wxT("A\0hH");
518 // wxString _eq3 = wxT("a\0hh");
519 // wxString _neq = wxT("H\0AH");
520 // wxString _neq2 = wxT("A\0H");
521 // wxString _neq3 = wxT("A\0HHH");
522
523 s1.insert(1,1,'\0');
524 eq.insert(1,1,'\0');
525 eq2.insert(1,1,'\0');
526 eq3.insert(1,1,'\0');
527 neq.insert(1,1,'\0');
528 neq2.insert(1,1,'\0');
529 neq3.insert(1,1,'\0');
530
531 CPPUNIT_CNCEQ_ASSERT( s1, eq );
532 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
533 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
534
535 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
536 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
537 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
538 }
539