[ 1054664 ] Implementation of wxMBConvUTF7 (Heavily modified in places), utf7 unit...
[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 CPPUNIT_TEST( ConversionUTF7 );
47 #endif
48 CPPUNIT_TEST( Extraction );
49 CPPUNIT_TEST( Find );
50 CPPUNIT_TEST( Tokenizer );
51 CPPUNIT_TEST( TokenizerGetPosition );
52 CPPUNIT_TEST( Replace );
53 CPPUNIT_TEST( Match );
54 CPPUNIT_TEST( CaseChanges );
55 CPPUNIT_TEST( Compare );
56 CPPUNIT_TEST( CompareNoCase );
57 CPPUNIT_TEST_SUITE_END();
58
59 void String();
60 void PChar();
61 void Format();
62 void Constructors();
63 #if wxUSE_WCHAR_T
64 void ConstructorsWithConversion();
65 void Conversion();
66 void ConversionUTF7();
67 #endif
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( wxMemcmp(szTheString2.c_str(), L"The\0String", 11) == 0 );
201 #else
202 wxString szTheString(wxT("TheString"));
203 szTheString.insert(3, 1, '\0');
204 wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
205
206 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
207
208 wxString szLocalTheString(wxT("TheString"));
209 szLocalTheString.insert(3, 1, '\0');
210 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
211
212 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
213 #endif
214 }
215
216 void StringTestCase::ConversionUTF7()
217 {
218 const wchar_t data[] = { 0x00A3, 0x00A3, 0x00A3, 0x00A3, 0 }; // pound signs
219
220 //utf7 and utf7alt are equivelent
221 const char *utf7 = "+AKM-+AKM-+AKM-+AKM-";
222 const char *utf7alt = "+AKMAowCjAKM-";
223
224 #if wxUSE_UNICODE
225 wxString str(data);
226
227 wxCSConv conv(_T("utf-7"));
228
229 wxCharBuffer theBuffer = str.mb_str(conv);
230
231 CPPUNIT_ASSERT( strcmp(theBuffer, utf7) == 0 || strcmp(theBuffer, utf7alt) == 0);
232 #else //ANSI
233 wxString str(utf7);
234
235 wxCSConv conv(_T("utf-7"));
236
237 wxWCharBuffer theWBuffer = str.wc_str(conv);
238
239 CPPUNIT_ASSERT( wxWcslen(theWBuffer) == wxWcslen(data) );
240 CPPUNIT_ASSERT( memcmp(theWBuffer, data, wxWcslen(data) * sizeof(wchar_t)) == 0 );
241
242 wxString stralt(utf7alt);
243
244 wxWCharBuffer theWBufferAlt = stralt.wc_str(conv);
245
246 CPPUNIT_ASSERT( wxWcslen(theWBufferAlt) == wxWcslen(data) );
247 CPPUNIT_ASSERT( memcmp(theWBufferAlt, data, wxWcslen(data) * sizeof(wchar_t)) == 0 );
248
249 #endif // wxUSE_UNICODE
250 }
251
252 #endif // wxUSE_WCHAR_T
253
254
255 void StringTestCase::Extraction()
256 {
257 wxString s(_T("Hello, world!"));
258
259 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
260 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
261 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
262 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
263 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
264 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
265 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
266
267 wxString rest;
268
269 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
270 CPPUNIT_ASSERT( \
271 ( s.StartsWith( prefix, &rest ) == result ) && \
272 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
273 )
274
275 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
276 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
277 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
278 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
279 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
280 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
281 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
282
283 #undef TEST_STARTS_WITH
284 }
285
286 void StringTestCase::Find()
287 {
288 #define TEST_FIND( str , start , result ) \
289 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
290
291 TEST_FIND( _T("Well, hello world"), 0, 1 );
292 TEST_FIND( _T("Well, hello world"), 6, 7 );
293 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
294
295 #undef TEST_FIND
296 }
297
298 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
299 {
300 wxStringTokenizer tkz( str, delims, mode);
301 CPPUNIT_ASSERT( tkz.CountTokens() == count );
302
303 wxChar *buf, *s = NULL, *last;
304
305 if ( tkz.GetMode() == wxTOKEN_STRTOK )
306 {
307 buf = new wxChar[wxStrlen(str) + 1];
308 wxStrcpy(buf, str);
309 s = wxStrtok(buf, delims, &last);
310 }
311 else
312 {
313 buf = NULL;
314 }
315
316 size_t count2 = 0;
317 while ( tkz.HasMoreTokens() )
318 {
319 wxString token = tkz.GetNextToken();
320 if ( buf )
321 {
322 CPPUNIT_ASSERT( token == s );
323 s = wxStrtok(NULL, delims, &last);
324 }
325 count2++;
326 }
327
328 CPPUNIT_ASSERT( count2 == count );
329 if ( buf )
330 {
331 delete [] buf;
332 }
333 }
334
335 void StringTestCase::Tokenizer()
336 {
337 SingleTokenizerTest( _T(""), _T(" "), 0, 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(",!"), 2, wxTOKEN_DEFAULT );
342 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT );
343 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL );
344 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT );
345 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT );
346 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY );
347 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL );
348 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT );
349 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS );
350 }
351
352 // call this with the string to tokenize, delimeters to use and the expected
353 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
354 // terminate positions with 0
355 static void
356 DoTokenizerGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
357 {
358 wxStringTokenizer tkz(s, delims);
359
360 CPPUNIT_ASSERT( tkz.GetPosition() == 0 );
361
362 va_list ap;
363 va_start(ap, pos);
364
365 for ( ;; )
366 {
367 if ( !pos )
368 {
369 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
370 break;
371 }
372
373 tkz.GetNextToken();
374
375 CPPUNIT_ASSERT( tkz.GetPosition() == (size_t)pos );
376
377 pos = va_arg(ap, int);
378 }
379
380 va_end(ap);
381 }
382
383 void StringTestCase::TokenizerGetPosition()
384 {
385 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
386 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
387 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
388 }
389
390 void StringTestCase::Replace()
391 {
392 #define TEST_REPLACE( original , pos , len , replacement , result ) \
393 { \
394 wxString s = original; \
395 s.replace( pos , len , replacement ); \
396 CPPUNIT_ASSERT( s == result ); \
397 }
398
399 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
400 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
401 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
402 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
403 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
404
405 #undef TEST_REPLACE
406 }
407
408 void StringTestCase::Match()
409 {
410 #define TEST_MATCH( s1 , s2 , result ) \
411 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
412
413 TEST_MATCH( _T("foobar"), _T("foo*"), true );
414 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
415 TEST_MATCH( _T("foobar"), _T("*bar"), true );
416 TEST_MATCH( _T("foobar"), _T("??????"), true );
417 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
418 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
419 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
420 TEST_MATCH( _T("foobar"), _T("*foo"), false );
421 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
422 TEST_MATCH( _T(""), _T("*"), true );
423 TEST_MATCH( _T(""), _T("?"), false );
424
425 #undef TEST_MATCH
426 }
427
428
429 void StringTestCase::CaseChanges()
430 {
431 wxString s1(_T("Hello!"));
432 wxString s1u(s1);
433 wxString s1l(s1);
434 s1u.MakeUpper();
435 s1l.MakeLower();
436 wxString s2u, s2l;
437 s2u.MakeUpper();
438 s2l.MakeLower();
439
440 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
441 CPPUNIT_ASSERT( s1l == _T("hello!") );
442 CPPUNIT_ASSERT( s2u == wxEmptyString );
443 CPPUNIT_ASSERT( s2l == wxEmptyString );
444
445 #if !wxUSE_UNICODE
446 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
447 if ( locRu.IsOk() )
448 {
449 // try upper casing 8bit strings
450 wxString sUpper("\xdf"),
451 sLower("\xff");
452
453 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
454 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
455 }
456 #endif // !wxUSE_UNICODE
457 }
458
459 void StringTestCase::Compare()
460 {
461 wxString s1 = wxT("AHH");
462 wxString eq = wxT("AHH");
463 wxString neq1 = wxT("HAH");
464 wxString neq2 = wxT("AH");
465 wxString neq3 = wxT("AHHH");
466 wxString neq4 = wxT("AhH");
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 // wxString _s1 = wxT("A\0HH");
475 // wxString _eq = wxT("A\0HH");
476 // wxString _neq1 = wxT("H\0AH");
477 // wxString _neq2 = wxT("A\0H");
478 // wxString _neq3 = wxT("A\0HHH");
479 // wxString _neq4 = wxT("A\0hH");
480 s1.insert(1,1,'\0');
481 eq.insert(1,1,'\0');
482 neq1.insert(1,1,'\0');
483 neq2.insert(1,1,'\0');
484 neq3.insert(1,1,'\0');
485 neq4.insert(1,1,'\0');
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
494 void StringTestCase::CompareNoCase()
495 {
496 wxString s1 = wxT("AHH");
497 wxString eq = wxT("AHH");
498 wxString eq2 = wxT("AhH");
499 wxString eq3 = wxT("ahh");
500 wxString neq = wxT("HAH");
501 wxString neq2 = wxT("AH");
502 wxString neq3 = wxT("AHHH");
503
504 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
505 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
506
507 CPPUNIT_CNCEQ_ASSERT( s1, eq );
508 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
509 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
510
511 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
512 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
513 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
514
515
516 // wxString _s1 = wxT("A\0HH");
517 // wxString _eq = wxT("A\0HH");
518 // wxString _eq2 = wxT("A\0hH");
519 // wxString _eq3 = wxT("a\0hh");
520 // wxString _neq = wxT("H\0AH");
521 // wxString _neq2 = wxT("A\0H");
522 // wxString _neq3 = wxT("A\0HHH");
523
524 s1.insert(1,1,'\0');
525 eq.insert(1,1,'\0');
526 eq2.insert(1,1,'\0');
527 eq3.insert(1,1,'\0');
528 neq.insert(1,1,'\0');
529 neq2.insert(1,1,'\0');
530 neq3.insert(1,1,'\0');
531
532 CPPUNIT_CNCEQ_ASSERT( s1, eq );
533 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
534 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
535
536 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
537 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
538 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
539 }
540