]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/strings.cpp
extract wxTextEntry unit tests in a reusable base class
[wxWidgets.git] / tests / strings / strings.cpp
CommitLineData
1cd53e88
VS
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
8899b155 14#include "testprec.h"
1cd53e88
VS
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif // WX_PRECOMP
23
1cd53e88
VS
24// ----------------------------------------------------------------------------
25// test class
26// ----------------------------------------------------------------------------
27
28class StringTestCase : public CppUnit::TestCase
29{
30public:
31 StringTestCase();
32
33private:
34 CPPUNIT_TEST_SUITE( StringTestCase );
35 CPPUNIT_TEST( String );
36 CPPUNIT_TEST( PChar );
37 CPPUNIT_TEST( Format );
38 CPPUNIT_TEST( Constructors );
f312f981 39 CPPUNIT_TEST( StaticConstructors );
1cd53e88 40 CPPUNIT_TEST( Extraction );
e6a99197 41 CPPUNIT_TEST( Trim );
1cd53e88 42 CPPUNIT_TEST( Find );
1cd53e88
VS
43 CPPUNIT_TEST( Replace );
44 CPPUNIT_TEST( Match );
bd7f096d 45 CPPUNIT_TEST( CaseChanges );
dcb68102
RN
46 CPPUNIT_TEST( Compare );
47 CPPUNIT_TEST( CompareNoCase );
c4f35063 48 CPPUNIT_TEST( Contains );
4f7ee81a
VZ
49 CPPUNIT_TEST( ToLong );
50 CPPUNIT_TEST( ToULong );
d6718dd1
VZ
51#ifdef wxLongLong_t
52 CPPUNIT_TEST( ToLongLong );
53 CPPUNIT_TEST( ToULongLong );
54#endif // wxLongLong_t
4f7ee81a 55 CPPUNIT_TEST( ToDouble );
062dc5fc 56 CPPUNIT_TEST( StringBuf );
628f87da 57 CPPUNIT_TEST( UTF8Buf );
ef0f1387 58 CPPUNIT_TEST( CStrDataTernaryOperator );
3a69bca1 59 CPPUNIT_TEST( CStrDataOperators );
ef0f1387
VS
60 CPPUNIT_TEST( CStrDataImplicitConversion );
61 CPPUNIT_TEST( ExplicitConversion );
6bd4f281 62 CPPUNIT_TEST( IndexedAccess );
1cd53e88
VS
63 CPPUNIT_TEST_SUITE_END();
64
65 void String();
66 void PChar();
67 void Format();
68 void Constructors();
f312f981 69 void StaticConstructors();
1cd53e88 70 void Extraction();
e6a99197 71 void Trim();
1cd53e88 72 void Find();
1cd53e88
VS
73 void Replace();
74 void Match();
bd7f096d 75 void CaseChanges();
dcb68102
RN
76 void Compare();
77 void CompareNoCase();
c4f35063 78 void Contains();
4f7ee81a
VZ
79 void ToLong();
80 void ToULong();
d6718dd1
VZ
81#ifdef wxLongLong_t
82 void ToLongLong();
83 void ToULongLong();
84#endif // wxLongLong_t
4f7ee81a 85 void ToDouble();
062dc5fc 86 void StringBuf();
628f87da 87 void UTF8Buf();
ef0f1387
VS
88 void CStrDataTernaryOperator();
89 void DoCStrDataTernaryOperator(bool cond);
3a69bca1 90 void CStrDataOperators();
ef0f1387
VS
91 void CStrDataImplicitConversion();
92 void ExplicitConversion();
6bd4f281 93 void IndexedAccess();
1cd53e88
VS
94
95 DECLARE_NO_COPY_CLASS(StringTestCase)
96};
97
98// register in the unnamed registry so that these tests are run by default
99CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
100
101// also include in it's own registry so that these tests can be run alone
102CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
103
104StringTestCase::StringTestCase()
105{
106}
107
108void StringTestCase::String()
109{
110 wxString a, b, c;
111
112 a.reserve (128);
113 b.reserve (128);
114 c.reserve (128);
115
116 for (int i = 0; i < 2; ++i)
117 {
118 a = _T("Hello");
119 b = _T(" world");
120 c = _T("! How'ya doin'?");
121 a += b;
122 a += c;
123 c = _T("Hello world! What's up?");
124 CPPUNIT_ASSERT( c != a );
125 }
126}
127
128void StringTestCase::PChar()
129{
130 wxChar a [128];
131 wxChar b [128];
132 wxChar c [128];
133
134 for (int i = 0; i < 2; ++i)
135 {
136 wxStrcpy (a, _T("Hello"));
137 wxStrcpy (b, _T(" world"));
138 wxStrcpy (c, _T("! How'ya doin'?"));
139 wxStrcat (a, b);
140 wxStrcat (a, c);
141 wxStrcpy (c, _T("Hello world! What's up?"));
142 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
143 }
144}
145
146void StringTestCase::Format()
147{
148 wxString s1,s2;
149 s1.Printf(_T("%03d"), 18);
150 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
151 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
152 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
b03cd7e6
VZ
153
154 static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
155 for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
156 {
157 const size_t len = lengths[n];
158
159 wxString s(_T('Z'), len);
160 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length());
161 }
1cd53e88
VS
162}
163
164void StringTestCase::Constructors()
165{
1de532f5
VZ
166 CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) );
167 CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') );
168 CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) );
169 CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) );
170 CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) );
f312f981
VZ
171
172#if wxUSE_UNICODE
1de532f5
VZ
173 CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) );
174 CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') );
175 CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) );
176 CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) );
177 CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) );
f312f981
VZ
178#endif // wxUSE_UNICODE
179
180 static const char *s = "?really!";
181 const char *start = wxStrchr(s, 'r');
182 const char *end = wxStrchr(s, '!');
1de532f5 183 CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) );
7652a41d
VS
184
185 // test if creating string from NULL C pointer works:
1de532f5 186 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) );
1cd53e88
VS
187}
188
f312f981
VZ
189void StringTestCase::StaticConstructors()
190{
1de532f5
VZ
191 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
192 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
193 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
194 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
195 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
f312f981
VZ
196
197 // FIXME: this doesn't work currently but should!
1de532f5 198 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
f312f981
VZ
199
200
1de532f5
VZ
201 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
202 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
203 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
204 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
205 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
f312f981 206
1de532f5 207 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
f312f981 208}
265d5cce 209
1cd53e88
VS
210void StringTestCase::Extraction()
211{
212 wxString s(_T("Hello, world!"));
213
214 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
215 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
216 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
217 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
218 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
219 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
220 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
221
478cbb08
VS
222#if wxUSE_UNICODE
223 static const char *germanUTF8 = "Oberfl\303\244che";
224 wxString strUnicode(wxString::FromUTF8(germanUTF8));
225
226 CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode );
227 CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" );
228#endif // wxUSE_UNICODE
229
1cd53e88
VS
230 wxString rest;
231
3affcd07
VZ
232 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
233 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
234 if ( result ) \
1de532f5 235 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
1cd53e88
VS
236
237 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
238 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
239 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
240 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
241 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
242 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
243 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
244
245 #undef TEST_STARTS_WITH
3affcd07 246
c8f313b2
VZ
247 rest = "Hello world";
248 CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) );
1de532f5 249 CPPUNIT_ASSERT_EQUAL("world", rest);
c8f313b2 250
3affcd07
VZ
251 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
252 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
253 if ( result ) \
1de532f5 254 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
3affcd07
VZ
255
256 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
257 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
258 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
259 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
260 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
261 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
262 TEST_ENDS_WITH( _T("?"), _T(""), false );
263 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
264 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
265
266 #undef TEST_ENDS_WITH
1cd53e88
VS
267}
268
e6a99197
WS
269void StringTestCase::Trim()
270{
271 #define TEST_TRIM( str , dir , result ) \
272 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
273
274 TEST_TRIM( _T(" Test "), true, _T(" Test") );
275 TEST_TRIM( _T(" "), true, _T("") );
276 TEST_TRIM( _T(" "), true, _T("") );
277 TEST_TRIM( _T(""), true, _T("") );
278
279 TEST_TRIM( _T(" Test "), false, _T("Test ") );
280 TEST_TRIM( _T(" "), false, _T("") );
281 TEST_TRIM( _T(" "), false, _T("") );
282 TEST_TRIM( _T(""), false, _T("") );
283
284 #undef TEST_TRIM
285}
286
1cd53e88
VS
287void StringTestCase::Find()
288{
289 #define TEST_FIND( str , start , result ) \
290 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
291
292 TEST_FIND( _T("Well, hello world"), 0, 1 );
293 TEST_FIND( _T("Well, hello world"), 6, 7 );
294 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
295
296 #undef TEST_FIND
297}
298
1cd53e88
VS
299void StringTestCase::Replace()
300{
301 #define TEST_REPLACE( original , pos , len , replacement , result ) \
302 { \
303 wxString s = original; \
304 s.replace( pos , len , replacement ); \
1de532f5 305 CPPUNIT_ASSERT_EQUAL( result, s ); \
1cd53e88
VS
306 }
307
308 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
309 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
310 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
311 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
312 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
313
1de532f5 314
7634e443
RN
315 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
316 { \
317 wxString s(o,olen); \
318 s.replace( pos , len , replacement ); \
319 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
320 }
1de532f5
VZ
321
322 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
323 _T("null\0dhar"), 9 );
7634e443
RN
324
325 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
326 { \
327 wxString s(o,olen); \
328 s.Replace( olds, news, all ); \
329 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
330 }
1de532f5 331
2df0258e 332 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
1de532f5 333 _T("null\0dehar"), 10 );
7634e443 334
4629f07e 335 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
1de532f5 336 _T("null\0char"), 9 );
4629f07e 337
817d0578
VZ
338 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
339
7634e443
RN
340 #undef TEST_WXREPLACE
341 #undef TEST_NULLCHARREPLACE
1cd53e88
VS
342 #undef TEST_REPLACE
343}
344
345void StringTestCase::Match()
346{
347 #define TEST_MATCH( s1 , s2 , result ) \
348 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
349
350 TEST_MATCH( _T("foobar"), _T("foo*"), true );
351 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
352 TEST_MATCH( _T("foobar"), _T("*bar"), true );
353 TEST_MATCH( _T("foobar"), _T("??????"), true );
354 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
355 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
356 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
357 TEST_MATCH( _T("foobar"), _T("*foo"), false );
358 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
359 TEST_MATCH( _T(""), _T("*"), true );
360 TEST_MATCH( _T(""), _T("?"), false );
361
362 #undef TEST_MATCH
363}
364
bd7f096d
VS
365
366void StringTestCase::CaseChanges()
367{
368 wxString s1(_T("Hello!"));
369 wxString s1u(s1);
370 wxString s1l(s1);
371 s1u.MakeUpper();
372 s1l.MakeLower();
0c7db140
VZ
373
374 CPPUNIT_ASSERT_EQUAL( _T("HELLO!"), s1u );
375 CPPUNIT_ASSERT_EQUAL( _T("hello!"), s1l );
376
bd7f096d
VS
377 wxString s2u, s2l;
378 s2u.MakeUpper();
379 s2l.MakeLower();
380
0c7db140
VZ
381 CPPUNIT_ASSERT_EQUAL( "", s2u );
382 CPPUNIT_ASSERT_EQUAL( "", s2l );
383
384
385 wxString s3("good bye");
386 CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() );
387 s3.MakeCapitalized();
388 CPPUNIT_ASSERT_EQUAL( "Good bye", s3 );
389
390 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
391
392 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
bd7f096d 393}
dcb68102
RN
394
395void StringTestCase::Compare()
396{
397 wxString s1 = wxT("AHH");
398 wxString eq = wxT("AHH");
399 wxString neq1 = wxT("HAH");
400 wxString neq2 = wxT("AH");
401 wxString neq3 = wxT("AHHH");
402 wxString neq4 = wxT("AhH");
0c924034 403
dcb68102
RN
404 CPPUNIT_ASSERT( s1 == eq );
405 CPPUNIT_ASSERT( s1 != neq1 );
406 CPPUNIT_ASSERT( s1 != neq2 );
407 CPPUNIT_ASSERT( s1 != neq3 );
408 CPPUNIT_ASSERT( s1 != neq4 );
409
d7330233
VS
410 CPPUNIT_ASSERT( s1 == wxT("AHH") );
411 CPPUNIT_ASSERT( s1 != wxT("no") );
412 CPPUNIT_ASSERT( s1 < wxT("AZ") );
413 CPPUNIT_ASSERT( s1 <= wxT("AZ") );
414 CPPUNIT_ASSERT( s1 <= wxT("AHH") );
415 CPPUNIT_ASSERT( s1 > wxT("AA") );
416 CPPUNIT_ASSERT( s1 >= wxT("AA") );
417 CPPUNIT_ASSERT( s1 >= wxT("AHH") );
418
419 // test comparison with C strings in Unicode build (must work in ANSI as
420 // well, of course):
421 CPPUNIT_ASSERT( s1 == "AHH" );
422 CPPUNIT_ASSERT( s1 != "no" );
423 CPPUNIT_ASSERT( s1 < "AZ" );
424 CPPUNIT_ASSERT( s1 <= "AZ" );
425 CPPUNIT_ASSERT( s1 <= "AHH" );
426 CPPUNIT_ASSERT( s1 > "AA" );
427 CPPUNIT_ASSERT( s1 >= "AA" );
428 CPPUNIT_ASSERT( s1 >= "AHH" );
429
dcb68102
RN
430// wxString _s1 = wxT("A\0HH");
431// wxString _eq = wxT("A\0HH");
432// wxString _neq1 = wxT("H\0AH");
433// wxString _neq2 = wxT("A\0H");
434// wxString _neq3 = wxT("A\0HHH");
435// wxString _neq4 = wxT("A\0hH");
436 s1.insert(1,1,'\0');
437 eq.insert(1,1,'\0');
438 neq1.insert(1,1,'\0');
439 neq2.insert(1,1,'\0');
440 neq3.insert(1,1,'\0');
441 neq4.insert(1,1,'\0');
0c924034 442
dcb68102
RN
443 CPPUNIT_ASSERT( s1 == eq );
444 CPPUNIT_ASSERT( s1 != neq1 );
445 CPPUNIT_ASSERT( s1 != neq2 );
446 CPPUNIT_ASSERT( s1 != neq3 );
447 CPPUNIT_ASSERT( s1 != neq4 );
448}
449
450void StringTestCase::CompareNoCase()
451{
452 wxString s1 = wxT("AHH");
453 wxString eq = wxT("AHH");
454 wxString eq2 = wxT("AhH");
455 wxString eq3 = wxT("ahh");
456 wxString neq = wxT("HAH");
457 wxString neq2 = wxT("AH");
458 wxString neq3 = wxT("AHHH");
0c924034 459
dcb68102
RN
460 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
461 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
462
463 CPPUNIT_CNCEQ_ASSERT( s1, eq );
464 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
465 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
466
467 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
468 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
469 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
470
471
472// wxString _s1 = wxT("A\0HH");
473// wxString _eq = wxT("A\0HH");
474// wxString _eq2 = wxT("A\0hH");
475// wxString _eq3 = wxT("a\0hh");
476// wxString _neq = wxT("H\0AH");
477// wxString _neq2 = wxT("A\0H");
478// wxString _neq3 = wxT("A\0HHH");
0c924034 479
dcb68102
RN
480 s1.insert(1,1,'\0');
481 eq.insert(1,1,'\0');
482 eq2.insert(1,1,'\0');
483 eq3.insert(1,1,'\0');
484 neq.insert(1,1,'\0');
485 neq2.insert(1,1,'\0');
486 neq3.insert(1,1,'\0');
487
488 CPPUNIT_CNCEQ_ASSERT( s1, eq );
489 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
490 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
491
492 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
493 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
494 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
0c924034
WS
495}
496
c4f35063
VZ
497void StringTestCase::Contains()
498{
499 static const struct ContainsData
500 {
501 const wxChar *hay;
502 const wxChar *needle;
503 bool contains;
504 } containsData[] =
505 {
506 { _T(""), _T(""), true },
507 { _T(""), _T("foo"), false },
508 { _T("foo"), _T(""), true },
509 { _T("foo"), _T("f"), true },
510 { _T("foo"), _T("o"), true },
511 { _T("foo"), _T("oo"), true },
512 { _T("foo"), _T("ooo"), false },
513 { _T("foo"), _T("oooo"), false },
514 { _T("foo"), _T("fooo"), false },
515 };
516
517 for ( size_t n = 0; n < WXSIZEOF(containsData); n++ )
518 {
519 const ContainsData& cd = containsData[n];
520 CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) );
521 }
522}
523
d6718dd1
VZ
524// flags used in ToLongData.flags
525enum
4f7ee81a 526{
d6718dd1
VZ
527 Number_Ok = 0,
528 Number_Invalid = 1,
529 Number_Unsigned = 2, // if not specified, works for signed conversion
530 Number_Signed = 4, // if not specified, works for unsigned
531 Number_LongLong = 8, // only for long long tests
532 Number_Long = 16, // only for long tests
533};
534
535static const struct ToLongData
536{
537 const wxChar *str;
d6718dd1 538#ifdef wxLongLong_t
efc57671
VZ
539 wxLongLong_t value;
540#else
541 long value;
d6718dd1 542#endif // wxLongLong_t
d6718dd1
VZ
543 int flags;
544
efc57671
VZ
545 long LValue() const { return value; }
546 unsigned long ULValue() const { return value; }
547#ifdef wxLongLong_t
548 wxLongLong_t LLValue() const { return value; }
549 wxULongLong_t ULLValue() const { return (wxULongLong_t)value; }
550#endif // wxLongLong_t
551
d6718dd1
VZ
552 bool IsOk() const { return !(flags & Number_Invalid); }
553} longData[] =
554{
555 { _T("1"), 1, Number_Ok },
556 { _T("0"), 0, Number_Ok },
557 { _T("a"), 0, Number_Invalid },
558 { _T("12345"), 12345, Number_Ok },
559 { _T("--1"), 0, Number_Invalid },
560
561 { _T("-1"), -1, Number_Signed | Number_Long },
562 // this is surprizing but consistent with strtoul() behaviour
563 { _T("-1"), ULONG_MAX, Number_Unsigned | Number_Long },
564
565 // this must overflow, even with 64 bit long
566 { _T("922337203685477580711"), 0, Number_Invalid },
567
568#ifdef wxLongLong_t
569 { _T("2147483648"), wxLL(2147483648), Number_LongLong },
570 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed },
571 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong |
572 Number_Unsigned },
573#endif // wxLongLong_t
574};
575
576void StringTestCase::ToLong()
577{
c4f35063
VZ
578 long l;
579 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a
VZ
580 {
581 const ToLongData& ld = longData[n];
d6718dd1
VZ
582
583 if ( ld.flags & (Number_LongLong | Number_Unsigned) )
584 continue;
585
586 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) );
587 if ( ld.IsOk() )
efc57671 588 CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
4f7ee81a
VZ
589 }
590}
591
592void StringTestCase::ToULong()
593{
594 unsigned long ul;
d6718dd1 595 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 596 {
d6718dd1
VZ
597 const ToLongData& ld = longData[n];
598
599 if ( ld.flags & (Number_LongLong | Number_Signed) )
600 continue;
601
602 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) );
603 if ( ld.IsOk() )
efc57671 604 CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
d6718dd1
VZ
605 }
606}
607
608#ifdef wxLongLong_t
609
610void StringTestCase::ToLongLong()
611{
612 wxLongLong_t l;
613 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 614 {
d6718dd1 615 const ToLongData& ld = longData[n];
4f7ee81a 616
d6718dd1
VZ
617 if ( ld.flags & (Number_Long | Number_Unsigned) )
618 continue;
619
620 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) );
621 if ( ld.IsOk() )
efc57671 622 CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l );
d6718dd1
VZ
623 }
624}
625
626void StringTestCase::ToULongLong()
627{
628 wxULongLong_t ul;
629 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 630 {
d6718dd1
VZ
631 const ToLongData& ld = longData[n];
632
633 if ( ld.flags & (Number_Long | Number_Signed) )
634 continue;
635
636 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) );
637 if ( ld.IsOk() )
efc57671 638 CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul );
4f7ee81a
VZ
639 }
640}
641
d6718dd1
VZ
642#endif // wxLongLong_t
643
4f7ee81a
VZ
644void StringTestCase::ToDouble()
645{
646 double d;
647 static const struct ToDoubleData
648 {
649 const wxChar *str;
650 double value;
651 bool ok;
652 } doubleData[] =
653 {
654 { _T("1"), 1, true },
655 { _T("1.23"), 1.23, true },
656 { _T(".1"), .1, true },
657 { _T("1."), 1, true },
658 { _T("1.."), 0, false },
659 { _T("0"), 0, true },
660 { _T("a"), 0, false },
661 { _T("12345"), 12345, true },
662 { _T("-1"), -1, true },
663 { _T("--1"), 0, false },
664 };
665
666 // we need to use decimal point, not comma or whatever is its value for the
667 // current locale
10de049c 668 wxSetlocale(LC_ALL, "C");
4f7ee81a
VZ
669
670 size_t n;
671 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
672 {
673 const ToDoubleData& ld = doubleData[n];
674 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
675 if ( ld.ok )
676 CPPUNIT_ASSERT_EQUAL( ld.value, d );
677 }
678}
db419f1f 679
062dc5fc 680void StringTestCase::StringBuf()
db419f1f 681{
062dc5fc 682 // check that buffer can be used to write into the string
db419f1f
VZ
683 wxString s;
684 wxStrcpy(wxStringBuffer(s, 10), _T("foo"));
685
1de532f5 686 CPPUNIT_ASSERT_EQUAL(3, s.length());
c9f78968
VS
687 CPPUNIT_ASSERT(_T('f') == s[0u]);
688 CPPUNIT_ASSERT(_T('o') == s[1]);
689 CPPUNIT_ASSERT(_T('o') == s[2]);
db419f1f 690
062dc5fc
VZ
691 {
692 // also check that the buffer initially contains the original string
693 // contents
694 wxStringBuffer buf(s, 10);
695 CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] );
696 CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] );
697 CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] );
698 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] );
699 }
d8a4b666 700
db419f1f 701 {
d8a4b666 702 wxStringBufferLength buf(s, 10);
062dc5fc
VZ
703 CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] );
704 CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] );
705 CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] );
706 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] );
707
708 // and check that it can be used to write only the specified number of
709 // characters to the string
d8a4b666
VS
710 wxStrcpy(buf, _T("barrbaz"));
711 buf.SetLength(4);
712 }
db419f1f 713
1de532f5 714 CPPUNIT_ASSERT_EQUAL(4, s.length());
c9f78968
VS
715 CPPUNIT_ASSERT(_T('b') == s[0u]);
716 CPPUNIT_ASSERT(_T('a') == s[1]);
717 CPPUNIT_ASSERT(_T('r') == s[2]);
718 CPPUNIT_ASSERT(_T('r') == s[3]);
db419f1f 719
062dc5fc
VZ
720 // check that creating buffer of length smaller than string works, i.e. at
721 // least doesn't crash (it would if we naively copied the entire original
722 // string contents in the buffer)
723 *wxStringBuffer(s, 1) = '!';
db419f1f
VZ
724}
725
628f87da
VS
726void StringTestCase::UTF8Buf()
727{
728#if wxUSE_UNICODE
729 // "czech" in Czech ("cestina"):
730 static const char *textUTF8 = "\304\215e\305\241tina";
731 static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
732
733 wxString s;
734 wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8);
735 CPPUNIT_ASSERT(s == textUTF16);
736
737 {
738 wxUTF8StringBufferLength buf(s, 20);
739 wxStrcpy(buf, textUTF8);
740 buf.SetLength(5);
741 }
742 CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3));
743#endif // wxUSE_UNICODE
744}
745
4ca056ea
VS
746
747
ef0f1387 748void StringTestCase::CStrDataTernaryOperator()
4ca056ea 749{
ef0f1387
VS
750 DoCStrDataTernaryOperator(true);
751 DoCStrDataTernaryOperator(false);
4ca056ea
VS
752}
753
754template<typename T> bool CheckStr(const wxString& expected, T s)
755{
756 return expected == wxString(s);
757}
758
ef0f1387 759void StringTestCase::DoCStrDataTernaryOperator(bool cond)
4ca056ea
VS
760{
761 // test compilation of wxCStrData when used with operator?: (the asserts
762 // are not very important, we're testing if the code compiles at all):
763
764 wxString s("foo");
4ca056ea 765
abc505b4 766 const wchar_t *wcStr = L"foo";
4ca056ea 767 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) );
9ffb659a 768 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) );
4ca056ea 769 CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) );
9ffb659a 770 CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) );
11aac4ba 771
9b59b90c 772 const char *mbStr = "foo";
4ca056ea
VS
773 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) );
774 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) );
775 CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) );
776 CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) );
92258cc1
VS
777
778 wxString empty("");
779 CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) );
780 CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) );
4ca056ea 781}
ef0f1387 782
3a69bca1
VS
783void StringTestCase::CStrDataOperators()
784{
785 wxString s("hello");
786
787 CPPUNIT_ASSERT( s.c_str()[0] == 'h' );
788 CPPUNIT_ASSERT( s.c_str()[1] == 'e' );
789 CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
790
791 CPPUNIT_ASSERT( *s.c_str() == 'h' );
792 CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' );
793 CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
794}
795
ef0f1387
VS
796bool CheckStrChar(const wxString& expected, char *s)
797 { return CheckStr(expected, s); }
798bool CheckStrWChar(const wxString& expected, wchar_t *s)
799 { return CheckStr(expected, s); }
800bool CheckStrConstChar(const wxString& expected, const char *s)
801 { return CheckStr(expected, s); }
802bool CheckStrConstWChar(const wxString& expected, const wchar_t *s)
803 { return CheckStr(expected, s); }
804
805void StringTestCase::CStrDataImplicitConversion()
806{
807 wxString s("foo");
808
ef0f1387 809 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) );
ef0f1387 810 CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) );
7978bc72
VS
811
812 // implicit conversion of wxString is not available in STL build
813#if !wxUSE_STL
814 CPPUNIT_ASSERT( CheckStrConstWChar(s, s) );
ef0f1387 815 CPPUNIT_ASSERT( CheckStrConstChar(s, s) );
7978bc72 816#endif
ef0f1387
VS
817}
818
819void StringTestCase::ExplicitConversion()
820{
821 wxString s("foo");
822
823 CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) );
824 CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) );
825 CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) );
826
827 CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) );
828 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) );
829 CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) );
830}
6bd4f281
VS
831
832void StringTestCase::IndexedAccess()
833{
834 wxString s("bar");
835 CPPUNIT_ASSERT_EQUAL( 'r', s[2] );
836
837 // this tests for a possible bug in UTF-8 based wxString implementation:
838 // the 3rd character of the underlying byte string is going to change, but
839 // the 3rd character of wxString should remain the same
840 s[0] = L'\u00e9';
841 CPPUNIT_ASSERT_EQUAL( 'r', s[2] );
842}
843