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