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( WriteBuf
);
57 CPPUNIT_TEST( UTF8Buf
);
58 CPPUNIT_TEST( CStrDataTernaryOperator
);
59 CPPUNIT_TEST( CStrDataOperators
);
60 CPPUNIT_TEST( CStrDataImplicitConversion
);
61 CPPUNIT_TEST( ExplicitConversion
);
62 CPPUNIT_TEST_SUITE_END();
68 void StaticConstructors();
83 #endif // wxLongLong_t
87 void CStrDataTernaryOperator();
88 void DoCStrDataTernaryOperator(bool cond
);
89 void CStrDataOperators();
90 void CStrDataImplicitConversion();
91 void ExplicitConversion();
93 DECLARE_NO_COPY_CLASS(StringTestCase
)
96 // register in the unnamed registry so that these tests are run by default
97 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
99 // also include in it's own registry so that these tests can be run alone
100 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
102 StringTestCase::StringTestCase()
106 void StringTestCase::String()
114 for (int i
= 0; i
< 2; ++i
)
118 c
= _T("! How'ya doin'?");
121 c
= _T("Hello world! What's up?");
122 CPPUNIT_ASSERT( c
!= a
);
126 void StringTestCase::PChar()
132 for (int i
= 0; i
< 2; ++i
)
134 wxStrcpy (a
, _T("Hello"));
135 wxStrcpy (b
, _T(" world"));
136 wxStrcpy (c
, _T("! How'ya doin'?"));
139 wxStrcpy (c
, _T("Hello world! What's up?"));
140 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
144 void StringTestCase::Format()
147 s1
.Printf(_T("%03d"), 18);
148 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
149 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
150 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
152 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
153 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
155 const size_t len
= lengths
[n
];
157 wxString
s(_T('Z'), len
);
158 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
162 void StringTestCase::Constructors()
164 WX_ASSERT_STR_EQUAL( "", wxString('Z', 0) );
165 WX_ASSERT_STR_EQUAL( "Z", wxString('Z') );
166 WX_ASSERT_STR_EQUAL( "ZZZZ", wxString('Z', 4) );
167 WX_ASSERT_STR_EQUAL( "Hell", wxString("Hello", 4) );
168 WX_ASSERT_STR_EQUAL( "Hello", wxString("Hello", 5) );
171 WX_ASSERT_STR_EQUAL( L
"", wxString(L
'Z', 0) );
172 WX_ASSERT_STR_EQUAL( L
"Z", wxString(L
'Z') );
173 WX_ASSERT_STR_EQUAL( L
"ZZZZ", wxString(L
'Z', 4) );
174 WX_ASSERT_STR_EQUAL( L
"Hell", wxString(L
"Hello", 4) );
175 WX_ASSERT_STR_EQUAL( L
"Hello", wxString(L
"Hello", 5) );
176 #endif // wxUSE_UNICODE
178 static const char *s
= "?really!";
179 const char *start
= wxStrchr(s
, 'r');
180 const char *end
= wxStrchr(s
, '!');
181 WX_ASSERT_STR_EQUAL( "really", wxString(start
, end
) );
183 // test if creating string from NULL C pointer works:
184 WX_ASSERT_STR_EQUAL( "", wxString((const char *)NULL
) );
187 void StringTestCase::StaticConstructors()
189 WX_ASSERT_STR_EQUAL( "", wxString::FromAscii("") );
190 WX_ASSERT_STR_EQUAL( "", wxString::FromAscii("Hello", 0) );
191 WX_ASSERT_STR_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
192 WX_ASSERT_STR_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
193 WX_ASSERT_STR_EQUAL( "Hello", wxString::FromAscii("Hello") );
195 // FIXME: this doesn't work currently but should!
196 //WX_ASSERT_SIZET_EQUAL( 1, wxString::FromAscii("", 1).length() );
199 WX_ASSERT_STR_EQUAL( "", wxString::FromUTF8("") );
200 WX_ASSERT_STR_EQUAL( "", wxString::FromUTF8("Hello", 0) );
201 WX_ASSERT_STR_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
202 WX_ASSERT_STR_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
203 WX_ASSERT_STR_EQUAL( "Hello", wxString::FromUTF8("Hello") );
205 //WX_ASSERT_SIZET_EQUAL( 1, wxString::FromUTF8("", 1).length() );
208 void StringTestCase::Extraction()
210 wxString
s(_T("Hello, world!"));
212 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
213 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
214 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
215 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
216 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
217 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
218 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
221 static const char *germanUTF8
= "Oberfl\303\244che";
222 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
224 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
225 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
226 #endif // wxUSE_UNICODE
230 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
231 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
233 WX_ASSERT_STR_EQUAL(correct_rest, rest)
235 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
236 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
237 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
238 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
239 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
240 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
241 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
243 #undef TEST_STARTS_WITH
245 rest
= "Hello world";
246 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
247 WX_ASSERT_STR_EQUAL("world", rest
);
249 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
250 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
252 WX_ASSERT_STR_EQUAL(correct_rest, rest)
254 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
255 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
256 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
257 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
258 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
259 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
260 TEST_ENDS_WITH( _T("?"), _T(""), false );
261 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
262 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
264 #undef TEST_ENDS_WITH
267 void StringTestCase::Trim()
269 #define TEST_TRIM( str , dir , result ) \
270 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
272 TEST_TRIM( _T(" Test "), true, _T(" Test") );
273 TEST_TRIM( _T(" "), true, _T("") );
274 TEST_TRIM( _T(" "), true, _T("") );
275 TEST_TRIM( _T(""), true, _T("") );
277 TEST_TRIM( _T(" Test "), false, _T("Test ") );
278 TEST_TRIM( _T(" "), false, _T("") );
279 TEST_TRIM( _T(" "), false, _T("") );
280 TEST_TRIM( _T(""), false, _T("") );
285 void StringTestCase::Find()
287 #define TEST_FIND( str , start , result ) \
288 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
290 TEST_FIND( _T("Well, hello world"), 0, 1 );
291 TEST_FIND( _T("Well, hello world"), 6, 7 );
292 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
297 void StringTestCase::Replace()
299 #define TEST_REPLACE( original , pos , len , replacement , result ) \
301 wxString s = original; \
302 s.replace( pos , len , replacement ); \
303 WX_ASSERT_STR_EQUAL( result, s ); \
306 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
307 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
308 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
309 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
310 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
313 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
315 wxString s(o,olen); \
316 s.replace( pos , len , replacement ); \
317 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
320 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
321 _T("null\0dhar"), 9 );
323 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
325 wxString s(o,olen); \
326 s.Replace( olds, news, all ); \
327 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
330 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
331 _T("null\0dehar"), 10 );
333 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
334 _T("null\0char"), 9 );
336 #undef TEST_WXREPLACE
337 #undef TEST_NULLCHARREPLACE
341 void StringTestCase::Match()
343 #define TEST_MATCH( s1 , s2 , result ) \
344 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
346 TEST_MATCH( _T("foobar"), _T("foo*"), true );
347 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
348 TEST_MATCH( _T("foobar"), _T("*bar"), true );
349 TEST_MATCH( _T("foobar"), _T("??????"), true );
350 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
351 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
352 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
353 TEST_MATCH( _T("foobar"), _T("*foo"), false );
354 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
355 TEST_MATCH( _T(""), _T("*"), true );
356 TEST_MATCH( _T(""), _T("?"), false );
362 void StringTestCase::CaseChanges()
364 wxString
s1(_T("Hello!"));
373 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
374 CPPUNIT_ASSERT( s1l
== _T("hello!") );
375 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
376 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
379 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
382 // try upper casing 8bit strings
383 const wchar_t capital_ya
[] = { 0x42f, 0 },
384 small_ya
[] = { 0x44f, 0 };
386 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
387 sLower(wxConvLibc
.cWC2MB(small_ya
));
389 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
390 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
392 #endif // !wxUSE_UNICODE
395 void StringTestCase::Compare()
397 wxString s1
= wxT("AHH");
398 wxString eq
= wxT("AHH");
399 wxString neq1
= wxT("HAH");
400 wxString neq2
= wxT("AH");
401 wxString neq3
= wxT("AHHH");
402 wxString neq4
= wxT("AhH");
404 CPPUNIT_ASSERT( s1
== eq
);
405 CPPUNIT_ASSERT( s1
!= neq1
);
406 CPPUNIT_ASSERT( s1
!= neq2
);
407 CPPUNIT_ASSERT( s1
!= neq3
);
408 CPPUNIT_ASSERT( s1
!= neq4
);
410 CPPUNIT_ASSERT( s1
== wxT("AHH") );
411 CPPUNIT_ASSERT( s1
!= wxT("no") );
412 CPPUNIT_ASSERT( s1
< wxT("AZ") );
413 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
414 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
415 CPPUNIT_ASSERT( s1
> wxT("AA") );
416 CPPUNIT_ASSERT( s1
>= wxT("AA") );
417 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
419 // test comparison with C strings in Unicode build (must work in ANSI as
421 CPPUNIT_ASSERT( s1
== "AHH" );
422 CPPUNIT_ASSERT( s1
!= "no" );
423 CPPUNIT_ASSERT( s1
< "AZ" );
424 CPPUNIT_ASSERT( s1
<= "AZ" );
425 CPPUNIT_ASSERT( s1
<= "AHH" );
426 CPPUNIT_ASSERT( s1
> "AA" );
427 CPPUNIT_ASSERT( s1
>= "AA" );
428 CPPUNIT_ASSERT( s1
>= "AHH" );
430 // wxString _s1 = wxT("A\0HH");
431 // wxString _eq = wxT("A\0HH");
432 // wxString _neq1 = wxT("H\0AH");
433 // wxString _neq2 = wxT("A\0H");
434 // wxString _neq3 = wxT("A\0HHH");
435 // wxString _neq4 = wxT("A\0hH");
438 neq1
.insert(1,1,'\0');
439 neq2
.insert(1,1,'\0');
440 neq3
.insert(1,1,'\0');
441 neq4
.insert(1,1,'\0');
443 CPPUNIT_ASSERT( s1
== eq
);
444 CPPUNIT_ASSERT( s1
!= neq1
);
445 CPPUNIT_ASSERT( s1
!= neq2
);
446 CPPUNIT_ASSERT( s1
!= neq3
);
447 CPPUNIT_ASSERT( s1
!= neq4
);
450 void StringTestCase::CompareNoCase()
452 wxString s1
= wxT("AHH");
453 wxString eq
= wxT("AHH");
454 wxString eq2
= wxT("AhH");
455 wxString eq3
= wxT("ahh");
456 wxString neq
= wxT("HAH");
457 wxString neq2
= wxT("AH");
458 wxString neq3
= wxT("AHHH");
460 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
461 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
463 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
464 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
465 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
467 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
468 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
469 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
472 // wxString _s1 = wxT("A\0HH");
473 // wxString _eq = wxT("A\0HH");
474 // wxString _eq2 = wxT("A\0hH");
475 // wxString _eq3 = wxT("a\0hh");
476 // wxString _neq = wxT("H\0AH");
477 // wxString _neq2 = wxT("A\0H");
478 // wxString _neq3 = wxT("A\0HHH");
482 eq2
.insert(1,1,'\0');
483 eq3
.insert(1,1,'\0');
484 neq
.insert(1,1,'\0');
485 neq2
.insert(1,1,'\0');
486 neq3
.insert(1,1,'\0');
488 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
489 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
490 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
492 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
493 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
494 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
497 void StringTestCase::Contains()
499 static const struct ContainsData
502 const wxChar
*needle
;
506 { _T(""), _T(""), true },
507 { _T(""), _T("foo"), false },
508 { _T("foo"), _T(""), true },
509 { _T("foo"), _T("f"), true },
510 { _T("foo"), _T("o"), true },
511 { _T("foo"), _T("oo"), true },
512 { _T("foo"), _T("ooo"), false },
513 { _T("foo"), _T("oooo"), false },
514 { _T("foo"), _T("fooo"), false },
517 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
519 const ContainsData
& cd
= containsData
[n
];
520 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
524 // flags used in ToLongData.flags
529 Number_Unsigned
= 2, // if not specified, works for signed conversion
530 Number_Signed
= 4, // if not specified, works for unsigned
531 Number_LongLong
= 8, // only for long long tests
532 Number_Long
= 16, // only for long tests
535 static const struct ToLongData
542 #endif // wxLongLong_t
545 long LValue() const { return value
; }
546 unsigned long ULValue() const { return value
; }
548 wxLongLong_t
LLValue() const { return value
; }
549 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
550 #endif // wxLongLong_t
552 bool IsOk() const { return !(flags
& Number_Invalid
); }
555 { _T("1"), 1, Number_Ok
},
556 { _T("0"), 0, Number_Ok
},
557 { _T("a"), 0, Number_Invalid
},
558 { _T("12345"), 12345, Number_Ok
},
559 { _T("--1"), 0, Number_Invalid
},
561 { _T("-1"), -1, Number_Signed
| Number_Long
},
562 // this is surprizing but consistent with strtoul() behaviour
563 { _T("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
565 // this must overflow, even with 64 bit long
566 { _T("922337203685477580711"), 0, Number_Invalid
},
569 { _T("2147483648"), wxLL(2147483648), Number_LongLong
},
570 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
571 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
573 #endif // wxLongLong_t
576 void StringTestCase::ToLong()
579 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
581 const ToLongData
& ld
= longData
[n
];
583 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
586 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
588 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
592 void StringTestCase::ToULong()
595 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
597 const ToLongData
& ld
= longData
[n
];
599 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
602 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
604 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
610 void StringTestCase::ToLongLong()
613 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
615 const ToLongData
& ld
= longData
[n
];
617 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
620 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
622 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
626 void StringTestCase::ToULongLong()
629 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
631 const ToLongData
& ld
= longData
[n
];
633 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
636 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
638 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
642 #endif // wxLongLong_t
644 void StringTestCase::ToDouble()
647 static const struct ToDoubleData
654 { _T("1"), 1, true },
655 { _T("1.23"), 1.23, true },
656 { _T(".1"), .1, true },
657 { _T("1."), 1, true },
658 { _T("1.."), 0, false },
659 { _T("0"), 0, true },
660 { _T("a"), 0, false },
661 { _T("12345"), 12345, true },
662 { _T("-1"), -1, true },
663 { _T("--1"), 0, false },
666 // we need to use decimal point, not comma or whatever is its value for the
668 wxSetlocale(LC_ALL
, "C");
671 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
673 const ToDoubleData
& ld
= doubleData
[n
];
674 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
676 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
680 void StringTestCase::WriteBuf()
683 wxStrcpy(wxStringBuffer(s
, 10), _T("foo"));
685 CPPUNIT_ASSERT(s
[0u] == _T('f') );
686 CPPUNIT_ASSERT(_T('f') == s
[0u]);
687 CPPUNIT_ASSERT(_T('o') == s
[1]);
688 CPPUNIT_ASSERT(_T('o') == s
[2]);
689 WX_ASSERT_SIZET_EQUAL(3, s
.length());
693 wxStringBufferLength
buf(s
, 10);
694 wxStrcpy(buf
, _T("barrbaz"));
698 CPPUNIT_ASSERT(_T('b') == s
[0u]);
699 CPPUNIT_ASSERT(_T('a') == s
[1]);
700 CPPUNIT_ASSERT(_T('r') == s
[2]);
701 CPPUNIT_ASSERT(_T('r') == s
[3]);
702 WX_ASSERT_SIZET_EQUAL(4, s
.length());
704 CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s
) );
707 void StringTestCase::UTF8Buf()
710 // "czech" in Czech ("cestina"):
711 static const char *textUTF8
= "\304\215e\305\241tina";
712 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
715 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
716 CPPUNIT_ASSERT(s
== textUTF16
);
719 wxUTF8StringBufferLength
buf(s
, 20);
720 wxStrcpy(buf
, textUTF8
);
723 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
724 #endif // wxUSE_UNICODE
729 void StringTestCase::CStrDataTernaryOperator()
731 DoCStrDataTernaryOperator(true);
732 DoCStrDataTernaryOperator(false);
735 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
737 return expected
== wxString(s
);
740 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
742 // test compilation of wxCStrData when used with operator?: (the asserts
743 // are not very important, we're testing if the code compiles at all):
747 const wchar_t *wcStr
= L
"foo";
748 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
749 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
750 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
751 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
753 const char *mbStr
= "foo";
754 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
755 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
756 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
757 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
760 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
761 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
764 void StringTestCase::CStrDataOperators()
768 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
769 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
770 CPPUNIT_ASSERT( s
.c_str()[5] == '\0' );
772 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
773 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
774 CPPUNIT_ASSERT( *(s
.c_str() + 5) == '\0' );
777 bool CheckStrChar(const wxString
& expected
, char *s
)
778 { return CheckStr(expected
, s
); }
779 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
780 { return CheckStr(expected
, s
); }
781 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
782 { return CheckStr(expected
, s
); }
783 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
784 { return CheckStr(expected
, s
); }
786 void StringTestCase::CStrDataImplicitConversion()
790 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
791 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
793 // implicit conversion of wxString is not available in STL build
795 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
796 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
800 void StringTestCase::ExplicitConversion()
804 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
805 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
806 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
808 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
809 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
810 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );