2005-12-20 11:30:12 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/strings/strings.cpp
|
|
|
|
// Purpose: wxStringTokenizer unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2005-12-20 (extacted from strings.cpp)
|
|
|
|
// Copyright: (c) 2004-2005 Vadim Zeitlin
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/tokenzr.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TokenizerTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenizerTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( TokenizerTestCase );
|
|
|
|
CPPUNIT_TEST( GetCount );
|
|
|
|
CPPUNIT_TEST( GetPosition );
|
2006-01-06 16:33:39 -05:00
|
|
|
CPPUNIT_TEST( GetString );
|
2005-12-23 19:12:54 -05:00
|
|
|
CPPUNIT_TEST( LastDelimiter );
|
2005-12-20 11:30:12 -05:00
|
|
|
CPPUNIT_TEST( StrtokCompat );
|
2014-10-20 11:08:09 -04:00
|
|
|
CPPUNIT_TEST( CopyObj );
|
|
|
|
CPPUNIT_TEST( AssignObj );
|
2005-12-20 11:30:12 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void GetCount();
|
|
|
|
void GetPosition();
|
2006-01-06 16:33:39 -05:00
|
|
|
void GetString();
|
2005-12-23 19:12:54 -05:00
|
|
|
void LastDelimiter();
|
2005-12-20 11:30:12 -05:00
|
|
|
void StrtokCompat();
|
2014-10-20 11:08:09 -04:00
|
|
|
void CopyObj();
|
|
|
|
void AssignObj();
|
2005-12-20 11:30:12 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(TokenizerTestCase);
|
2005-12-20 11:30:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase );
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2005-12-20 11:30:12 -05:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase, "TokenizerTestCase" );
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test data
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static const struct TokenizerTestData
|
|
|
|
{
|
|
|
|
// the string to tokenize
|
|
|
|
const wxChar *str;
|
|
|
|
|
|
|
|
// the delimiters to use
|
|
|
|
const wxChar *delims;
|
|
|
|
|
|
|
|
// the tokenizer mode
|
|
|
|
wxStringTokenizerMode mode;
|
|
|
|
|
|
|
|
// expected number of tokens
|
|
|
|
size_t count;
|
|
|
|
}
|
|
|
|
gs_testData[] =
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
{ wxT(""), wxT(" "), wxTOKEN_DEFAULT, 0 },
|
|
|
|
{ wxT(""), wxT(" "), wxTOKEN_RET_EMPTY, 0 },
|
|
|
|
{ wxT(""), wxT(" "), wxTOKEN_RET_EMPTY_ALL, 0 },
|
|
|
|
{ wxT(""), wxT(" "), wxTOKEN_RET_DELIMS, 0 },
|
|
|
|
{ wxT(":"), wxT(":"), wxTOKEN_RET_EMPTY, 1 },
|
|
|
|
{ wxT(":"), wxT(":"), wxTOKEN_RET_DELIMS, 1 },
|
|
|
|
{ wxT(":"), wxT(":"), wxTOKEN_RET_EMPTY_ALL, 2 },
|
|
|
|
{ wxT("::"), wxT(":"), wxTOKEN_RET_EMPTY, 1 },
|
|
|
|
{ wxT("::"), wxT(":"), wxTOKEN_RET_DELIMS, 1 },
|
|
|
|
{ wxT("::"), wxT(":"), wxTOKEN_RET_EMPTY_ALL, 3 },
|
|
|
|
|
|
|
|
{ wxT("Hello, world"), wxT(" "), wxTOKEN_DEFAULT, 2 },
|
|
|
|
{ wxT("Hello, world "), wxT(" "), wxTOKEN_DEFAULT, 2 },
|
|
|
|
{ wxT("Hello, world"), wxT(","), wxTOKEN_DEFAULT, 2 },
|
|
|
|
{ wxT("Hello, world!"), wxT(",!"), wxTOKEN_DEFAULT, 2 },
|
|
|
|
{ wxT("Hello,, world!"), wxT(",!"), wxTOKEN_DEFAULT, 3 },
|
|
|
|
{ wxT("Hello,, world!"), wxT(",!"), wxTOKEN_STRTOK, 2 },
|
|
|
|
{ wxT("Hello, world!"), wxT(",!"), wxTOKEN_RET_EMPTY_ALL, 3 },
|
|
|
|
|
|
|
|
{ wxT("username:password:uid:gid:gecos:home:shell"),
|
|
|
|
wxT(":"), wxTOKEN_DEFAULT, 7 },
|
|
|
|
|
|
|
|
{ wxT("1:2::3:"), wxT(":"), wxTOKEN_DEFAULT, 4 },
|
|
|
|
{ wxT("1:2::3:"), wxT(":"), wxTOKEN_RET_EMPTY, 4 },
|
|
|
|
{ wxT("1:2::3:"), wxT(":"), wxTOKEN_RET_EMPTY_ALL, 5 },
|
|
|
|
{ wxT("1:2::3:"), wxT(":"), wxTOKEN_RET_DELIMS, 4 },
|
|
|
|
{ wxT("1:2::3:"), wxT(":"), wxTOKEN_STRTOK, 3 },
|
|
|
|
|
|
|
|
{ wxT("1:2::3::"), wxT(":"), wxTOKEN_DEFAULT, 4 },
|
|
|
|
{ wxT("1:2::3::"), wxT(":"), wxTOKEN_RET_EMPTY, 4 },
|
|
|
|
{ wxT("1:2::3::"), wxT(":"), wxTOKEN_RET_EMPTY_ALL, 6 },
|
|
|
|
{ wxT("1:2::3::"), wxT(":"), wxTOKEN_RET_DELIMS, 4 },
|
|
|
|
{ wxT("1:2::3::"), wxT(":"), wxTOKEN_STRTOK, 3 },
|
|
|
|
|
|
|
|
{ wxT("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT, 4 },
|
|
|
|
{ wxT("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK, 4 },
|
|
|
|
{ wxT("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY, 6 },
|
|
|
|
{ wxT("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY_ALL, 9 },
|
|
|
|
|
|
|
|
{ wxT("01/02/99"), wxT("/-"), wxTOKEN_DEFAULT, 3 },
|
|
|
|
{ wxT("01-02/99"), wxT("/-"), wxTOKEN_RET_DELIMS, 3 },
|
2005-12-20 11:30:12 -05:00
|
|
|
};
|
|
|
|
|
2005-12-23 18:38:22 -05:00
|
|
|
// helper function returning the string showing the index for which the test
|
|
|
|
// fails in the diagnostic message
|
|
|
|
static std::string Nth(size_t n)
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
return std::string(wxString::Format(wxT("for loop index %lu"),
|
2006-01-06 16:44:44 -05:00
|
|
|
(unsigned long)n).mb_str());
|
2005-12-23 18:38:22 -05:00
|
|
|
}
|
|
|
|
|
2005-12-20 11:30:12 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the tests
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void TokenizerTestCase::GetCount()
|
|
|
|
{
|
|
|
|
for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
|
|
|
|
{
|
|
|
|
const TokenizerTestData& ttd = gs_testData[n];
|
|
|
|
|
|
|
|
wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
|
2005-12-23 18:38:22 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() );
|
2005-12-20 11:30:12 -05:00
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
while ( tkz.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
tkz.GetNextToken();
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2005-12-23 18:38:22 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count );
|
2005-12-20 11:30:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// call this with the string to tokenize, delimeters to use and the expected
|
|
|
|
// positions (i.e. results of GetPosition()) after each GetNextToken() call,
|
|
|
|
// terminate positions with 0
|
|
|
|
static void
|
|
|
|
DoTestGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
|
|
|
|
{
|
|
|
|
wxStringTokenizer tkz(s, delims);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz.GetPosition() );
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, pos);
|
|
|
|
|
|
|
|
for ( ;; )
|
|
|
|
{
|
|
|
|
if ( !pos )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tkz.GetNextToken();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( (size_t)pos, tkz.GetPosition() );
|
|
|
|
|
|
|
|
pos = va_arg(ap, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TokenizerTestCase::GetPosition()
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
DoTestGetPosition(wxT("foo"), wxT("_"), 3, 0);
|
|
|
|
DoTestGetPosition(wxT("foo_bar"), wxT("_"), 4, 7, 0);
|
|
|
|
DoTestGetPosition(wxT("foo_bar_"), wxT("_"), 4, 8, 0);
|
2005-12-20 11:30:12 -05:00
|
|
|
}
|
|
|
|
|
2006-01-06 16:33:39 -05:00
|
|
|
// 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 )
|
|
|
|
{
|
2006-02-05 09:01:37 -05:00
|
|
|
CPPUNIT_ASSERT( tkz.GetString().empty() ) ;
|
2006-01-06 16:33:39 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tkz.GetNextToken();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxString(s + pos), tkz.GetString() );
|
|
|
|
|
|
|
|
pos = va_arg(ap, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TokenizerTestCase::GetString()
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
DoTestGetString(wxT("foo"), wxT("_"), 3, 0);
|
|
|
|
DoTestGetString(wxT("foo_bar"), wxT("_"), 4, 7, 0);
|
|
|
|
DoTestGetString(wxT("foo_bar_"), wxT("_"), 4, 8, 0);
|
2006-01-06 16:33:39 -05:00
|
|
|
}
|
|
|
|
|
2005-12-23 19:12:54 -05:00
|
|
|
void TokenizerTestCase::LastDelimiter()
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxStringTokenizer tkz(wxT("a+-b=c"), wxT("+-="));
|
2005-12-23 19:12:54 -05:00
|
|
|
|
|
|
|
tkz.GetNextToken();
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxT('+'), tkz.GetLastDelimiter() );
|
2005-12-23 19:12:54 -05:00
|
|
|
|
|
|
|
tkz.GetNextToken();
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxT('-'), tkz.GetLastDelimiter() );
|
2005-12-23 19:12:54 -05:00
|
|
|
|
|
|
|
tkz.GetNextToken();
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxT('='), tkz.GetLastDelimiter() );
|
2005-12-23 19:12:54 -05:00
|
|
|
|
|
|
|
tkz.GetNextToken();
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxT('\0'), tkz.GetLastDelimiter() );
|
2005-12-23 19:12:54 -05:00
|
|
|
}
|
|
|
|
|
2005-12-20 11:30:12 -05:00
|
|
|
void TokenizerTestCase::StrtokCompat()
|
|
|
|
{
|
|
|
|
for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
|
|
|
|
{
|
|
|
|
const TokenizerTestData& ttd = gs_testData[n];
|
|
|
|
if ( ttd.mode != wxTOKEN_STRTOK )
|
|
|
|
continue;
|
|
|
|
|
2005-12-21 19:17:22 -05:00
|
|
|
#if wxUSE_UNICODE
|
|
|
|
wxWCharBuffer
|
|
|
|
#else
|
|
|
|
wxCharBuffer
|
|
|
|
#endif
|
|
|
|
buf(ttd.str);
|
2005-12-20 11:30:12 -05:00
|
|
|
wxChar *last;
|
|
|
|
wxChar *s = wxStrtok(buf.data(), ttd.delims, &last);
|
|
|
|
|
|
|
|
wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
|
|
|
|
while ( tkz.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetNextToken() );
|
|
|
|
s = wxStrtok(NULL, ttd.delims, &last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 11:08:09 -04:00
|
|
|
void TokenizerTestCase::CopyObj()
|
|
|
|
{
|
|
|
|
// Test copy ctor
|
|
|
|
wxStringTokenizer tkzSrc(wxT("first:second:third:fourth"), wxT(":"));
|
|
|
|
while ( tkzSrc.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
wxString tokenSrc = tkzSrc.GetNextToken();
|
|
|
|
wxStringTokenizer tkz = tkzSrc;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetPosition(), tkz.GetPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetString(), tkz.GetString() );
|
|
|
|
|
|
|
|
// Change the state of both objects and compare again...
|
|
|
|
tokenSrc = tkzSrc.GetNextToken();
|
|
|
|
wxString token = tkz.GetNextToken();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetPosition(), tkz.GetPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetString(), tkz.GetString() );
|
|
|
|
}
|
|
|
|
}
|
2005-12-20 11:30:12 -05:00
|
|
|
|
2014-10-20 11:08:09 -04:00
|
|
|
void TokenizerTestCase::AssignObj()
|
|
|
|
{
|
|
|
|
// Test assignment
|
|
|
|
wxStringTokenizer tkzSrc(wxT("first:second:third:fourth"), wxT(":"));
|
|
|
|
wxStringTokenizer tkz;
|
|
|
|
while ( tkzSrc.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
wxString tokenSrc = tkzSrc.GetNextToken();
|
|
|
|
tkz = tkzSrc;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetPosition(), tkz.GetPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetString(), tkz.GetString() );
|
|
|
|
|
|
|
|
// Change the state of both objects and compare again...
|
|
|
|
tokenSrc = tkzSrc.GetNextToken();
|
|
|
|
wxString token = tkz.GetNextToken();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetPosition(), tkz.GetPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( tkzSrc.GetString(), tkz.GetString() );
|
|
|
|
}
|
|
|
|
}
|