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