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