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