]>
Commit | Line | Data |
---|---|---|
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 "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 | // ---------------------------------------------------------------------------- | |
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 ); | |
39 | CPPUNIT_TEST( StaticConstructors ); | |
40 | CPPUNIT_TEST( Extraction ); | |
41 | CPPUNIT_TEST( Trim ); | |
42 | CPPUNIT_TEST( Find ); | |
43 | CPPUNIT_TEST( Replace ); | |
44 | CPPUNIT_TEST( Match ); | |
45 | CPPUNIT_TEST( CaseChanges ); | |
46 | CPPUNIT_TEST( Compare ); | |
47 | CPPUNIT_TEST( CompareNoCase ); | |
48 | CPPUNIT_TEST( Contains ); | |
49 | CPPUNIT_TEST( ToLong ); | |
50 | CPPUNIT_TEST( ToULong ); | |
51 | #ifdef wxLongLong_t | |
52 | CPPUNIT_TEST( ToLongLong ); | |
53 | CPPUNIT_TEST( ToULongLong ); | |
54 | #endif // wxLongLong_t | |
55 | CPPUNIT_TEST( ToDouble ); | |
56 | CPPUNIT_TEST( FromDouble ); | |
57 | CPPUNIT_TEST( StringBuf ); | |
58 | CPPUNIT_TEST( UTF8Buf ); | |
59 | CPPUNIT_TEST( CStrDataTernaryOperator ); | |
60 | CPPUNIT_TEST( CStrDataOperators ); | |
61 | CPPUNIT_TEST( CStrDataImplicitConversion ); | |
62 | CPPUNIT_TEST( ExplicitConversion ); | |
63 | CPPUNIT_TEST( IndexedAccess ); | |
64 | CPPUNIT_TEST( BeforeAndAfter ); | |
65 | CPPUNIT_TEST( ScopedBuffers ); | |
66 | CPPUNIT_TEST_SUITE_END(); | |
67 | ||
68 | void String(); | |
69 | void PChar(); | |
70 | void Format(); | |
71 | void Constructors(); | |
72 | void StaticConstructors(); | |
73 | void Extraction(); | |
74 | void Trim(); | |
75 | void Find(); | |
76 | void Replace(); | |
77 | void Match(); | |
78 | void CaseChanges(); | |
79 | void Compare(); | |
80 | void CompareNoCase(); | |
81 | void Contains(); | |
82 | void ToLong(); | |
83 | void ToULong(); | |
84 | #ifdef wxLongLong_t | |
85 | void ToLongLong(); | |
86 | void ToULongLong(); | |
87 | #endif // wxLongLong_t | |
88 | void ToDouble(); | |
89 | void FromDouble(); | |
90 | void StringBuf(); | |
91 | void UTF8Buf(); | |
92 | void CStrDataTernaryOperator(); | |
93 | void DoCStrDataTernaryOperator(bool cond); | |
94 | void CStrDataOperators(); | |
95 | void CStrDataImplicitConversion(); | |
96 | void ExplicitConversion(); | |
97 | void IndexedAccess(); | |
98 | void BeforeAndAfter(); | |
99 | void ScopedBuffers(); | |
100 | ||
101 | DECLARE_NO_COPY_CLASS(StringTestCase) | |
102 | }; | |
103 | ||
104 | // register in the unnamed registry so that these tests are run by default | |
105 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
106 | ||
107 | // also include in it's own registry so that these tests can be run alone | |
108 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
109 | ||
110 | StringTestCase::StringTestCase() | |
111 | { | |
112 | } | |
113 | ||
114 | void StringTestCase::String() | |
115 | { | |
116 | wxString a, b, c; | |
117 | ||
118 | a.reserve (128); | |
119 | b.reserve (128); | |
120 | c.reserve (128); | |
121 | ||
122 | for (int i = 0; i < 2; ++i) | |
123 | { | |
124 | a = wxT("Hello"); | |
125 | b = wxT(" world"); | |
126 | c = wxT("! How'ya doin'?"); | |
127 | a += b; | |
128 | a += c; | |
129 | c = wxT("Hello world! What's up?"); | |
130 | CPPUNIT_ASSERT( c != a ); | |
131 | } | |
132 | } | |
133 | ||
134 | void StringTestCase::PChar() | |
135 | { | |
136 | wxChar a [128]; | |
137 | wxChar b [128]; | |
138 | wxChar c [128]; | |
139 | ||
140 | for (int i = 0; i < 2; ++i) | |
141 | { | |
142 | wxStrcpy (a, wxT("Hello")); | |
143 | wxStrcpy (b, wxT(" world")); | |
144 | wxStrcpy (c, wxT("! How'ya doin'?")); | |
145 | wxStrcat (a, b); | |
146 | wxStrcat (a, c); | |
147 | wxStrcpy (c, wxT("Hello world! What's up?")); | |
148 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); | |
149 | } | |
150 | } | |
151 | ||
152 | void StringTestCase::Format() | |
153 | { | |
154 | wxString s1,s2; | |
155 | s1.Printf(wxT("%03d"), 18); | |
156 | CPPUNIT_ASSERT( s1 == wxString::Format(wxT("%03d"), 18) ); | |
157 | s2.Printf(wxT("Number 18: %s\n"), s1.c_str()); | |
158 | CPPUNIT_ASSERT( s2 == wxString::Format(wxT("Number 18: %s\n"), s1.c_str()) ); | |
159 | ||
160 | static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 }; | |
161 | for ( size_t n = 0; n < WXSIZEOF(lengths); n++ ) | |
162 | { | |
163 | const size_t len = lengths[n]; | |
164 | ||
165 | wxString s(wxT('Z'), len); | |
166 | CPPUNIT_ASSERT_EQUAL( len, wxString::Format(wxT("%s"), s.c_str()).length()); | |
167 | } | |
168 | } | |
169 | ||
170 | void StringTestCase::Constructors() | |
171 | { | |
172 | CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) ); | |
173 | CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') ); | |
174 | CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) ); | |
175 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) ); | |
176 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) ); | |
177 | ||
178 | #if wxUSE_UNICODE | |
179 | CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) ); | |
180 | CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') ); | |
181 | CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) ); | |
182 | CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) ); | |
183 | CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) ); | |
184 | #endif // wxUSE_UNICODE | |
185 | ||
186 | static const char *s = "?really!"; | |
187 | const char *start = wxStrchr(s, 'r'); | |
188 | const char *end = wxStrchr(s, '!'); | |
189 | CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) ); | |
190 | ||
191 | // test if creating string from NULL C pointer works: | |
192 | CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) ); | |
193 | } | |
194 | ||
195 | void StringTestCase::StaticConstructors() | |
196 | { | |
197 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") ); | |
198 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) ); | |
199 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) ); | |
200 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) ); | |
201 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") ); | |
202 | ||
203 | // FIXME: this doesn't work currently but should! | |
204 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() ); | |
205 | ||
206 | ||
207 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") ); | |
208 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) ); | |
209 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) ); | |
210 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) ); | |
211 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") ); | |
212 | ||
213 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() ); | |
214 | } | |
215 | ||
216 | void StringTestCase::Extraction() | |
217 | { | |
218 | wxString s(wxT("Hello, world!")); | |
219 | ||
220 | CPPUNIT_ASSERT( wxStrcmp( s.c_str() , wxT("Hello, world!") ) == 0 ); | |
221 | CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , wxT("Hello") ) == 0 ); | |
222 | CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , wxT("world!") ) == 0 ); | |
223 | CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 ); | |
224 | CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , wxT("lo, world!") ) == 0 ); | |
225 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , wxT("lo, w") ) == 0 ); | |
226 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , wxT("lo, world!") ) == 0 ); | |
227 | ||
228 | #if wxUSE_UNICODE | |
229 | static const char *germanUTF8 = "Oberfl\303\244che"; | |
230 | wxString strUnicode(wxString::FromUTF8(germanUTF8)); | |
231 | ||
232 | CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode ); | |
233 | CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" ); | |
234 | #endif // wxUSE_UNICODE | |
235 | ||
236 | wxString rest; | |
237 | ||
238 | #define TEST_STARTS_WITH(prefix, correct_rest, result) \ | |
239 | CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \ | |
240 | if ( result ) \ | |
241 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) | |
242 | ||
243 | TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true ); | |
244 | TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true ); | |
245 | TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true ); | |
246 | TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false ); | |
247 | TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true ); | |
248 | TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false ); | |
249 | TEST_STARTS_WITH( wxT("Hi"), wxT(""), false ); | |
250 | ||
251 | #undef TEST_STARTS_WITH | |
252 | ||
253 | rest = "Hello world"; | |
254 | CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) ); | |
255 | CPPUNIT_ASSERT_EQUAL("world", rest); | |
256 | ||
257 | #define TEST_ENDS_WITH(suffix, correct_rest, result) \ | |
258 | CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \ | |
259 | if ( result ) \ | |
260 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) | |
261 | ||
262 | TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true ); | |
263 | TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true ); | |
264 | TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true ); | |
265 | TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true ); | |
266 | TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true ); | |
267 | TEST_ENDS_WITH( wxT("very long string"), wxT(""), false ); | |
268 | TEST_ENDS_WITH( wxT("?"), wxT(""), false ); | |
269 | TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false ); | |
270 | TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false ); | |
271 | ||
272 | #undef TEST_ENDS_WITH | |
273 | } | |
274 | ||
275 | void StringTestCase::Trim() | |
276 | { | |
277 | #define TEST_TRIM( str , dir , result ) \ | |
278 | CPPUNIT_ASSERT( wxString(str).Trim(dir) == result ) | |
279 | ||
280 | TEST_TRIM( wxT(" Test "), true, wxT(" Test") ); | |
281 | TEST_TRIM( wxT(" "), true, wxT("") ); | |
282 | TEST_TRIM( wxT(" "), true, wxT("") ); | |
283 | TEST_TRIM( wxT(""), true, wxT("") ); | |
284 | ||
285 | TEST_TRIM( wxT(" Test "), false, wxT("Test ") ); | |
286 | TEST_TRIM( wxT(" "), false, wxT("") ); | |
287 | TEST_TRIM( wxT(" "), false, wxT("") ); | |
288 | TEST_TRIM( wxT(""), false, wxT("") ); | |
289 | ||
290 | #undef TEST_TRIM | |
291 | } | |
292 | ||
293 | void StringTestCase::Find() | |
294 | { | |
295 | #define TEST_FIND( str , start , result ) \ | |
296 | CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result ); | |
297 | ||
298 | TEST_FIND( wxT("Well, hello world"), 0, 1 ); | |
299 | TEST_FIND( wxT("Well, hello world"), 6, 7 ); | |
300 | TEST_FIND( wxT("Well, hello world"), 9, wxString::npos ); | |
301 | ||
302 | #undef TEST_FIND | |
303 | } | |
304 | ||
305 | void StringTestCase::Replace() | |
306 | { | |
307 | #define TEST_REPLACE( original , pos , len , replacement , result ) \ | |
308 | { \ | |
309 | wxString s = original; \ | |
310 | s.replace( pos , len , replacement ); \ | |
311 | CPPUNIT_ASSERT_EQUAL( result, s ); \ | |
312 | } | |
313 | ||
314 | TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") ); | |
315 | TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") ); | |
316 | TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") ); | |
317 | TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") ); | |
318 | TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") ); | |
319 | ||
320 | ||
321 | #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \ | |
322 | { \ | |
323 | wxString s(o,olen); \ | |
324 | s.replace( pos , len , replacement ); \ | |
325 | CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ | |
326 | } | |
327 | ||
328 | TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"), | |
329 | wxT("null\0dhar"), 9 ); | |
330 | ||
331 | #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \ | |
332 | { \ | |
333 | wxString s(o,olen); \ | |
334 | s.Replace( olds, news, all ); \ | |
335 | CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ | |
336 | } | |
337 | ||
338 | TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true, | |
339 | wxT("null\0dehar"), 10 ); | |
340 | ||
341 | TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true, | |
342 | wxT("null\0char"), 9 ); | |
343 | ||
344 | TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 ); | |
345 | TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 ); | |
346 | TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 ); | |
347 | TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 ); | |
348 | TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 ); | |
349 | ||
350 | #undef TEST_WXREPLACE | |
351 | #undef TEST_NULLCHARREPLACE | |
352 | #undef TEST_REPLACE | |
353 | } | |
354 | ||
355 | void StringTestCase::Match() | |
356 | { | |
357 | #define TEST_MATCH( s1 , s2 , result ) \ | |
358 | CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result ) | |
359 | ||
360 | TEST_MATCH( wxT("foobar"), wxT("foo*"), true ); | |
361 | TEST_MATCH( wxT("foobar"), wxT("*oo*"), true ); | |
362 | TEST_MATCH( wxT("foobar"), wxT("*bar"), true ); | |
363 | TEST_MATCH( wxT("foobar"), wxT("??????"), true ); | |
364 | TEST_MATCH( wxT("foobar"), wxT("f??b*"), true ); | |
365 | TEST_MATCH( wxT("foobar"), wxT("f?b*"), false ); | |
366 | TEST_MATCH( wxT("foobar"), wxT("*goo*"), false ); | |
367 | TEST_MATCH( wxT("foobar"), wxT("*foo"), false ); | |
368 | TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true ); | |
369 | TEST_MATCH( wxT(""), wxT("*"), true ); | |
370 | TEST_MATCH( wxT(""), wxT("?"), false ); | |
371 | ||
372 | #undef TEST_MATCH | |
373 | } | |
374 | ||
375 | ||
376 | void StringTestCase::CaseChanges() | |
377 | { | |
378 | wxString s1(wxT("Hello!")); | |
379 | wxString s1u(s1); | |
380 | wxString s1l(s1); | |
381 | s1u.MakeUpper(); | |
382 | s1l.MakeLower(); | |
383 | ||
384 | CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u ); | |
385 | CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l ); | |
386 | ||
387 | wxString s2u, s2l; | |
388 | s2u.MakeUpper(); | |
389 | s2l.MakeLower(); | |
390 | ||
391 | CPPUNIT_ASSERT_EQUAL( "", s2u ); | |
392 | CPPUNIT_ASSERT_EQUAL( "", s2l ); | |
393 | ||
394 | ||
395 | wxString s3("good bye"); | |
396 | CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() ); | |
397 | s3.MakeCapitalized(); | |
398 | CPPUNIT_ASSERT_EQUAL( "Good bye", s3 ); | |
399 | ||
400 | CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() ); | |
401 | ||
402 | CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() ); | |
403 | } | |
404 | ||
405 | void StringTestCase::Compare() | |
406 | { | |
407 | wxString s1 = wxT("AHH"); | |
408 | wxString eq = wxT("AHH"); | |
409 | wxString neq1 = wxT("HAH"); | |
410 | wxString neq2 = wxT("AH"); | |
411 | wxString neq3 = wxT("AHHH"); | |
412 | wxString neq4 = wxT("AhH"); | |
413 | ||
414 | CPPUNIT_ASSERT( s1 == eq ); | |
415 | CPPUNIT_ASSERT( s1 != neq1 ); | |
416 | CPPUNIT_ASSERT( s1 != neq2 ); | |
417 | CPPUNIT_ASSERT( s1 != neq3 ); | |
418 | CPPUNIT_ASSERT( s1 != neq4 ); | |
419 | ||
420 | CPPUNIT_ASSERT( s1 == wxT("AHH") ); | |
421 | CPPUNIT_ASSERT( s1 != wxT("no") ); | |
422 | CPPUNIT_ASSERT( s1 < wxT("AZ") ); | |
423 | CPPUNIT_ASSERT( s1 <= wxT("AZ") ); | |
424 | CPPUNIT_ASSERT( s1 <= wxT("AHH") ); | |
425 | CPPUNIT_ASSERT( s1 > wxT("AA") ); | |
426 | CPPUNIT_ASSERT( s1 >= wxT("AA") ); | |
427 | CPPUNIT_ASSERT( s1 >= wxT("AHH") ); | |
428 | ||
429 | // test comparison with C strings in Unicode build (must work in ANSI as | |
430 | // well, of course): | |
431 | CPPUNIT_ASSERT( s1 == "AHH" ); | |
432 | CPPUNIT_ASSERT( s1 != "no" ); | |
433 | CPPUNIT_ASSERT( s1 < "AZ" ); | |
434 | CPPUNIT_ASSERT( s1 <= "AZ" ); | |
435 | CPPUNIT_ASSERT( s1 <= "AHH" ); | |
436 | CPPUNIT_ASSERT( s1 > "AA" ); | |
437 | CPPUNIT_ASSERT( s1 >= "AA" ); | |
438 | CPPUNIT_ASSERT( s1 >= "AHH" ); | |
439 | ||
440 | // wxString _s1 = wxT("A\0HH"); | |
441 | // wxString _eq = wxT("A\0HH"); | |
442 | // wxString _neq1 = wxT("H\0AH"); | |
443 | // wxString _neq2 = wxT("A\0H"); | |
444 | // wxString _neq3 = wxT("A\0HHH"); | |
445 | // wxString _neq4 = wxT("A\0hH"); | |
446 | s1.insert(1,1,'\0'); | |
447 | eq.insert(1,1,'\0'); | |
448 | neq1.insert(1,1,'\0'); | |
449 | neq2.insert(1,1,'\0'); | |
450 | neq3.insert(1,1,'\0'); | |
451 | neq4.insert(1,1,'\0'); | |
452 | ||
453 | CPPUNIT_ASSERT( s1 == eq ); | |
454 | CPPUNIT_ASSERT( s1 != neq1 ); | |
455 | CPPUNIT_ASSERT( s1 != neq2 ); | |
456 | CPPUNIT_ASSERT( s1 != neq3 ); | |
457 | CPPUNIT_ASSERT( s1 != neq4 ); | |
458 | } | |
459 | ||
460 | void StringTestCase::CompareNoCase() | |
461 | { | |
462 | wxString s1 = wxT("AHH"); | |
463 | wxString eq = wxT("AHH"); | |
464 | wxString eq2 = wxT("AhH"); | |
465 | wxString eq3 = wxT("ahh"); | |
466 | wxString neq = wxT("HAH"); | |
467 | wxString neq2 = wxT("AH"); | |
468 | wxString neq3 = wxT("AHHH"); | |
469 | ||
470 | #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0) | |
471 | #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0) | |
472 | ||
473 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
474 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
475 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
476 | ||
477 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
478 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
479 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
480 | ||
481 | ||
482 | // wxString _s1 = wxT("A\0HH"); | |
483 | // wxString _eq = wxT("A\0HH"); | |
484 | // wxString _eq2 = wxT("A\0hH"); | |
485 | // wxString _eq3 = wxT("a\0hh"); | |
486 | // wxString _neq = wxT("H\0AH"); | |
487 | // wxString _neq2 = wxT("A\0H"); | |
488 | // wxString _neq3 = wxT("A\0HHH"); | |
489 | ||
490 | s1.insert(1,1,'\0'); | |
491 | eq.insert(1,1,'\0'); | |
492 | eq2.insert(1,1,'\0'); | |
493 | eq3.insert(1,1,'\0'); | |
494 | neq.insert(1,1,'\0'); | |
495 | neq2.insert(1,1,'\0'); | |
496 | neq3.insert(1,1,'\0'); | |
497 | ||
498 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
499 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
500 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
501 | ||
502 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
503 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
504 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
505 | } | |
506 | ||
507 | void StringTestCase::Contains() | |
508 | { | |
509 | static const struct ContainsData | |
510 | { | |
511 | const wxChar *hay; | |
512 | const wxChar *needle; | |
513 | bool contains; | |
514 | } containsData[] = | |
515 | { | |
516 | { wxT(""), wxT(""), true }, | |
517 | { wxT(""), wxT("foo"), false }, | |
518 | { wxT("foo"), wxT(""), true }, | |
519 | { wxT("foo"), wxT("f"), true }, | |
520 | { wxT("foo"), wxT("o"), true }, | |
521 | { wxT("foo"), wxT("oo"), true }, | |
522 | { wxT("foo"), wxT("ooo"), false }, | |
523 | { wxT("foo"), wxT("oooo"), false }, | |
524 | { wxT("foo"), wxT("fooo"), false }, | |
525 | }; | |
526 | ||
527 | for ( size_t n = 0; n < WXSIZEOF(containsData); n++ ) | |
528 | { | |
529 | const ContainsData& cd = containsData[n]; | |
530 | CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) ); | |
531 | } | |
532 | } | |
533 | ||
534 | // flags used in ToLongData.flags | |
535 | enum | |
536 | { | |
537 | Number_Ok = 0, | |
538 | Number_Invalid = 1, | |
539 | Number_Unsigned = 2, // if not specified, works for signed conversion | |
540 | Number_Signed = 4, // if not specified, works for unsigned | |
541 | Number_LongLong = 8, // only for long long tests | |
542 | Number_Long = 16 // only for long tests | |
543 | }; | |
544 | ||
545 | static const struct ToLongData | |
546 | { | |
547 | const wxChar *str; | |
548 | #ifdef wxLongLong_t | |
549 | wxLongLong_t value; | |
550 | #else | |
551 | long value; | |
552 | #endif // wxLongLong_t | |
553 | int flags; | |
554 | ||
555 | long LValue() const { return value; } | |
556 | unsigned long ULValue() const { return value; } | |
557 | #ifdef wxLongLong_t | |
558 | wxLongLong_t LLValue() const { return value; } | |
559 | wxULongLong_t ULLValue() const { return (wxULongLong_t)value; } | |
560 | #endif // wxLongLong_t | |
561 | ||
562 | bool IsOk() const { return !(flags & Number_Invalid); } | |
563 | } longData[] = | |
564 | { | |
565 | { wxT("1"), 1, Number_Ok }, | |
566 | { wxT("0"), 0, Number_Ok }, | |
567 | { wxT("a"), 0, Number_Invalid }, | |
568 | { wxT("12345"), 12345, Number_Ok }, | |
569 | { wxT("--1"), 0, Number_Invalid }, | |
570 | ||
571 | { wxT("-1"), -1, Number_Signed | Number_Long }, | |
572 | // this is surprising but consistent with strtoul() behaviour | |
573 | { wxT("-1"), ULONG_MAX, Number_Unsigned | Number_Long }, | |
574 | ||
575 | // this must overflow, even with 64 bit long | |
576 | { wxT("922337203685477580711"), 0, Number_Invalid }, | |
577 | ||
578 | #ifdef wxLongLong_t | |
579 | { wxT("2147483648"), wxLL(2147483648), Number_LongLong }, | |
580 | { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed }, | |
581 | { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong | | |
582 | Number_Unsigned }, | |
583 | #endif // wxLongLong_t | |
584 | }; | |
585 | ||
586 | void StringTestCase::ToLong() | |
587 | { | |
588 | long l; | |
589 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
590 | { | |
591 | const ToLongData& ld = longData[n]; | |
592 | ||
593 | if ( ld.flags & (Number_LongLong | Number_Unsigned) ) | |
594 | continue; | |
595 | ||
596 | // NOTE: unless you're using some exotic locale, ToCLong and ToLong | |
597 | // should behave the same for our test data set: | |
598 | ||
599 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCLong(&l) ); | |
600 | if ( ld.IsOk() ) | |
601 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); | |
602 | ||
603 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) ); | |
604 | if ( ld.IsOk() ) | |
605 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); | |
606 | } | |
607 | ||
608 | // special case: check that the output is not modified if the parsing | |
609 | // failed completely | |
610 | l = 17; | |
611 | CPPUNIT_ASSERT( !wxString("foo").ToLong(&l) ); | |
612 | CPPUNIT_ASSERT_EQUAL( 17, l ); | |
613 | ||
614 | // also check that it is modified if we did parse something successfully in | |
615 | // the beginning of the string | |
616 | CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l) ); | |
617 | CPPUNIT_ASSERT_EQUAL( 9, l ); | |
618 | } | |
619 | ||
620 | void StringTestCase::ToULong() | |
621 | { | |
622 | unsigned long ul; | |
623 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
624 | { | |
625 | const ToLongData& ld = longData[n]; | |
626 | ||
627 | if ( ld.flags & (Number_LongLong | Number_Signed) ) | |
628 | continue; | |
629 | ||
630 | // NOTE: unless you're using some exotic locale, ToCLong and ToLong | |
631 | // should behave the same for our test data set: | |
632 | ||
633 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCULong(&ul) ); | |
634 | if ( ld.IsOk() ) | |
635 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); | |
636 | ||
637 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) ); | |
638 | if ( ld.IsOk() ) | |
639 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); | |
640 | } | |
641 | } | |
642 | ||
643 | #ifdef wxLongLong_t | |
644 | ||
645 | void StringTestCase::ToLongLong() | |
646 | { | |
647 | wxLongLong_t l; | |
648 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
649 | { | |
650 | const ToLongData& ld = longData[n]; | |
651 | ||
652 | if ( ld.flags & (Number_Long | Number_Unsigned) ) | |
653 | continue; | |
654 | ||
655 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) ); | |
656 | if ( ld.IsOk() ) | |
657 | CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l ); | |
658 | } | |
659 | } | |
660 | ||
661 | void StringTestCase::ToULongLong() | |
662 | { | |
663 | wxULongLong_t ul; | |
664 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
665 | { | |
666 | const ToLongData& ld = longData[n]; | |
667 | ||
668 | if ( ld.flags & (Number_Long | Number_Signed) ) | |
669 | continue; | |
670 | ||
671 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) ); | |
672 | if ( ld.IsOk() ) | |
673 | CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul ); | |
674 | } | |
675 | } | |
676 | ||
677 | #endif // wxLongLong_t | |
678 | ||
679 | void StringTestCase::ToDouble() | |
680 | { | |
681 | double d; | |
682 | static const struct ToDoubleData | |
683 | { | |
684 | const wxChar *str; | |
685 | double value; | |
686 | bool ok; | |
687 | } doubleData[] = | |
688 | { | |
689 | { wxT("1"), 1, true }, | |
690 | { wxT("1.23"), 1.23, true }, | |
691 | { wxT(".1"), .1, true }, | |
692 | { wxT("1."), 1, true }, | |
693 | { wxT("1.."), 0, false }, | |
694 | { wxT("0"), 0, true }, | |
695 | { wxT("a"), 0, false }, | |
696 | { wxT("12345"), 12345, true }, | |
697 | { wxT("-1"), -1, true }, | |
698 | { wxT("--1"), 0, false }, | |
699 | { wxT("-3E-5"), -3E-5, true }, | |
700 | { wxT("-3E-abcde5"), 0, false }, | |
701 | }; | |
702 | ||
703 | // test ToCDouble() first: | |
704 | ||
705 | size_t n; | |
706 | for ( n = 0; n < WXSIZEOF(doubleData); n++ ) | |
707 | { | |
708 | const ToDoubleData& ld = doubleData[n]; | |
709 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToCDouble(&d) ); | |
710 | if ( ld.ok ) | |
711 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
712 | } | |
713 | ||
714 | ||
715 | // test ToDouble() now: | |
716 | // NOTE: for the test to be reliable, we need to set the locale explicitly | |
717 | // so that we know the decimal point character to use | |
718 | ||
719 | if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH)) | |
720 | return; // you should have french support installed to continue this test! | |
721 | ||
722 | wxLocale locale; | |
723 | ||
724 | // don't load default catalog, it may be unavailable: | |
725 | CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) ); | |
726 | ||
727 | static const struct ToDoubleData doubleData2[] = | |
728 | { | |
729 | { wxT("1"), 1, true }, | |
730 | { wxT("1,23"), 1.23, true }, | |
731 | { wxT(",1"), .1, true }, | |
732 | { wxT("1,"), 1, true }, | |
733 | { wxT("1,,"), 0, false }, | |
734 | { wxT("0"), 0, true }, | |
735 | { wxT("a"), 0, false }, | |
736 | { wxT("12345"), 12345, true }, | |
737 | { wxT("-1"), -1, true }, | |
738 | { wxT("--1"), 0, false }, | |
739 | { wxT("-3E-5"), -3E-5, true }, | |
740 | { wxT("-3E-abcde5"), 0, false }, | |
741 | }; | |
742 | ||
743 | for ( n = 0; n < WXSIZEOF(doubleData2); n++ ) | |
744 | { | |
745 | const ToDoubleData& ld = doubleData2[n]; | |
746 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); | |
747 | if ( ld.ok ) | |
748 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
749 | } | |
750 | } | |
751 | ||
752 | void StringTestCase::FromDouble() | |
753 | { | |
754 | static const struct FromDoubleTestData | |
755 | { | |
756 | double value; | |
757 | const char *str; | |
758 | } testData[] = | |
759 | { | |
760 | { 1.23, "1.23" }, | |
761 | // NB: there are no standards about the minimum exponent width | |
762 | // and newer MSVC versions use 3 digits as minimum exponent | |
763 | // width while GNU libc uses 2 digits as minimum width... | |
764 | #ifdef __VISUALC__ | |
765 | { -3e-10, "-3e-010" }, | |
766 | #else | |
767 | { -3e-10, "-3e-10" }, | |
768 | #endif | |
769 | { -0.45678, "-0.45678" }, | |
770 | }; | |
771 | ||
772 | for ( unsigned n = 0; n < WXSIZEOF(testData); n++ ) | |
773 | { | |
774 | const FromDoubleTestData& td = testData[n]; | |
775 | CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value) ); | |
776 | } | |
777 | ||
778 | if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) ) | |
779 | return; | |
780 | ||
781 | wxLocale locale; | |
782 | CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) ); | |
783 | ||
784 | for ( unsigned m = 0; m < WXSIZEOF(testData); m++ ) | |
785 | { | |
786 | const FromDoubleTestData& td = testData[m]; | |
787 | ||
788 | wxString str(td.str); | |
789 | str.Replace(".", ","); | |
790 | CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value) ); | |
791 | } | |
792 | } | |
793 | ||
794 | void StringTestCase::StringBuf() | |
795 | { | |
796 | // check that buffer can be used to write into the string | |
797 | wxString s; | |
798 | wxStrcpy(wxStringBuffer(s, 10), wxT("foo")); | |
799 | ||
800 | CPPUNIT_ASSERT_EQUAL(3, s.length()); | |
801 | CPPUNIT_ASSERT(wxT('f') == s[0u]); | |
802 | CPPUNIT_ASSERT(wxT('o') == s[1]); | |
803 | CPPUNIT_ASSERT(wxT('o') == s[2]); | |
804 | ||
805 | { | |
806 | // also check that the buffer initially contains the original string | |
807 | // contents | |
808 | wxStringBuffer buf(s, 10); | |
809 | CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] ); | |
810 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] ); | |
811 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] ); | |
812 | CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] ); | |
813 | } | |
814 | ||
815 | { | |
816 | wxStringBufferLength buf(s, 10); | |
817 | CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] ); | |
818 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] ); | |
819 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] ); | |
820 | CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] ); | |
821 | ||
822 | // and check that it can be used to write only the specified number of | |
823 | // characters to the string | |
824 | wxStrcpy(buf, wxT("barrbaz")); | |
825 | buf.SetLength(4); | |
826 | } | |
827 | ||
828 | CPPUNIT_ASSERT_EQUAL(4, s.length()); | |
829 | CPPUNIT_ASSERT(wxT('b') == s[0u]); | |
830 | CPPUNIT_ASSERT(wxT('a') == s[1]); | |
831 | CPPUNIT_ASSERT(wxT('r') == s[2]); | |
832 | CPPUNIT_ASSERT(wxT('r') == s[3]); | |
833 | ||
834 | // check that creating buffer of length smaller than string works, i.e. at | |
835 | // least doesn't crash (it would if we naively copied the entire original | |
836 | // string contents in the buffer) | |
837 | *wxStringBuffer(s, 1) = '!'; | |
838 | } | |
839 | ||
840 | void StringTestCase::UTF8Buf() | |
841 | { | |
842 | #if wxUSE_UNICODE | |
843 | // "czech" in Czech ("cestina"): | |
844 | static const char *textUTF8 = "\304\215e\305\241tina"; | |
845 | static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0}; | |
846 | ||
847 | wxString s; | |
848 | wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8); | |
849 | CPPUNIT_ASSERT(s == textUTF16); | |
850 | ||
851 | { | |
852 | wxUTF8StringBufferLength buf(s, 20); | |
853 | wxStrcpy(buf, textUTF8); | |
854 | buf.SetLength(5); | |
855 | } | |
856 | CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3)); | |
857 | #endif // wxUSE_UNICODE | |
858 | } | |
859 | ||
860 | ||
861 | ||
862 | void StringTestCase::CStrDataTernaryOperator() | |
863 | { | |
864 | DoCStrDataTernaryOperator(true); | |
865 | DoCStrDataTernaryOperator(false); | |
866 | } | |
867 | ||
868 | template<typename T> bool CheckStr(const wxString& expected, T s) | |
869 | { | |
870 | return expected == wxString(s); | |
871 | } | |
872 | ||
873 | void StringTestCase::DoCStrDataTernaryOperator(bool cond) | |
874 | { | |
875 | // test compilation of wxCStrData when used with operator?: (the asserts | |
876 | // are not very important, we're testing if the code compiles at all): | |
877 | ||
878 | wxString s("foo"); | |
879 | ||
880 | const wchar_t *wcStr = L"foo"; | |
881 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) ); | |
882 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) ); | |
883 | CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) ); | |
884 | CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) ); | |
885 | ||
886 | const char *mbStr = "foo"; | |
887 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) ); | |
888 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) ); | |
889 | CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) ); | |
890 | CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) ); | |
891 | ||
892 | wxString empty(""); | |
893 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) ); | |
894 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) ); | |
895 | } | |
896 | ||
897 | void StringTestCase::CStrDataOperators() | |
898 | { | |
899 | wxString s("hello"); | |
900 | ||
901 | CPPUNIT_ASSERT( s.c_str()[0] == 'h' ); | |
902 | CPPUNIT_ASSERT( s.c_str()[1] == 'e' ); | |
903 | ||
904 | // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access | |
905 | // the final character results in an assert failure (with debug CRT) | |
906 | //CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); | |
907 | ||
908 | CPPUNIT_ASSERT( *s.c_str() == 'h' ); | |
909 | CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' ); | |
910 | //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); | |
911 | } | |
912 | ||
913 | bool CheckStrChar(const wxString& expected, char *s) | |
914 | { return CheckStr(expected, s); } | |
915 | bool CheckStrWChar(const wxString& expected, wchar_t *s) | |
916 | { return CheckStr(expected, s); } | |
917 | bool CheckStrConstChar(const wxString& expected, const char *s) | |
918 | { return CheckStr(expected, s); } | |
919 | bool CheckStrConstWChar(const wxString& expected, const wchar_t *s) | |
920 | { return CheckStr(expected, s); } | |
921 | ||
922 | void StringTestCase::CStrDataImplicitConversion() | |
923 | { | |
924 | wxString s("foo"); | |
925 | ||
926 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) ); | |
927 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) ); | |
928 | ||
929 | // implicit conversion of wxString is not available in STL build | |
930 | #if !wxUSE_STL | |
931 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s) ); | |
932 | CPPUNIT_ASSERT( CheckStrConstChar(s, s) ); | |
933 | #endif | |
934 | } | |
935 | ||
936 | void StringTestCase::ExplicitConversion() | |
937 | { | |
938 | wxString s("foo"); | |
939 | ||
940 | CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) ); | |
941 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) ); | |
942 | CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) ); | |
943 | ||
944 | CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) ); | |
945 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) ); | |
946 | CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) ); | |
947 | } | |
948 | ||
949 | void StringTestCase::IndexedAccess() | |
950 | { | |
951 | wxString s("bar"); | |
952 | CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); | |
953 | ||
954 | // this tests for a possible bug in UTF-8 based wxString implementation: | |
955 | // the 3rd character of the underlying byte string is going to change, but | |
956 | // the 3rd character of wxString should remain the same | |
957 | s[0] = L'\xe9'; | |
958 | CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); | |
959 | } | |
960 | ||
961 | void StringTestCase::BeforeAndAfter() | |
962 | { | |
963 | const wxString s(L"letter=\xe9;\xe7a=l\xe0"); | |
964 | ||
965 | CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); | |
966 | CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); | |
967 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';') ); | |
968 | ||
969 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=') ); | |
970 | CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); | |
971 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';') ); | |
972 | ||
973 | CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", s.AfterFirst('=') ); | |
974 | CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') ); | |
975 | CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterFirst(';') ); | |
976 | ||
977 | CPPUNIT_ASSERT_EQUAL( L"l\xe0", s.AfterLast('=') ); | |
978 | CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') ); | |
979 | CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') ); | |
980 | } | |
981 | ||
982 | void StringTestCase::ScopedBuffers() | |
983 | { | |
984 | // wxString relies on efficient buffers, verify they work as they should | |
985 | ||
986 | const char *literal = "Hello World!"; | |
987 | ||
988 | // non-owned buffer points to the string passed to it | |
989 | wxScopedCharBuffer sbuf = wxScopedCharBuffer::CreateNonOwned(literal); | |
990 | CPPUNIT_ASSERT( sbuf.data() == literal ); | |
991 | ||
992 | // a copy of scoped non-owned buffer still points to the same string | |
993 | wxScopedCharBuffer sbuf2(sbuf); | |
994 | CPPUNIT_ASSERT( sbuf.data() == sbuf2.data() ); | |
995 | ||
996 | // but assigning it to wxCharBuffer makes a full copy | |
997 | wxCharBuffer buf(sbuf); | |
998 | CPPUNIT_ASSERT( buf.data() != literal ); | |
999 | CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); | |
1000 | ||
1001 | wxCharBuffer buf2 = sbuf; | |
1002 | CPPUNIT_ASSERT( buf2.data() != literal ); | |
1003 | CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); | |
1004 | } |