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