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