]>
Commit | Line | Data |
---|---|---|
1cd53e88 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/strings/strings.cpp | |
3 | // Purpose: wxString unit test | |
4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba | |
5 | // Created: 2004-04-19 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "wx/wxprec.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 | #include "wx/cppunit.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // test class | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | class StringTestCase : public CppUnit::TestCase | |
33 | { | |
34 | public: | |
35 | StringTestCase(); | |
36 | ||
37 | private: | |
38 | CPPUNIT_TEST_SUITE( StringTestCase ); | |
39 | CPPUNIT_TEST( String ); | |
40 | CPPUNIT_TEST( PChar ); | |
41 | CPPUNIT_TEST( Format ); | |
42 | CPPUNIT_TEST( Constructors ); | |
43 | CPPUNIT_TEST( Extraction ); | |
44 | CPPUNIT_TEST( Find ); | |
45 | CPPUNIT_TEST( Tokenizer ); | |
46 | CPPUNIT_TEST( Replace ); | |
47 | CPPUNIT_TEST( Match ); | |
bd7f096d | 48 | CPPUNIT_TEST( CaseChanges ); |
1cd53e88 VS |
49 | CPPUNIT_TEST_SUITE_END(); |
50 | ||
51 | void String(); | |
52 | void PChar(); | |
53 | void Format(); | |
54 | void Constructors(); | |
55 | void Extraction(); | |
56 | void Find(); | |
0a199299 | 57 | void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode ); |
1cd53e88 VS |
58 | void Tokenizer(); |
59 | void Replace(); | |
60 | void Match(); | |
bd7f096d | 61 | void CaseChanges(); |
1cd53e88 VS |
62 | |
63 | DECLARE_NO_COPY_CLASS(StringTestCase) | |
64 | }; | |
65 | ||
66 | // register in the unnamed registry so that these tests are run by default | |
67 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
68 | ||
69 | // also include in it's own registry so that these tests can be run alone | |
70 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
71 | ||
72 | StringTestCase::StringTestCase() | |
73 | { | |
74 | } | |
75 | ||
76 | void StringTestCase::String() | |
77 | { | |
78 | wxString a, b, c; | |
79 | ||
80 | a.reserve (128); | |
81 | b.reserve (128); | |
82 | c.reserve (128); | |
83 | ||
84 | for (int i = 0; i < 2; ++i) | |
85 | { | |
86 | a = _T("Hello"); | |
87 | b = _T(" world"); | |
88 | c = _T("! How'ya doin'?"); | |
89 | a += b; | |
90 | a += c; | |
91 | c = _T("Hello world! What's up?"); | |
92 | CPPUNIT_ASSERT( c != a ); | |
93 | } | |
94 | } | |
95 | ||
96 | void StringTestCase::PChar() | |
97 | { | |
98 | wxChar a [128]; | |
99 | wxChar b [128]; | |
100 | wxChar c [128]; | |
101 | ||
102 | for (int i = 0; i < 2; ++i) | |
103 | { | |
104 | wxStrcpy (a, _T("Hello")); | |
105 | wxStrcpy (b, _T(" world")); | |
106 | wxStrcpy (c, _T("! How'ya doin'?")); | |
107 | wxStrcat (a, b); | |
108 | wxStrcat (a, c); | |
109 | wxStrcpy (c, _T("Hello world! What's up?")); | |
110 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); | |
111 | } | |
112 | } | |
113 | ||
114 | void StringTestCase::Format() | |
115 | { | |
116 | wxString s1,s2; | |
117 | s1.Printf(_T("%03d"), 18); | |
118 | CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) ); | |
119 | s2.Printf(_T("Number 18: %s\n"), s1.c_str()); | |
120 | CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) ); | |
121 | } | |
122 | ||
123 | void StringTestCase::Constructors() | |
124 | { | |
125 | #define TEST_CTOR(args, res) \ | |
126 | { \ | |
127 | wxString s args ; \ | |
128 | CPPUNIT_ASSERT( s == res ); \ | |
129 | } | |
130 | ||
131 | TEST_CTOR((_T('Z'), 4), _T("ZZZZ")); | |
132 | TEST_CTOR((_T("Hello"), 4), _T("Hell")); | |
133 | TEST_CTOR((_T("Hello"), 5), _T("Hello")); | |
134 | ||
135 | static const wxChar *s = _T("?really!"); | |
136 | const wxChar *start = wxStrchr(s, _T('r')); | |
137 | const wxChar *end = wxStrchr(s, _T('!')); | |
138 | TEST_CTOR((start, end), _T("really")); | |
139 | } | |
140 | ||
141 | void StringTestCase::Extraction() | |
142 | { | |
143 | wxString s(_T("Hello, world!")); | |
144 | ||
145 | CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 ); | |
146 | CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 ); | |
147 | CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 ); | |
148 | CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
149 | CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 ); | |
150 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
151 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 ); | |
152 | ||
153 | wxString rest; | |
154 | ||
155 | #define TEST_STARTS_WITH( prefix , correct_rest, result ) \ | |
156 | CPPUNIT_ASSERT( \ | |
157 | ( s.StartsWith( prefix, &rest ) == result ) && \ | |
158 | ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \ | |
159 | ) | |
160 | ||
161 | TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true ); | |
162 | TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true ); | |
163 | TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true ); | |
164 | TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false ); | |
165 | TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true ); | |
166 | TEST_STARTS_WITH( _T("Goodbye"), _T(""), false ); | |
167 | TEST_STARTS_WITH( _T("Hi"), _T(""), false ); | |
168 | ||
169 | #undef TEST_STARTS_WITH | |
170 | } | |
171 | ||
172 | void StringTestCase::Find() | |
173 | { | |
174 | #define TEST_FIND( str , start , result ) \ | |
175 | CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result ); | |
176 | ||
177 | TEST_FIND( _T("Well, hello world"), 0, 1 ); | |
178 | TEST_FIND( _T("Well, hello world"), 6, 7 ); | |
179 | TEST_FIND( _T("Well, hello world"), 9, wxString::npos ); | |
180 | ||
181 | #undef TEST_FIND | |
182 | } | |
183 | ||
0a199299 | 184 | void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode ) |
1cd53e88 | 185 | { |
0a199299 WS |
186 | wxStringTokenizer tkz( str, delims, mode); |
187 | CPPUNIT_ASSERT( tkz.CountTokens() == count ); | |
188 | ||
189 | wxChar *buf, *s = NULL, *last; | |
190 | ||
191 | if ( tkz.GetMode() == wxTOKEN_STRTOK ) | |
192 | { | |
193 | buf = new wxChar[wxStrlen(str) + 1]; | |
194 | wxStrcpy(buf, str); | |
195 | s = wxStrtok(buf, delims, &last); | |
196 | } | |
197 | else | |
198 | { | |
199 | buf = NULL; | |
200 | } | |
201 | ||
202 | size_t count2 = 0; | |
203 | while ( tkz.HasMoreTokens() ) | |
204 | { | |
205 | wxString token = tkz.GetNextToken(); | |
206 | if ( buf ) | |
207 | { | |
208 | CPPUNIT_ASSERT( token == s ); | |
209 | s = wxStrtok(NULL, delims, &last); | |
210 | } | |
211 | count2++; | |
212 | } | |
213 | ||
214 | CPPUNIT_ASSERT( count2 == count ); | |
215 | if ( buf ) | |
216 | { | |
217 | delete [] buf; | |
218 | } | |
219 | } | |
1cd53e88 | 220 | |
0a199299 WS |
221 | void StringTestCase::Tokenizer() |
222 | { | |
223 | SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT ); | |
224 | SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT ); | |
225 | SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT ); | |
226 | SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT ); | |
227 | SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT ); | |
228 | SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT ); | |
229 | SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL ); | |
230 | SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT ); | |
231 | SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT ); | |
232 | SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY ); | |
233 | SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL ); | |
234 | SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT ); | |
235 | SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS ); | |
1cd53e88 VS |
236 | } |
237 | ||
238 | void StringTestCase::Replace() | |
239 | { | |
240 | #define TEST_REPLACE( original , pos , len , replacement , result ) \ | |
241 | { \ | |
242 | wxString s = original; \ | |
243 | s.replace( pos , len , replacement ); \ | |
244 | CPPUNIT_ASSERT( s == result ); \ | |
245 | } | |
246 | ||
247 | TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") ); | |
248 | TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") ); | |
249 | TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") ); | |
250 | TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") ); | |
251 | TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") ); | |
252 | ||
253 | #undef TEST_REPLACE | |
254 | } | |
255 | ||
256 | void StringTestCase::Match() | |
257 | { | |
258 | #define TEST_MATCH( s1 , s2 , result ) \ | |
259 | CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result ) | |
260 | ||
261 | TEST_MATCH( _T("foobar"), _T("foo*"), true ); | |
262 | TEST_MATCH( _T("foobar"), _T("*oo*"), true ); | |
263 | TEST_MATCH( _T("foobar"), _T("*bar"), true ); | |
264 | TEST_MATCH( _T("foobar"), _T("??????"), true ); | |
265 | TEST_MATCH( _T("foobar"), _T("f??b*"), true ); | |
266 | TEST_MATCH( _T("foobar"), _T("f?b*"), false ); | |
267 | TEST_MATCH( _T("foobar"), _T("*goo*"), false ); | |
268 | TEST_MATCH( _T("foobar"), _T("*foo"), false ); | |
269 | TEST_MATCH( _T("foobarfoo"), _T("*foo"), true ); | |
270 | TEST_MATCH( _T(""), _T("*"), true ); | |
271 | TEST_MATCH( _T(""), _T("?"), false ); | |
272 | ||
273 | #undef TEST_MATCH | |
274 | } | |
275 | ||
bd7f096d VS |
276 | |
277 | void StringTestCase::CaseChanges() | |
278 | { | |
279 | wxString s1(_T("Hello!")); | |
280 | wxString s1u(s1); | |
281 | wxString s1l(s1); | |
282 | s1u.MakeUpper(); | |
283 | s1l.MakeLower(); | |
284 | wxString s2u, s2l; | |
285 | s2u.MakeUpper(); | |
286 | s2l.MakeLower(); | |
287 | ||
288 | CPPUNIT_ASSERT( s1u == _T("HELLO!") ); | |
289 | CPPUNIT_ASSERT( s1l == _T("hello!") ); | |
290 | CPPUNIT_ASSERT( s2u == wxEmptyString ); | |
291 | CPPUNIT_ASSERT( s2l == wxEmptyString ); | |
292 | } |