added a couple more wxTOKEN_RET_DELIMS tests
[wxWidgets.git] / tests / strings / tokenizer.cpp
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)
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004-2005 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/tokenzr.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class TokenizerTestCase : public CppUnit::TestCase
31 {
32 public:
33 TokenizerTestCase() { }
34
35 private:
36 CPPUNIT_TEST_SUITE( TokenizerTestCase );
37 CPPUNIT_TEST( GetCount );
38 CPPUNIT_TEST( GetPosition );
39 CPPUNIT_TEST( StrtokCompat );
40 CPPUNIT_TEST_SUITE_END();
41
42 void GetCount();
43 void GetPosition();
44 void StrtokCompat();
45
46 DECLARE_NO_COPY_CLASS(TokenizerTestCase)
47 };
48
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase );
51
52 // also include in it's own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase, "TokenizerTestCase" );
54
55 // ----------------------------------------------------------------------------
56 // test data
57 // ----------------------------------------------------------------------------
58
59 static const struct TokenizerTestData
60 {
61 // the string to tokenize
62 const wxChar *str;
63
64 // the delimiters to use
65 const wxChar *delims;
66
67 // the tokenizer mode
68 wxStringTokenizerMode mode;
69
70 // expected number of tokens
71 size_t count;
72 }
73 gs_testData[] =
74 {
75 { _T(""), _T(" "), wxTOKEN_DEFAULT, 0 },
76 { _T(""), _T(" "), wxTOKEN_RET_EMPTY, 0 },
77 { _T(""), _T(" "), wxTOKEN_RET_EMPTY_ALL, 0 },
78 { _T(""), _T(" "), wxTOKEN_RET_DELIMS, 0 },
79 { _T(":"), _T(":"), wxTOKEN_RET_EMPTY, 1 },
80 { _T(":"), _T(":"), wxTOKEN_RET_DELIMS, 1 },
81 { _T(":"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 2 },
82 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY, 1 },
83 { _T("::"), _T(":"), wxTOKEN_RET_DELIMS, 1 },
84 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 3 },
85
86 { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT, 2 },
87 { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT, 2 },
88 { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT, 2 },
89 { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT, 2 },
90 { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT, 3 },
91 { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK, 2 },
92 { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL, 3 },
93
94 { _T("username:password:uid:gid:gecos:home:shell"),
95 _T(":"), wxTOKEN_DEFAULT, 7 },
96
97 { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT, 4 },
98 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY, 4 },
99 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 5 },
100 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS, 4 },
101 { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK, 3 },
102
103 { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT, 4 },
104 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY, 4 },
105 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 6 },
106 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS, 4 },
107 { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK, 3 },
108
109 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT, 4 },
110 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK, 4 },
111 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY, 6 },
112 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY_ALL, 9 },
113
114 { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT, 3 },
115 { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS, 3 },
116 };
117
118 // helper function returning the string showing the index for which the test
119 // fails in the diagnostic message
120 static std::string Nth(size_t n)
121 {
122 return wxString::Format(_T("for loop index %lu"), (unsigned long)n).mb_str();
123 }
124
125 // ----------------------------------------------------------------------------
126 // the tests
127 // ----------------------------------------------------------------------------
128
129 void TokenizerTestCase::GetCount()
130 {
131 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
132 {
133 const TokenizerTestData& ttd = gs_testData[n];
134
135 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
136 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() );
137
138 size_t count = 0;
139 while ( tkz.HasMoreTokens() )
140 {
141 tkz.GetNextToken();
142 count++;
143 }
144
145 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count );
146 }
147 }
148
149 // call this with the string to tokenize, delimeters to use and the expected
150 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
151 // terminate positions with 0
152 static void
153 DoTestGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
154 {
155 wxStringTokenizer tkz(s, delims);
156
157 CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz.GetPosition() );
158
159 va_list ap;
160 va_start(ap, pos);
161
162 for ( ;; )
163 {
164 if ( !pos )
165 {
166 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
167 break;
168 }
169
170 tkz.GetNextToken();
171
172 CPPUNIT_ASSERT_EQUAL( (size_t)pos, tkz.GetPosition() );
173
174 pos = va_arg(ap, int);
175 }
176
177 va_end(ap);
178 }
179
180 void TokenizerTestCase::GetPosition()
181 {
182 DoTestGetPosition(_T("foo"), _T("_"), 3, 0);
183 DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
184 DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
185 }
186
187 void TokenizerTestCase::StrtokCompat()
188 {
189 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
190 {
191 const TokenizerTestData& ttd = gs_testData[n];
192 if ( ttd.mode != wxTOKEN_STRTOK )
193 continue;
194
195 #if wxUSE_UNICODE
196 wxWCharBuffer
197 #else
198 wxCharBuffer
199 #endif
200 buf(ttd.str);
201 wxChar *last;
202 wxChar *s = wxStrtok(buf.data(), ttd.delims, &last);
203
204 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
205 while ( tkz.HasMoreTokens() )
206 {
207 CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetNextToken() );
208 s = wxStrtok(NULL, ttd.delims, &last);
209 }
210 }
211 }
212
213