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