X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/af9c764aa16404608ffd7b9664006683e28f13bd..3c01c5951189e13b8b4a5d7b288b54d4a57a30f8:/tests/strings/crt.cpp?ds=inline diff --git a/tests/strings/crt.cpp b/tests/strings/crt.cpp index 68b752c295..e4e69caa16 100644 --- a/tests/strings/crt.cpp +++ b/tests/strings/crt.cpp @@ -4,7 +4,7 @@ // Author: Vaclav Slavik // Created: 2004-06-03 // RCS-ID: $Id$ -// Copyright: (c) 2004 Vaclav Slavik +// Copyright: (c) 2004 Vaclav Slavik // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -47,12 +47,16 @@ private: CPPUNIT_TEST( Strcmp ); CPPUNIT_TEST( Strspn ); CPPUNIT_TEST( Strcspn ); + CPPUNIT_TEST( Strpbrk ); + CPPUNIT_TEST( Strnlen ); CPPUNIT_TEST_SUITE_END(); void SetGetEnv(); void Strcmp(); void Strspn(); void Strcspn(); + void Strpbrk(); + void Strnlen(); DECLARE_NO_COPY_CLASS(CrtTestCase) }; @@ -65,15 +69,24 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" ); void CrtTestCase::SetGetEnv() { +#define TESTVAR_NAME _T("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, _T("value")); + CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) ); + CPPUNIT_ASSERT_EQUAL( "value", val ); + CPPUNIT_ASSERT_EQUAL( "value", wxString(wxGetenv(TESTVAR_NAME)) ); + + wxSetEnv(TESTVAR_NAME, _T("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 } void CrtTestCase::Strcmp() @@ -138,42 +151,105 @@ void CrtTestCase::Strcmp() void CrtTestCase::Strspn() { - CPPUNIT_ASSERT( wxStrspn(strMB, "xyz") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWC, "xyz") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWX, "xyz") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strMB, L"xyz") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWC, L"xyz") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWX, L"xyz") == 0 ); - - CPPUNIT_ASSERT( wxStrspn(strMB, "hleo") == 5 ); - CPPUNIT_ASSERT( wxStrspn(strWC, "hleo") == 5 ); - CPPUNIT_ASSERT( wxStrspn(strWX, "hleo") == 5 ); - - CPPUNIT_ASSERT( wxStrspn(strMB, "ld") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWC, "ld") == 0 ); - CPPUNIT_ASSERT( wxStrspn(strWX, "ld") == 0 ); - - CPPUNIT_ASSERT( wxStrspn(strMB, strWC) == strWX.length() ); - CPPUNIT_ASSERT( wxStrspn(strWC, strWX) == strWX.length() ); - CPPUNIT_ASSERT( wxStrspn(strWX, strMB) == strWX.length() ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "xyz") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "xyz") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "xyz") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, L"xyz") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, L"xyz") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, L"xyz") ); + + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strMB, "hleo") ); + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWC, "hleo") ); + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWX, "hleo") ); + + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "ld") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "ld") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "ld") ); + + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strMB, strWC) ); + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWC, strWX) ); + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWX, strMB) ); } void CrtTestCase::Strcspn() { - CPPUNIT_ASSERT( wxStrcspn(strMB, strWX) == 0 ); - CPPUNIT_ASSERT( wxStrcspn(strWC, strMB) == 0 ); - CPPUNIT_ASSERT( wxStrcspn(strWX, strWC) == 0 ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, strWX) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, strMB) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, strWC) ); + + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strMB, ", ") ); + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWC, ", ") ); + CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWX, ", ") ); + + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, "hel") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, "hel") ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, "hel") ); - CPPUNIT_ASSERT( wxStrcspn(strMB, ", ") == 5 ); - CPPUNIT_ASSERT( wxStrcspn(strWC, ", ") == 5 ); - CPPUNIT_ASSERT( wxStrcspn(strWX, ", ") == 5 ); + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strMB, "xy") ); + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWC, "xy") ); + CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWX, "xy") ); +} - CPPUNIT_ASSERT( wxStrcspn(strMB, "hel") == 0 ); - CPPUNIT_ASSERT( wxStrcspn(strWC, "hel") == 0 ); - CPPUNIT_ASSERT( wxStrcspn(strWX, "hel") == 0 ); +void CrtTestCase::Strpbrk() +{ + const wxString s(", "); + + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, ", ") ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, L", ") ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, ", ") ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, L", ") ); + + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s) ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s) ); + + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.c_str()) ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.c_str()) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.c_str()) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.c_str()) ); + + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.mb_str()) ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.wc_str()) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.mb_str()) ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, s.wc_str()) ); + CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.mb_str()) ); + CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX.c_str(), s.wc_str()) ); + + CPPUNIT_ASSERT( !wxStrpbrk(strWX, "xyz") ); + CPPUNIT_ASSERT( !wxStrpbrk(strWX.c_str(), L"xyz") ); +} - CPPUNIT_ASSERT( wxStrcspn(strMB, "xy") == strWX.length() ); - CPPUNIT_ASSERT( wxStrcspn(strWC, "xy") == strWX.length() ); - CPPUNIT_ASSERT( wxStrcspn(strWX, "xy") == strWX.length() ); +void CrtTestCase::Strnlen() +{ + // other misc tests for wxStrnlen(const char*, size_t) + + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 0) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("1234", 0) ); + CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen("1234", 2) ); + CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen("1234567", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("12345678", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("123456789", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen("123456789", 12) ); + + // other misc tests for wxStrnlen(const wchar_t*, size_t) + + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 0) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"1234", 0) ); + CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen(L"1234", 2) ); + CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen(L"1234567", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"12345678", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"123456789", 8) ); + CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen(L"123456789", 12) ); + + // wxStrlen() is only for NULL-terminated strings: + CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234" "\0" "78", 12) ); + CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234" L"\0" L"5678", 12) ); } + +