]>
Commit | Line | Data |
---|---|---|
a67f601b VZ |
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 ); | |
4626c57c | 39 | CPPUNIT_TEST( LastDelimiter ); |
a67f601b VZ |
40 | CPPUNIT_TEST( StrtokCompat ); |
41 | CPPUNIT_TEST_SUITE_END(); | |
42 | ||
43 | void GetCount(); | |
44 | void GetPosition(); | |
4626c57c | 45 | void LastDelimiter(); |
a67f601b VZ |
46 | void StrtokCompat(); |
47 | ||
48 | DECLARE_NO_COPY_CLASS(TokenizerTestCase) | |
49 | }; | |
50 | ||
51 | // register in the unnamed registry so that these tests are run by default | |
52 | CPPUNIT_TEST_SUITE_REGISTRATION( TokenizerTestCase ); | |
53 | ||
54 | // also include in it's own registry so that these tests can be run alone | |
55 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TokenizerTestCase, "TokenizerTestCase" ); | |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // test data | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | static const struct TokenizerTestData | |
62 | { | |
63 | // the string to tokenize | |
64 | const wxChar *str; | |
65 | ||
66 | // the delimiters to use | |
67 | const wxChar *delims; | |
68 | ||
69 | // the tokenizer mode | |
70 | wxStringTokenizerMode mode; | |
71 | ||
72 | // expected number of tokens | |
73 | size_t count; | |
74 | } | |
75 | gs_testData[] = | |
76 | { | |
24611170 VZ |
77 | { _T(""), _T(" "), wxTOKEN_DEFAULT, 0 }, |
78 | { _T(""), _T(" "), wxTOKEN_RET_EMPTY, 0 }, | |
79 | { _T(""), _T(" "), wxTOKEN_RET_EMPTY_ALL, 0 }, | |
505a8c2c | 80 | { _T(""), _T(" "), wxTOKEN_RET_DELIMS, 0 }, |
8aa57160 | 81 | { _T(":"), _T(":"), wxTOKEN_RET_EMPTY, 1 }, |
505a8c2c | 82 | { _T(":"), _T(":"), wxTOKEN_RET_DELIMS, 1 }, |
8aa57160 VZ |
83 | { _T(":"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 2 }, |
84 | { _T("::"), _T(":"), wxTOKEN_RET_EMPTY, 1 }, | |
505a8c2c | 85 | { _T("::"), _T(":"), wxTOKEN_RET_DELIMS, 1 }, |
8aa57160 | 86 | { _T("::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 3 }, |
24611170 VZ |
87 | |
88 | { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT, 2 }, | |
89 | { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT, 2 }, | |
90 | { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT, 2 }, | |
91 | { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT, 2 }, | |
92 | { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT, 3 }, | |
93 | { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK, 2 }, | |
a67f601b | 94 | { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL, 3 }, |
421274fa | 95 | |
a67f601b | 96 | { _T("username:password:uid:gid:gecos:home:shell"), |
24611170 | 97 | _T(":"), wxTOKEN_DEFAULT, 7 }, |
421274fa | 98 | |
24611170 VZ |
99 | { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT, 4 }, |
100 | { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY, 4 }, | |
421274fa | 101 | { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 5 }, |
24611170 VZ |
102 | { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS, 4 }, |
103 | { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK, 3 }, | |
421274fa | 104 | |
822c2b9f | 105 | { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT, 4 }, |
24611170 | 106 | { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY, 4 }, |
421274fa | 107 | { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 6 }, |
822c2b9f | 108 | { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS, 4 }, |
24611170 | 109 | { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK, 3 }, |
421274fa | 110 | |
24611170 VZ |
111 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT, 4 }, |
112 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK, 4 }, | |
113 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY, 6 }, | |
a67f601b | 114 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY_ALL, 9 }, |
421274fa | 115 | |
24611170 VZ |
116 | { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT, 3 }, |
117 | { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS, 3 }, | |
a67f601b VZ |
118 | }; |
119 | ||
822c2b9f VZ |
120 | // helper function returning the string showing the index for which the test |
121 | // fails in the diagnostic message | |
122 | static std::string Nth(size_t n) | |
123 | { | |
124 | return wxString::Format(_T("for loop index %lu"), (unsigned long)n).mb_str(); | |
125 | } | |
126 | ||
a67f601b VZ |
127 | // ---------------------------------------------------------------------------- |
128 | // the tests | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | void TokenizerTestCase::GetCount() | |
132 | { | |
133 | for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ ) | |
134 | { | |
135 | const TokenizerTestData& ttd = gs_testData[n]; | |
136 | ||
137 | wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode); | |
822c2b9f | 138 | CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() ); |
a67f601b VZ |
139 | |
140 | size_t count = 0; | |
141 | while ( tkz.HasMoreTokens() ) | |
142 | { | |
143 | tkz.GetNextToken(); | |
144 | count++; | |
145 | } | |
146 | ||
822c2b9f | 147 | CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count ); |
a67f601b VZ |
148 | } |
149 | } | |
150 | ||
151 | // call this with the string to tokenize, delimeters to use and the expected | |
152 | // positions (i.e. results of GetPosition()) after each GetNextToken() call, | |
153 | // terminate positions with 0 | |
154 | static void | |
155 | DoTestGetPosition(const wxChar *s, const wxChar *delims, int pos, ...) | |
156 | { | |
157 | wxStringTokenizer tkz(s, delims); | |
158 | ||
159 | CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz.GetPosition() ); | |
160 | ||
161 | va_list ap; | |
162 | va_start(ap, pos); | |
163 | ||
164 | for ( ;; ) | |
165 | { | |
166 | if ( !pos ) | |
167 | { | |
168 | CPPUNIT_ASSERT( !tkz.HasMoreTokens() ); | |
169 | break; | |
170 | } | |
171 | ||
172 | tkz.GetNextToken(); | |
173 | ||
174 | CPPUNIT_ASSERT_EQUAL( (size_t)pos, tkz.GetPosition() ); | |
175 | ||
176 | pos = va_arg(ap, int); | |
177 | } | |
178 | ||
179 | va_end(ap); | |
180 | } | |
181 | ||
182 | void TokenizerTestCase::GetPosition() | |
183 | { | |
184 | DoTestGetPosition(_T("foo"), _T("_"), 3, 0); | |
185 | DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0); | |
186 | DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0); | |
187 | } | |
188 | ||
4626c57c VZ |
189 | void TokenizerTestCase::LastDelimiter() |
190 | { | |
191 | wxStringTokenizer tkz(_T("a+-b=c"), _T("+-=")); | |
192 | ||
193 | tkz.GetNextToken(); | |
194 | CPPUNIT_ASSERT_EQUAL( _T('+'), tkz.GetLastDelimiter() ); | |
195 | ||
196 | tkz.GetNextToken(); | |
197 | CPPUNIT_ASSERT_EQUAL( _T('-'), tkz.GetLastDelimiter() ); | |
198 | ||
199 | tkz.GetNextToken(); | |
200 | CPPUNIT_ASSERT_EQUAL( _T('='), tkz.GetLastDelimiter() ); | |
201 | ||
202 | tkz.GetNextToken(); | |
203 | CPPUNIT_ASSERT_EQUAL( _T('\0'), tkz.GetLastDelimiter() ); | |
204 | } | |
205 | ||
a67f601b VZ |
206 | void TokenizerTestCase::StrtokCompat() |
207 | { | |
208 | for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ ) | |
209 | { | |
210 | const TokenizerTestData& ttd = gs_testData[n]; | |
211 | if ( ttd.mode != wxTOKEN_STRTOK ) | |
212 | continue; | |
213 | ||
53599fbb VZ |
214 | #if wxUSE_UNICODE |
215 | wxWCharBuffer | |
216 | #else | |
217 | wxCharBuffer | |
218 | #endif | |
219 | buf(ttd.str); | |
a67f601b VZ |
220 | wxChar *last; |
221 | wxChar *s = wxStrtok(buf.data(), ttd.delims, &last); | |
222 | ||
223 | wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode); | |
224 | while ( tkz.HasMoreTokens() ) | |
225 | { | |
226 | CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetNextToken() ); | |
227 | s = wxStrtok(NULL, ttd.delims, &last); | |
228 | } | |
229 | } | |
230 | } | |
231 | ||
232 |