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( GetString
);
40 CPPUNIT_TEST( LastDelimiter
);
41 CPPUNIT_TEST( StrtokCompat
);
42 CPPUNIT_TEST_SUITE_END();
50 DECLARE_NO_COPY_CLASS(TokenizerTestCase
)
53 // register in the unnamed registry so that these tests are run by default
54 CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase
);
56 // also include in it's own registry so that these tests can be run alone
57 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase
, "TokenizerTestCase" );
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 static const struct TokenizerTestData
65 // the string to tokenize
68 // the delimiters to use
72 wxStringTokenizerMode mode
;
74 // expected number of tokens
79 { _T(""), _T(" "), wxTOKEN_DEFAULT
, 0 },
80 { _T(""), _T(" "), wxTOKEN_RET_EMPTY
, 0 },
81 { _T(""), _T(" "), wxTOKEN_RET_EMPTY_ALL
, 0 },
82 { _T(""), _T(" "), wxTOKEN_RET_DELIMS
, 0 },
83 { _T(":"), _T(":"), wxTOKEN_RET_EMPTY
, 1 },
84 { _T(":"), _T(":"), wxTOKEN_RET_DELIMS
, 1 },
85 { _T(":"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 2 },
86 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY
, 1 },
87 { _T("::"), _T(":"), wxTOKEN_RET_DELIMS
, 1 },
88 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 3 },
90 { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT
, 2 },
91 { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT
, 2 },
92 { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT
, 2 },
93 { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT
, 2 },
94 { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT
, 3 },
95 { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK
, 2 },
96 { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL
, 3 },
98 { _T("username:password:uid:gid:gecos:home:shell"),
99 _T(":"), wxTOKEN_DEFAULT
, 7 },
101 { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT
, 4 },
102 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY
, 4 },
103 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 5 },
104 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS
, 4 },
105 { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK
, 3 },
107 { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT
, 4 },
108 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY
, 4 },
109 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL
, 6 },
110 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS
, 4 },
111 { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK
, 3 },
113 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_DEFAULT
, 4 },
114 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_STRTOK
, 4 },
115 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_RET_EMPTY
, 6 },
116 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, wxTOKEN_RET_EMPTY_ALL
, 9 },
118 { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT
, 3 },
119 { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS
, 3 },
122 // helper function returning the string showing the index for which the test
123 // fails in the diagnostic message
124 static std::string
Nth(size_t n
)
126 return std::string(wxString::Format(_T("for loop index %lu"),
127 (unsigned long)n
).mb_str());
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 void TokenizerTestCase::GetCount()
136 for ( size_t n
= 0; n
< WXSIZEOF(gs_testData
); n
++ )
138 const TokenizerTestData
& ttd
= gs_testData
[n
];
140 wxStringTokenizer
tkz(ttd
.str
, ttd
.delims
, ttd
.mode
);
141 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n
), ttd
.count
, tkz
.CountTokens() );
144 while ( tkz
.HasMoreTokens() )
150 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n
), ttd
.count
, count
);
154 // call this with the string to tokenize, delimeters to use and the expected
155 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
156 // terminate positions with 0
158 DoTestGetPosition(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
160 wxStringTokenizer
tkz(s
, delims
);
162 CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz
.GetPosition() );
171 CPPUNIT_ASSERT( !tkz
.HasMoreTokens() );
177 CPPUNIT_ASSERT_EQUAL( (size_t)pos
, tkz
.GetPosition() );
179 pos
= va_arg(ap
, int);
185 void TokenizerTestCase::GetPosition()
187 DoTestGetPosition(_T("foo"), _T("_"), 3, 0);
188 DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
189 DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
192 // helper for GetString(): the parameters are the same as for DoTestGetPosition
193 // but it checks GetString() return value instead of GetPosition()
195 DoTestGetString(const wxChar
*s
, const wxChar
*delims
, int pos
, ...)
197 wxStringTokenizer
tkz(s
, delims
);
199 CPPUNIT_ASSERT_EQUAL( wxString(s
), tkz
.GetString() );
208 CPPUNIT_ASSERT( tkz
.GetString().empty() ) ;
214 CPPUNIT_ASSERT_EQUAL( wxString(s
+ pos
), tkz
.GetString() );
216 pos
= va_arg(ap
, int);
222 void TokenizerTestCase::GetString()
224 DoTestGetString(_T("foo"), _T("_"), 3, 0);
225 DoTestGetString(_T("foo_bar"), _T("_"), 4, 7, 0);
226 DoTestGetString(_T("foo_bar_"), _T("_"), 4, 8, 0);
229 void TokenizerTestCase::LastDelimiter()
231 wxStringTokenizer
tkz(_T("a+-b=c"), _T("+-="));
234 CPPUNIT_ASSERT_EQUAL( _T('+'), tkz
.GetLastDelimiter() );
237 CPPUNIT_ASSERT_EQUAL( _T('-'), tkz
.GetLastDelimiter() );
240 CPPUNIT_ASSERT_EQUAL( _T('='), tkz
.GetLastDelimiter() );
243 CPPUNIT_ASSERT_EQUAL( _T('\0'), tkz
.GetLastDelimiter() );
246 void TokenizerTestCase::StrtokCompat()
248 for ( size_t n
= 0; n
< WXSIZEOF(gs_testData
); n
++ )
250 const TokenizerTestData
& ttd
= gs_testData
[n
];
251 if ( ttd
.mode
!= wxTOKEN_STRTOK
)
261 wxChar
*s
= wxStrtok(buf
.data(), ttd
.delims
, &last
);
263 wxStringTokenizer
tkz(ttd
.str
, ttd
.delims
, ttd
.mode
);
264 while ( tkz
.HasMoreTokens() )
266 CPPUNIT_ASSERT_EQUAL( wxString(s
), tkz
.GetNextToken() );
267 s
= wxStrtok(NULL
, ttd
.delims
, &last
);