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