X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4ca056ea2be22bacda4a7f0a0787803cd18b6b10..c8b26ef59ed823f0f5306880374d3828868b6494:/tests/strings/strings.cpp diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index b486a32142..e66615565b 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -53,7 +53,10 @@ private: #endif // wxLongLong_t CPPUNIT_TEST( ToDouble ); CPPUNIT_TEST( WriteBuf ); - CPPUNIT_TEST( CStrData ); + CPPUNIT_TEST( CStrDataTernaryOperator ); + CPPUNIT_TEST( CStrDataOperators ); + CPPUNIT_TEST( CStrDataImplicitConversion ); + CPPUNIT_TEST( ExplicitConversion ); CPPUNIT_TEST_SUITE_END(); void String(); @@ -77,8 +80,11 @@ private: #endif // wxLongLong_t void ToDouble(); void WriteBuf(); - void CStrData(); - void DoCStrData(bool cond); + void CStrDataTernaryOperator(); + void DoCStrDataTernaryOperator(bool cond); + void CStrDataOperators(); + void CStrDataImplicitConversion(); + void ExplicitConversion(); DECLARE_NO_COPY_CLASS(StringTestCase) }; @@ -657,10 +663,10 @@ void StringTestCase::WriteBuf() -void StringTestCase::CStrData() +void StringTestCase::CStrDataTernaryOperator() { - DoCStrData(true); - DoCStrData(false); + DoCStrDataTernaryOperator(true); + DoCStrDataTernaryOperator(false); } template bool CheckStr(const wxString& expected, T s) @@ -668,26 +674,72 @@ template bool CheckStr(const wxString& expected, T s) return expected == wxString(s); } -void StringTestCase::DoCStrData(bool cond) +void StringTestCase::DoCStrDataTernaryOperator(bool cond) { // test compilation of wxCStrData when used with operator?: (the asserts // are not very important, we're testing if the code compiles at all): wxString s("foo"); - const char *mbStr = "foo"; - const wchar_t *wcStr = L"foo"; - // FIXME-UTF8: when wxCStrData can handle both conversions, this should - // be changed to always test all versions, both MB and WC -#if wxUSE_UNICODE + const wchar_t *wcStr = L"foo"; CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) ); - CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"bar")) ); + CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) ); CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) ); - CPPUNIT_ASSERT( CheckStr(s, (cond ? L"bar" : s.c_str())) ); -#else + CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) ); + + const char *mbStr = "foo"; CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) ); CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) ); CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) ); CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) ); -#endif + + wxString empty(""); + CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) ); + CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) ); +} + +void StringTestCase::CStrDataOperators() +{ + wxString s("hello"); + + CPPUNIT_ASSERT( s.c_str()[0] == 'h' ); + CPPUNIT_ASSERT( s.c_str()[1] == 'e' ); + CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); + + CPPUNIT_ASSERT( *s.c_str() == 'h' ); + CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' ); + CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); +} + +bool CheckStrChar(const wxString& expected, char *s) + { return CheckStr(expected, s); } +bool CheckStrWChar(const wxString& expected, wchar_t *s) + { return CheckStr(expected, s); } +bool CheckStrConstChar(const wxString& expected, const char *s) + { return CheckStr(expected, s); } +bool CheckStrConstWChar(const wxString& expected, const wchar_t *s) + { return CheckStr(expected, s); } + +void StringTestCase::CStrDataImplicitConversion() +{ + wxString s("foo"); + + CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) ); + CPPUNIT_ASSERT( CheckStrConstWChar(s, s) ); + + CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) ); + CPPUNIT_ASSERT( CheckStrConstChar(s, s) ); +} + +void StringTestCase::ExplicitConversion() +{ + wxString s("foo"); + + CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) ); + CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) ); + CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) ); + + CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) ); + CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) ); + CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) ); }