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