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