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( Extraction
);
42 CPPUNIT_TEST( Replace
);
43 CPPUNIT_TEST( Match
);
44 CPPUNIT_TEST( CaseChanges
);
45 CPPUNIT_TEST( Compare
);
46 CPPUNIT_TEST( CompareNoCase
);
47 CPPUNIT_TEST( Contains
);
48 CPPUNIT_TEST( ToLong
);
49 CPPUNIT_TEST( ToULong
);
51 CPPUNIT_TEST( ToLongLong
);
52 CPPUNIT_TEST( ToULongLong
);
53 #endif // wxLongLong_t
54 CPPUNIT_TEST( ToDouble
);
55 CPPUNIT_TEST( WriteBuf
);
56 CPPUNIT_TEST( UTF8Buf
);
57 CPPUNIT_TEST( CStrDataTernaryOperator
);
58 CPPUNIT_TEST( CStrDataOperators
);
59 CPPUNIT_TEST( CStrDataImplicitConversion
);
60 CPPUNIT_TEST( ExplicitConversion
);
61 CPPUNIT_TEST_SUITE_END();
81 #endif // wxLongLong_t
85 void CStrDataTernaryOperator();
86 void DoCStrDataTernaryOperator(bool cond
);
87 void CStrDataOperators();
88 void CStrDataImplicitConversion();
89 void ExplicitConversion();
91 DECLARE_NO_COPY_CLASS(StringTestCase
)
94 // register in the unnamed registry so that these tests are run by default
95 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
97 // also include in it's own registry so that these tests can be run alone
98 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
100 StringTestCase::StringTestCase()
104 void StringTestCase::String()
112 for (int i
= 0; i
< 2; ++i
)
116 c
= _T("! How'ya doin'?");
119 c
= _T("Hello world! What's up?");
120 CPPUNIT_ASSERT( c
!= a
);
124 void StringTestCase::PChar()
130 for (int i
= 0; i
< 2; ++i
)
132 wxStrcpy (a
, _T("Hello"));
133 wxStrcpy (b
, _T(" world"));
134 wxStrcpy (c
, _T("! How'ya doin'?"));
137 wxStrcpy (c
, _T("Hello world! What's up?"));
138 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
142 void StringTestCase::Format()
145 s1
.Printf(_T("%03d"), 18);
146 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
147 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
148 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
150 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
151 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
153 const size_t len
= lengths
[n
];
155 wxString
s(_T('Z'), len
);
156 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
160 void StringTestCase::Constructors()
162 #define TEST_CTOR(args, res) \
165 CPPUNIT_ASSERT( s == res ); \
168 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
169 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
170 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
172 static const wxChar
*s
= _T("?really!");
173 const wxChar
*start
= wxStrchr(s
, _T('r'));
174 const wxChar
*end
= wxStrchr(s
, _T('!'));
175 TEST_CTOR((start
, end
), _T("really"));
177 // test if creating string from NULL C pointer works:
178 TEST_CTOR(((char*)NULL
), "");
182 void StringTestCase::Extraction()
184 wxString
s(_T("Hello, world!"));
186 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
187 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
188 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
189 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
190 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
191 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
192 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
195 static const char *germanUTF8
= "Oberfl\303\244che";
196 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
198 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
199 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
200 #endif // wxUSE_UNICODE
204 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
205 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
207 WX_ASSERT_STR_EQUAL(correct_rest, rest)
209 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
210 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
211 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
212 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
213 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
214 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
215 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
217 #undef TEST_STARTS_WITH
219 rest
= "Hello world";
220 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
221 WX_ASSERT_STR_EQUAL("world", rest
);
223 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
224 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
226 WX_ASSERT_STR_EQUAL(correct_rest, rest)
228 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
229 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
230 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
231 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
232 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
233 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
234 TEST_ENDS_WITH( _T("?"), _T(""), false );
235 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
236 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
238 #undef TEST_ENDS_WITH
241 void StringTestCase::Trim()
243 #define TEST_TRIM( str , dir , result ) \
244 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
246 TEST_TRIM( _T(" Test "), true, _T(" Test") );
247 TEST_TRIM( _T(" "), true, _T("") );
248 TEST_TRIM( _T(" "), true, _T("") );
249 TEST_TRIM( _T(""), true, _T("") );
251 TEST_TRIM( _T(" Test "), false, _T("Test ") );
252 TEST_TRIM( _T(" "), false, _T("") );
253 TEST_TRIM( _T(" "), false, _T("") );
254 TEST_TRIM( _T(""), false, _T("") );
259 void StringTestCase::Find()
261 #define TEST_FIND( str , start , result ) \
262 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
264 TEST_FIND( _T("Well, hello world"), 0, 1 );
265 TEST_FIND( _T("Well, hello world"), 6, 7 );
266 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
271 void StringTestCase::Replace()
273 #define TEST_REPLACE( original , pos , len , replacement , result ) \
275 wxString s = original; \
276 s.replace( pos , len , replacement ); \
277 WX_ASSERT_STR_EQUAL( result, s ); \
280 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
281 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
282 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
283 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
284 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
287 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
289 wxString s(o,olen); \
290 s.replace( pos , len , replacement ); \
291 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
294 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
295 _T("null\0dhar"), 9 );
297 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
299 wxString s(o,olen); \
300 s.Replace( olds, news, all ); \
301 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
304 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
305 _T("null\0dehar"), 10 );
307 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
308 _T("null\0char"), 9 );
310 #undef TEST_WXREPLACE
311 #undef TEST_NULLCHARREPLACE
315 void StringTestCase::Match()
317 #define TEST_MATCH( s1 , s2 , result ) \
318 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
320 TEST_MATCH( _T("foobar"), _T("foo*"), true );
321 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
322 TEST_MATCH( _T("foobar"), _T("*bar"), true );
323 TEST_MATCH( _T("foobar"), _T("??????"), true );
324 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
325 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
326 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
327 TEST_MATCH( _T("foobar"), _T("*foo"), false );
328 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
329 TEST_MATCH( _T(""), _T("*"), true );
330 TEST_MATCH( _T(""), _T("?"), false );
336 void StringTestCase::CaseChanges()
338 wxString
s1(_T("Hello!"));
347 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
348 CPPUNIT_ASSERT( s1l
== _T("hello!") );
349 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
350 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
353 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
356 // try upper casing 8bit strings
357 const wchar_t capital_ya
[] = { 0x42f, 0 },
358 small_ya
[] = { 0x44f, 0 };
360 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
361 sLower(wxConvLibc
.cWC2MB(small_ya
));
363 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
364 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
366 #endif // !wxUSE_UNICODE
369 void StringTestCase::Compare()
371 wxString s1
= wxT("AHH");
372 wxString eq
= wxT("AHH");
373 wxString neq1
= wxT("HAH");
374 wxString neq2
= wxT("AH");
375 wxString neq3
= wxT("AHHH");
376 wxString neq4
= wxT("AhH");
378 CPPUNIT_ASSERT( s1
== eq
);
379 CPPUNIT_ASSERT( s1
!= neq1
);
380 CPPUNIT_ASSERT( s1
!= neq2
);
381 CPPUNIT_ASSERT( s1
!= neq3
);
382 CPPUNIT_ASSERT( s1
!= neq4
);
384 CPPUNIT_ASSERT( s1
== wxT("AHH") );
385 CPPUNIT_ASSERT( s1
!= wxT("no") );
386 CPPUNIT_ASSERT( s1
< wxT("AZ") );
387 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
388 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
389 CPPUNIT_ASSERT( s1
> wxT("AA") );
390 CPPUNIT_ASSERT( s1
>= wxT("AA") );
391 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
393 // test comparison with C strings in Unicode build (must work in ANSI as
395 CPPUNIT_ASSERT( s1
== "AHH" );
396 CPPUNIT_ASSERT( s1
!= "no" );
397 CPPUNIT_ASSERT( s1
< "AZ" );
398 CPPUNIT_ASSERT( s1
<= "AZ" );
399 CPPUNIT_ASSERT( s1
<= "AHH" );
400 CPPUNIT_ASSERT( s1
> "AA" );
401 CPPUNIT_ASSERT( s1
>= "AA" );
402 CPPUNIT_ASSERT( s1
>= "AHH" );
404 // wxString _s1 = wxT("A\0HH");
405 // wxString _eq = wxT("A\0HH");
406 // wxString _neq1 = wxT("H\0AH");
407 // wxString _neq2 = wxT("A\0H");
408 // wxString _neq3 = wxT("A\0HHH");
409 // wxString _neq4 = wxT("A\0hH");
412 neq1
.insert(1,1,'\0');
413 neq2
.insert(1,1,'\0');
414 neq3
.insert(1,1,'\0');
415 neq4
.insert(1,1,'\0');
417 CPPUNIT_ASSERT( s1
== eq
);
418 CPPUNIT_ASSERT( s1
!= neq1
);
419 CPPUNIT_ASSERT( s1
!= neq2
);
420 CPPUNIT_ASSERT( s1
!= neq3
);
421 CPPUNIT_ASSERT( s1
!= neq4
);
424 void StringTestCase::CompareNoCase()
426 wxString s1
= wxT("AHH");
427 wxString eq
= wxT("AHH");
428 wxString eq2
= wxT("AhH");
429 wxString eq3
= wxT("ahh");
430 wxString neq
= wxT("HAH");
431 wxString neq2
= wxT("AH");
432 wxString neq3
= wxT("AHHH");
434 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
435 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
437 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
438 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
439 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
441 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
442 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
443 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
446 // wxString _s1 = wxT("A\0HH");
447 // wxString _eq = wxT("A\0HH");
448 // wxString _eq2 = wxT("A\0hH");
449 // wxString _eq3 = wxT("a\0hh");
450 // wxString _neq = wxT("H\0AH");
451 // wxString _neq2 = wxT("A\0H");
452 // wxString _neq3 = wxT("A\0HHH");
456 eq2
.insert(1,1,'\0');
457 eq3
.insert(1,1,'\0');
458 neq
.insert(1,1,'\0');
459 neq2
.insert(1,1,'\0');
460 neq3
.insert(1,1,'\0');
462 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
463 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
464 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
466 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
467 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
468 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
471 void StringTestCase::Contains()
473 static const struct ContainsData
476 const wxChar
*needle
;
480 { _T(""), _T(""), true },
481 { _T(""), _T("foo"), false },
482 { _T("foo"), _T(""), true },
483 { _T("foo"), _T("f"), true },
484 { _T("foo"), _T("o"), true },
485 { _T("foo"), _T("oo"), true },
486 { _T("foo"), _T("ooo"), false },
487 { _T("foo"), _T("oooo"), false },
488 { _T("foo"), _T("fooo"), false },
491 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
493 const ContainsData
& cd
= containsData
[n
];
494 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
498 // flags used in ToLongData.flags
503 Number_Unsigned
= 2, // if not specified, works for signed conversion
504 Number_Signed
= 4, // if not specified, works for unsigned
505 Number_LongLong
= 8, // only for long long tests
506 Number_Long
= 16, // only for long tests
509 static const struct ToLongData
516 #endif // wxLongLong_t
519 long LValue() const { return value
; }
520 unsigned long ULValue() const { return value
; }
522 wxLongLong_t
LLValue() const { return value
; }
523 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
524 #endif // wxLongLong_t
526 bool IsOk() const { return !(flags
& Number_Invalid
); }
529 { _T("1"), 1, Number_Ok
},
530 { _T("0"), 0, Number_Ok
},
531 { _T("a"), 0, Number_Invalid
},
532 { _T("12345"), 12345, Number_Ok
},
533 { _T("--1"), 0, Number_Invalid
},
535 { _T("-1"), -1, Number_Signed
| Number_Long
},
536 // this is surprizing but consistent with strtoul() behaviour
537 { _T("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
539 // this must overflow, even with 64 bit long
540 { _T("922337203685477580711"), 0, Number_Invalid
},
543 { _T("2147483648"), wxLL(2147483648), Number_LongLong
},
544 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
545 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
547 #endif // wxLongLong_t
550 void StringTestCase::ToLong()
553 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
555 const ToLongData
& ld
= longData
[n
];
557 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
560 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
562 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
566 void StringTestCase::ToULong()
569 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
571 const ToLongData
& ld
= longData
[n
];
573 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
576 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
578 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
584 void StringTestCase::ToLongLong()
587 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
589 const ToLongData
& ld
= longData
[n
];
591 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
594 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
596 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
600 void StringTestCase::ToULongLong()
603 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
605 const ToLongData
& ld
= longData
[n
];
607 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
610 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
612 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
616 #endif // wxLongLong_t
618 void StringTestCase::ToDouble()
621 static const struct ToDoubleData
628 { _T("1"), 1, true },
629 { _T("1.23"), 1.23, true },
630 { _T(".1"), .1, true },
631 { _T("1."), 1, true },
632 { _T("1.."), 0, false },
633 { _T("0"), 0, true },
634 { _T("a"), 0, false },
635 { _T("12345"), 12345, true },
636 { _T("-1"), -1, true },
637 { _T("--1"), 0, false },
640 // we need to use decimal point, not comma or whatever is its value for the
642 wxSetlocale(LC_ALL
, "C");
645 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
647 const ToDoubleData
& ld
= doubleData
[n
];
648 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
650 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
654 void StringTestCase::WriteBuf()
657 wxStrcpy(wxStringBuffer(s
, 10), _T("foo"));
659 CPPUNIT_ASSERT(s
[0u] == _T('f') );
660 CPPUNIT_ASSERT(_T('f') == s
[0u]);
661 CPPUNIT_ASSERT(_T('o') == s
[1]);
662 CPPUNIT_ASSERT(_T('o') == s
[2]);
663 WX_ASSERT_SIZET_EQUAL(3, s
.length());
667 wxStringBufferLength
buf(s
, 10);
668 wxStrcpy(buf
, _T("barrbaz"));
672 CPPUNIT_ASSERT(_T('b') == s
[0u]);
673 CPPUNIT_ASSERT(_T('a') == s
[1]);
674 CPPUNIT_ASSERT(_T('r') == s
[2]);
675 CPPUNIT_ASSERT(_T('r') == s
[3]);
676 WX_ASSERT_SIZET_EQUAL(4, s
.length());
678 CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s
) );
681 void StringTestCase::UTF8Buf()
684 // "czech" in Czech ("cestina"):
685 static const char *textUTF8
= "\304\215e\305\241tina";
686 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
689 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
690 CPPUNIT_ASSERT(s
== textUTF16
);
693 wxUTF8StringBufferLength
buf(s
, 20);
694 wxStrcpy(buf
, textUTF8
);
697 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
698 #endif // wxUSE_UNICODE
703 void StringTestCase::CStrDataTernaryOperator()
705 DoCStrDataTernaryOperator(true);
706 DoCStrDataTernaryOperator(false);
709 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
711 return expected
== wxString(s
);
714 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
716 // test compilation of wxCStrData when used with operator?: (the asserts
717 // are not very important, we're testing if the code compiles at all):
721 const wchar_t *wcStr
= L
"foo";
722 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
723 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
724 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
725 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
727 const char *mbStr
= "foo";
728 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
729 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
730 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
731 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
734 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
735 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
738 void StringTestCase::CStrDataOperators()
742 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
743 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
744 CPPUNIT_ASSERT( s
.c_str()[5] == '\0' );
746 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
747 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
748 CPPUNIT_ASSERT( *(s
.c_str() + 5) == '\0' );
751 bool CheckStrChar(const wxString
& expected
, char *s
)
752 { return CheckStr(expected
, s
); }
753 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
754 { return CheckStr(expected
, s
); }
755 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
756 { return CheckStr(expected
, s
); }
757 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
758 { return CheckStr(expected
, s
); }
760 void StringTestCase::CStrDataImplicitConversion()
764 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
765 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
767 // implicit conversion of wxString is not available in STL build
769 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
770 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
774 void StringTestCase::ExplicitConversion()
778 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
779 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
780 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
782 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
783 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
784 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );