even more test cases
[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_EMPTY, 1 },
79 { _T(":"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 2 },
80 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY, 1 },
81 { _T("::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 3 },
82
83 { _T("Hello, world"), _T(" "), wxTOKEN_DEFAULT, 2 },
84 { _T("Hello, world "), _T(" "), wxTOKEN_DEFAULT, 2 },
85 { _T("Hello, world"), _T(","), wxTOKEN_DEFAULT, 2 },
86 { _T("Hello, world!"), _T(",!"), wxTOKEN_DEFAULT, 2 },
87 { _T("Hello,, world!"), _T(",!"), wxTOKEN_DEFAULT, 3 },
88 { _T("Hello,, world!"), _T(",!"), wxTOKEN_STRTOK, 2 },
89 { _T("Hello, world!"), _T(",!"), wxTOKEN_RET_EMPTY_ALL, 3 },
90
91 { _T("username:password:uid:gid:gecos:home:shell"),
92 _T(":"), wxTOKEN_DEFAULT, 7 },
93
94 { _T("1:2::3:"), _T(":"), wxTOKEN_DEFAULT, 4 },
95 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY, 4 },
96 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 5 },
97 { _T("1:2::3:"), _T(":"), wxTOKEN_RET_DELIMS, 4 },
98 { _T("1:2::3:"), _T(":"), wxTOKEN_STRTOK, 3 },
99
100 { _T("1:2::3::"), _T(":"), wxTOKEN_DEFAULT, 4 },
101 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY, 4 },
102 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_EMPTY_ALL, 6 },
103 { _T("1:2::3::"), _T(":"), wxTOKEN_RET_DELIMS, 4 },
104 { _T("1:2::3::"), _T(":"), wxTOKEN_STRTOK, 3 },
105
106 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_DEFAULT, 4 },
107 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_STRTOK, 4 },
108 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY, 6 },
109 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, wxTOKEN_RET_EMPTY_ALL, 9 },
110
111 { _T("01/02/99"), _T("/-"), wxTOKEN_DEFAULT, 3 },
112 { _T("01-02/99"), _T("/-"), wxTOKEN_RET_DELIMS, 3 },
113 };
114
115 // helper function returning the string showing the index for which the test
116 // fails in the diagnostic message
117 static std::string Nth(size_t n)
118 {
119 return wxString::Format(_T("for loop index %lu"), (unsigned long)n).mb_str();
120 }
121
122 // ----------------------------------------------------------------------------
123 // the tests
124 // ----------------------------------------------------------------------------
125
126 void TokenizerTestCase::GetCount()
127 {
128 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
129 {
130 const TokenizerTestData& ttd = gs_testData[n];
131
132 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
133 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, tkz.CountTokens() );
134
135 size_t count = 0;
136 while ( tkz.HasMoreTokens() )
137 {
138 tkz.GetNextToken();
139 count++;
140 }
141
142 CPPUNIT_ASSERT_EQUAL_MESSAGE( Nth(n), ttd.count, count );
143 }
144 }
145
146 // call this with the string to tokenize, delimeters to use and the expected
147 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
148 // terminate positions with 0
149 static void
150 DoTestGetPosition(const wxChar *s, const wxChar *delims, int pos, ...)
151 {
152 wxStringTokenizer tkz(s, delims);
153
154 CPPUNIT_ASSERT_EQUAL( (size_t)0, tkz.GetPosition() );
155
156 va_list ap;
157 va_start(ap, pos);
158
159 for ( ;; )
160 {
161 if ( !pos )
162 {
163 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
164 break;
165 }
166
167 tkz.GetNextToken();
168
169 CPPUNIT_ASSERT_EQUAL( (size_t)pos, tkz.GetPosition() );
170
171 pos = va_arg(ap, int);
172 }
173
174 va_end(ap);
175 }
176
177 void TokenizerTestCase::GetPosition()
178 {
179 DoTestGetPosition(_T("foo"), _T("_"), 3, 0);
180 DoTestGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
181 DoTestGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
182 }
183
184 void TokenizerTestCase::StrtokCompat()
185 {
186 for ( size_t n = 0; n < WXSIZEOF(gs_testData); n++ )
187 {
188 const TokenizerTestData& ttd = gs_testData[n];
189 if ( ttd.mode != wxTOKEN_STRTOK )
190 continue;
191
192 #if wxUSE_UNICODE
193 wxWCharBuffer
194 #else
195 wxCharBuffer
196 #endif
197 buf(ttd.str);
198 wxChar *last;
199 wxChar *s = wxStrtok(buf.data(), ttd.delims, &last);
200
201 wxStringTokenizer tkz(ttd.str, ttd.delims, ttd.mode);
202 while ( tkz.HasMoreTokens() )
203 {
204 CPPUNIT_ASSERT_EQUAL( wxString(s), tkz.GetNextToken() );
205 s = wxStrtok(NULL, ttd.delims, &last);
206 }
207 }
208 }
209
210