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