1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxStringTokenizer unit test
4 // Author: Vadim Zeitlin
5 // Created: 2005-12-20 (extacted from strings.cpp)
7 // Copyright: (c) 2004-2005 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/tokenzr.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class TokenizerTestCase
: public CppUnit::TestCase
33 TokenizerTestCase() { }
36 CPPUNIT_TEST_SUITE( TokenizerTestCase
);
37 CPPUNIT_TEST( GetCount
);
38 CPPUNIT_TEST( GetPosition
);
39 CPPUNIT_TEST( StrtokCompat
);
40 CPPUNIT_TEST_SUITE_END();
46 DECLARE_NO_COPY_CLASS(TokenizerTestCase
)
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase
);
52 // also include in it's own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase
, "TokenizerTestCase" );
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const struct TokenizerTestData
61 // the string to tokenize
64 // the delimiters to use
68 wxStringTokenizerMode mode
;
70 // expected number of tokens
75 { _T(""), _T(" "), wxTOKEN_DEFAULT
, 0 },
77 { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT
, 2 },
78 { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT
, 2 },
79 { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT
, 2 },
80 { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT
, 2 },
81 { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT
, 3 },
82 { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK
, 2 },
83 { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL
, 3 },
85 { _T("username:password:uid:gid:gecos:home:shell"),
86 _T(":"), wxTOKEN_DEFAULT
, 7 },
88 { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT
, 4 },
89 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY
, 4 },
90 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 5 },
91 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS
, 4 },
92 { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK
, 3 },
94 { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT
, 5 },
95 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY
, 5 },
96 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 6 },
97 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS
, 5 },
98 { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK
, 3 },
100 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_DEFAULT
, 4 },
101 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_STRTOK
, 4 },
102 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_RET_EMPTY
, 6 },
103 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_RET_EMPTY_ALL
, 9 },
105 { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT
, 3 },
106 { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS
, 3 },
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 void TokenizerTestCase::GetCount()
115 for ( size_t n
= 0; n
< WXSIZEOF(gs_testData
); n
++ )
117 const TokenizerTestData
& ttd
= gs_testData
[n
];
119 wxStringTokenizer
tkz(ttd
.str
, ttd
.delims
, ttd
.mode
);
120 CPPUNIT_ASSERT( tkz
.CountTokens() == ttd
.count
);
123 while ( tkz
.HasMoreTokens() )
129 CPPUNIT_ASSERT_EQUAL( ttd
.count
, count
);
133 // call this with the string to tokenize, delimeters to use and the expected
134 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
135 // terminate positions with 0
137 DoTestGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
139 wxStringTokenizer
tkz(s
, delims
);
141 CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz
.GetPosition() );
150 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
156 CPPUNIT_ASSERT_EQUAL( (size_t)pos
, tkz
.GetPosition() );
158 pos
= va_arg(ap
, int);
164 void TokenizerTestCase::GetPosition()
166 DoTestGetPosition(_T("foo"), _T("_"), 3, 0);
167 DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
168 DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
171 void TokenizerTestCase::StrtokCompat()
173 for ( size_t n
= 0; n
< WXSIZEOF(gs_testData
); n
++ )
175 const TokenizerTestData
& ttd
= gs_testData
[n
];
176 if ( ttd
.mode
!= wxTOKEN_STRTOK
)
186 wxChar
*s
= wxStrtok(buf
.data(), ttd
.delims
, &last
);
188 wxStringTokenizer
tkz(ttd
.str
, ttd
.delims
, ttd
.mode
);
189 while ( tkz
.HasMoreTokens() )
191 CPPUNIT_ASSERT_EQUAL( wxString(s
), tkz
.GetNextToken() );
192 s
= wxStrtok(NULL
, ttd
.delims
, &last
);