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