]>
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 | ||
8899b155 | 14 | #include "testprec.h" |
1cd53e88 VS |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
1cd53e88 VS |
24 | // ---------------------------------------------------------------------------- |
25 | // test class | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class StringTestCase : public CppUnit::TestCase | |
29 | { | |
30 | public: | |
31 | StringTestCase(); | |
32 | ||
33 | private: | |
34 | CPPUNIT_TEST_SUITE( StringTestCase ); | |
35 | CPPUNIT_TEST( String ); | |
36 | CPPUNIT_TEST( PChar ); | |
37 | CPPUNIT_TEST( Format ); | |
38 | CPPUNIT_TEST( Constructors ); | |
f312f981 | 39 | CPPUNIT_TEST( StaticConstructors ); |
1cd53e88 | 40 | CPPUNIT_TEST( Extraction ); |
e6a99197 | 41 | CPPUNIT_TEST( Trim ); |
1cd53e88 | 42 | CPPUNIT_TEST( Find ); |
1cd53e88 VS |
43 | CPPUNIT_TEST( Replace ); |
44 | CPPUNIT_TEST( Match ); | |
bd7f096d | 45 | CPPUNIT_TEST( CaseChanges ); |
dcb68102 RN |
46 | CPPUNIT_TEST( Compare ); |
47 | CPPUNIT_TEST( CompareNoCase ); | |
c4f35063 | 48 | CPPUNIT_TEST( Contains ); |
4f7ee81a VZ |
49 | CPPUNIT_TEST( ToLong ); |
50 | CPPUNIT_TEST( ToULong ); | |
d6718dd1 VZ |
51 | #ifdef wxLongLong_t |
52 | CPPUNIT_TEST( ToLongLong ); | |
53 | CPPUNIT_TEST( ToULongLong ); | |
54 | #endif // wxLongLong_t | |
4f7ee81a | 55 | CPPUNIT_TEST( ToDouble ); |
062dc5fc | 56 | CPPUNIT_TEST( StringBuf ); |
628f87da | 57 | CPPUNIT_TEST( UTF8Buf ); |
ef0f1387 | 58 | CPPUNIT_TEST( CStrDataTernaryOperator ); |
3a69bca1 | 59 | CPPUNIT_TEST( CStrDataOperators ); |
ef0f1387 VS |
60 | CPPUNIT_TEST( CStrDataImplicitConversion ); |
61 | CPPUNIT_TEST( ExplicitConversion ); | |
1cd53e88 VS |
62 | CPPUNIT_TEST_SUITE_END(); |
63 | ||
64 | void String(); | |
65 | void PChar(); | |
66 | void Format(); | |
67 | void Constructors(); | |
f312f981 | 68 | void StaticConstructors(); |
1cd53e88 | 69 | void Extraction(); |
e6a99197 | 70 | void Trim(); |
1cd53e88 | 71 | void Find(); |
1cd53e88 VS |
72 | void Replace(); |
73 | void Match(); | |
bd7f096d | 74 | void CaseChanges(); |
dcb68102 RN |
75 | void Compare(); |
76 | void CompareNoCase(); | |
c4f35063 | 77 | void Contains(); |
4f7ee81a VZ |
78 | void ToLong(); |
79 | void ToULong(); | |
d6718dd1 VZ |
80 | #ifdef wxLongLong_t |
81 | void ToLongLong(); | |
82 | void ToULongLong(); | |
83 | #endif // wxLongLong_t | |
4f7ee81a | 84 | void ToDouble(); |
062dc5fc | 85 | void StringBuf(); |
628f87da | 86 | void UTF8Buf(); |
ef0f1387 VS |
87 | void CStrDataTernaryOperator(); |
88 | void DoCStrDataTernaryOperator(bool cond); | |
3a69bca1 | 89 | void CStrDataOperators(); |
ef0f1387 VS |
90 | void CStrDataImplicitConversion(); |
91 | void ExplicitConversion(); | |
1cd53e88 VS |
92 | |
93 | DECLARE_NO_COPY_CLASS(StringTestCase) | |
94 | }; | |
95 | ||
96 | // register in the unnamed registry so that these tests are run by default | |
97 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
98 | ||
99 | // also include in it's own registry so that these tests can be run alone | |
100 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
101 | ||
102 | StringTestCase::StringTestCase() | |
103 | { | |
104 | } | |
105 | ||
106 | void StringTestCase::String() | |
107 | { | |
108 | wxString a, b, c; | |
109 | ||
110 | a.reserve (128); | |
111 | b.reserve (128); | |
112 | c.reserve (128); | |
113 | ||
114 | for (int i = 0; i < 2; ++i) | |
115 | { | |
116 | a = _T("Hello"); | |
117 | b = _T(" world"); | |
118 | c = _T("! How'ya doin'?"); | |
119 | a += b; | |
120 | a += c; | |
121 | c = _T("Hello world! What's up?"); | |
122 | CPPUNIT_ASSERT( c != a ); | |
123 | } | |
124 | } | |
125 | ||
126 | void StringTestCase::PChar() | |
127 | { | |
128 | wxChar a [128]; | |
129 | wxChar b [128]; | |
130 | wxChar c [128]; | |
131 | ||
132 | for (int i = 0; i < 2; ++i) | |
133 | { | |
134 | wxStrcpy (a, _T("Hello")); | |
135 | wxStrcpy (b, _T(" world")); | |
136 | wxStrcpy (c, _T("! How'ya doin'?")); | |
137 | wxStrcat (a, b); | |
138 | wxStrcat (a, c); | |
139 | wxStrcpy (c, _T("Hello world! What's up?")); | |
140 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); | |
141 | } | |
142 | } | |
143 | ||
144 | void StringTestCase::Format() | |
145 | { | |
146 | wxString s1,s2; | |
147 | s1.Printf(_T("%03d"), 18); | |
148 | CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) ); | |
149 | s2.Printf(_T("Number 18: %s\n"), s1.c_str()); | |
150 | CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) ); | |
b03cd7e6 VZ |
151 | |
152 | static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 }; | |
153 | for ( size_t n = 0; n < WXSIZEOF(lengths); n++ ) | |
154 | { | |
155 | const size_t len = lengths[n]; | |
156 | ||
157 | wxString s(_T('Z'), len); | |
158 | CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length()); | |
159 | } | |
1cd53e88 VS |
160 | } |
161 | ||
162 | void StringTestCase::Constructors() | |
163 | { | |
1de532f5 VZ |
164 | CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) ); |
165 | CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') ); | |
166 | CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) ); | |
167 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) ); | |
168 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) ); | |
f312f981 VZ |
169 | |
170 | #if wxUSE_UNICODE | |
1de532f5 VZ |
171 | CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) ); |
172 | CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') ); | |
173 | CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) ); | |
174 | CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) ); | |
175 | CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) ); | |
f312f981 VZ |
176 | #endif // wxUSE_UNICODE |
177 | ||
178 | static const char *s = "?really!"; | |
179 | const char *start = wxStrchr(s, 'r'); | |
180 | const char *end = wxStrchr(s, '!'); | |
1de532f5 | 181 | CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) ); |
7652a41d VS |
182 | |
183 | // test if creating string from NULL C pointer works: | |
1de532f5 | 184 | CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) ); |
1cd53e88 VS |
185 | } |
186 | ||
f312f981 VZ |
187 | void StringTestCase::StaticConstructors() |
188 | { | |
1de532f5 VZ |
189 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") ); |
190 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) ); | |
191 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) ); | |
192 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) ); | |
193 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") ); | |
f312f981 VZ |
194 | |
195 | // FIXME: this doesn't work currently but should! | |
1de532f5 | 196 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() ); |
f312f981 VZ |
197 | |
198 | ||
1de532f5 VZ |
199 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") ); |
200 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) ); | |
201 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) ); | |
202 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) ); | |
203 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") ); | |
f312f981 | 204 | |
1de532f5 | 205 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() ); |
f312f981 | 206 | } |
265d5cce | 207 | |
1cd53e88 VS |
208 | void StringTestCase::Extraction() |
209 | { | |
210 | wxString s(_T("Hello, world!")); | |
211 | ||
212 | CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 ); | |
213 | CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 ); | |
214 | CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 ); | |
215 | CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
216 | CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 ); | |
217 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
218 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 ); | |
219 | ||
478cbb08 VS |
220 | #if wxUSE_UNICODE |
221 | static const char *germanUTF8 = "Oberfl\303\244che"; | |
222 | wxString strUnicode(wxString::FromUTF8(germanUTF8)); | |
223 | ||
224 | CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode ); | |
225 | CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" ); | |
226 | #endif // wxUSE_UNICODE | |
227 | ||
1cd53e88 VS |
228 | wxString rest; |
229 | ||
3affcd07 VZ |
230 | #define TEST_STARTS_WITH(prefix, correct_rest, result) \ |
231 | CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \ | |
232 | if ( result ) \ | |
1de532f5 | 233 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) |
1cd53e88 VS |
234 | |
235 | TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true ); | |
236 | TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true ); | |
237 | TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true ); | |
238 | TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false ); | |
239 | TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true ); | |
240 | TEST_STARTS_WITH( _T("Goodbye"), _T(""), false ); | |
241 | TEST_STARTS_WITH( _T("Hi"), _T(""), false ); | |
242 | ||
243 | #undef TEST_STARTS_WITH | |
3affcd07 | 244 | |
c8f313b2 VZ |
245 | rest = "Hello world"; |
246 | CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) ); | |
1de532f5 | 247 | CPPUNIT_ASSERT_EQUAL("world", rest); |
c8f313b2 | 248 | |
3affcd07 VZ |
249 | #define TEST_ENDS_WITH(suffix, correct_rest, result) \ |
250 | CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \ | |
251 | if ( result ) \ | |
1de532f5 | 252 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) |
3affcd07 VZ |
253 | |
254 | TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true ); | |
255 | TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true ); | |
256 | TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true ); | |
257 | TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true ); | |
258 | TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true ); | |
259 | TEST_ENDS_WITH( _T("very long string"), _T(""), false ); | |
260 | TEST_ENDS_WITH( _T("?"), _T(""), false ); | |
261 | TEST_ENDS_WITH( _T("Hello, world"), _T(""), false ); | |
262 | TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false ); | |
263 | ||
264 | #undef TEST_ENDS_WITH | |
1cd53e88 VS |
265 | } |
266 | ||
e6a99197 WS |
267 | void StringTestCase::Trim() |
268 | { | |
269 | #define TEST_TRIM( str , dir , result ) \ | |
270 | CPPUNIT_ASSERT( wxString(str).Trim(dir) == result ) | |
271 | ||
272 | TEST_TRIM( _T(" Test "), true, _T(" Test") ); | |
273 | TEST_TRIM( _T(" "), true, _T("") ); | |
274 | TEST_TRIM( _T(" "), true, _T("") ); | |
275 | TEST_TRIM( _T(""), true, _T("") ); | |
276 | ||
277 | TEST_TRIM( _T(" Test "), false, _T("Test ") ); | |
278 | TEST_TRIM( _T(" "), false, _T("") ); | |
279 | TEST_TRIM( _T(" "), false, _T("") ); | |
280 | TEST_TRIM( _T(""), false, _T("") ); | |
281 | ||
282 | #undef TEST_TRIM | |
283 | } | |
284 | ||
1cd53e88 VS |
285 | void StringTestCase::Find() |
286 | { | |
287 | #define TEST_FIND( str , start , result ) \ | |
288 | CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result ); | |
289 | ||
290 | TEST_FIND( _T("Well, hello world"), 0, 1 ); | |
291 | TEST_FIND( _T("Well, hello world"), 6, 7 ); | |
292 | TEST_FIND( _T("Well, hello world"), 9, wxString::npos ); | |
293 | ||
294 | #undef TEST_FIND | |
295 | } | |
296 | ||
1cd53e88 VS |
297 | void StringTestCase::Replace() |
298 | { | |
299 | #define TEST_REPLACE( original , pos , len , replacement , result ) \ | |
300 | { \ | |
301 | wxString s = original; \ | |
302 | s.replace( pos , len , replacement ); \ | |
1de532f5 | 303 | CPPUNIT_ASSERT_EQUAL( result, s ); \ |
1cd53e88 VS |
304 | } |
305 | ||
306 | TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") ); | |
307 | TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") ); | |
308 | TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") ); | |
309 | TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") ); | |
310 | TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") ); | |
311 | ||
1de532f5 | 312 | |
7634e443 RN |
313 | #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \ |
314 | { \ | |
315 | wxString s(o,olen); \ | |
316 | s.replace( pos , len , replacement ); \ | |
317 | CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ | |
318 | } | |
1de532f5 VZ |
319 | |
320 | TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"), | |
321 | _T("null\0dhar"), 9 ); | |
7634e443 RN |
322 | |
323 | #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \ | |
324 | { \ | |
325 | wxString s(o,olen); \ | |
326 | s.Replace( olds, news, all ); \ | |
327 | CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ | |
328 | } | |
1de532f5 | 329 | |
2df0258e | 330 | TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true, |
1de532f5 | 331 | _T("null\0dehar"), 10 ); |
7634e443 | 332 | |
4629f07e | 333 | TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true, |
1de532f5 | 334 | _T("null\0char"), 9 ); |
4629f07e | 335 | |
7634e443 RN |
336 | #undef TEST_WXREPLACE |
337 | #undef TEST_NULLCHARREPLACE | |
1cd53e88 VS |
338 | #undef TEST_REPLACE |
339 | } | |
340 | ||
341 | void StringTestCase::Match() | |
342 | { | |
343 | #define TEST_MATCH( s1 , s2 , result ) \ | |
344 | CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result ) | |
345 | ||
346 | TEST_MATCH( _T("foobar"), _T("foo*"), true ); | |
347 | TEST_MATCH( _T("foobar"), _T("*oo*"), true ); | |
348 | TEST_MATCH( _T("foobar"), _T("*bar"), true ); | |
349 | TEST_MATCH( _T("foobar"), _T("??????"), true ); | |
350 | TEST_MATCH( _T("foobar"), _T("f??b*"), true ); | |
351 | TEST_MATCH( _T("foobar"), _T("f?b*"), false ); | |
352 | TEST_MATCH( _T("foobar"), _T("*goo*"), false ); | |
353 | TEST_MATCH( _T("foobar"), _T("*foo"), false ); | |
354 | TEST_MATCH( _T("foobarfoo"), _T("*foo"), true ); | |
355 | TEST_MATCH( _T(""), _T("*"), true ); | |
356 | TEST_MATCH( _T(""), _T("?"), false ); | |
357 | ||
358 | #undef TEST_MATCH | |
359 | } | |
360 | ||
bd7f096d VS |
361 | |
362 | void StringTestCase::CaseChanges() | |
363 | { | |
364 | wxString s1(_T("Hello!")); | |
365 | wxString s1u(s1); | |
366 | wxString s1l(s1); | |
367 | s1u.MakeUpper(); | |
368 | s1l.MakeLower(); | |
369 | wxString s2u, s2l; | |
370 | s2u.MakeUpper(); | |
371 | s2l.MakeLower(); | |
372 | ||
373 | CPPUNIT_ASSERT( s1u == _T("HELLO!") ); | |
374 | CPPUNIT_ASSERT( s1l == _T("hello!") ); | |
375 | CPPUNIT_ASSERT( s2u == wxEmptyString ); | |
376 | CPPUNIT_ASSERT( s2l == wxEmptyString ); | |
377 | } | |
dcb68102 RN |
378 | |
379 | void StringTestCase::Compare() | |
380 | { | |
381 | wxString s1 = wxT("AHH"); | |
382 | wxString eq = wxT("AHH"); | |
383 | wxString neq1 = wxT("HAH"); | |
384 | wxString neq2 = wxT("AH"); | |
385 | wxString neq3 = wxT("AHHH"); | |
386 | wxString neq4 = wxT("AhH"); | |
0c924034 | 387 | |
dcb68102 RN |
388 | CPPUNIT_ASSERT( s1 == eq ); |
389 | CPPUNIT_ASSERT( s1 != neq1 ); | |
390 | CPPUNIT_ASSERT( s1 != neq2 ); | |
391 | CPPUNIT_ASSERT( s1 != neq3 ); | |
392 | CPPUNIT_ASSERT( s1 != neq4 ); | |
393 | ||
d7330233 VS |
394 | CPPUNIT_ASSERT( s1 == wxT("AHH") ); |
395 | CPPUNIT_ASSERT( s1 != wxT("no") ); | |
396 | CPPUNIT_ASSERT( s1 < wxT("AZ") ); | |
397 | CPPUNIT_ASSERT( s1 <= wxT("AZ") ); | |
398 | CPPUNIT_ASSERT( s1 <= wxT("AHH") ); | |
399 | CPPUNIT_ASSERT( s1 > wxT("AA") ); | |
400 | CPPUNIT_ASSERT( s1 >= wxT("AA") ); | |
401 | CPPUNIT_ASSERT( s1 >= wxT("AHH") ); | |
402 | ||
403 | // test comparison with C strings in Unicode build (must work in ANSI as | |
404 | // well, of course): | |
405 | CPPUNIT_ASSERT( s1 == "AHH" ); | |
406 | CPPUNIT_ASSERT( s1 != "no" ); | |
407 | CPPUNIT_ASSERT( s1 < "AZ" ); | |
408 | CPPUNIT_ASSERT( s1 <= "AZ" ); | |
409 | CPPUNIT_ASSERT( s1 <= "AHH" ); | |
410 | CPPUNIT_ASSERT( s1 > "AA" ); | |
411 | CPPUNIT_ASSERT( s1 >= "AA" ); | |
412 | CPPUNIT_ASSERT( s1 >= "AHH" ); | |
413 | ||
dcb68102 RN |
414 | // wxString _s1 = wxT("A\0HH"); |
415 | // wxString _eq = wxT("A\0HH"); | |
416 | // wxString _neq1 = wxT("H\0AH"); | |
417 | // wxString _neq2 = wxT("A\0H"); | |
418 | // wxString _neq3 = wxT("A\0HHH"); | |
419 | // wxString _neq4 = wxT("A\0hH"); | |
420 | s1.insert(1,1,'\0'); | |
421 | eq.insert(1,1,'\0'); | |
422 | neq1.insert(1,1,'\0'); | |
423 | neq2.insert(1,1,'\0'); | |
424 | neq3.insert(1,1,'\0'); | |
425 | neq4.insert(1,1,'\0'); | |
0c924034 | 426 | |
dcb68102 RN |
427 | CPPUNIT_ASSERT( s1 == eq ); |
428 | CPPUNIT_ASSERT( s1 != neq1 ); | |
429 | CPPUNIT_ASSERT( s1 != neq2 ); | |
430 | CPPUNIT_ASSERT( s1 != neq3 ); | |
431 | CPPUNIT_ASSERT( s1 != neq4 ); | |
432 | } | |
433 | ||
434 | void StringTestCase::CompareNoCase() | |
435 | { | |
436 | wxString s1 = wxT("AHH"); | |
437 | wxString eq = wxT("AHH"); | |
438 | wxString eq2 = wxT("AhH"); | |
439 | wxString eq3 = wxT("ahh"); | |
440 | wxString neq = wxT("HAH"); | |
441 | wxString neq2 = wxT("AH"); | |
442 | wxString neq3 = wxT("AHHH"); | |
0c924034 | 443 | |
dcb68102 RN |
444 | #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0) |
445 | #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0) | |
446 | ||
447 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
448 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
449 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
450 | ||
451 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
452 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
453 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
454 | ||
455 | ||
456 | // wxString _s1 = wxT("A\0HH"); | |
457 | // wxString _eq = wxT("A\0HH"); | |
458 | // wxString _eq2 = wxT("A\0hH"); | |
459 | // wxString _eq3 = wxT("a\0hh"); | |
460 | // wxString _neq = wxT("H\0AH"); | |
461 | // wxString _neq2 = wxT("A\0H"); | |
462 | // wxString _neq3 = wxT("A\0HHH"); | |
0c924034 | 463 | |
dcb68102 RN |
464 | s1.insert(1,1,'\0'); |
465 | eq.insert(1,1,'\0'); | |
466 | eq2.insert(1,1,'\0'); | |
467 | eq3.insert(1,1,'\0'); | |
468 | neq.insert(1,1,'\0'); | |
469 | neq2.insert(1,1,'\0'); | |
470 | neq3.insert(1,1,'\0'); | |
471 | ||
472 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
473 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
474 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
475 | ||
476 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
477 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
478 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
0c924034 WS |
479 | } |
480 | ||
c4f35063 VZ |
481 | void StringTestCase::Contains() |
482 | { | |
483 | static const struct ContainsData | |
484 | { | |
485 | const wxChar *hay; | |
486 | const wxChar *needle; | |
487 | bool contains; | |
488 | } containsData[] = | |
489 | { | |
490 | { _T(""), _T(""), true }, | |
491 | { _T(""), _T("foo"), false }, | |
492 | { _T("foo"), _T(""), true }, | |
493 | { _T("foo"), _T("f"), true }, | |
494 | { _T("foo"), _T("o"), true }, | |
495 | { _T("foo"), _T("oo"), true }, | |
496 | { _T("foo"), _T("ooo"), false }, | |
497 | { _T("foo"), _T("oooo"), false }, | |
498 | { _T("foo"), _T("fooo"), false }, | |
499 | }; | |
500 | ||
501 | for ( size_t n = 0; n < WXSIZEOF(containsData); n++ ) | |
502 | { | |
503 | const ContainsData& cd = containsData[n]; | |
504 | CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) ); | |
505 | } | |
506 | } | |
507 | ||
d6718dd1 VZ |
508 | // flags used in ToLongData.flags |
509 | enum | |
4f7ee81a | 510 | { |
d6718dd1 VZ |
511 | Number_Ok = 0, |
512 | Number_Invalid = 1, | |
513 | Number_Unsigned = 2, // if not specified, works for signed conversion | |
514 | Number_Signed = 4, // if not specified, works for unsigned | |
515 | Number_LongLong = 8, // only for long long tests | |
516 | Number_Long = 16, // only for long tests | |
517 | }; | |
518 | ||
519 | static const struct ToLongData | |
520 | { | |
521 | const wxChar *str; | |
d6718dd1 | 522 | #ifdef wxLongLong_t |
efc57671 VZ |
523 | wxLongLong_t value; |
524 | #else | |
525 | long value; | |
d6718dd1 | 526 | #endif // wxLongLong_t |
d6718dd1 VZ |
527 | int flags; |
528 | ||
efc57671 VZ |
529 | long LValue() const { return value; } |
530 | unsigned long ULValue() const { return value; } | |
531 | #ifdef wxLongLong_t | |
532 | wxLongLong_t LLValue() const { return value; } | |
533 | wxULongLong_t ULLValue() const { return (wxULongLong_t)value; } | |
534 | #endif // wxLongLong_t | |
535 | ||
d6718dd1 VZ |
536 | bool IsOk() const { return !(flags & Number_Invalid); } |
537 | } longData[] = | |
538 | { | |
539 | { _T("1"), 1, Number_Ok }, | |
540 | { _T("0"), 0, Number_Ok }, | |
541 | { _T("a"), 0, Number_Invalid }, | |
542 | { _T("12345"), 12345, Number_Ok }, | |
543 | { _T("--1"), 0, Number_Invalid }, | |
544 | ||
545 | { _T("-1"), -1, Number_Signed | Number_Long }, | |
546 | // this is surprizing but consistent with strtoul() behaviour | |
547 | { _T("-1"), ULONG_MAX, Number_Unsigned | Number_Long }, | |
548 | ||
549 | // this must overflow, even with 64 bit long | |
550 | { _T("922337203685477580711"), 0, Number_Invalid }, | |
551 | ||
552 | #ifdef wxLongLong_t | |
553 | { _T("2147483648"), wxLL(2147483648), Number_LongLong }, | |
554 | { _T("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed }, | |
555 | { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong | | |
556 | Number_Unsigned }, | |
557 | #endif // wxLongLong_t | |
558 | }; | |
559 | ||
560 | void StringTestCase::ToLong() | |
561 | { | |
c4f35063 VZ |
562 | long l; |
563 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a VZ |
564 | { |
565 | const ToLongData& ld = longData[n]; | |
d6718dd1 VZ |
566 | |
567 | if ( ld.flags & (Number_LongLong | Number_Unsigned) ) | |
568 | continue; | |
569 | ||
570 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) ); | |
571 | if ( ld.IsOk() ) | |
efc57671 | 572 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); |
4f7ee81a VZ |
573 | } |
574 | } | |
575 | ||
576 | void StringTestCase::ToULong() | |
577 | { | |
578 | unsigned long ul; | |
d6718dd1 | 579 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) |
4f7ee81a | 580 | { |
d6718dd1 VZ |
581 | const ToLongData& ld = longData[n]; |
582 | ||
583 | if ( ld.flags & (Number_LongLong | Number_Signed) ) | |
584 | continue; | |
585 | ||
586 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) ); | |
587 | if ( ld.IsOk() ) | |
efc57671 | 588 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); |
d6718dd1 VZ |
589 | } |
590 | } | |
591 | ||
592 | #ifdef wxLongLong_t | |
593 | ||
594 | void StringTestCase::ToLongLong() | |
595 | { | |
596 | wxLongLong_t l; | |
597 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 598 | { |
d6718dd1 | 599 | const ToLongData& ld = longData[n]; |
4f7ee81a | 600 | |
d6718dd1 VZ |
601 | if ( ld.flags & (Number_Long | Number_Unsigned) ) |
602 | continue; | |
603 | ||
604 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) ); | |
605 | if ( ld.IsOk() ) | |
efc57671 | 606 | CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l ); |
d6718dd1 VZ |
607 | } |
608 | } | |
609 | ||
610 | void StringTestCase::ToULongLong() | |
611 | { | |
612 | wxULongLong_t ul; | |
613 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 614 | { |
d6718dd1 VZ |
615 | const ToLongData& ld = longData[n]; |
616 | ||
617 | if ( ld.flags & (Number_Long | Number_Signed) ) | |
618 | continue; | |
619 | ||
620 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) ); | |
621 | if ( ld.IsOk() ) | |
efc57671 | 622 | CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul ); |
4f7ee81a VZ |
623 | } |
624 | } | |
625 | ||
d6718dd1 VZ |
626 | #endif // wxLongLong_t |
627 | ||
4f7ee81a VZ |
628 | void StringTestCase::ToDouble() |
629 | { | |
630 | double d; | |
631 | static const struct ToDoubleData | |
632 | { | |
633 | const wxChar *str; | |
634 | double value; | |
635 | bool ok; | |
636 | } doubleData[] = | |
637 | { | |
638 | { _T("1"), 1, true }, | |
639 | { _T("1.23"), 1.23, true }, | |
640 | { _T(".1"), .1, true }, | |
641 | { _T("1."), 1, true }, | |
642 | { _T("1.."), 0, false }, | |
643 | { _T("0"), 0, true }, | |
644 | { _T("a"), 0, false }, | |
645 | { _T("12345"), 12345, true }, | |
646 | { _T("-1"), -1, true }, | |
647 | { _T("--1"), 0, false }, | |
648 | }; | |
649 | ||
650 | // we need to use decimal point, not comma or whatever is its value for the | |
651 | // current locale | |
10de049c | 652 | wxSetlocale(LC_ALL, "C"); |
4f7ee81a VZ |
653 | |
654 | size_t n; | |
655 | for ( n = 0; n < WXSIZEOF(doubleData); n++ ) | |
656 | { | |
657 | const ToDoubleData& ld = doubleData[n]; | |
658 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); | |
659 | if ( ld.ok ) | |
660 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
661 | } | |
662 | } | |
db419f1f | 663 | |
062dc5fc | 664 | void StringTestCase::StringBuf() |
db419f1f | 665 | { |
062dc5fc | 666 | // check that buffer can be used to write into the string |
db419f1f VZ |
667 | wxString s; |
668 | wxStrcpy(wxStringBuffer(s, 10), _T("foo")); | |
669 | ||
1de532f5 | 670 | CPPUNIT_ASSERT_EQUAL(3, s.length()); |
c9f78968 VS |
671 | CPPUNIT_ASSERT(_T('f') == s[0u]); |
672 | CPPUNIT_ASSERT(_T('o') == s[1]); | |
673 | CPPUNIT_ASSERT(_T('o') == s[2]); | |
db419f1f | 674 | |
062dc5fc VZ |
675 | { |
676 | // also check that the buffer initially contains the original string | |
677 | // contents | |
678 | wxStringBuffer buf(s, 10); | |
679 | CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] ); | |
680 | CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] ); | |
681 | CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] ); | |
682 | CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] ); | |
683 | } | |
d8a4b666 | 684 | |
db419f1f | 685 | { |
d8a4b666 | 686 | wxStringBufferLength buf(s, 10); |
062dc5fc VZ |
687 | CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] ); |
688 | CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] ); | |
689 | CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] ); | |
690 | CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] ); | |
691 | ||
692 | // and check that it can be used to write only the specified number of | |
693 | // characters to the string | |
d8a4b666 VS |
694 | wxStrcpy(buf, _T("barrbaz")); |
695 | buf.SetLength(4); | |
696 | } | |
db419f1f | 697 | |
1de532f5 | 698 | CPPUNIT_ASSERT_EQUAL(4, s.length()); |
c9f78968 VS |
699 | CPPUNIT_ASSERT(_T('b') == s[0u]); |
700 | CPPUNIT_ASSERT(_T('a') == s[1]); | |
701 | CPPUNIT_ASSERT(_T('r') == s[2]); | |
702 | CPPUNIT_ASSERT(_T('r') == s[3]); | |
db419f1f | 703 | |
062dc5fc VZ |
704 | // check that creating buffer of length smaller than string works, i.e. at |
705 | // least doesn't crash (it would if we naively copied the entire original | |
706 | // string contents in the buffer) | |
707 | *wxStringBuffer(s, 1) = '!'; | |
db419f1f VZ |
708 | } |
709 | ||
628f87da VS |
710 | void StringTestCase::UTF8Buf() |
711 | { | |
712 | #if wxUSE_UNICODE | |
713 | // "czech" in Czech ("cestina"): | |
714 | static const char *textUTF8 = "\304\215e\305\241tina"; | |
715 | static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0}; | |
716 | ||
717 | wxString s; | |
718 | wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8); | |
719 | CPPUNIT_ASSERT(s == textUTF16); | |
720 | ||
721 | { | |
722 | wxUTF8StringBufferLength buf(s, 20); | |
723 | wxStrcpy(buf, textUTF8); | |
724 | buf.SetLength(5); | |
725 | } | |
726 | CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3)); | |
727 | #endif // wxUSE_UNICODE | |
728 | } | |
729 | ||
4ca056ea VS |
730 | |
731 | ||
ef0f1387 | 732 | void StringTestCase::CStrDataTernaryOperator() |
4ca056ea | 733 | { |
ef0f1387 VS |
734 | DoCStrDataTernaryOperator(true); |
735 | DoCStrDataTernaryOperator(false); | |
4ca056ea VS |
736 | } |
737 | ||
738 | template<typename T> bool CheckStr(const wxString& expected, T s) | |
739 | { | |
740 | return expected == wxString(s); | |
741 | } | |
742 | ||
ef0f1387 | 743 | void StringTestCase::DoCStrDataTernaryOperator(bool cond) |
4ca056ea VS |
744 | { |
745 | // test compilation of wxCStrData when used with operator?: (the asserts | |
746 | // are not very important, we're testing if the code compiles at all): | |
747 | ||
748 | wxString s("foo"); | |
4ca056ea | 749 | |
abc505b4 | 750 | const wchar_t *wcStr = L"foo"; |
4ca056ea | 751 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) ); |
9ffb659a | 752 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) ); |
4ca056ea | 753 | CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) ); |
9ffb659a | 754 | CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) ); |
11aac4ba | 755 | |
9b59b90c | 756 | const char *mbStr = "foo"; |
4ca056ea VS |
757 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) ); |
758 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) ); | |
759 | CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) ); | |
760 | CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) ); | |
92258cc1 VS |
761 | |
762 | wxString empty(""); | |
763 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) ); | |
764 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) ); | |
4ca056ea | 765 | } |
ef0f1387 | 766 | |
3a69bca1 VS |
767 | void StringTestCase::CStrDataOperators() |
768 | { | |
769 | wxString s("hello"); | |
770 | ||
771 | CPPUNIT_ASSERT( s.c_str()[0] == 'h' ); | |
772 | CPPUNIT_ASSERT( s.c_str()[1] == 'e' ); | |
773 | CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); | |
774 | ||
775 | CPPUNIT_ASSERT( *s.c_str() == 'h' ); | |
776 | CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' ); | |
777 | CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); | |
778 | } | |
779 | ||
ef0f1387 VS |
780 | bool CheckStrChar(const wxString& expected, char *s) |
781 | { return CheckStr(expected, s); } | |
782 | bool CheckStrWChar(const wxString& expected, wchar_t *s) | |
783 | { return CheckStr(expected, s); } | |
784 | bool CheckStrConstChar(const wxString& expected, const char *s) | |
785 | { return CheckStr(expected, s); } | |
786 | bool CheckStrConstWChar(const wxString& expected, const wchar_t *s) | |
787 | { return CheckStr(expected, s); } | |
788 | ||
789 | void StringTestCase::CStrDataImplicitConversion() | |
790 | { | |
791 | wxString s("foo"); | |
792 | ||
ef0f1387 | 793 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) ); |
ef0f1387 | 794 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) ); |
7978bc72 VS |
795 | |
796 | // implicit conversion of wxString is not available in STL build | |
797 | #if !wxUSE_STL | |
798 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s) ); | |
ef0f1387 | 799 | CPPUNIT_ASSERT( CheckStrConstChar(s, s) ); |
7978bc72 | 800 | #endif |
ef0f1387 VS |
801 | } |
802 | ||
803 | void StringTestCase::ExplicitConversion() | |
804 | { | |
805 | wxString s("foo"); | |
806 | ||
807 | CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) ); | |
808 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) ); | |
809 | CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) ); | |
810 | ||
811 | CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) ); | |
812 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) ); | |
813 | CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) ); | |
814 | } |