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