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