X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7c5ac499c84648ef1f58afa1187656f5d72c56d8..b736d59eb531794e6b1cef2b4997c517569ff0dd:/tests/strings/crt.cpp diff --git a/tests/strings/crt.cpp b/tests/strings/crt.cpp index 8855068748..7f108749c0 100644 --- a/tests/strings/crt.cpp +++ b/tests/strings/crt.cpp @@ -3,7 +3,6 @@ // Purpose: Test for wx C runtime functions wrappers // Author: Vaclav Slavik // Created: 2004-06-03 -// RCS-ID: $Id$ // Copyright: (c) 2004 Vaclav Slavik // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -44,6 +43,9 @@ public: private: CPPUNIT_TEST_SUITE( CrtTestCase ); CPPUNIT_TEST( SetGetEnv ); +#if wxUSE_UNICODE + CPPUNIT_TEST( Strchr ); +#endif // wxUSE_UNICODE CPPUNIT_TEST( Strcmp ); CPPUNIT_TEST( Strspn ); CPPUNIT_TEST( Strcspn ); @@ -52,6 +54,9 @@ private: CPPUNIT_TEST_SUITE_END(); void SetGetEnv(); +#if wxUSE_UNICODE + void Strchr(); +#endif // wxUSE_UNICODE void Strcmp(); void Strspn(); void Strcspn(); @@ -64,21 +69,45 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( CrtTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" ); void CrtTestCase::SetGetEnv() { +#define TESTVAR_NAME wxT("WXTESTVAR") + wxString val; - wxSetEnv(_T("TESTVAR"), _T("value")); - CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) == true ); - CPPUNIT_ASSERT( val == _T("value") ); - wxSetEnv(_T("TESTVAR"), _T("something else")); - CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) ); - CPPUNIT_ASSERT( val == _T("something else") ); - CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) ); - CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL) == false ); + wxSetEnv(TESTVAR_NAME, wxT("value")); + CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) ); + CPPUNIT_ASSERT_EQUAL( "value", val ); + CPPUNIT_ASSERT_EQUAL( "value", wxString(wxGetenv(TESTVAR_NAME)) ); + + wxSetEnv(TESTVAR_NAME, wxT("something else")); + CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) ); + CPPUNIT_ASSERT_EQUAL( "something else", val ); + CPPUNIT_ASSERT_EQUAL( "something else", wxString(wxGetenv(TESTVAR_NAME)) ); + + CPPUNIT_ASSERT( wxUnsetEnv(TESTVAR_NAME) ); + CPPUNIT_ASSERT( !wxGetEnv(TESTVAR_NAME, NULL) ); + CPPUNIT_ASSERT( !wxGetenv(TESTVAR_NAME) ); + +#undef TESTVAR_NAME +} + +#if wxUSE_UNICODE +void CrtTestCase::Strchr() +{ + // test that searching for a wide character in a narrow string simply + // doesn't find it but doesn't fail with an assert (#11487) + const wxUniChar smiley = *wxString::FromUTF8("\xe2\x98\xba").begin(); + + CPPUNIT_ASSERT( !wxStrchr("hello", smiley) ); + + // but searching for an explicitly wide character does find it + CPPUNIT_ASSERT( wxStrchr(wxString::FromUTF8(":-) == \xe2\x98\xba"), + static_cast(smiley)) ); } +#endif // wxUSE_UNICODE void CrtTestCase::Strcmp() { @@ -207,18 +236,12 @@ void CrtTestCase::Strpbrk() CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.mb_str()) ); CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX.c_str(), s.wc_str()) ); - CPPUNIT_ASSERT_EQUAL( (char *)NULL, wxStrpbrk(strWX, "xyz") ); - CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL, wxStrpbrk(strWX.c_str(), L"xyz") ); + CPPUNIT_ASSERT( !wxStrpbrk(strWX, "xyz") ); + CPPUNIT_ASSERT( !wxStrpbrk(strWX.c_str(), L"xyz") ); } void CrtTestCase::Strnlen() { - // the following calls should not segfault/crash because when passing 0 - // as maxlen, no bytes at all should be read from the given buffer - // (which is NULL in this case!) - CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen((const char*)NULL, 0) ); - CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen((const wchar_t*)NULL, 0) ); - // other misc tests for wxStrnlen(const char*, size_t) CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 0) );