]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/tokenizer.cpp
fixed bug in GetString() introduced during the latest round of changes
[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( GetString );
40 CPPUNIT_TEST( LastDelimiter );
41 CPPUNIT_TEST( StrtokCompat );
42 CPPUNIT_TEST_SUITE_END();
43
44 void GetCount();
45 void GetPosition();
46 void GetString();
47 void LastDelimiter();
48 void StrtokCompat();
49
50 DECLARE_NO_COPY_CLASS(TokenizerTestCase)
51 };
52
53 // register in the unnamed registry so that these tests are run by default
54 CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase );
55
56 // also include in it's own registry so that these tests can be run alone
57 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase, "TokenizerTestCase" );
58
59 // ----------------------------------------------------------------------------
60 // test data
61 // ----------------------------------------------------------------------------
62
63 static const struct TokenizerTestData
64 {
65 // the string to tokenize
66 const wxChar *str;
67
68 // the delimiters to use
69 const wxChar *delims;
70
71 // the tokenizer mode
72 wxStringTokenizerMode mode;
73
74 // expected number of tokens
75 size_t count;
76 }
77 gs_testData[] =
78 {
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 },
89
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 },
97
98 { _T("username:password:uid:gid:gecos:home:shell"),
99 _T(":"), wxTOKEN_DEFAULT, 7 },
100
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 },
106
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 },
112
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 },
117
118 { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT, 3 },
119 { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS, 3 },
120 };
121
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)
125 {
126 return wxString::Format(_T("for loop index %lu"), (unsigned long)n).mb_str();
127 }
128
129 // ----------------------------------------------------------------------------
130 // the tests
131 // ----------------------------------------------------------------------------
132
133 void TokenizerTestCase::GetCount()
134 {
135 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
136 {
137 const TokenizerTestData& ttd = gs_testData[n];
138
139 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
140 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() );
141
142 size_t count = 0;
143 while ( tkz.HasMoreTokens() )
144 {
145 tkz.GetNextToken();
146 count++;
147 }
148
149 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count );
150 }
151 }
152
153 // call this with the string to tokenize, delimeters to use and the expected
154 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
155 // terminate positions with 0
156 static void
157 DoTestGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
158 {
159 wxStringTokenizer tkz(s, delims);
160
161 CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz.GetPosition() );
162
163 va_list ap;
164 va_start(ap, pos);
165
166 for ( ;; )
167 {
168 if ( !pos )
169 {
170 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
171 break;
172 }
173
174 tkz.GetNextToken();
175
176 CPPUNIT_ASSERT_EQUAL( (size_t)pos, tkz.GetPosition() );
177
178 pos = va_arg(ap, int);
179 }
180
181 va_end(ap);
182 }
183
184 void TokenizerTestCase::GetPosition()
185 {
186 DoTestGetPosition(_T("foo"), _T("_"), 3, 0);
187 DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
188 DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
189 }
190
191 // helper for GetString(): the parameters are the same as for DoTestGetPosition
192 // but it checks GetString() return value instead of GetPosition()
193 static void
194 DoTestGetString(const wxChar *s, const wxChar *delims, int pos, ...)
195 {
196 wxStringTokenizer tkz(s, delims);
197
198 CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetString() );
199
200 va_list ap;
201 va_start(ap, pos);
202
203 for ( ;; )
204 {
205 if ( !pos )
206 {
207 CPPUNIT_ASSERT_EQUAL( wxString(), tkz.GetString() );
208 break;
209 }
210
211 tkz.GetNextToken();
212
213 CPPUNIT_ASSERT_EQUAL( wxString(s + pos), tkz.GetString() );
214
215 pos = va_arg(ap, int);
216 }
217
218 va_end(ap);
219 }
220
221 void TokenizerTestCase::GetString()
222 {
223 DoTestGetString(_T("foo"), _T("_"), 3, 0);
224 DoTestGetString(_T("foo_bar"), _T("_"), 4, 7, 0);
225 DoTestGetString(_T("foo_bar_"), _T("_"), 4, 8, 0);
226 }
227
228 void TokenizerTestCase::LastDelimiter()
229 {
230 wxStringTokenizer tkz(_T("a+-b=c"), _T("+-="));
231
232 tkz.GetNextToken();
233 CPPUNIT_ASSERT_EQUAL( _T('+'), tkz.GetLastDelimiter() );
234
235 tkz.GetNextToken();
236 CPPUNIT_ASSERT_EQUAL( _T('-'), tkz.GetLastDelimiter() );
237
238 tkz.GetNextToken();
239 CPPUNIT_ASSERT_EQUAL( _T('='), tkz.GetLastDelimiter() );
240
241 tkz.GetNextToken();
242 CPPUNIT_ASSERT_EQUAL( _T('\0'), tkz.GetLastDelimiter() );
243 }
244
245 void TokenizerTestCase::StrtokCompat()
246 {
247 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
248 {
249 const TokenizerTestData& ttd = gs_testData[n];
250 if ( ttd.mode != wxTOKEN_STRTOK )
251 continue;
252
253 #if wxUSE_UNICODE
254 wxWCharBuffer
255 #else
256 wxCharBuffer
257 #endif
258 buf(ttd.str);
259 wxChar *last;
260 wxChar *s = wxStrtok(buf.data(), ttd.delims, &last);
261
262 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
263 while ( tkz.HasMoreTokens() )
264 {
265 CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetNextToken() );
266 s = wxStrtok(NULL, ttd.delims, &last);
267 }
268 }
269 }
270
271