]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/strings.cpp
Add wxDocument::Activate() method.
[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 );
bb0907b7 38 CPPUNIT_TEST( FormatUnicode );
1cd53e88 39 CPPUNIT_TEST( Constructors );
f312f981 40 CPPUNIT_TEST( StaticConstructors );
1cd53e88 41 CPPUNIT_TEST( Extraction );
e6a99197 42 CPPUNIT_TEST( Trim );
1cd53e88 43 CPPUNIT_TEST( Find );
1cd53e88
VS
44 CPPUNIT_TEST( Replace );
45 CPPUNIT_TEST( Match );
bd7f096d 46 CPPUNIT_TEST( CaseChanges );
dcb68102
RN
47 CPPUNIT_TEST( Compare );
48 CPPUNIT_TEST( CompareNoCase );
c4f35063 49 CPPUNIT_TEST( Contains );
4f7ee81a
VZ
50 CPPUNIT_TEST( ToLong );
51 CPPUNIT_TEST( ToULong );
d6718dd1
VZ
52#ifdef wxLongLong_t
53 CPPUNIT_TEST( ToLongLong );
54 CPPUNIT_TEST( ToULongLong );
55#endif // wxLongLong_t
4f7ee81a 56 CPPUNIT_TEST( ToDouble );
951201d8 57 CPPUNIT_TEST( FromDouble );
062dc5fc 58 CPPUNIT_TEST( StringBuf );
628f87da 59 CPPUNIT_TEST( UTF8Buf );
ef0f1387 60 CPPUNIT_TEST( CStrDataTernaryOperator );
3a69bca1 61 CPPUNIT_TEST( CStrDataOperators );
ef0f1387
VS
62 CPPUNIT_TEST( CStrDataImplicitConversion );
63 CPPUNIT_TEST( ExplicitConversion );
6bd4f281 64 CPPUNIT_TEST( IndexedAccess );
c565abe1 65 CPPUNIT_TEST( BeforeAndAfter );
de4983f3 66 CPPUNIT_TEST( ScopedBuffers );
1cd53e88
VS
67 CPPUNIT_TEST_SUITE_END();
68
69 void String();
70 void PChar();
71 void Format();
bb0907b7 72 void FormatUnicode();
1cd53e88 73 void Constructors();
f312f981 74 void StaticConstructors();
1cd53e88 75 void Extraction();
e6a99197 76 void Trim();
1cd53e88 77 void Find();
1cd53e88
VS
78 void Replace();
79 void Match();
bd7f096d 80 void CaseChanges();
dcb68102
RN
81 void Compare();
82 void CompareNoCase();
c4f35063 83 void Contains();
4f7ee81a
VZ
84 void ToLong();
85 void ToULong();
d6718dd1
VZ
86#ifdef wxLongLong_t
87 void ToLongLong();
88 void ToULongLong();
89#endif // wxLongLong_t
4f7ee81a 90 void ToDouble();
951201d8 91 void FromDouble();
062dc5fc 92 void StringBuf();
628f87da 93 void UTF8Buf();
ef0f1387
VS
94 void CStrDataTernaryOperator();
95 void DoCStrDataTernaryOperator(bool cond);
3a69bca1 96 void CStrDataOperators();
ef0f1387
VS
97 void CStrDataImplicitConversion();
98 void ExplicitConversion();
6bd4f281 99 void IndexedAccess();
c565abe1 100 void BeforeAndAfter();
de4983f3 101 void ScopedBuffers();
1cd53e88
VS
102
103 DECLARE_NO_COPY_CLASS(StringTestCase)
104};
105
106// register in the unnamed registry so that these tests are run by default
107CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
108
e3778b4d 109// also include in its own registry so that these tests can be run alone
1cd53e88
VS
110CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
111
112StringTestCase::StringTestCase()
113{
114}
115
116void StringTestCase::String()
117{
118 wxString a, b, c;
119
120 a.reserve (128);
121 b.reserve (128);
122 c.reserve (128);
123
124 for (int i = 0; i < 2; ++i)
125 {
9a83f860
VZ
126 a = wxT("Hello");
127 b = wxT(" world");
128 c = wxT("! How'ya doin'?");
1cd53e88
VS
129 a += b;
130 a += c;
9a83f860 131 c = wxT("Hello world! What's up?");
1cd53e88
VS
132 CPPUNIT_ASSERT( c != a );
133 }
134}
135
136void StringTestCase::PChar()
137{
138 wxChar a [128];
139 wxChar b [128];
140 wxChar c [128];
141
142 for (int i = 0; i < 2; ++i)
143 {
9a83f860
VZ
144 wxStrcpy (a, wxT("Hello"));
145 wxStrcpy (b, wxT(" world"));
146 wxStrcpy (c, wxT("! How'ya doin'?"));
1cd53e88
VS
147 wxStrcat (a, b);
148 wxStrcat (a, c);
9a83f860 149 wxStrcpy (c, wxT("Hello world! What's up?"));
1cd53e88
VS
150 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
151 }
152}
153
154void StringTestCase::Format()
155{
156 wxString s1,s2;
9a83f860
VZ
157 s1.Printf(wxT("%03d"), 18);
158 CPPUNIT_ASSERT( s1 == wxString::Format(wxT("%03d"), 18) );
159 s2.Printf(wxT("Number 18: %s\n"), s1.c_str());
160 CPPUNIT_ASSERT( s2 == wxString::Format(wxT("Number 18: %s\n"), s1.c_str()) );
b03cd7e6
VZ
161
162 static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
163 for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
164 {
165 const size_t len = lengths[n];
166
9a83f860
VZ
167 wxString s(wxT('Z'), len);
168 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(wxT("%s"), s.c_str()).length());
b03cd7e6 169 }
75717cb1
VZ
170
171
172 CPPUNIT_ASSERT_EQUAL
173 (
174 "two one",
175 wxString::Format(wxT("%2$s %1$s"), wxT("one"), wxT("two"))
176 );
1cd53e88
VS
177}
178
bb0907b7
VS
179void StringTestCase::FormatUnicode()
180{
181#if wxUSE_UNICODE
182 const char *UNICODE_STR = "Iestat\xC4\xAB %i%i";
183 //const char *UNICODE_STR = "Iestat\xCC\x84 %i%i";
184
185 wxString fmt = wxString::FromUTF8(UNICODE_STR);
186 wxString s = wxString::Format(fmt, 1, 1);
187 wxString expected(fmt);
188 expected.Replace("%i", "1");
189 CPPUNIT_ASSERT_EQUAL( expected, s );
190#endif // wxUSE_UNICODE
191}
192
1cd53e88
VS
193void StringTestCase::Constructors()
194{
1de532f5
VZ
195 CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) );
196 CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') );
197 CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) );
198 CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) );
199 CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) );
f312f981
VZ
200
201#if wxUSE_UNICODE
1de532f5
VZ
202 CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) );
203 CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') );
204 CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) );
205 CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) );
206 CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) );
f312f981
VZ
207#endif // wxUSE_UNICODE
208
bd42a07c
VZ
209 CPPUNIT_ASSERT_EQUAL( 0, wxString(wxString(), 17).length() );
210
39d16963
VZ
211#if wxUSE_UNICODE_UTF8
212 // This string has 3 characters (<h>, <e'> and <l>), not 4 when using UTF-8
213 // locale!
214 if ( wxConvLibc.IsUTF8() )
215 {
216 wxString s3("h\xc3\xa9llo", 4);
217 CPPUNIT_ASSERT_EQUAL( 3, s3.length() );
218 CPPUNIT_ASSERT_EQUAL( 'l', (char)s3[2] );
219 }
220#endif // wxUSE_UNICODE_UTF8
556a836e
VZ
221
222
f312f981
VZ
223 static const char *s = "?really!";
224 const char *start = wxStrchr(s, 'r');
225 const char *end = wxStrchr(s, '!');
1de532f5 226 CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) );
7652a41d
VS
227
228 // test if creating string from NULL C pointer works:
1de532f5 229 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) );
1cd53e88
VS
230}
231
f312f981
VZ
232void StringTestCase::StaticConstructors()
233{
1de532f5
VZ
234 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
235 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
236 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
237 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
238 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
f312f981
VZ
239
240 // FIXME: this doesn't work currently but should!
1de532f5 241 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
f312f981
VZ
242
243
1de532f5
VZ
244 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
245 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
246 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
247 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
248 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
f312f981 249
556a836e
VZ
250 CPPUNIT_ASSERT_EQUAL( 2, wxString::FromUTF8("h\xc3\xa9llo", 3).length() );
251
252
1de532f5 253 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
f312f981 254}
265d5cce 255
1cd53e88
VS
256void StringTestCase::Extraction()
257{
9a83f860 258 wxString s(wxT("Hello, world!"));
1cd53e88 259
9a83f860
VZ
260 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , wxT("Hello, world!") ) == 0 );
261 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , wxT("Hello") ) == 0 );
262 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , wxT("world!") ) == 0 );
263 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 );
264 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , wxT("lo, world!") ) == 0 );
265 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , wxT("lo, w") ) == 0 );
266 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , wxT("lo, world!") ) == 0 );
1cd53e88 267
478cbb08
VS
268#if wxUSE_UNICODE
269 static const char *germanUTF8 = "Oberfl\303\244che";
270 wxString strUnicode(wxString::FromUTF8(germanUTF8));
271
272 CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode );
273 CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" );
274#endif // wxUSE_UNICODE
275
1cd53e88
VS
276 wxString rest;
277
3affcd07
VZ
278 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
279 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
280 if ( result ) \
1de532f5 281 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
1cd53e88 282
9a83f860
VZ
283 TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true );
284 TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true );
285 TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true );
286 TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false );
287 TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true );
288 TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false );
289 TEST_STARTS_WITH( wxT("Hi"), wxT(""), false );
1cd53e88
VS
290
291 #undef TEST_STARTS_WITH
3affcd07 292
c8f313b2
VZ
293 rest = "Hello world";
294 CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) );
1de532f5 295 CPPUNIT_ASSERT_EQUAL("world", rest);
c8f313b2 296
3affcd07
VZ
297 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
298 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
299 if ( result ) \
1de532f5 300 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
3affcd07 301
9a83f860
VZ
302 TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true );
303 TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true );
304 TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true );
305 TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true );
306 TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true );
307 TEST_ENDS_WITH( wxT("very long string"), wxT(""), false );
308 TEST_ENDS_WITH( wxT("?"), wxT(""), false );
309 TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false );
310 TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false );
3affcd07
VZ
311
312 #undef TEST_ENDS_WITH
1cd53e88
VS
313}
314
e6a99197
WS
315void StringTestCase::Trim()
316{
317 #define TEST_TRIM( str , dir , result ) \
318 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
319
9a83f860
VZ
320 TEST_TRIM( wxT(" Test "), true, wxT(" Test") );
321 TEST_TRIM( wxT(" "), true, wxT("") );
322 TEST_TRIM( wxT(" "), true, wxT("") );
323 TEST_TRIM( wxT(""), true, wxT("") );
e6a99197 324
9a83f860
VZ
325 TEST_TRIM( wxT(" Test "), false, wxT("Test ") );
326 TEST_TRIM( wxT(" "), false, wxT("") );
327 TEST_TRIM( wxT(" "), false, wxT("") );
328 TEST_TRIM( wxT(""), false, wxT("") );
e6a99197
WS
329
330 #undef TEST_TRIM
331}
332
1cd53e88
VS
333void StringTestCase::Find()
334{
335 #define TEST_FIND( str , start , result ) \
9a83f860 336 CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
1cd53e88 337
9a83f860
VZ
338 TEST_FIND( wxT("Well, hello world"), 0, 1 );
339 TEST_FIND( wxT("Well, hello world"), 6, 7 );
340 TEST_FIND( wxT("Well, hello world"), 9, wxString::npos );
1cd53e88
VS
341
342 #undef TEST_FIND
343}
344
1cd53e88
VS
345void StringTestCase::Replace()
346{
347 #define TEST_REPLACE( original , pos , len , replacement , result ) \
348 { \
349 wxString s = original; \
350 s.replace( pos , len , replacement ); \
1de532f5 351 CPPUNIT_ASSERT_EQUAL( result, s ); \
1cd53e88
VS
352 }
353
9a83f860
VZ
354 TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") );
355 TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") );
356 TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") );
357 TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") );
358 TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") );
1cd53e88 359
1de532f5 360
7634e443
RN
361 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
362 { \
363 wxString s(o,olen); \
364 s.replace( pos , len , replacement ); \
072682ce 365 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
7634e443 366 }
1de532f5 367
9a83f860
VZ
368 TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
369 wxT("null\0dhar"), 9 );
7634e443
RN
370
371 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
372 { \
373 wxString s(o,olen); \
374 s.Replace( olds, news, all ); \
072682ce 375 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
7634e443 376 }
1de532f5 377
9a83f860
VZ
378 TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
379 wxT("null\0dehar"), 10 );
7634e443 380
9a83f860
VZ
381 TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
382 wxT("null\0char"), 9 );
4629f07e 383
817d0578 384 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
072682ce
VZ
385 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
386 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
387 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
388 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
817d0578 389
7634e443
RN
390 #undef TEST_WXREPLACE
391 #undef TEST_NULLCHARREPLACE
1cd53e88
VS
392 #undef TEST_REPLACE
393}
394
395void StringTestCase::Match()
396{
397 #define TEST_MATCH( s1 , s2 , result ) \
398 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
399
9a83f860
VZ
400 TEST_MATCH( wxT("foobar"), wxT("foo*"), true );
401 TEST_MATCH( wxT("foobar"), wxT("*oo*"), true );
402 TEST_MATCH( wxT("foobar"), wxT("*bar"), true );
403 TEST_MATCH( wxT("foobar"), wxT("??????"), true );
404 TEST_MATCH( wxT("foobar"), wxT("f??b*"), true );
405 TEST_MATCH( wxT("foobar"), wxT("f?b*"), false );
406 TEST_MATCH( wxT("foobar"), wxT("*goo*"), false );
407 TEST_MATCH( wxT("foobar"), wxT("*foo"), false );
408 TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true );
409 TEST_MATCH( wxT(""), wxT("*"), true );
410 TEST_MATCH( wxT(""), wxT("?"), false );
1cd53e88
VS
411
412 #undef TEST_MATCH
413}
414
bd7f096d
VS
415
416void StringTestCase::CaseChanges()
417{
9a83f860 418 wxString s1(wxT("Hello!"));
bd7f096d
VS
419 wxString s1u(s1);
420 wxString s1l(s1);
421 s1u.MakeUpper();
422 s1l.MakeLower();
0c7db140 423
9a83f860
VZ
424 CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u );
425 CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l );
0c7db140 426
bd7f096d
VS
427 wxString s2u, s2l;
428 s2u.MakeUpper();
429 s2l.MakeLower();
430
0c7db140
VZ
431 CPPUNIT_ASSERT_EQUAL( "", s2u );
432 CPPUNIT_ASSERT_EQUAL( "", s2l );
433
434
435 wxString s3("good bye");
436 CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() );
437 s3.MakeCapitalized();
438 CPPUNIT_ASSERT_EQUAL( "Good bye", s3 );
439
440 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
441
442 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
bd7f096d 443}
dcb68102
RN
444
445void StringTestCase::Compare()
446{
447 wxString s1 = wxT("AHH");
448 wxString eq = wxT("AHH");
449 wxString neq1 = wxT("HAH");
450 wxString neq2 = wxT("AH");
451 wxString neq3 = wxT("AHHH");
452 wxString neq4 = wxT("AhH");
0c924034 453
dcb68102
RN
454 CPPUNIT_ASSERT( s1 == eq );
455 CPPUNIT_ASSERT( s1 != neq1 );
456 CPPUNIT_ASSERT( s1 != neq2 );
457 CPPUNIT_ASSERT( s1 != neq3 );
458 CPPUNIT_ASSERT( s1 != neq4 );
459
d7330233
VS
460 CPPUNIT_ASSERT( s1 == wxT("AHH") );
461 CPPUNIT_ASSERT( s1 != wxT("no") );
462 CPPUNIT_ASSERT( s1 < wxT("AZ") );
463 CPPUNIT_ASSERT( s1 <= wxT("AZ") );
464 CPPUNIT_ASSERT( s1 <= wxT("AHH") );
465 CPPUNIT_ASSERT( s1 > wxT("AA") );
466 CPPUNIT_ASSERT( s1 >= wxT("AA") );
467 CPPUNIT_ASSERT( s1 >= wxT("AHH") );
468
469 // test comparison with C strings in Unicode build (must work in ANSI as
470 // well, of course):
471 CPPUNIT_ASSERT( s1 == "AHH" );
472 CPPUNIT_ASSERT( s1 != "no" );
473 CPPUNIT_ASSERT( s1 < "AZ" );
474 CPPUNIT_ASSERT( s1 <= "AZ" );
475 CPPUNIT_ASSERT( s1 <= "AHH" );
476 CPPUNIT_ASSERT( s1 > "AA" );
477 CPPUNIT_ASSERT( s1 >= "AA" );
478 CPPUNIT_ASSERT( s1 >= "AHH" );
479
dcb68102
RN
480// wxString _s1 = wxT("A\0HH");
481// wxString _eq = wxT("A\0HH");
482// wxString _neq1 = wxT("H\0AH");
483// wxString _neq2 = wxT("A\0H");
484// wxString _neq3 = wxT("A\0HHH");
485// wxString _neq4 = wxT("A\0hH");
486 s1.insert(1,1,'\0');
487 eq.insert(1,1,'\0');
488 neq1.insert(1,1,'\0');
489 neq2.insert(1,1,'\0');
490 neq3.insert(1,1,'\0');
491 neq4.insert(1,1,'\0');
0c924034 492
dcb68102
RN
493 CPPUNIT_ASSERT( s1 == eq );
494 CPPUNIT_ASSERT( s1 != neq1 );
495 CPPUNIT_ASSERT( s1 != neq2 );
496 CPPUNIT_ASSERT( s1 != neq3 );
497 CPPUNIT_ASSERT( s1 != neq4 );
5858fe68
VZ
498
499 CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
500 CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
501 CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
dcb68102
RN
502}
503
504void StringTestCase::CompareNoCase()
505{
506 wxString s1 = wxT("AHH");
507 wxString eq = wxT("AHH");
508 wxString eq2 = wxT("AhH");
509 wxString eq3 = wxT("ahh");
510 wxString neq = wxT("HAH");
511 wxString neq2 = wxT("AH");
512 wxString neq3 = wxT("AHHH");
0c924034 513
dcb68102
RN
514 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
515 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
516
517 CPPUNIT_CNCEQ_ASSERT( s1, eq );
518 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
519 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
520
521 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
522 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
523 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
524
525
526// wxString _s1 = wxT("A\0HH");
527// wxString _eq = wxT("A\0HH");
528// wxString _eq2 = wxT("A\0hH");
529// wxString _eq3 = wxT("a\0hh");
530// wxString _neq = wxT("H\0AH");
531// wxString _neq2 = wxT("A\0H");
532// wxString _neq3 = wxT("A\0HHH");
0c924034 533
dcb68102
RN
534 s1.insert(1,1,'\0');
535 eq.insert(1,1,'\0');
536 eq2.insert(1,1,'\0');
537 eq3.insert(1,1,'\0');
538 neq.insert(1,1,'\0');
539 neq2.insert(1,1,'\0');
540 neq3.insert(1,1,'\0');
541
542 CPPUNIT_CNCEQ_ASSERT( s1, eq );
543 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
544 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
545
546 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
547 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
548 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
5858fe68
VZ
549
550 CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
551 CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
552 CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
0c924034
WS
553}
554
c4f35063
VZ
555void StringTestCase::Contains()
556{
557 static const struct ContainsData
558 {
559 const wxChar *hay;
560 const wxChar *needle;
561 bool contains;
562 } containsData[] =
563 {
9a83f860
VZ
564 { wxT(""), wxT(""), true },
565 { wxT(""), wxT("foo"), false },
566 { wxT("foo"), wxT(""), true },
567 { wxT("foo"), wxT("f"), true },
568 { wxT("foo"), wxT("o"), true },
569 { wxT("foo"), wxT("oo"), true },
570 { wxT("foo"), wxT("ooo"), false },
571 { wxT("foo"), wxT("oooo"), false },
572 { wxT("foo"), wxT("fooo"), false },
c4f35063
VZ
573 };
574
575 for ( size_t n = 0; n < WXSIZEOF(containsData); n++ )
576 {
577 const ContainsData& cd = containsData[n];
578 CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) );
579 }
580}
581
d6718dd1
VZ
582// flags used in ToLongData.flags
583enum
4f7ee81a 584{
d6718dd1
VZ
585 Number_Ok = 0,
586 Number_Invalid = 1,
587 Number_Unsigned = 2, // if not specified, works for signed conversion
588 Number_Signed = 4, // if not specified, works for unsigned
589 Number_LongLong = 8, // only for long long tests
93a800a9 590 Number_Long = 16 // only for long tests
d6718dd1
VZ
591};
592
593static const struct ToLongData
594{
595 const wxChar *str;
d6718dd1 596#ifdef wxLongLong_t
efc57671
VZ
597 wxLongLong_t value;
598#else
599 long value;
d6718dd1 600#endif // wxLongLong_t
d6718dd1 601 int flags;
8421cb3c 602 int base;
d6718dd1 603
efc57671
VZ
604 long LValue() const { return value; }
605 unsigned long ULValue() const { return value; }
606#ifdef wxLongLong_t
607 wxLongLong_t LLValue() const { return value; }
608 wxULongLong_t ULLValue() const { return (wxULongLong_t)value; }
609#endif // wxLongLong_t
610
d6718dd1
VZ
611 bool IsOk() const { return !(flags & Number_Invalid); }
612} longData[] =
613{
9a83f860
VZ
614 { wxT("1"), 1, Number_Ok },
615 { wxT("0"), 0, Number_Ok },
616 { wxT("a"), 0, Number_Invalid },
617 { wxT("12345"), 12345, Number_Ok },
618 { wxT("--1"), 0, Number_Invalid },
d6718dd1 619
9a83f860 620 { wxT("-1"), -1, Number_Signed | Number_Long },
cbab1556 621 // this is surprising but consistent with strtoul() behaviour
9a83f860 622 { wxT("-1"), ULONG_MAX, Number_Unsigned | Number_Long },
d6718dd1
VZ
623
624 // this must overflow, even with 64 bit long
9a83f860 625 { wxT("922337203685477580711"), 0, Number_Invalid },
d6718dd1
VZ
626
627#ifdef wxLongLong_t
9a83f860
VZ
628 { wxT("2147483648"), wxLL(2147483648), Number_LongLong },
629 { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed },
630 { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong |
d6718dd1
VZ
631 Number_Unsigned },
632#endif // wxLongLong_t
8421cb3c
VZ
633
634 // Base tests.
635 { wxT("010"), 10, Number_Ok, 10 },
636 { wxT("010"), 8, Number_Ok, 0 },
637 { wxT("010"), 8, Number_Ok, 8 },
638 { wxT("010"), 16, Number_Ok, 16 },
639
640 { wxT("0010"), 10, Number_Ok, 10 },
641 { wxT("0010"), 8, Number_Ok, 0 },
642 { wxT("0010"), 8, Number_Ok, 8 },
643 { wxT("0010"), 16, Number_Ok, 16 },
644
645 { wxT("0x11"), 0, Number_Invalid, 10 },
646 { wxT("0x11"), 17, Number_Ok, 0 },
647 { wxT("0x11"), 0, Number_Invalid, 8 },
648 { wxT("0x11"), 17, Number_Ok, 16 },
d6718dd1
VZ
649};
650
651void StringTestCase::ToLong()
652{
c4f35063
VZ
653 long l;
654 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a
VZ
655 {
656 const ToLongData& ld = longData[n];
d6718dd1
VZ
657
658 if ( ld.flags & (Number_LongLong | Number_Unsigned) )
659 continue;
1a39b013 660
529e491c
FM
661 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
662 // should behave the same for our test data set:
663
8421cb3c
VZ
664 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
665 wxString(ld.str).ToCLong(&l, ld.base) );
529e491c
FM
666 if ( ld.IsOk() )
667 CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
d6718dd1 668
8421cb3c
VZ
669 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
670 wxString(ld.str).ToLong(&l, ld.base) );
d6718dd1 671 if ( ld.IsOk() )
efc57671 672 CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
4f7ee81a 673 }
69d31e31
VZ
674
675 // special case: check that the output is not modified if the parsing
676 // failed completely
677 l = 17;
678 CPPUNIT_ASSERT( !wxString("foo").ToLong(&l) );
679 CPPUNIT_ASSERT_EQUAL( 17, l );
680
681 // also check that it is modified if we did parse something successfully in
682 // the beginning of the string
683 CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l) );
684 CPPUNIT_ASSERT_EQUAL( 9, l );
4f7ee81a
VZ
685}
686
687void StringTestCase::ToULong()
688{
689 unsigned long ul;
d6718dd1 690 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 691 {
d6718dd1
VZ
692 const ToLongData& ld = longData[n];
693
694 if ( ld.flags & (Number_LongLong | Number_Signed) )
695 continue;
696
529e491c
FM
697 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
698 // should behave the same for our test data set:
699
8421cb3c
VZ
700 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
701 wxString(ld.str).ToCULong(&ul, ld.base) );
529e491c
FM
702 if ( ld.IsOk() )
703 CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
1a39b013 704
8421cb3c
VZ
705 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
706 wxString(ld.str).ToULong(&ul, ld.base) );
d6718dd1 707 if ( ld.IsOk() )
efc57671 708 CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
d6718dd1
VZ
709 }
710}
711
712#ifdef wxLongLong_t
713
714void StringTestCase::ToLongLong()
715{
716 wxLongLong_t l;
717 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 718 {
d6718dd1 719 const ToLongData& ld = longData[n];
4f7ee81a 720
d6718dd1
VZ
721 if ( ld.flags & (Number_Long | Number_Unsigned) )
722 continue;
723
8421cb3c
VZ
724 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
725 wxString(ld.str).ToLongLong(&l, ld.base) );
d6718dd1 726 if ( ld.IsOk() )
efc57671 727 CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l );
d6718dd1
VZ
728 }
729}
730
731void StringTestCase::ToULongLong()
732{
733 wxULongLong_t ul;
734 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 735 {
d6718dd1
VZ
736 const ToLongData& ld = longData[n];
737
738 if ( ld.flags & (Number_Long | Number_Signed) )
739 continue;
740
8421cb3c
VZ
741 CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
742 wxString(ld.str).ToULongLong(&ul, ld.base) );
d6718dd1 743 if ( ld.IsOk() )
efc57671 744 CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul );
4f7ee81a
VZ
745 }
746}
747
d6718dd1
VZ
748#endif // wxLongLong_t
749
4f7ee81a
VZ
750void StringTestCase::ToDouble()
751{
752 double d;
753 static const struct ToDoubleData
754 {
755 const wxChar *str;
756 double value;
757 bool ok;
758 } doubleData[] =
759 {
9a83f860
VZ
760 { wxT("1"), 1, true },
761 { wxT("1.23"), 1.23, true },
762 { wxT(".1"), .1, true },
763 { wxT("1."), 1, true },
764 { wxT("1.."), 0, false },
765 { wxT("0"), 0, true },
766 { wxT("a"), 0, false },
767 { wxT("12345"), 12345, true },
768 { wxT("-1"), -1, true },
769 { wxT("--1"), 0, false },
770 { wxT("-3E-5"), -3E-5, true },
771 { wxT("-3E-abcde5"), 0, false },
4f7ee81a
VZ
772 };
773
529e491c 774 // test ToCDouble() first:
4f7ee81a
VZ
775
776 size_t n;
777 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
778 {
779 const ToDoubleData& ld = doubleData[n];
529e491c
FM
780 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToCDouble(&d) );
781 if ( ld.ok )
782 CPPUNIT_ASSERT_EQUAL( ld.value, d );
783 }
784
785
786 // test ToDouble() now:
1a39b013 787 // NOTE: for the test to be reliable, we need to set the locale explicitly
529e491c
FM
788 // so that we know the decimal point character to use
789
790 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH))
791 return; // you should have french support installed to continue this test!
792
1a39b013
VZ
793 wxLocale locale;
794
529e491c 795 // don't load default catalog, it may be unavailable:
1a39b013
VZ
796 CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) );
797
529e491c
FM
798 static const struct ToDoubleData doubleData2[] =
799 {
9a83f860
VZ
800 { wxT("1"), 1, true },
801 { wxT("1,23"), 1.23, true },
802 { wxT(",1"), .1, true },
803 { wxT("1,"), 1, true },
804 { wxT("1,,"), 0, false },
805 { wxT("0"), 0, true },
806 { wxT("a"), 0, false },
807 { wxT("12345"), 12345, true },
808 { wxT("-1"), -1, true },
809 { wxT("--1"), 0, false },
810 { wxT("-3E-5"), -3E-5, true },
811 { wxT("-3E-abcde5"), 0, false },
529e491c
FM
812 };
813
814 for ( n = 0; n < WXSIZEOF(doubleData2); n++ )
815 {
816 const ToDoubleData& ld = doubleData2[n];
4f7ee81a
VZ
817 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
818 if ( ld.ok )
819 CPPUNIT_ASSERT_EQUAL( ld.value, d );
820 }
821}
db419f1f 822
951201d8
VZ
823void StringTestCase::FromDouble()
824{
825 static const struct FromDoubleTestData
826 {
827 double value;
fd3a4cb9 828 int prec;
951201d8
VZ
829 const char *str;
830 } testData[] =
831 {
fd3a4cb9 832 { 1.23, -1, "1.23" },
ffcd6cc6
FM
833 // NB: there are no standards about the minimum exponent width
834 // and newer MSVC versions use 3 digits as minimum exponent
835 // width while GNU libc uses 2 digits as minimum width...
43f8864b 836#ifdef wxUSING_VC_CRT_IO
fd3a4cb9 837 { -3e-10, -1, "-3e-010" },
ffcd6cc6 838#else
fd3a4cb9 839 { -3e-10, -1, "-3e-10" },
ffcd6cc6 840#endif
fd3a4cb9
VZ
841 { -0.45678, -1, "-0.45678" },
842 { 1.2345678, 0, "1" },
843 { 1.2345678, 1, "1.2" },
844 { 1.2345678, 2, "1.23" },
845 { 1.2345678, 3, "1.235" },
951201d8
VZ
846 };
847
848 for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
849 {
850 const FromDoubleTestData& td = testData[n];
fd3a4cb9 851 CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value, td.prec) );
951201d8
VZ
852 }
853
854 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
855 return;
856
857 wxLocale locale;
858 CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) );
859
860 for ( unsigned m = 0; m < WXSIZEOF(testData); m++ )
861 {
862 const FromDoubleTestData& td = testData[m];
863
864 wxString str(td.str);
865 str.Replace(".", ",");
fd3a4cb9 866 CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );
951201d8
VZ
867 }
868}
869
062dc5fc 870void StringTestCase::StringBuf()
db419f1f 871{
062dc5fc 872 // check that buffer can be used to write into the string
db419f1f 873 wxString s;
9a83f860 874 wxStrcpy(wxStringBuffer(s, 10), wxT("foo"));
db419f1f 875
1de532f5 876 CPPUNIT_ASSERT_EQUAL(3, s.length());
9a83f860
VZ
877 CPPUNIT_ASSERT(wxT('f') == s[0u]);
878 CPPUNIT_ASSERT(wxT('o') == s[1]);
879 CPPUNIT_ASSERT(wxT('o') == s[2]);
db419f1f 880
062dc5fc
VZ
881 {
882 // also check that the buffer initially contains the original string
883 // contents
884 wxStringBuffer buf(s, 10);
9a83f860
VZ
885 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] );
886 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] );
887 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] );
888 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] );
062dc5fc 889 }
d8a4b666 890
db419f1f 891 {
d8a4b666 892 wxStringBufferLength buf(s, 10);
9a83f860
VZ
893 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] );
894 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] );
895 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] );
896 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] );
062dc5fc
VZ
897
898 // and check that it can be used to write only the specified number of
899 // characters to the string
9a83f860 900 wxStrcpy(buf, wxT("barrbaz"));
d8a4b666
VS
901 buf.SetLength(4);
902 }
db419f1f 903
1de532f5 904 CPPUNIT_ASSERT_EQUAL(4, s.length());
9a83f860
VZ
905 CPPUNIT_ASSERT(wxT('b') == s[0u]);
906 CPPUNIT_ASSERT(wxT('a') == s[1]);
907 CPPUNIT_ASSERT(wxT('r') == s[2]);
908 CPPUNIT_ASSERT(wxT('r') == s[3]);
db419f1f 909
062dc5fc
VZ
910 // check that creating buffer of length smaller than string works, i.e. at
911 // least doesn't crash (it would if we naively copied the entire original
912 // string contents in the buffer)
913 *wxStringBuffer(s, 1) = '!';
db419f1f
VZ
914}
915
628f87da
VS
916void StringTestCase::UTF8Buf()
917{
918#if wxUSE_UNICODE
919 // "czech" in Czech ("cestina"):
920 static const char *textUTF8 = "\304\215e\305\241tina";
921 static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
922
923 wxString s;
924 wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8);
925 CPPUNIT_ASSERT(s == textUTF16);
926
927 {
928 wxUTF8StringBufferLength buf(s, 20);
929 wxStrcpy(buf, textUTF8);
930 buf.SetLength(5);
931 }
932 CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3));
933#endif // wxUSE_UNICODE
934}
935
4ca056ea
VS
936
937
ef0f1387 938void StringTestCase::CStrDataTernaryOperator()
4ca056ea 939{
ef0f1387
VS
940 DoCStrDataTernaryOperator(true);
941 DoCStrDataTernaryOperator(false);
4ca056ea
VS
942}
943
944template<typename T> bool CheckStr(const wxString& expected, T s)
945{
946 return expected == wxString(s);
947}
948
ef0f1387 949void StringTestCase::DoCStrDataTernaryOperator(bool cond)
4ca056ea
VS
950{
951 // test compilation of wxCStrData when used with operator?: (the asserts
952 // are not very important, we're testing if the code compiles at all):
953
954 wxString s("foo");
4ca056ea 955
abc505b4 956 const wchar_t *wcStr = L"foo";
4ca056ea 957 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) );
9ffb659a 958 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) );
4ca056ea 959 CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) );
9ffb659a 960 CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) );
11aac4ba 961
9b59b90c 962 const char *mbStr = "foo";
4ca056ea
VS
963 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) );
964 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) );
965 CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) );
966 CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) );
92258cc1
VS
967
968 wxString empty("");
969 CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) );
970 CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) );
4ca056ea 971}
ef0f1387 972
3a69bca1
VS
973void StringTestCase::CStrDataOperators()
974{
975 wxString s("hello");
976
977 CPPUNIT_ASSERT( s.c_str()[0] == 'h' );
978 CPPUNIT_ASSERT( s.c_str()[1] == 'e' );
fdfc5e18
FM
979
980 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
981 // the final character results in an assert failure (with debug CRT)
982 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
3a69bca1
VS
983
984 CPPUNIT_ASSERT( *s.c_str() == 'h' );
985 CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' );
fdfc5e18 986 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
3a69bca1
VS
987}
988
ef0f1387
VS
989bool CheckStrChar(const wxString& expected, char *s)
990 { return CheckStr(expected, s); }
991bool CheckStrWChar(const wxString& expected, wchar_t *s)
992 { return CheckStr(expected, s); }
993bool CheckStrConstChar(const wxString& expected, const char *s)
994 { return CheckStr(expected, s); }
995bool CheckStrConstWChar(const wxString& expected, const wchar_t *s)
996 { return CheckStr(expected, s); }
997
998void StringTestCase::CStrDataImplicitConversion()
999{
1000 wxString s("foo");
1001
ef0f1387 1002 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) );
ef0f1387 1003 CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) );
7978bc72
VS
1004
1005 // implicit conversion of wxString is not available in STL build
1006#if !wxUSE_STL
1007 CPPUNIT_ASSERT( CheckStrConstWChar(s, s) );
ef0f1387 1008 CPPUNIT_ASSERT( CheckStrConstChar(s, s) );
7978bc72 1009#endif
ef0f1387
VS
1010}
1011
1012void StringTestCase::ExplicitConversion()
1013{
1014 wxString s("foo");
1015
1016 CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) );
1017 CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) );
1018 CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) );
1019
1020 CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) );
1021 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) );
1022 CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) );
1023}
6bd4f281
VS
1024
1025void StringTestCase::IndexedAccess()
1026{
1027 wxString s("bar");
94bc35dc 1028 CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
6bd4f281
VS
1029
1030 // this tests for a possible bug in UTF-8 based wxString implementation:
1031 // the 3rd character of the underlying byte string is going to change, but
1032 // the 3rd character of wxString should remain the same
39e12b2d 1033 s[0] = L'\xe9';
94bc35dc 1034 CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
6bd4f281
VS
1035}
1036
c565abe1
VZ
1037void StringTestCase::BeforeAndAfter()
1038{
23231f1c
VZ
1039 // Construct a string with 2 equal signs in it by concatenating its three
1040 // parts: before the first "=", in between the two "="s and after the last
1041 // one. This allows to avoid duplicating the string contents (which has to
1042 // be different for Unicode and ANSI builds) in the tests below.
1043#if wxUSE_UNICODE
1044 #define FIRST_PART L"letter"
1045 #define MIDDLE_PART L"\xe9;\xe7a"
1046 #define LAST_PART L"l\xe0"
1047#else // !wxUSE_UNICODE
1048 #define FIRST_PART "letter"
1049 #define MIDDLE_PART "e;ca"
1050 #define LAST_PART "la"
1051#endif // wxUSE_UNICODE/!wxUSE_UNICODE
1052
1053 const wxString s(FIRST_PART wxT("=") MIDDLE_PART wxT("=") LAST_PART);
c565abe1 1054
6becc1e6
VZ
1055 wxString r;
1056
23231f1c
VZ
1057 CPPUNIT_ASSERT_EQUAL( FIRST_PART, s.BeforeFirst('=', &r) );
1058 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, r );
6becc1e6
VZ
1059
1060 CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!', &r) );
1061 CPPUNIT_ASSERT_EQUAL( "", r );
1062
6becc1e6 1063
23231f1c
VZ
1064 CPPUNIT_ASSERT_EQUAL( FIRST_PART wxT("=") MIDDLE_PART, s.BeforeLast('=', &r) );
1065 CPPUNIT_ASSERT_EQUAL( LAST_PART, r );
6becc1e6
VZ
1066
1067 CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!', &r) );
1068 CPPUNIT_ASSERT_EQUAL( s, r );
1069
c565abe1 1070
23231f1c 1071 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, s.AfterFirst('=') );
c565abe1 1072 CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') );
c565abe1 1073
6becc1e6 1074
23231f1c 1075 CPPUNIT_ASSERT_EQUAL( LAST_PART, s.AfterLast('=') );
c565abe1 1076 CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') );
23231f1c
VZ
1077
1078 #undef LAST_PART
1079 #undef MIDDLE_PART
1080 #undef FIRST_PART
c565abe1
VZ
1081}
1082
de4983f3
VS
1083void StringTestCase::ScopedBuffers()
1084{
1085 // wxString relies on efficient buffers, verify they work as they should
1086
1087 const char *literal = "Hello World!";
1088
1089 // non-owned buffer points to the string passed to it
1090 wxScopedCharBuffer sbuf = wxScopedCharBuffer::CreateNonOwned(literal);
1091 CPPUNIT_ASSERT( sbuf.data() == literal );
1092
1093 // a copy of scoped non-owned buffer still points to the same string
1094 wxScopedCharBuffer sbuf2(sbuf);
1095 CPPUNIT_ASSERT( sbuf.data() == sbuf2.data() );
1096
1097 // but assigning it to wxCharBuffer makes a full copy
1098 wxCharBuffer buf(sbuf);
1099 CPPUNIT_ASSERT( buf.data() != literal );
390b8241 1100 CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
de4983f3
VS
1101
1102 wxCharBuffer buf2 = sbuf;
1103 CPPUNIT_ASSERT( buf2.data() != literal );
390b8241 1104 CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
f2c6e607
VZ
1105
1106 // Check that extending the buffer keeps it NUL-terminated.
1107 size_t len = 10;
1108
1109 wxCharBuffer buf3(len);
1110 CPPUNIT_ASSERT_EQUAL('\0', buf3.data()[len]);
1111
1112 wxCharBuffer buf4;
1113 buf4.extend(len);
1114 CPPUNIT_ASSERT_EQUAL('\0', buf4.data()[len]);
1115
1116 wxCharBuffer buf5(5);
1117 buf5.extend(len);
1118 CPPUNIT_ASSERT_EQUAL('\0', buf5.data()[len]);
de4983f3 1119}