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