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