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