]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
docstring update
[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 // ----------------------------------------------------------------------------
25 // test class
26 // ----------------------------------------------------------------------------
27
28 class StringTestCase : public CppUnit::TestCase
29 {
30 public:
31 StringTestCase();
32
33 private:
34 CPPUNIT_TEST_SUITE( StringTestCase );
35 CPPUNIT_TEST( String );
36 CPPUNIT_TEST( PChar );
37 CPPUNIT_TEST( Format );
38 CPPUNIT_TEST( Constructors );
39 CPPUNIT_TEST( Extraction );
40 CPPUNIT_TEST( Trim );
41 CPPUNIT_TEST( Find );
42 CPPUNIT_TEST( Replace );
43 CPPUNIT_TEST( Match );
44 CPPUNIT_TEST( CaseChanges );
45 CPPUNIT_TEST( Compare );
46 CPPUNIT_TEST( CompareNoCase );
47 CPPUNIT_TEST( Contains );
48 CPPUNIT_TEST( ToLong );
49 CPPUNIT_TEST( ToULong );
50 CPPUNIT_TEST( ToDouble );
51 CPPUNIT_TEST_SUITE_END();
52
53 void String();
54 void PChar();
55 void Format();
56 void Constructors();
57 void Extraction();
58 void Trim();
59 void Find();
60 void Replace();
61 void Match();
62 void CaseChanges();
63 void Compare();
64 void CompareNoCase();
65 void Contains();
66 void ToLong();
67 void ToULong();
68 void ToDouble();
69
70 DECLARE_NO_COPY_CLASS(StringTestCase)
71 };
72
73 // register in the unnamed registry so that these tests are run by default
74 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
75
76 // also include in it's own registry so that these tests can be run alone
77 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
78
79 StringTestCase::StringTestCase()
80 {
81 }
82
83 void StringTestCase::String()
84 {
85 wxString a, b, c;
86
87 a.reserve (128);
88 b.reserve (128);
89 c.reserve (128);
90
91 for (int i = 0; i < 2; ++i)
92 {
93 a = _T("Hello");
94 b = _T(" world");
95 c = _T("! How'ya doin'?");
96 a += b;
97 a += c;
98 c = _T("Hello world! What's up?");
99 CPPUNIT_ASSERT( c != a );
100 }
101 }
102
103 void StringTestCase::PChar()
104 {
105 wxChar a [128];
106 wxChar b [128];
107 wxChar c [128];
108
109 for (int i = 0; i < 2; ++i)
110 {
111 wxStrcpy (a, _T("Hello"));
112 wxStrcpy (b, _T(" world"));
113 wxStrcpy (c, _T("! How'ya doin'?"));
114 wxStrcat (a, b);
115 wxStrcat (a, c);
116 wxStrcpy (c, _T("Hello world! What's up?"));
117 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
118 }
119 }
120
121 void StringTestCase::Format()
122 {
123 wxString s1,s2;
124 s1.Printf(_T("%03d"), 18);
125 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
126 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
127 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
128
129 static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
130 for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
131 {
132 const size_t len = lengths[n];
133
134 wxString s(_T('Z'), len);
135 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length());
136 }
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
158 void StringTestCase::Extraction()
159 {
160 wxString s(_T("Hello, world!"));
161
162 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
163 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
164 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
165 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
166 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
167 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
168 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
169
170 wxString rest;
171
172 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
173 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
174 if ( result ) \
175 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
176
177 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
178 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
179 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
180 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
181 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
182 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
183 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
184
185 #undef TEST_STARTS_WITH
186
187 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
188 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
189 if ( result ) \
190 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
191
192 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
193 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
194 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
195 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
196 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
197 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
198 TEST_ENDS_WITH( _T("?"), _T(""), false );
199 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
200 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
201
202 #undef TEST_ENDS_WITH
203 }
204
205 void StringTestCase::Trim()
206 {
207 #define TEST_TRIM( str , dir , result ) \
208 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
209
210 TEST_TRIM( _T(" Test "), true, _T(" Test") );
211 TEST_TRIM( _T(" "), true, _T("") );
212 TEST_TRIM( _T(" "), true, _T("") );
213 TEST_TRIM( _T(""), true, _T("") );
214
215 TEST_TRIM( _T(" Test "), false, _T("Test ") );
216 TEST_TRIM( _T(" "), false, _T("") );
217 TEST_TRIM( _T(" "), false, _T("") );
218 TEST_TRIM( _T(""), false, _T("") );
219
220 #undef TEST_TRIM
221 }
222
223 void StringTestCase::Find()
224 {
225 #define TEST_FIND( str , start , result ) \
226 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
227
228 TEST_FIND( _T("Well, hello world"), 0, 1 );
229 TEST_FIND( _T("Well, hello world"), 6, 7 );
230 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
231
232 #undef TEST_FIND
233 }
234
235 void StringTestCase::Replace()
236 {
237 #define TEST_REPLACE( original , pos , len , replacement , result ) \
238 { \
239 wxString s = original; \
240 s.replace( pos , len , replacement ); \
241 CPPUNIT_ASSERT( s == result ); \
242 }
243
244 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
245 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
246 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
247 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
248 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
249
250
251 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
252 { \
253 wxString s(o,olen); \
254 s.replace( pos , len , replacement ); \
255 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
256 }
257
258 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
259 _T("null\0dhar"), 9 );
260
261 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
262 { \
263 wxString s(o,olen); \
264 s.Replace( olds, news, all ); \
265 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
266 }
267
268 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
269 _T("null\0dehar"), 10 );
270
271 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
272 _T("null\0char"), 9 );
273
274 #undef TEST_WXREPLACE
275 #undef TEST_NULLCHARREPLACE
276 #undef TEST_REPLACE
277 }
278
279 void StringTestCase::Match()
280 {
281 #define TEST_MATCH( s1 , s2 , result ) \
282 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
283
284 TEST_MATCH( _T("foobar"), _T("foo*"), true );
285 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
286 TEST_MATCH( _T("foobar"), _T("*bar"), true );
287 TEST_MATCH( _T("foobar"), _T("??????"), true );
288 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
289 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
290 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
291 TEST_MATCH( _T("foobar"), _T("*foo"), false );
292 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
293 TEST_MATCH( _T(""), _T("*"), true );
294 TEST_MATCH( _T(""), _T("?"), false );
295
296 #undef TEST_MATCH
297 }
298
299
300 void StringTestCase::CaseChanges()
301 {
302 wxString s1(_T("Hello!"));
303 wxString s1u(s1);
304 wxString s1l(s1);
305 s1u.MakeUpper();
306 s1l.MakeLower();
307 wxString s2u, s2l;
308 s2u.MakeUpper();
309 s2l.MakeLower();
310
311 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
312 CPPUNIT_ASSERT( s1l == _T("hello!") );
313 CPPUNIT_ASSERT( s2u == wxEmptyString );
314 CPPUNIT_ASSERT( s2l == wxEmptyString );
315
316 #if !wxUSE_UNICODE
317 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
318 if ( locRu.IsOk() )
319 {
320 // try upper casing 8bit strings
321 const wchar_t capital_ya[] = { 0x42f, 0 },
322 small_ya[] = { 0x44f, 0 };
323
324 wxString sUpper(wxConvLibc.cWC2MB(capital_ya)),
325 sLower(wxConvLibc.cWC2MB(small_ya));
326
327 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
328 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
329 }
330 #endif // !wxUSE_UNICODE
331 }
332
333 void StringTestCase::Compare()
334 {
335 wxString s1 = wxT("AHH");
336 wxString eq = wxT("AHH");
337 wxString neq1 = wxT("HAH");
338 wxString neq2 = wxT("AH");
339 wxString neq3 = wxT("AHHH");
340 wxString neq4 = wxT("AhH");
341
342 CPPUNIT_ASSERT( s1 == eq );
343 CPPUNIT_ASSERT( s1 != neq1 );
344 CPPUNIT_ASSERT( s1 != neq2 );
345 CPPUNIT_ASSERT( s1 != neq3 );
346 CPPUNIT_ASSERT( s1 != neq4 );
347
348 // wxString _s1 = wxT("A\0HH");
349 // wxString _eq = wxT("A\0HH");
350 // wxString _neq1 = wxT("H\0AH");
351 // wxString _neq2 = wxT("A\0H");
352 // wxString _neq3 = wxT("A\0HHH");
353 // wxString _neq4 = wxT("A\0hH");
354 s1.insert(1,1,'\0');
355 eq.insert(1,1,'\0');
356 neq1.insert(1,1,'\0');
357 neq2.insert(1,1,'\0');
358 neq3.insert(1,1,'\0');
359 neq4.insert(1,1,'\0');
360
361 CPPUNIT_ASSERT( s1 == eq );
362 CPPUNIT_ASSERT( s1 != neq1 );
363 CPPUNIT_ASSERT( s1 != neq2 );
364 CPPUNIT_ASSERT( s1 != neq3 );
365 CPPUNIT_ASSERT( s1 != neq4 );
366 }
367
368 void StringTestCase::CompareNoCase()
369 {
370 wxString s1 = wxT("AHH");
371 wxString eq = wxT("AHH");
372 wxString eq2 = wxT("AhH");
373 wxString eq3 = wxT("ahh");
374 wxString neq = wxT("HAH");
375 wxString neq2 = wxT("AH");
376 wxString neq3 = wxT("AHHH");
377
378 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
379 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
380
381 CPPUNIT_CNCEQ_ASSERT( s1, eq );
382 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
383 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
384
385 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
386 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
387 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
388
389
390 // wxString _s1 = wxT("A\0HH");
391 // wxString _eq = wxT("A\0HH");
392 // wxString _eq2 = wxT("A\0hH");
393 // wxString _eq3 = wxT("a\0hh");
394 // wxString _neq = wxT("H\0AH");
395 // wxString _neq2 = wxT("A\0H");
396 // wxString _neq3 = wxT("A\0HHH");
397
398 s1.insert(1,1,'\0');
399 eq.insert(1,1,'\0');
400 eq2.insert(1,1,'\0');
401 eq3.insert(1,1,'\0');
402 neq.insert(1,1,'\0');
403 neq2.insert(1,1,'\0');
404 neq3.insert(1,1,'\0');
405
406 CPPUNIT_CNCEQ_ASSERT( s1, eq );
407 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
408 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
409
410 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
411 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
412 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
413 }
414
415 void StringTestCase::Contains()
416 {
417 static const struct ContainsData
418 {
419 const wxChar *hay;
420 const wxChar *needle;
421 bool contains;
422 } containsData[] =
423 {
424 { _T(""), _T(""), true },
425 { _T(""), _T("foo"), false },
426 { _T("foo"), _T(""), true },
427 { _T("foo"), _T("f"), true },
428 { _T("foo"), _T("o"), true },
429 { _T("foo"), _T("oo"), true },
430 { _T("foo"), _T("ooo"), false },
431 { _T("foo"), _T("oooo"), false },
432 { _T("foo"), _T("fooo"), false },
433 };
434
435 for ( size_t n = 0; n < WXSIZEOF(containsData); n++ )
436 {
437 const ContainsData& cd = containsData[n];
438 CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) );
439 }
440 }
441
442 void StringTestCase::ToLong()
443 {
444 static const struct ToLongData
445 {
446 const wxChar *str;
447 long value;
448 bool ok;
449 } longData[] =
450 {
451 { _T("1"), 1, true },
452 { _T("0"), 0, true },
453 { _T("a"), 0, false },
454 { _T("12345"), 12345, true },
455 { _T("-1"), -1, true },
456 { _T("--1"), 0, false },
457 };
458
459 long l;
460 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
461 {
462 const ToLongData& ld = longData[n];
463 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) );
464 if ( ld.ok )
465 CPPUNIT_ASSERT_EQUAL( ld.value, l );
466 }
467 }
468
469 void StringTestCase::ToULong()
470 {
471 unsigned long ul;
472 static const struct ToULongData
473 {
474 const wxChar *str;
475 unsigned long value;
476 bool ok;
477 } ulongData[] =
478 {
479 { _T("1"), 1, true },
480 { _T("0"), 0, true },
481 { _T("a"), 0, false },
482 { _T("12345"), 12345, true },
483 // this is surprizing but consistent with strtoul() behaviour
484 { _T("-1"), ULONG_MAX, true },
485 };
486
487 size_t n;
488 for ( n = 0; n < WXSIZEOF(ulongData); n++ )
489 {
490 const ToULongData& uld = ulongData[n];
491 CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) );
492 if ( uld.ok )
493 CPPUNIT_ASSERT_EQUAL( uld.value, ul );
494 }
495 }
496
497 void StringTestCase::ToDouble()
498 {
499 double d;
500 static const struct ToDoubleData
501 {
502 const wxChar *str;
503 double value;
504 bool ok;
505 } doubleData[] =
506 {
507 { _T("1"), 1, true },
508 { _T("1.23"), 1.23, true },
509 { _T(".1"), .1, true },
510 { _T("1."), 1, true },
511 { _T("1.."), 0, false },
512 { _T("0"), 0, true },
513 { _T("a"), 0, false },
514 { _T("12345"), 12345, true },
515 { _T("-1"), -1, true },
516 { _T("--1"), 0, false },
517 };
518
519 // we need to use decimal point, not comma or whatever is its value for the
520 // current locale
521 wxSetlocale(LC_ALL, _T("C"));
522
523 size_t n;
524 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
525 {
526 const ToDoubleData& ld = doubleData[n];
527 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
528 if ( ld.ok )
529 CPPUNIT_ASSERT_EQUAL( ld.value, d );
530 }
531 }