X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/421274fade3347a7ca7cfea2e86473dab7f8837e..69deca265ea7f4d53496bafddec79a8536cfde02:/tests/strings/tokenizer.cpp?ds=inline diff --git a/tests/strings/tokenizer.cpp b/tests/strings/tokenizer.cpp index 8394d5a761..c22c5ba3b5 100644 --- a/tests/strings/tokenizer.cpp +++ b/tests/strings/tokenizer.cpp @@ -36,11 +36,15 @@ private: CPPUNIT_TEST_SUITE( TokenizerTestCase ); CPPUNIT_TEST( GetCount ); CPPUNIT_TEST( GetPosition ); + CPPUNIT_TEST( GetString ); + CPPUNIT_TEST( LastDelimiter ); CPPUNIT_TEST( StrtokCompat ); CPPUNIT_TEST_SUITE_END(); void GetCount(); void GetPosition(); + void GetString(); + void LastDelimiter(); void StrtokCompat(); DECLARE_NO_COPY_CLASS(TokenizerTestCase) @@ -72,40 +76,57 @@ static const struct TokenizerTestData } gs_testData[] = { - { _T(""), _T(" "), wxTOKEN_DEFAULT , 0 }, - - { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT , 2 }, - { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT , 2 }, - { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT , 2 }, - { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT , 2 }, - { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT , 3 }, - { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK , 2 }, + { _T(""), _T(" "), wxTOKEN_DEFAULT, 0 }, + { _T(""), _T(" "), wxTOKEN_RET_EMPTY, 0 }, + { _T(""), _T(" "), wxTOKEN_RET_EMPTY_ALL, 0 }, + { _T(""), _T(" "), wxTOKEN_RET_DELIMS, 0 }, + { _T(":"), _T(":"), wxTOKEN_RET_EMPTY, 1 }, + { _T(":"), _T(":"), wxTOKEN_RET_DELIMS, 1 }, + { _T(":"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 2 }, + { _T("::"), _T(":"), wxTOKEN_RET_EMPTY, 1 }, + { _T("::"), _T(":"), wxTOKEN_RET_DELIMS, 1 }, + { _T("::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 3 }, + + { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT, 2 }, + { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT, 2 }, + { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT, 2 }, + { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT, 2 }, + { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT, 3 }, + { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK, 2 }, { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL, 3 }, { _T("username:password:uid:gid:gecos:home:shell"), - _T(":"), wxTOKEN_DEFAULT , 7 }, + _T(":"), wxTOKEN_DEFAULT, 7 }, - { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT , 4 }, - { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY , 4 }, + { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT, 4 }, + { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY, 4 }, { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 5 }, - { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS , 4 }, - { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK , 3 }, + { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS, 4 }, + { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK, 3 }, - { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT , 5 }, - { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY , 5 }, + { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT, 4 }, + { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY, 4 }, { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 6 }, - { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS , 5 }, - { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK , 3 }, + { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS, 4 }, + { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK, 3 }, - { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT , 4 }, - { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK , 4 }, - { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY , 6 }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT, 4 }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK, 4 }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY, 6 }, { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY_ALL, 9 }, - { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT , 3 }, - { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS , 3 }, + { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT, 3 }, + { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS, 3 }, }; +// helper function returning the string showing the index for which the test +// fails in the diagnostic message +static std::string Nth(size_t n) +{ + return std::string(wxString::Format(_T("for loop index %lu"), + (unsigned long)n).mb_str()); +} + // ---------------------------------------------------------------------------- // the tests // ---------------------------------------------------------------------------- @@ -117,7 +138,7 @@ void TokenizerTestCase::GetCount() const TokenizerTestData& ttd = gs_testData[n]; wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode); - CPPUNIT_ASSERT( tkz.CountTokens() == ttd.count ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() ); size_t count = 0; while ( tkz.HasMoreTokens() ) @@ -126,7 +147,7 @@ void TokenizerTestCase::GetCount() count++; } - CPPUNIT_ASSERT_EQUAL( ttd.count, count ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count ); } } @@ -168,6 +189,60 @@ void TokenizerTestCase::GetPosition() DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0); } +// helper for GetString(): the parameters are the same as for DoTestGetPosition +// but it checks GetString() return value instead of GetPosition() +static void +DoTestGetString(const wxChar *s, const wxChar *delims, int pos, ...) +{ + wxStringTokenizer tkz(s, delims); + + CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetString() ); + + va_list ap; + va_start(ap, pos); + + for ( ;; ) + { + if ( !pos ) + { + CPPUNIT_ASSERT( tkz.GetString().empty() ) ; + break; + } + + tkz.GetNextToken(); + + CPPUNIT_ASSERT_EQUAL( wxString(s + pos), tkz.GetString() ); + + pos = va_arg(ap, int); + } + + va_end(ap); +} + +void TokenizerTestCase::GetString() +{ + DoTestGetString(_T("foo"), _T("_"), 3, 0); + DoTestGetString(_T("foo_bar"), _T("_"), 4, 7, 0); + DoTestGetString(_T("foo_bar_"), _T("_"), 4, 8, 0); +} + +void TokenizerTestCase::LastDelimiter() +{ + wxStringTokenizer tkz(_T("a+-b=c"), _T("+-=")); + + tkz.GetNextToken(); + CPPUNIT_ASSERT_EQUAL( _T('+'), tkz.GetLastDelimiter() ); + + tkz.GetNextToken(); + CPPUNIT_ASSERT_EQUAL( _T('-'), tkz.GetLastDelimiter() ); + + tkz.GetNextToken(); + CPPUNIT_ASSERT_EQUAL( _T('='), tkz.GetLastDelimiter() ); + + tkz.GetNextToken(); + CPPUNIT_ASSERT_EQUAL( _T('\0'), tkz.GetLastDelimiter() ); +} + void TokenizerTestCase::StrtokCompat() { for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ ) @@ -176,7 +251,12 @@ void TokenizerTestCase::StrtokCompat() if ( ttd.mode != wxTOKEN_STRTOK ) continue; - wxCharBuffer buf(ttd.str); +#if wxUSE_UNICODE + wxWCharBuffer +#else + wxCharBuffer +#endif + buf(ttd.str); wxChar *last; wxChar *s = wxStrtok(buf.data(), ttd.delims, &last);