1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class StringTestCase
: public CppUnit::TestCase
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
);
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
);
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();
70 void StaticConstructors();
85 #endif // wxLongLong_t
89 void CStrDataTernaryOperator();
90 void DoCStrDataTernaryOperator(bool cond
);
91 void CStrDataOperators();
92 void CStrDataImplicitConversion();
93 void ExplicitConversion();
95 void BeforeAndAfter();
97 DECLARE_NO_COPY_CLASS(StringTestCase
)
100 // register in the unnamed registry so that these tests are run by default
101 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
103 // also include in it's own registry so that these tests can be run alone
104 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
106 StringTestCase::StringTestCase()
110 void StringTestCase::String()
118 for (int i
= 0; i
< 2; ++i
)
122 c
= _T("! How'ya doin'?");
125 c
= _T("Hello world! What's up?");
126 CPPUNIT_ASSERT( c
!= a
);
130 void StringTestCase::PChar()
136 for (int i
= 0; i
< 2; ++i
)
138 wxStrcpy (a
, _T("Hello"));
139 wxStrcpy (b
, _T(" world"));
140 wxStrcpy (c
, _T("! How'ya doin'?"));
143 wxStrcpy (c
, _T("Hello world! What's up?"));
144 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
148 void StringTestCase::Format()
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()) );
156 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
157 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
159 const size_t len
= lengths
[n
];
161 wxString
s(_T('Z'), len
);
162 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
166 void StringTestCase::Constructors()
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) );
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
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
) );
187 // test if creating string from NULL C pointer works:
188 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL
) );
191 void StringTestCase::StaticConstructors()
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") );
199 // FIXME: this doesn't work currently but should!
200 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
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") );
209 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
212 void StringTestCase::Extraction()
214 wxString
s(_T("Hello, world!"));
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 );
225 static const char *germanUTF8
= "Oberfl\303\244che";
226 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
228 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
229 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
230 #endif // wxUSE_UNICODE
234 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
235 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
237 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
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 );
247 #undef TEST_STARTS_WITH
249 rest
= "Hello world";
250 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
251 CPPUNIT_ASSERT_EQUAL("world", rest
);
253 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
254 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
256 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
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 );
268 #undef TEST_ENDS_WITH
271 void StringTestCase::Trim()
273 #define TEST_TRIM( str , dir , result ) \
274 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
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("") );
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("") );
289 void StringTestCase::Find()
291 #define TEST_FIND( str , start , result ) \
292 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
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
);
301 void StringTestCase::Replace()
303 #define TEST_REPLACE( original , pos , len , replacement , result ) \
305 wxString s = original; \
306 s.replace( pos , len , replacement ); \
307 CPPUNIT_ASSERT_EQUAL( result, s ); \
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") );
317 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
319 wxString s(o,olen); \
320 s.replace( pos , len , replacement ); \
321 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
324 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
325 _T("null\0dhar"), 9 );
327 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
329 wxString s(o,olen); \
330 s.Replace( olds, news, all ); \
331 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
334 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
335 _T("null\0dehar"), 10 );
337 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
338 _T("null\0char"), 9 );
340 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
341 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
342 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
343 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
344 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
346 #undef TEST_WXREPLACE
347 #undef TEST_NULLCHARREPLACE
351 void StringTestCase::Match()
353 #define TEST_MATCH( s1 , s2 , result ) \
354 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
356 TEST_MATCH( _T("foobar"), _T("foo*"), true );
357 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
358 TEST_MATCH( _T("foobar"), _T("*bar"), true );
359 TEST_MATCH( _T("foobar"), _T("??????"), true );
360 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
361 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
362 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
363 TEST_MATCH( _T("foobar"), _T("*foo"), false );
364 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
365 TEST_MATCH( _T(""), _T("*"), true );
366 TEST_MATCH( _T(""), _T("?"), false );
372 void StringTestCase::CaseChanges()
374 wxString
s1(_T("Hello!"));
380 CPPUNIT_ASSERT_EQUAL( _T("HELLO!"), s1u
);
381 CPPUNIT_ASSERT_EQUAL( _T("hello!"), s1l
);
387 CPPUNIT_ASSERT_EQUAL( "", s2u
);
388 CPPUNIT_ASSERT_EQUAL( "", s2l
);
391 wxString
s3("good bye");
392 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
.Capitalize() );
393 s3
.MakeCapitalized();
394 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
);
396 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
398 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
401 void StringTestCase::Compare()
403 wxString s1
= wxT("AHH");
404 wxString eq
= wxT("AHH");
405 wxString neq1
= wxT("HAH");
406 wxString neq2
= wxT("AH");
407 wxString neq3
= wxT("AHHH");
408 wxString neq4
= wxT("AhH");
410 CPPUNIT_ASSERT( s1
== eq
);
411 CPPUNIT_ASSERT( s1
!= neq1
);
412 CPPUNIT_ASSERT( s1
!= neq2
);
413 CPPUNIT_ASSERT( s1
!= neq3
);
414 CPPUNIT_ASSERT( s1
!= neq4
);
416 CPPUNIT_ASSERT( s1
== wxT("AHH") );
417 CPPUNIT_ASSERT( s1
!= wxT("no") );
418 CPPUNIT_ASSERT( s1
< wxT("AZ") );
419 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
420 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
421 CPPUNIT_ASSERT( s1
> wxT("AA") );
422 CPPUNIT_ASSERT( s1
>= wxT("AA") );
423 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
425 // test comparison with C strings in Unicode build (must work in ANSI as
427 CPPUNIT_ASSERT( s1
== "AHH" );
428 CPPUNIT_ASSERT( s1
!= "no" );
429 CPPUNIT_ASSERT( s1
< "AZ" );
430 CPPUNIT_ASSERT( s1
<= "AZ" );
431 CPPUNIT_ASSERT( s1
<= "AHH" );
432 CPPUNIT_ASSERT( s1
> "AA" );
433 CPPUNIT_ASSERT( s1
>= "AA" );
434 CPPUNIT_ASSERT( s1
>= "AHH" );
436 // wxString _s1 = wxT("A\0HH");
437 // wxString _eq = wxT("A\0HH");
438 // wxString _neq1 = wxT("H\0AH");
439 // wxString _neq2 = wxT("A\0H");
440 // wxString _neq3 = wxT("A\0HHH");
441 // wxString _neq4 = wxT("A\0hH");
444 neq1
.insert(1,1,'\0');
445 neq2
.insert(1,1,'\0');
446 neq3
.insert(1,1,'\0');
447 neq4
.insert(1,1,'\0');
449 CPPUNIT_ASSERT( s1
== eq
);
450 CPPUNIT_ASSERT( s1
!= neq1
);
451 CPPUNIT_ASSERT( s1
!= neq2
);
452 CPPUNIT_ASSERT( s1
!= neq3
);
453 CPPUNIT_ASSERT( s1
!= neq4
);
456 void StringTestCase::CompareNoCase()
458 wxString s1
= wxT("AHH");
459 wxString eq
= wxT("AHH");
460 wxString eq2
= wxT("AhH");
461 wxString eq3
= wxT("ahh");
462 wxString neq
= wxT("HAH");
463 wxString neq2
= wxT("AH");
464 wxString neq3
= wxT("AHHH");
466 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
467 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
469 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
470 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
471 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
473 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
474 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
475 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
478 // wxString _s1 = wxT("A\0HH");
479 // wxString _eq = wxT("A\0HH");
480 // wxString _eq2 = wxT("A\0hH");
481 // wxString _eq3 = wxT("a\0hh");
482 // wxString _neq = wxT("H\0AH");
483 // wxString _neq2 = wxT("A\0H");
484 // wxString _neq3 = wxT("A\0HHH");
488 eq2
.insert(1,1,'\0');
489 eq3
.insert(1,1,'\0');
490 neq
.insert(1,1,'\0');
491 neq2
.insert(1,1,'\0');
492 neq3
.insert(1,1,'\0');
494 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
495 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
496 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
498 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
499 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
500 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
503 void StringTestCase::Contains()
505 static const struct ContainsData
508 const wxChar
*needle
;
512 { _T(""), _T(""), true },
513 { _T(""), _T("foo"), false },
514 { _T("foo"), _T(""), true },
515 { _T("foo"), _T("f"), true },
516 { _T("foo"), _T("o"), true },
517 { _T("foo"), _T("oo"), true },
518 { _T("foo"), _T("ooo"), false },
519 { _T("foo"), _T("oooo"), false },
520 { _T("foo"), _T("fooo"), false },
523 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
525 const ContainsData
& cd
= containsData
[n
];
526 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
530 // flags used in ToLongData.flags
535 Number_Unsigned
= 2, // if not specified, works for signed conversion
536 Number_Signed
= 4, // if not specified, works for unsigned
537 Number_LongLong
= 8, // only for long long tests
538 Number_Long
= 16 // only for long tests
541 static const struct ToLongData
548 #endif // wxLongLong_t
551 long LValue() const { return value
; }
552 unsigned long ULValue() const { return value
; }
554 wxLongLong_t
LLValue() const { return value
; }
555 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
556 #endif // wxLongLong_t
558 bool IsOk() const { return !(flags
& Number_Invalid
); }
561 { _T("1"), 1, Number_Ok
},
562 { _T("0"), 0, Number_Ok
},
563 { _T("a"), 0, Number_Invalid
},
564 { _T("12345"), 12345, Number_Ok
},
565 { _T("--1"), 0, Number_Invalid
},
567 { _T("-1"), -1, Number_Signed
| Number_Long
},
568 // this is surprizing but consistent with strtoul() behaviour
569 { _T("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
571 // this must overflow, even with 64 bit long
572 { _T("922337203685477580711"), 0, Number_Invalid
},
575 { _T("2147483648"), wxLL(2147483648), Number_LongLong
},
576 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
577 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
579 #endif // wxLongLong_t
582 void StringTestCase::ToLong()
585 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
587 const ToLongData
& ld
= longData
[n
];
589 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
592 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
593 // should behave the same for our test data set:
595 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCLong(&l
) );
597 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
599 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
601 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
605 void StringTestCase::ToULong()
608 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
610 const ToLongData
& ld
= longData
[n
];
612 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
615 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
616 // should behave the same for our test data set:
618 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCULong(&ul
) );
620 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
622 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
624 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
630 void StringTestCase::ToLongLong()
633 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
635 const ToLongData
& ld
= longData
[n
];
637 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
640 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
642 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
646 void StringTestCase::ToULongLong()
649 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
651 const ToLongData
& ld
= longData
[n
];
653 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
656 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
658 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
662 #endif // wxLongLong_t
664 void StringTestCase::ToDouble()
667 static const struct ToDoubleData
674 { _T("1"), 1, true },
675 { _T("1.23"), 1.23, true },
676 { _T(".1"), .1, true },
677 { _T("1."), 1, true },
678 { _T("1.."), 0, false },
679 { _T("0"), 0, true },
680 { _T("a"), 0, false },
681 { _T("12345"), 12345, true },
682 { _T("-1"), -1, true },
683 { _T("--1"), 0, false },
684 { _T("-3E-5"), -3E-5, true },
685 { _T("-3E-abcde5"), 0, false },
688 // test ToCDouble() first:
691 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
693 const ToDoubleData
& ld
= doubleData
[n
];
694 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToCDouble(&d
) );
696 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
700 // test ToDouble() now:
701 // NOTE: for the test to be reliable, we need to set the locale explicitely
702 // so that we know the decimal point character to use
704 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
705 return; // you should have french support installed to continue this test!
707 wxLocale
*locale
= new wxLocale
;
709 // don't load default catalog, it may be unavailable:
710 CPPUNIT_ASSERT( locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_CONV_ENCODING
) );
712 static const struct ToDoubleData doubleData2
[] =
714 { _T("1"), 1, true },
715 { _T("1,23"), 1.23, true },
716 { _T(",1"), .1, true },
717 { _T("1,"), 1, true },
718 { _T("1,,"), 0, false },
719 { _T("0"), 0, true },
720 { _T("a"), 0, false },
721 { _T("12345"), 12345, true },
722 { _T("-1"), -1, true },
723 { _T("--1"), 0, false },
724 { _T("-3E-5"), -3E-5, true },
725 { _T("-3E-abcde5"), 0, false },
728 for ( n
= 0; n
< WXSIZEOF(doubleData2
); n
++ )
730 const ToDoubleData
& ld
= doubleData2
[n
];
731 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
733 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
739 void StringTestCase::StringBuf()
741 // check that buffer can be used to write into the string
743 wxStrcpy(wxStringBuffer(s
, 10), _T("foo"));
745 CPPUNIT_ASSERT_EQUAL(3, s
.length());
746 CPPUNIT_ASSERT(_T('f') == s
[0u]);
747 CPPUNIT_ASSERT(_T('o') == s
[1]);
748 CPPUNIT_ASSERT(_T('o') == s
[2]);
751 // also check that the buffer initially contains the original string
753 wxStringBuffer
buf(s
, 10);
754 CPPUNIT_ASSERT_EQUAL( _T('f'), buf
[0] );
755 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[1] );
756 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[2] );
757 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf
[3] );
761 wxStringBufferLength
buf(s
, 10);
762 CPPUNIT_ASSERT_EQUAL( _T('f'), buf
[0] );
763 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[1] );
764 CPPUNIT_ASSERT_EQUAL( _T('o'), buf
[2] );
765 CPPUNIT_ASSERT_EQUAL( _T('\0'), buf
[3] );
767 // and check that it can be used to write only the specified number of
768 // characters to the string
769 wxStrcpy(buf
, _T("barrbaz"));
773 CPPUNIT_ASSERT_EQUAL(4, s
.length());
774 CPPUNIT_ASSERT(_T('b') == s
[0u]);
775 CPPUNIT_ASSERT(_T('a') == s
[1]);
776 CPPUNIT_ASSERT(_T('r') == s
[2]);
777 CPPUNIT_ASSERT(_T('r') == s
[3]);
779 // check that creating buffer of length smaller than string works, i.e. at
780 // least doesn't crash (it would if we naively copied the entire original
781 // string contents in the buffer)
782 *wxStringBuffer(s
, 1) = '!';
785 void StringTestCase::UTF8Buf()
788 // "czech" in Czech ("cestina"):
789 static const char *textUTF8
= "\304\215e\305\241tina";
790 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
793 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
794 CPPUNIT_ASSERT(s
== textUTF16
);
797 wxUTF8StringBufferLength
buf(s
, 20);
798 wxStrcpy(buf
, textUTF8
);
801 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
802 #endif // wxUSE_UNICODE
807 void StringTestCase::CStrDataTernaryOperator()
809 DoCStrDataTernaryOperator(true);
810 DoCStrDataTernaryOperator(false);
813 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
815 return expected
== wxString(s
);
818 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
820 // test compilation of wxCStrData when used with operator?: (the asserts
821 // are not very important, we're testing if the code compiles at all):
825 const wchar_t *wcStr
= L
"foo";
826 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
827 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
828 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
829 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
831 const char *mbStr
= "foo";
832 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
833 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
834 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
835 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
838 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
839 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
842 void StringTestCase::CStrDataOperators()
846 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
847 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
849 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
850 // the final character results in an assert failure (with debug CRT)
851 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
853 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
854 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
855 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
858 bool CheckStrChar(const wxString
& expected
, char *s
)
859 { return CheckStr(expected
, s
); }
860 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
861 { return CheckStr(expected
, s
); }
862 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
863 { return CheckStr(expected
, s
); }
864 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
865 { return CheckStr(expected
, s
); }
867 void StringTestCase::CStrDataImplicitConversion()
871 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
872 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
874 // implicit conversion of wxString is not available in STL build
876 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
877 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
881 void StringTestCase::ExplicitConversion()
885 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
886 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
887 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
889 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
890 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
891 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );
894 void StringTestCase::IndexedAccess()
897 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
899 // this tests for a possible bug in UTF-8 based wxString implementation:
900 // the 3rd character of the underlying byte string is going to change, but
901 // the 3rd character of wxString should remain the same
903 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
906 void StringTestCase::BeforeAndAfter()
908 const wxString
s(L
"letter=\xe9;\xe7a=l\xe0");
910 CPPUNIT_ASSERT_EQUAL( "letter", s
.BeforeFirst('=') );
911 CPPUNIT_ASSERT_EQUAL( s
, s
.BeforeFirst('!') );
912 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9", s
.BeforeFirst(';') );
914 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9;\xe7a", s
.BeforeLast('=') );
915 CPPUNIT_ASSERT_EQUAL( "", s
.BeforeLast('!') );
916 CPPUNIT_ASSERT_EQUAL( L
"letter=\xe9", s
.BeforeLast(';') );
918 CPPUNIT_ASSERT_EQUAL( L
"\xe9;\xe7a=l\xe0", s
.AfterFirst('=') );
919 CPPUNIT_ASSERT_EQUAL( "", s
.AfterFirst('!') );
920 CPPUNIT_ASSERT_EQUAL( L
"\xe7a=l\xe0", s
.AfterFirst(';') );
922 CPPUNIT_ASSERT_EQUAL( L
"l\xe0", s
.AfterLast('=') );
923 CPPUNIT_ASSERT_EQUAL( s
, s
.AfterLast('!') );
924 CPPUNIT_ASSERT_EQUAL( L
"\xe7a=l\xe0", s
.AfterLast(';') );