Fix handling of positional parameters in wxPrintf() and related.
[wxWidgets.git] / tests / strings / strings.cpp
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
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 // ----------------------------------------------------------------------------
25 // test class
26 // ----------------------------------------------------------------------------
27
28 class StringTestCase : public CppUnit::TestCase
29 {
30 public:
31 StringTestCase();
32
33 private:
34 CPPUNIT_TEST_SUITE( StringTestCase );
35 CPPUNIT_TEST( String );
36 CPPUNIT_TEST( PChar );
37 CPPUNIT_TEST( Format );
38 CPPUNIT_TEST( Constructors );
39 CPPUNIT_TEST( StaticConstructors );
40 CPPUNIT_TEST( Extraction );
41 CPPUNIT_TEST( Trim );
42 CPPUNIT_TEST( Find );
43 CPPUNIT_TEST( Replace );
44 CPPUNIT_TEST( Match );
45 CPPUNIT_TEST( CaseChanges );
46 CPPUNIT_TEST( Compare );
47 CPPUNIT_TEST( CompareNoCase );
48 CPPUNIT_TEST( Contains );
49 CPPUNIT_TEST( ToLong );
50 CPPUNIT_TEST( ToULong );
51 #ifdef wxLongLong_t
52 CPPUNIT_TEST( ToLongLong );
53 CPPUNIT_TEST( ToULongLong );
54 #endif // wxLongLong_t
55 CPPUNIT_TEST( ToDouble );
56 CPPUNIT_TEST( FromDouble );
57 CPPUNIT_TEST( StringBuf );
58 CPPUNIT_TEST( UTF8Buf );
59 CPPUNIT_TEST( CStrDataTernaryOperator );
60 CPPUNIT_TEST( CStrDataOperators );
61 CPPUNIT_TEST( CStrDataImplicitConversion );
62 CPPUNIT_TEST( ExplicitConversion );
63 CPPUNIT_TEST( IndexedAccess );
64 CPPUNIT_TEST( BeforeAndAfter );
65 CPPUNIT_TEST( ScopedBuffers );
66 CPPUNIT_TEST_SUITE_END();
67
68 void String();
69 void PChar();
70 void Format();
71 void Constructors();
72 void StaticConstructors();
73 void Extraction();
74 void Trim();
75 void Find();
76 void Replace();
77 void Match();
78 void CaseChanges();
79 void Compare();
80 void CompareNoCase();
81 void Contains();
82 void ToLong();
83 void ToULong();
84 #ifdef wxLongLong_t
85 void ToLongLong();
86 void ToULongLong();
87 #endif // wxLongLong_t
88 void ToDouble();
89 void FromDouble();
90 void StringBuf();
91 void UTF8Buf();
92 void CStrDataTernaryOperator();
93 void DoCStrDataTernaryOperator(bool cond);
94 void CStrDataOperators();
95 void CStrDataImplicitConversion();
96 void ExplicitConversion();
97 void IndexedAccess();
98 void BeforeAndAfter();
99 void ScopedBuffers();
100
101 DECLARE_NO_COPY_CLASS(StringTestCase)
102 };
103
104 // register in the unnamed registry so that these tests are run by default
105 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
106
107 // also include in its own registry so that these tests can be run alone
108 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
109
110 StringTestCase::StringTestCase()
111 {
112 }
113
114 void 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 {
124 a = wxT("Hello");
125 b = wxT(" world");
126 c = wxT("! How'ya doin'?");
127 a += b;
128 a += c;
129 c = wxT("Hello world! What's up?");
130 CPPUNIT_ASSERT( c != a );
131 }
132 }
133
134 void 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 {
142 wxStrcpy (a, wxT("Hello"));
143 wxStrcpy (b, wxT(" world"));
144 wxStrcpy (c, wxT("! How'ya doin'?"));
145 wxStrcat (a, b);
146 wxStrcat (a, c);
147 wxStrcpy (c, wxT("Hello world! What's up?"));
148 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
149 }
150 }
151
152 void StringTestCase::Format()
153 {
154 wxString s1,s2;
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()) );
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
165 wxString s(wxT('Z'), len);
166 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(wxT("%s"), s.c_str()).length());
167 }
168
169
170 CPPUNIT_ASSERT_EQUAL
171 (
172 "two one",
173 wxString::Format(wxT("%2$s %1$s"), wxT("one"), wxT("two"))
174 );
175 }
176
177 void StringTestCase::Constructors()
178 {
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) );
184
185 #if wxUSE_UNICODE
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) );
191 #endif // wxUSE_UNICODE
192
193 static const char *s = "?really!";
194 const char *start = wxStrchr(s, 'r');
195 const char *end = wxStrchr(s, '!');
196 CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) );
197
198 // test if creating string from NULL C pointer works:
199 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) );
200 }
201
202 void StringTestCase::StaticConstructors()
203 {
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") );
209
210 // FIXME: this doesn't work currently but should!
211 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
212
213
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") );
219
220 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
221 }
222
223 void StringTestCase::Extraction()
224 {
225 wxString s(wxT("Hello, world!"));
226
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 );
234
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
243 wxString rest;
244
245 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
246 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
247 if ( result ) \
248 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
249
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 );
257
258 #undef TEST_STARTS_WITH
259
260 rest = "Hello world";
261 CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) );
262 CPPUNIT_ASSERT_EQUAL("world", rest);
263
264 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
265 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
266 if ( result ) \
267 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
268
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 );
278
279 #undef TEST_ENDS_WITH
280 }
281
282 void StringTestCase::Trim()
283 {
284 #define TEST_TRIM( str , dir , result ) \
285 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
286
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("") );
291
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("") );
296
297 #undef TEST_TRIM
298 }
299
300 void StringTestCase::Find()
301 {
302 #define TEST_FIND( str , start , result ) \
303 CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
304
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 );
308
309 #undef TEST_FIND
310 }
311
312 void StringTestCase::Replace()
313 {
314 #define TEST_REPLACE( original , pos , len , replacement , result ) \
315 { \
316 wxString s = original; \
317 s.replace( pos , len , replacement ); \
318 CPPUNIT_ASSERT_EQUAL( result, s ); \
319 }
320
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") );
326
327
328 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
329 { \
330 wxString s(o,olen); \
331 s.replace( pos , len , replacement ); \
332 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
333 }
334
335 TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
336 wxT("null\0dhar"), 9 );
337
338 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
339 { \
340 wxString s(o,olen); \
341 s.Replace( olds, news, all ); \
342 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
343 }
344
345 TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
346 wxT("null\0dehar"), 10 );
347
348 TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
349 wxT("null\0char"), 9 );
350
351 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
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 );
356
357 #undef TEST_WXREPLACE
358 #undef TEST_NULLCHARREPLACE
359 #undef TEST_REPLACE
360 }
361
362 void StringTestCase::Match()
363 {
364 #define TEST_MATCH( s1 , s2 , result ) \
365 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
366
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 );
378
379 #undef TEST_MATCH
380 }
381
382
383 void StringTestCase::CaseChanges()
384 {
385 wxString s1(wxT("Hello!"));
386 wxString s1u(s1);
387 wxString s1l(s1);
388 s1u.MakeUpper();
389 s1l.MakeLower();
390
391 CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u );
392 CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l );
393
394 wxString s2u, s2l;
395 s2u.MakeUpper();
396 s2l.MakeLower();
397
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() );
410 }
411
412 void 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");
420
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
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
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');
459
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 );
465
466 CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
467 CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
468 CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
469 }
470
471 void 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");
480
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");
500
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 );
516
517 CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
518 CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
519 CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
520 }
521
522 void StringTestCase::Contains()
523 {
524 static const struct ContainsData
525 {
526 const wxChar *hay;
527 const wxChar *needle;
528 bool contains;
529 } containsData[] =
530 {
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 },
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
549 // flags used in ToLongData.flags
550 enum
551 {
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
557 Number_Long = 16 // only for long tests
558 };
559
560 static const struct ToLongData
561 {
562 const wxChar *str;
563 #ifdef wxLongLong_t
564 wxLongLong_t value;
565 #else
566 long value;
567 #endif // wxLongLong_t
568 int flags;
569
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
577 bool IsOk() const { return !(flags & Number_Invalid); }
578 } longData[] =
579 {
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 },
585
586 { wxT("-1"), -1, Number_Signed | Number_Long },
587 // this is surprising but consistent with strtoul() behaviour
588 { wxT("-1"), ULONG_MAX, Number_Unsigned | Number_Long },
589
590 // this must overflow, even with 64 bit long
591 { wxT("922337203685477580711"), 0, Number_Invalid },
592
593 #ifdef wxLongLong_t
594 { wxT("2147483648"), wxLL(2147483648), Number_LongLong },
595 { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed },
596 { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong |
597 Number_Unsigned },
598 #endif // wxLongLong_t
599 };
600
601 void StringTestCase::ToLong()
602 {
603 long l;
604 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
605 {
606 const ToLongData& ld = longData[n];
607
608 if ( ld.flags & (Number_LongLong | Number_Unsigned) )
609 continue;
610
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 );
617
618 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) );
619 if ( ld.IsOk() )
620 CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
621 }
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 );
633 }
634
635 void StringTestCase::ToULong()
636 {
637 unsigned long ul;
638 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
639 {
640 const ToLongData& ld = longData[n];
641
642 if ( ld.flags & (Number_LongLong | Number_Signed) )
643 continue;
644
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 );
651
652 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) );
653 if ( ld.IsOk() )
654 CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
655 }
656 }
657
658 #ifdef wxLongLong_t
659
660 void StringTestCase::ToLongLong()
661 {
662 wxLongLong_t l;
663 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
664 {
665 const ToLongData& ld = longData[n];
666
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() )
672 CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l );
673 }
674 }
675
676 void StringTestCase::ToULongLong()
677 {
678 wxULongLong_t ul;
679 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
680 {
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() )
688 CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul );
689 }
690 }
691
692 #endif // wxLongLong_t
693
694 void 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 {
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 },
716 };
717
718 // test ToCDouble() first:
719
720 size_t n;
721 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
722 {
723 const ToDoubleData& ld = doubleData[n];
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:
731 // NOTE: for the test to be reliable, we need to set the locale explicitly
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
737 wxLocale locale;
738
739 // don't load default catalog, it may be unavailable:
740 CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) );
741
742 static const struct ToDoubleData doubleData2[] =
743 {
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 },
756 };
757
758 for ( n = 0; n < WXSIZEOF(doubleData2); n++ )
759 {
760 const ToDoubleData& ld = doubleData2[n];
761 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
762 if ( ld.ok )
763 CPPUNIT_ASSERT_EQUAL( ld.value, d );
764 }
765 }
766
767 void StringTestCase::FromDouble()
768 {
769 static const struct FromDoubleTestData
770 {
771 double value;
772 int prec;
773 const char *str;
774 } testData[] =
775 {
776 { 1.23, -1, "1.23" },
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...
780 #ifdef wxUSING_VC_CRT_IO
781 { -3e-10, -1, "-3e-010" },
782 #else
783 { -3e-10, -1, "-3e-10" },
784 #endif
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" },
790 };
791
792 for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
793 {
794 const FromDoubleTestData& td = testData[n];
795 CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value, td.prec) );
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(".", ",");
810 CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );
811 }
812 }
813
814 void StringTestCase::StringBuf()
815 {
816 // check that buffer can be used to write into the string
817 wxString s;
818 wxStrcpy(wxStringBuffer(s, 10), wxT("foo"));
819
820 CPPUNIT_ASSERT_EQUAL(3, s.length());
821 CPPUNIT_ASSERT(wxT('f') == s[0u]);
822 CPPUNIT_ASSERT(wxT('o') == s[1]);
823 CPPUNIT_ASSERT(wxT('o') == s[2]);
824
825 {
826 // also check that the buffer initially contains the original string
827 // contents
828 wxStringBuffer buf(s, 10);
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] );
833 }
834
835 {
836 wxStringBufferLength buf(s, 10);
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] );
841
842 // and check that it can be used to write only the specified number of
843 // characters to the string
844 wxStrcpy(buf, wxT("barrbaz"));
845 buf.SetLength(4);
846 }
847
848 CPPUNIT_ASSERT_EQUAL(4, s.length());
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]);
853
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) = '!';
858 }
859
860 void 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
880
881
882 void StringTestCase::CStrDataTernaryOperator()
883 {
884 DoCStrDataTernaryOperator(true);
885 DoCStrDataTernaryOperator(false);
886 }
887
888 template<typename T> bool CheckStr(const wxString& expected, T s)
889 {
890 return expected == wxString(s);
891 }
892
893 void StringTestCase::DoCStrDataTernaryOperator(bool cond)
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");
899
900 const wchar_t *wcStr = L"foo";
901 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) );
902 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) );
903 CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) );
904 CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) );
905
906 const char *mbStr = "foo";
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())) );
911
912 wxString empty("");
913 CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) );
914 CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) );
915 }
916
917 void StringTestCase::CStrDataOperators()
918 {
919 wxString s("hello");
920
921 CPPUNIT_ASSERT( s.c_str()[0] == 'h' );
922 CPPUNIT_ASSERT( s.c_str()[1] == 'e' );
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' );
927
928 CPPUNIT_ASSERT( *s.c_str() == 'h' );
929 CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' );
930 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
931 }
932
933 bool CheckStrChar(const wxString& expected, char *s)
934 { return CheckStr(expected, s); }
935 bool CheckStrWChar(const wxString& expected, wchar_t *s)
936 { return CheckStr(expected, s); }
937 bool CheckStrConstChar(const wxString& expected, const char *s)
938 { return CheckStr(expected, s); }
939 bool CheckStrConstWChar(const wxString& expected, const wchar_t *s)
940 { return CheckStr(expected, s); }
941
942 void StringTestCase::CStrDataImplicitConversion()
943 {
944 wxString s("foo");
945
946 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) );
947 CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) );
948
949 // implicit conversion of wxString is not available in STL build
950 #if !wxUSE_STL
951 CPPUNIT_ASSERT( CheckStrConstWChar(s, s) );
952 CPPUNIT_ASSERT( CheckStrConstChar(s, s) );
953 #endif
954 }
955
956 void 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 }
968
969 void StringTestCase::IndexedAccess()
970 {
971 wxString s("bar");
972 CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
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
977 s[0] = L'\xe9';
978 CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
979 }
980
981 void StringTestCase::BeforeAndAfter()
982 {
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);
998
999 wxString r;
1000
1001 CPPUNIT_ASSERT_EQUAL( FIRST_PART, s.BeforeFirst('=', &r) );
1002 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, r );
1003
1004 CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!', &r) );
1005 CPPUNIT_ASSERT_EQUAL( "", r );
1006
1007
1008 CPPUNIT_ASSERT_EQUAL( FIRST_PART wxT("=") MIDDLE_PART, s.BeforeLast('=', &r) );
1009 CPPUNIT_ASSERT_EQUAL( LAST_PART, r );
1010
1011 CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!', &r) );
1012 CPPUNIT_ASSERT_EQUAL( s, r );
1013
1014
1015 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, s.AfterFirst('=') );
1016 CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') );
1017
1018
1019 CPPUNIT_ASSERT_EQUAL( LAST_PART, s.AfterLast('=') );
1020 CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') );
1021
1022 #undef LAST_PART
1023 #undef MIDDLE_PART
1024 #undef FIRST_PART
1025 }
1026
1027 void 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 );
1044 CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
1045
1046 wxCharBuffer buf2 = sbuf;
1047 CPPUNIT_ASSERT( buf2.data() != literal );
1048 CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
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]);
1063 }