1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class StringTestCase
: public CppUnit::TestCase
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
);
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
);
52 CPPUNIT_TEST( ToLongLong
);
53 CPPUNIT_TEST( ToULongLong
);
54 #endif // wxLongLong_t
55 CPPUNIT_TEST( ToDouble
);
56 CPPUNIT_TEST( StringBuf
);
57 CPPUNIT_TEST( UTF8Buf
);
58 CPPUNIT_TEST( CStrDataTernaryOperator
);
59 CPPUNIT_TEST( CStrDataOperators
);
60 CPPUNIT_TEST( CStrDataImplicitConversion
);
61 CPPUNIT_TEST( ExplicitConversion
);
62 CPPUNIT_TEST( IndexedAccess
);
63 CPPUNIT_TEST( BeforeAndAfter
);
64 CPPUNIT_TEST( ScopedBuffers
);
65 CPPUNIT_TEST_SUITE_END();
71 void StaticConstructors();
86 #endif // wxLongLong_t
90 void CStrDataTernaryOperator();
91 void DoCStrDataTernaryOperator(bool cond
);
92 void CStrDataOperators();
93 void CStrDataImplicitConversion();
94 void ExplicitConversion();
96 void BeforeAndAfter();
99 DECLARE_NO_COPY_CLASS(StringTestCase
)
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
105 // also include in it's own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
108 StringTestCase::StringTestCase()
112 void StringTestCase::String()
120 for (int i
= 0; i
< 2; ++i
)
124 c
= _T("! How'ya doin'?");
127 c
= _T("Hello world! What's up?");
128 CPPUNIT_ASSERT( c
!= a
);
132 void StringTestCase::PChar()
138 for (int i
= 0; i
< 2; ++i
)
140 wxStrcpy (a
, _T("Hello"));
141 wxStrcpy (b
, _T(" world"));
142 wxStrcpy (c
, _T("! How'ya doin'?"));
145 wxStrcpy (c
, _T("Hello world! What's up?"));
146 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
150 void StringTestCase::Format()
153 s1
.Printf(_T("%03d"), 18);
154 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
155 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
156 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
158 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
159 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
161 const size_t len
= lengths
[n
];
163 wxString
s(_T('Z'), len
);
164 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
168 void StringTestCase::Constructors()
170 CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) );
171 CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') );
172 CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) );
173 CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) );
174 CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) );
177 CPPUNIT_ASSERT_EQUAL( L
"", wxString(L
'Z', 0) );
178 CPPUNIT_ASSERT_EQUAL( L
"Z", wxString(L
'Z') );
179 CPPUNIT_ASSERT_EQUAL( L
"ZZZZ", wxString(L
'Z', 4) );
180 CPPUNIT_ASSERT_EQUAL( L
"Hell", wxString(L
"Hello", 4) );
181 CPPUNIT_ASSERT_EQUAL( L
"Hello", wxString(L
"Hello", 5) );
182 #endif // wxUSE_UNICODE
184 static const char *s
= "?really!";
185 const char *start
= wxStrchr(s
, 'r');
186 const char *end
= wxStrchr(s
, '!');
187 CPPUNIT_ASSERT_EQUAL( "really", wxString(start
, end
) );
189 // test if creating string from NULL C pointer works:
190 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL
) );
193 void StringTestCase::StaticConstructors()
195 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
196 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
197 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
198 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
199 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
201 // FIXME: this doesn't work currently but should!
202 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
205 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
206 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
207 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
208 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
209 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
211 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
214 void StringTestCase::Extraction()
216 wxString
s(_T("Hello, world!"));
218 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
219 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
220 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
221 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
222 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
223 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
224 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
227 static const char *germanUTF8
= "Oberfl\303\244che";
228 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
230 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
231 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
232 #endif // wxUSE_UNICODE
236 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
237 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
239 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
241 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
242 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
243 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
244 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
245 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
246 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
247 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
249 #undef TEST_STARTS_WITH
251 rest
= "Hello world";
252 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
253 CPPUNIT_ASSERT_EQUAL("world", rest
);
255 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
256 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
258 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
260 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
261 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
262 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
263 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
264 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
265 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
266 TEST_ENDS_WITH( _T("?"), _T(""), false );
267 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
268 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
270 #undef TEST_ENDS_WITH
273 void StringTestCase::Trim()
275 #define TEST_TRIM( str , dir , result ) \
276 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
278 TEST_TRIM( _T(" Test "), true, _T(" Test") );
279 TEST_TRIM( _T(" "), true, _T("") );
280 TEST_TRIM( _T(" "), true, _T("") );
281 TEST_TRIM( _T(""), true, _T("") );
283 TEST_TRIM( _T(" Test "), false, _T("Test ") );
284 TEST_TRIM( _T(" "), false, _T("") );
285 TEST_TRIM( _T(" "), false, _T("") );
286 TEST_TRIM( _T(""), false, _T("") );
291 void StringTestCase::Find()
293 #define TEST_FIND( str , start , result ) \
294 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
296 TEST_FIND( _T("Well, hello world"), 0, 1 );
297 TEST_FIND( _T("Well, hello world"), 6, 7 );
298 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
303 void StringTestCase::Replace()
305 #define TEST_REPLACE( original , pos , len , replacement , result ) \
307 wxString s = original; \
308 s.replace( pos , len , replacement ); \
309 CPPUNIT_ASSERT_EQUAL( result, s ); \
312 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
313 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
314 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
315 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
316 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
319 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
321 wxString s(o,olen); \
322 s.replace( pos , len , replacement ); \
323 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
326 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
327 _T("null\0dhar"), 9 );
329 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
331 wxString s(o,olen); \
332 s.Replace( olds, news, all ); \
333 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
336 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
337 _T("null\0dehar"), 10 );
339 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
340 _T("null\0char"), 9 );
342 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
343 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
344 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
345 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
346 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
348 #undef TEST_WXREPLACE
349 #undef TEST_NULLCHARREPLACE
353 void StringTestCase::Match()
355 #define TEST_MATCH( s1 , s2 , result ) \
356 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
358 TEST_MATCH( _T("foobar"), _T("foo*"), true );
359 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
360 TEST_MATCH( _T("foobar"), _T("*bar"), true );
361 TEST_MATCH( _T("foobar"), _T("??????"), true );
362 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
363 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
364 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
365 TEST_MATCH( _T("foobar"), _T("*foo"), false );
366 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
367 TEST_MATCH( _T(""), _T("*"), true );
368 TEST_MATCH( _T(""), _T("?"), false );
374 void StringTestCase::CaseChanges()
376 wxString
s1(_T("Hello!"));
382 CPPUNIT_ASSERT_EQUAL( _T("HELLO!"), s1u
);
383 CPPUNIT_ASSERT_EQUAL( _T("hello!"), s1l
);
389 CPPUNIT_ASSERT_EQUAL( "", s2u
);
390 CPPUNIT_ASSERT_EQUAL( "", s2l
);
393 wxString
s3("good bye");
394 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
.Capitalize() );
395 s3
.MakeCapitalized();
396 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
);
398 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
400 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
403 void StringTestCase::Compare()
405 wxString s1
= wxT("AHH");
406 wxString eq
= wxT("AHH");
407 wxString neq1
= wxT("HAH");
408 wxString neq2
= wxT("AH");
409 wxString neq3
= wxT("AHHH");
410 wxString neq4
= wxT("AhH");
412 CPPUNIT_ASSERT( s1
== eq
);
413 CPPUNIT_ASSERT( s1
!= neq1
);
414 CPPUNIT_ASSERT( s1
!= neq2
);
415 CPPUNIT_ASSERT( s1
!= neq3
);
416 CPPUNIT_ASSERT( s1
!= neq4
);
418 CPPUNIT_ASSERT( s1
== wxT("AHH") );
419 CPPUNIT_ASSERT( s1
!= wxT("no") );
420 CPPUNIT_ASSERT( s1
< wxT("AZ") );
421 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
422 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
423 CPPUNIT_ASSERT( s1
> wxT("AA") );
424 CPPUNIT_ASSERT( s1
>= wxT("AA") );
425 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
427 // test comparison with C strings in Unicode build (must work in ANSI as
429 CPPUNIT_ASSERT( s1
== "AHH" );
430 CPPUNIT_ASSERT( s1
!= "no" );
431 CPPUNIT_ASSERT( s1
< "AZ" );
432 CPPUNIT_ASSERT( s1
<= "AZ" );
433 CPPUNIT_ASSERT( s1
<= "AHH" );
434 CPPUNIT_ASSERT( s1
> "AA" );
435 CPPUNIT_ASSERT( s1
>= "AA" );
436 CPPUNIT_ASSERT( s1
>= "AHH" );
438 // wxString _s1 = wxT("A\0HH");
439 // wxString _eq = wxT("A\0HH");
440 // wxString _neq1 = wxT("H\0AH");
441 // wxString _neq2 = wxT("A\0H");
442 // wxString _neq3 = wxT("A\0HHH");
443 // wxString _neq4 = wxT("A\0hH");
446 neq1
.insert(1,1,'\0');
447 neq2
.insert(1,1,'\0');
448 neq3
.insert(1,1,'\0');
449 neq4
.insert(1,1,'\0');
451 CPPUNIT_ASSERT( s1
== eq
);
452 CPPUNIT_ASSERT( s1
!= neq1
);
453 CPPUNIT_ASSERT( s1
!= neq2
);
454 CPPUNIT_ASSERT( s1
!= neq3
);
455 CPPUNIT_ASSERT( s1
!= neq4
);
458 void StringTestCase::CompareNoCase()
460 wxString s1
= wxT("AHH");
461 wxString eq
= wxT("AHH");
462 wxString eq2
= wxT("AhH");
463 wxString eq3
= wxT("ahh");
464 wxString neq
= wxT("HAH");
465 wxString neq2
= wxT("AH");
466 wxString neq3
= wxT("AHHH");
468 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
469 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
471 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
472 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
473 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
475 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
476 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
477 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
480 // wxString _s1 = wxT("A\0HH");
481 // wxString _eq = wxT("A\0HH");
482 // wxString _eq2 = wxT("A\0hH");
483 // wxString _eq3 = wxT("a\0hh");
484 // wxString _neq = wxT("H\0AH");
485 // wxString _neq2 = wxT("A\0H");
486 // wxString _neq3 = wxT("A\0HHH");
490 eq2
.insert(1,1,'\0');
491 eq3
.insert(1,1,'\0');
492 neq
.insert(1,1,'\0');
493 neq2
.insert(1,1,'\0');
494 neq3
.insert(1,1,'\0');
496 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
497 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
498 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
500 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
501 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
502 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
505 void StringTestCase::Contains()
507 static const struct ContainsData
510 const wxChar
*needle
;
514 { _T(""), _T(""), true },
515 { _T(""), _T("foo"), false },
516 { _T("foo"), _T(""), true },
517 { _T("foo"), _T("f"), true },
518 { _T("foo"), _T("o"), true },
519 { _T("foo"), _T("oo"), true },
520 { _T("foo"), _T("ooo"), false },
521 { _T("foo"), _T("oooo"), false },
522 { _T("foo"), _T("fooo"), false },
525 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
527 const ContainsData
& cd
= containsData
[n
];
528 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
532 // flags used in ToLongData.flags
537 Number_Unsigned
= 2, // if not specified, works for signed conversion
538 Number_Signed
= 4, // if not specified, works for unsigned
539 Number_LongLong
= 8, // only for long long tests
540 Number_Long
= 16 // only for long tests
543 static const struct ToLongData
550 #endif // wxLongLong_t
553 long LValue() const { return value
; }
554 unsigned long ULValue() const { return value
; }
556 wxLongLong_t
LLValue() const { return value
; }
557 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
558 #endif // wxLongLong_t
560 bool IsOk() const { return !(flags
& Number_Invalid
); }
563 { _T("1"), 1, Number_Ok
},
564 { _T("0"), 0, Number_Ok
},
565 { _T("a"), 0, Number_Invalid
},
566 { _T("12345"), 12345, Number_Ok
},
567 { _T("--1"), 0, Number_Invalid
},
569 { _T("-1"), -1, Number_Signed
| Number_Long
},
570 // this is surprizing but consistent with strtoul() behaviour
571 { _T("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
573 // this must overflow, even with 64 bit long
574 { _T("922337203685477580711"), 0, Number_Invalid
},
577 { _T("2147483648"), wxLL(2147483648), Number_LongLong
},
578 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
579 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
581 #endif // wxLongLong_t
584 void StringTestCase::ToLong()
587 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
589 const ToLongData
& ld
= longData
[n
];
591 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
594 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
595 // should behave the same for our test data set:
597 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCLong(&l
) );
599 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
601 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
603 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
607 void StringTestCase::ToULong()
610 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
612 const ToLongData
& ld
= longData
[n
];
614 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
617 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
618 // should behave the same for our test data set:
620 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCULong(&ul
) );
622 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
624 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
626 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
632 void StringTestCase::ToLongLong()
635 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
637 const ToLongData
& ld
= longData
[n
];
639 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
642 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
644 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
648 void StringTestCase::ToULongLong()
651 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
653 const ToLongData
& ld
= longData
[n
];
655 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
658 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
660 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
664 #endif // wxLongLong_t
666 void StringTestCase::ToDouble()
669 static const struct ToDoubleData
676 { _T("1"), 1, true },
677 { _T("1.23"), 1.23, true },
678 { _T(".1"), .1, true },
679 { _T("1."), 1, true },
680 { _T("1.."), 0, false },
681 { _T("0"), 0, true },
682 { _T("a"), 0, false },
683 { _T("12345"), 12345, true },
684 { _T("-1"), -1, true },
685 { _T("--1"), 0, false },
686 { _T("-3E-5"), -3E-5, true },
687 { _T("-3E-abcde5"), 0, false },
690 // test ToCDouble() first:
693 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
695 const ToDoubleData
& ld
= doubleData
[n
];
696 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToCDouble(&d
) );
698 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
702 // test ToDouble() now:
703 // NOTE: for the test to be reliable, we need to set the locale explicitely
704 // so that we know the decimal point character to use
706 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
707 return; // you should have french support installed to continue this test!
709 wxLocale
*locale
= new wxLocale
;
711 // don't load default catalog, it may be unavailable:
712 CPPUNIT_ASSERT( locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_CONV_ENCODING
) );
714 static const struct ToDoubleData doubleData2
[] =
716 { _T("1"), 1, true },
717 { _T("1,23"), 1.23, true },
718 { _T(",1"), .1, true },
719 { _T("1,"), 1, true },
720 { _T("1,,"), 0, false },
721 { _T("0"), 0, true },
722 { _T("a"), 0, false },
723 { _T("12345"), 12345, true },
724 { _T("-1"), -1, true },
725 { _T("--1"), 0, false },
726 { _T("-3E-5"), -3E-5, true },
727 { _T("-3E-abcde5"), 0, false },
730 for ( n
= 0; n
< WXSIZEOF(doubleData2
); n
++ )
732 const ToDoubleData
& ld
= doubleData2
[n
];
733 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
735 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
741 void StringTestCase::StringBuf()
743 // check that buffer can be used to write into the string
745 wxStrcpy(wxStringBuffer(s
, 10), _T("foo"));
747 CPPUNIT_ASSERT_EQUAL(3, s
.length());
748 CPPUNIT_ASSERT(_T('f') == s
[0u]);
749 CPPUNIT_ASSERT(_T('o') == s
[1]);
750 CPPUNIT_ASSERT(_T('o') == s
[2]);
753 // also check that the buffer initially contains the original string
755 wxStringBuffer
buf(s
, 10);
756 CPPUNIT_ASSERT_EQUAL( _T('f'), buf
[0] );
757 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[1] );
758 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[2] );
759 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf
[3] );
763 wxStringBufferLength
buf(s
, 10);
764 CPPUNIT_ASSERT_EQUAL( _T('f'), buf
[0] );
765 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[1] );
766 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[2] );
767 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf
[3] );
769 // and check that it can be used to write only the specified number of
770 // characters to the string
771 wxStrcpy(buf
, _T("barrbaz"));
775 CPPUNIT_ASSERT_EQUAL(4, s
.length());
776 CPPUNIT_ASSERT(_T('b') == s
[0u]);
777 CPPUNIT_ASSERT(_T('a') == s
[1]);
778 CPPUNIT_ASSERT(_T('r') == s
[2]);
779 CPPUNIT_ASSERT(_T('r') == s
[3]);
781 // check that creating buffer of length smaller than string works, i.e. at
782 // least doesn't crash (it would if we naively copied the entire original
783 // string contents in the buffer)
784 *wxStringBuffer(s
, 1) = '!';
787 void StringTestCase::UTF8Buf()
790 // "czech" in Czech ("cestina"):
791 static const char *textUTF8
= "\304\215e\305\241tina";
792 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
795 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
796 CPPUNIT_ASSERT(s
== textUTF16
);
799 wxUTF8StringBufferLength
buf(s
, 20);
800 wxStrcpy(buf
, textUTF8
);
803 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
804 #endif // wxUSE_UNICODE
809 void StringTestCase::CStrDataTernaryOperator()
811 DoCStrDataTernaryOperator(true);
812 DoCStrDataTernaryOperator(false);
815 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
817 return expected
== wxString(s
);
820 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
822 // test compilation of wxCStrData when used with operator?: (the asserts
823 // are not very important, we're testing if the code compiles at all):
827 const wchar_t *wcStr
= L
"foo";
828 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
829 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
830 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
831 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
833 const char *mbStr
= "foo";
834 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
835 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
836 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
837 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
840 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
841 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
844 void StringTestCase::CStrDataOperators()
848 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
849 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
851 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
852 // the final character results in an assert failure (with debug CRT)
853 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
855 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
856 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
857 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
860 bool CheckStrChar(const wxString
& expected
, char *s
)
861 { return CheckStr(expected
, s
); }
862 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
863 { return CheckStr(expected
, s
); }
864 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
865 { return CheckStr(expected
, s
); }
866 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
867 { return CheckStr(expected
, s
); }
869 void StringTestCase::CStrDataImplicitConversion()
873 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
874 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
876 // implicit conversion of wxString is not available in STL build
878 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
879 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
883 void StringTestCase::ExplicitConversion()
887 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
888 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
889 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
891 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
892 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
893 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );
896 void StringTestCase::IndexedAccess()
899 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
901 // this tests for a possible bug in UTF-8 based wxString implementation:
902 // the 3rd character of the underlying byte string is going to change, but
903 // the 3rd character of wxString should remain the same
905 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
908 void StringTestCase::BeforeAndAfter()
910 const wxString
s(L
"letter=\xe9;\xe7a=l\xe0");
912 CPPUNIT_ASSERT_EQUAL( "letter", s
.BeforeFirst('=') );
913 CPPUNIT_ASSERT_EQUAL( s
, s
.BeforeFirst('!') );
914 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9", s
.BeforeFirst(';') );
916 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9;\xe7a", s
.BeforeLast('=') );
917 CPPUNIT_ASSERT_EQUAL( "", s
.BeforeLast('!') );
918 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9", s
.BeforeLast(';') );
920 CPPUNIT_ASSERT_EQUAL( L
"\xe9;\xe7a=l\xe0", s
.AfterFirst('=') );
921 CPPUNIT_ASSERT_EQUAL( "", s
.AfterFirst('!') );
922 CPPUNIT_ASSERT_EQUAL( L
"\xe7a=l\xe0", s
.AfterFirst(';') );
924 CPPUNIT_ASSERT_EQUAL( L
"l\xe0", s
.AfterLast('=') );
925 CPPUNIT_ASSERT_EQUAL( s
, s
.AfterLast('!') );
926 CPPUNIT_ASSERT_EQUAL( L
"\xe7a=l\xe0", s
.AfterLast(';') );
929 void StringTestCase::ScopedBuffers()
931 // wxString relies on efficient buffers, verify they work as they should
933 const char *literal
= "Hello World!";
935 // non-owned buffer points to the string passed to it
936 wxScopedCharBuffer sbuf
= wxScopedCharBuffer::CreateNonOwned(literal
);
937 CPPUNIT_ASSERT( sbuf
.data() == literal
);
939 // a copy of scoped non-owned buffer still points to the same string
940 wxScopedCharBuffer
sbuf2(sbuf
);
941 CPPUNIT_ASSERT( sbuf
.data() == sbuf2
.data() );
943 // but assigning it to wxCharBuffer makes a full copy
944 wxCharBuffer
buf(sbuf
);
945 CPPUNIT_ASSERT( buf
.data() != literal
);
946 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
948 wxCharBuffer buf2
= sbuf
;
949 CPPUNIT_ASSERT( buf2
.data() != literal
);
950 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );