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( 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();
72 void StaticConstructors();
87 #endif // wxLongLong_t
92 void CStrDataTernaryOperator();
93 void DoCStrDataTernaryOperator(bool cond
);
94 void CStrDataOperators();
95 void CStrDataImplicitConversion();
96 void ExplicitConversion();
98 void BeforeAndAfter();
101 DECLARE_NO_COPY_CLASS(StringTestCase
)
104 // register in the unnamed registry so that these tests are run by default
105 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
107 // also include in its own registry so that these tests can be run alone
108 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
110 StringTestCase::StringTestCase()
114 void StringTestCase::String()
122 for (int i
= 0; i
< 2; ++i
)
126 c
= wxT("! How'ya doin'?");
129 c
= wxT("Hello world! What's up?");
130 CPPUNIT_ASSERT( c
!= a
);
134 void StringTestCase::PChar()
140 for (int i
= 0; i
< 2; ++i
)
142 wxStrcpy (a
, wxT("Hello"));
143 wxStrcpy (b
, wxT(" world"));
144 wxStrcpy (c
, wxT("! How'ya doin'?"));
147 wxStrcpy (c
, wxT("Hello world! What's up?"));
148 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
152 void StringTestCase::Format()
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()) );
160 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
161 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
163 const size_t len
= lengths
[n
];
165 wxString
s(wxT('Z'), len
);
166 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(wxT("%s"), s
.c_str()).length());
173 wxString::Format(wxT("%2$s %1$s"), wxT("one"), wxT("two"))
177 void StringTestCase::Constructors()
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) );
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
193 CPPUNIT_ASSERT_EQUAL( 0, wxString(wxString(), 17).length() );
195 // This string has 3 characters (<h>, <e'> and <l>), not 4!
196 wxString
s3("h\xc3\xa9llo", 4);
197 CPPUNIT_ASSERT_EQUAL( 3, s3
.length() );
198 CPPUNIT_ASSERT_EQUAL( 'l', (char)s3
[2] );
201 static const char *s
= "?really!";
202 const char *start
= wxStrchr(s
, 'r');
203 const char *end
= wxStrchr(s
, '!');
204 CPPUNIT_ASSERT_EQUAL( "really", wxString(start
, end
) );
206 // test if creating string from NULL C pointer works:
207 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL
) );
210 void StringTestCase::StaticConstructors()
212 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
213 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
214 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
215 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
216 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
218 // FIXME: this doesn't work currently but should!
219 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
222 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
223 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
224 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
225 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
226 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
228 CPPUNIT_ASSERT_EQUAL( 2, wxString::FromUTF8("h\xc3\xa9llo", 3).length() );
231 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
234 void StringTestCase::Extraction()
236 wxString
s(wxT("Hello, world!"));
238 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , wxT("Hello, world!") ) == 0 );
239 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , wxT("Hello") ) == 0 );
240 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , wxT("world!") ) == 0 );
241 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 );
242 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , wxT("lo, world!") ) == 0 );
243 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , wxT("lo, w") ) == 0 );
244 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , wxT("lo, world!") ) == 0 );
247 static const char *germanUTF8
= "Oberfl\303\244che";
248 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
250 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
251 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
252 #endif // wxUSE_UNICODE
256 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
257 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
259 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
261 TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true );
262 TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true );
263 TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true );
264 TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false );
265 TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true );
266 TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false );
267 TEST_STARTS_WITH( wxT("Hi"), wxT(""), false );
269 #undef TEST_STARTS_WITH
271 rest
= "Hello world";
272 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
273 CPPUNIT_ASSERT_EQUAL("world", rest
);
275 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
276 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
278 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
280 TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true );
281 TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true );
282 TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true );
283 TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true );
284 TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true );
285 TEST_ENDS_WITH( wxT("very long string"), wxT(""), false );
286 TEST_ENDS_WITH( wxT("?"), wxT(""), false );
287 TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false );
288 TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false );
290 #undef TEST_ENDS_WITH
293 void StringTestCase::Trim()
295 #define TEST_TRIM( str , dir , result ) \
296 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
298 TEST_TRIM( wxT(" Test "), true, wxT(" Test") );
299 TEST_TRIM( wxT(" "), true, wxT("") );
300 TEST_TRIM( wxT(" "), true, wxT("") );
301 TEST_TRIM( wxT(""), true, wxT("") );
303 TEST_TRIM( wxT(" Test "), false, wxT("Test ") );
304 TEST_TRIM( wxT(" "), false, wxT("") );
305 TEST_TRIM( wxT(" "), false, wxT("") );
306 TEST_TRIM( wxT(""), false, wxT("") );
311 void StringTestCase::Find()
313 #define TEST_FIND( str , start , result ) \
314 CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
316 TEST_FIND( wxT("Well, hello world"), 0, 1 );
317 TEST_FIND( wxT("Well, hello world"), 6, 7 );
318 TEST_FIND( wxT("Well, hello world"), 9, wxString::npos
);
323 void StringTestCase::Replace()
325 #define TEST_REPLACE( original , pos , len , replacement , result ) \
327 wxString s = original; \
328 s.replace( pos , len , replacement ); \
329 CPPUNIT_ASSERT_EQUAL( result, s ); \
332 TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") );
333 TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") );
334 TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") );
335 TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") );
336 TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") );
339 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
341 wxString s(o,olen); \
342 s.replace( pos , len , replacement ); \
343 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
346 TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
347 wxT("null\0dhar"), 9 );
349 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
351 wxString s(o,olen); \
352 s.Replace( olds, news, all ); \
353 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
356 TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
357 wxT("null\0dehar"), 10 );
359 TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
360 wxT("null\0char"), 9 );
362 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
363 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
364 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
365 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
366 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
368 #undef TEST_WXREPLACE
369 #undef TEST_NULLCHARREPLACE
373 void StringTestCase::Match()
375 #define TEST_MATCH( s1 , s2 , result ) \
376 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
378 TEST_MATCH( wxT("foobar"), wxT("foo*"), true );
379 TEST_MATCH( wxT("foobar"), wxT("*oo*"), true );
380 TEST_MATCH( wxT("foobar"), wxT("*bar"), true );
381 TEST_MATCH( wxT("foobar"), wxT("??????"), true );
382 TEST_MATCH( wxT("foobar"), wxT("f??b*"), true );
383 TEST_MATCH( wxT("foobar"), wxT("f?b*"), false );
384 TEST_MATCH( wxT("foobar"), wxT("*goo*"), false );
385 TEST_MATCH( wxT("foobar"), wxT("*foo"), false );
386 TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true );
387 TEST_MATCH( wxT(""), wxT("*"), true );
388 TEST_MATCH( wxT(""), wxT("?"), false );
394 void StringTestCase::CaseChanges()
396 wxString
s1(wxT("Hello!"));
402 CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u
);
403 CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l
);
409 CPPUNIT_ASSERT_EQUAL( "", s2u
);
410 CPPUNIT_ASSERT_EQUAL( "", s2l
);
413 wxString
s3("good bye");
414 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
.Capitalize() );
415 s3
.MakeCapitalized();
416 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
);
418 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
420 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
423 void StringTestCase::Compare()
425 wxString s1
= wxT("AHH");
426 wxString eq
= wxT("AHH");
427 wxString neq1
= wxT("HAH");
428 wxString neq2
= wxT("AH");
429 wxString neq3
= wxT("AHHH");
430 wxString neq4
= wxT("AhH");
432 CPPUNIT_ASSERT( s1
== eq
);
433 CPPUNIT_ASSERT( s1
!= neq1
);
434 CPPUNIT_ASSERT( s1
!= neq2
);
435 CPPUNIT_ASSERT( s1
!= neq3
);
436 CPPUNIT_ASSERT( s1
!= neq4
);
438 CPPUNIT_ASSERT( s1
== wxT("AHH") );
439 CPPUNIT_ASSERT( s1
!= wxT("no") );
440 CPPUNIT_ASSERT( s1
< wxT("AZ") );
441 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
442 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
443 CPPUNIT_ASSERT( s1
> wxT("AA") );
444 CPPUNIT_ASSERT( s1
>= wxT("AA") );
445 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
447 // test comparison with C strings in Unicode build (must work in ANSI as
449 CPPUNIT_ASSERT( s1
== "AHH" );
450 CPPUNIT_ASSERT( s1
!= "no" );
451 CPPUNIT_ASSERT( s1
< "AZ" );
452 CPPUNIT_ASSERT( s1
<= "AZ" );
453 CPPUNIT_ASSERT( s1
<= "AHH" );
454 CPPUNIT_ASSERT( s1
> "AA" );
455 CPPUNIT_ASSERT( s1
>= "AA" );
456 CPPUNIT_ASSERT( s1
>= "AHH" );
458 // wxString _s1 = wxT("A\0HH");
459 // wxString _eq = wxT("A\0HH");
460 // wxString _neq1 = wxT("H\0AH");
461 // wxString _neq2 = wxT("A\0H");
462 // wxString _neq3 = wxT("A\0HHH");
463 // wxString _neq4 = wxT("A\0hH");
466 neq1
.insert(1,1,'\0');
467 neq2
.insert(1,1,'\0');
468 neq3
.insert(1,1,'\0');
469 neq4
.insert(1,1,'\0');
471 CPPUNIT_ASSERT( s1
== eq
);
472 CPPUNIT_ASSERT( s1
!= neq1
);
473 CPPUNIT_ASSERT( s1
!= neq2
);
474 CPPUNIT_ASSERT( s1
!= neq3
);
475 CPPUNIT_ASSERT( s1
!= neq4
);
477 CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
478 CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
479 CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
482 void StringTestCase::CompareNoCase()
484 wxString s1
= wxT("AHH");
485 wxString eq
= wxT("AHH");
486 wxString eq2
= wxT("AhH");
487 wxString eq3
= wxT("ahh");
488 wxString neq
= wxT("HAH");
489 wxString neq2
= wxT("AH");
490 wxString neq3
= wxT("AHHH");
492 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
493 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
495 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
496 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
497 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
499 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
500 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
501 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
504 // wxString _s1 = wxT("A\0HH");
505 // wxString _eq = wxT("A\0HH");
506 // wxString _eq2 = wxT("A\0hH");
507 // wxString _eq3 = wxT("a\0hh");
508 // wxString _neq = wxT("H\0AH");
509 // wxString _neq2 = wxT("A\0H");
510 // wxString _neq3 = wxT("A\0HHH");
514 eq2
.insert(1,1,'\0');
515 eq3
.insert(1,1,'\0');
516 neq
.insert(1,1,'\0');
517 neq2
.insert(1,1,'\0');
518 neq3
.insert(1,1,'\0');
520 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
521 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
522 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
524 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
525 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
526 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
528 CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
529 CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
530 CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
533 void StringTestCase::Contains()
535 static const struct ContainsData
538 const wxChar
*needle
;
542 { wxT(""), wxT(""), true },
543 { wxT(""), wxT("foo"), false },
544 { wxT("foo"), wxT(""), true },
545 { wxT("foo"), wxT("f"), true },
546 { wxT("foo"), wxT("o"), true },
547 { wxT("foo"), wxT("oo"), true },
548 { wxT("foo"), wxT("ooo"), false },
549 { wxT("foo"), wxT("oooo"), false },
550 { wxT("foo"), wxT("fooo"), false },
553 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
555 const ContainsData
& cd
= containsData
[n
];
556 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
560 // flags used in ToLongData.flags
565 Number_Unsigned
= 2, // if not specified, works for signed conversion
566 Number_Signed
= 4, // if not specified, works for unsigned
567 Number_LongLong
= 8, // only for long long tests
568 Number_Long
= 16 // only for long tests
571 static const struct ToLongData
578 #endif // wxLongLong_t
581 long LValue() const { return value
; }
582 unsigned long ULValue() const { return value
; }
584 wxLongLong_t
LLValue() const { return value
; }
585 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
586 #endif // wxLongLong_t
588 bool IsOk() const { return !(flags
& Number_Invalid
); }
591 { wxT("1"), 1, Number_Ok
},
592 { wxT("0"), 0, Number_Ok
},
593 { wxT("a"), 0, Number_Invalid
},
594 { wxT("12345"), 12345, Number_Ok
},
595 { wxT("--1"), 0, Number_Invalid
},
597 { wxT("-1"), -1, Number_Signed
| Number_Long
},
598 // this is surprising but consistent with strtoul() behaviour
599 { wxT("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
601 // this must overflow, even with 64 bit long
602 { wxT("922337203685477580711"), 0, Number_Invalid
},
605 { wxT("2147483648"), wxLL(2147483648), Number_LongLong
},
606 { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
607 { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
609 #endif // wxLongLong_t
612 void StringTestCase::ToLong()
615 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
617 const ToLongData
& ld
= longData
[n
];
619 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
622 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
623 // should behave the same for our test data set:
625 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCLong(&l
) );
627 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
629 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
631 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
634 // special case: check that the output is not modified if the parsing
637 CPPUNIT_ASSERT( !wxString("foo").ToLong(&l
) );
638 CPPUNIT_ASSERT_EQUAL( 17, l
);
640 // also check that it is modified if we did parse something successfully in
641 // the beginning of the string
642 CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l
) );
643 CPPUNIT_ASSERT_EQUAL( 9, l
);
646 void StringTestCase::ToULong()
649 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
651 const ToLongData
& ld
= longData
[n
];
653 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
656 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
657 // should behave the same for our test data set:
659 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCULong(&ul
) );
661 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
663 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
665 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
671 void StringTestCase::ToLongLong()
674 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
676 const ToLongData
& ld
= longData
[n
];
678 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
681 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
683 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
687 void StringTestCase::ToULongLong()
690 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
692 const ToLongData
& ld
= longData
[n
];
694 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
697 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
699 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
703 #endif // wxLongLong_t
705 void StringTestCase::ToDouble()
708 static const struct ToDoubleData
715 { wxT("1"), 1, true },
716 { wxT("1.23"), 1.23, true },
717 { wxT(".1"), .1, true },
718 { wxT("1."), 1, true },
719 { wxT("1.."), 0, false },
720 { wxT("0"), 0, true },
721 { wxT("a"), 0, false },
722 { wxT("12345"), 12345, true },
723 { wxT("-1"), -1, true },
724 { wxT("--1"), 0, false },
725 { wxT("-3E-5"), -3E-5, true },
726 { wxT("-3E-abcde5"), 0, false },
729 // test ToCDouble() first:
732 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
734 const ToDoubleData
& ld
= doubleData
[n
];
735 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToCDouble(&d
) );
737 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
741 // test ToDouble() now:
742 // NOTE: for the test to be reliable, we need to set the locale explicitly
743 // so that we know the decimal point character to use
745 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
746 return; // you should have french support installed to continue this test!
750 // don't load default catalog, it may be unavailable:
751 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
753 static const struct ToDoubleData doubleData2
[] =
755 { wxT("1"), 1, true },
756 { wxT("1,23"), 1.23, true },
757 { wxT(",1"), .1, true },
758 { wxT("1,"), 1, true },
759 { wxT("1,,"), 0, false },
760 { wxT("0"), 0, true },
761 { wxT("a"), 0, false },
762 { wxT("12345"), 12345, true },
763 { wxT("-1"), -1, true },
764 { wxT("--1"), 0, false },
765 { wxT("-3E-5"), -3E-5, true },
766 { wxT("-3E-abcde5"), 0, false },
769 for ( n
= 0; n
< WXSIZEOF(doubleData2
); n
++ )
771 const ToDoubleData
& ld
= doubleData2
[n
];
772 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
774 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
778 void StringTestCase::FromDouble()
780 static const struct FromDoubleTestData
787 { 1.23, -1, "1.23" },
788 // NB: there are no standards about the minimum exponent width
789 // and newer MSVC versions use 3 digits as minimum exponent
790 // width while GNU libc uses 2 digits as minimum width...
791 #ifdef wxUSING_VC_CRT_IO
792 { -3e-10, -1, "-3e-010" },
794 { -3e-10, -1, "-3e-10" },
796 { -0.45678, -1, "-0.45678" },
797 { 1.2345678, 0, "1" },
798 { 1.2345678, 1, "1.2" },
799 { 1.2345678, 2, "1.23" },
800 { 1.2345678, 3, "1.235" },
803 for ( unsigned n
= 0; n
< WXSIZEOF(testData
); n
++ )
805 const FromDoubleTestData
& td
= testData
[n
];
806 CPPUNIT_ASSERT_EQUAL( td
.str
, wxString::FromCDouble(td
.value
, td
.prec
) );
809 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
813 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
815 for ( unsigned m
= 0; m
< WXSIZEOF(testData
); m
++ )
817 const FromDoubleTestData
& td
= testData
[m
];
819 wxString
str(td
.str
);
820 str
.Replace(".", ",");
821 CPPUNIT_ASSERT_EQUAL( str
, wxString::FromDouble(td
.value
, td
.prec
) );
825 void StringTestCase::StringBuf()
827 // check that buffer can be used to write into the string
829 wxStrcpy(wxStringBuffer(s
, 10), wxT("foo"));
831 CPPUNIT_ASSERT_EQUAL(3, s
.length());
832 CPPUNIT_ASSERT(wxT('f') == s
[0u]);
833 CPPUNIT_ASSERT(wxT('o') == s
[1]);
834 CPPUNIT_ASSERT(wxT('o') == s
[2]);
837 // also check that the buffer initially contains the original string
839 wxStringBuffer
buf(s
, 10);
840 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
841 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
842 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
843 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
847 wxStringBufferLength
buf(s
, 10);
848 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
849 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
850 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
851 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
853 // and check that it can be used to write only the specified number of
854 // characters to the string
855 wxStrcpy(buf
, wxT("barrbaz"));
859 CPPUNIT_ASSERT_EQUAL(4, s
.length());
860 CPPUNIT_ASSERT(wxT('b') == s
[0u]);
861 CPPUNIT_ASSERT(wxT('a') == s
[1]);
862 CPPUNIT_ASSERT(wxT('r') == s
[2]);
863 CPPUNIT_ASSERT(wxT('r') == s
[3]);
865 // check that creating buffer of length smaller than string works, i.e. at
866 // least doesn't crash (it would if we naively copied the entire original
867 // string contents in the buffer)
868 *wxStringBuffer(s
, 1) = '!';
871 void StringTestCase::UTF8Buf()
874 // "czech" in Czech ("cestina"):
875 static const char *textUTF8
= "\304\215e\305\241tina";
876 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
879 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
880 CPPUNIT_ASSERT(s
== textUTF16
);
883 wxUTF8StringBufferLength
buf(s
, 20);
884 wxStrcpy(buf
, textUTF8
);
887 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
888 #endif // wxUSE_UNICODE
893 void StringTestCase::CStrDataTernaryOperator()
895 DoCStrDataTernaryOperator(true);
896 DoCStrDataTernaryOperator(false);
899 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
901 return expected
== wxString(s
);
904 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
906 // test compilation of wxCStrData when used with operator?: (the asserts
907 // are not very important, we're testing if the code compiles at all):
911 const wchar_t *wcStr
= L
"foo";
912 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
913 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
914 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
915 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
917 const char *mbStr
= "foo";
918 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
919 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
920 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
921 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
924 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
925 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
928 void StringTestCase::CStrDataOperators()
932 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
933 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
935 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
936 // the final character results in an assert failure (with debug CRT)
937 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
939 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
940 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
941 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
944 bool CheckStrChar(const wxString
& expected
, char *s
)
945 { return CheckStr(expected
, s
); }
946 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
947 { return CheckStr(expected
, s
); }
948 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
949 { return CheckStr(expected
, s
); }
950 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
951 { return CheckStr(expected
, s
); }
953 void StringTestCase::CStrDataImplicitConversion()
957 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
958 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
960 // implicit conversion of wxString is not available in STL build
962 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
963 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
967 void StringTestCase::ExplicitConversion()
971 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
972 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
973 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
975 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
976 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
977 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );
980 void StringTestCase::IndexedAccess()
983 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
985 // this tests for a possible bug in UTF-8 based wxString implementation:
986 // the 3rd character of the underlying byte string is going to change, but
987 // the 3rd character of wxString should remain the same
989 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
992 void StringTestCase::BeforeAndAfter()
994 // Construct a string with 2 equal signs in it by concatenating its three
995 // parts: before the first "=", in between the two "="s and after the last
996 // one. This allows to avoid duplicating the string contents (which has to
997 // be different for Unicode and ANSI builds) in the tests below.
999 #define FIRST_PART L"letter"
1000 #define MIDDLE_PART L"\xe9;\xe7a"
1001 #define LAST_PART L"l\xe0"
1002 #else // !wxUSE_UNICODE
1003 #define FIRST_PART "letter"
1004 #define MIDDLE_PART "e;ca"
1005 #define LAST_PART "la"
1006 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1008 const wxString
s(FIRST_PART
wxT("=") MIDDLE_PART
wxT("=") LAST_PART
);
1012 CPPUNIT_ASSERT_EQUAL( FIRST_PART
, s
.BeforeFirst('=', &r
) );
1013 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, r
);
1015 CPPUNIT_ASSERT_EQUAL( s
, s
.BeforeFirst('!', &r
) );
1016 CPPUNIT_ASSERT_EQUAL( "", r
);
1019 CPPUNIT_ASSERT_EQUAL( FIRST_PART
wxT("=") MIDDLE_PART
, s
.BeforeLast('=', &r
) );
1020 CPPUNIT_ASSERT_EQUAL( LAST_PART
, r
);
1022 CPPUNIT_ASSERT_EQUAL( "", s
.BeforeLast('!', &r
) );
1023 CPPUNIT_ASSERT_EQUAL( s
, r
);
1026 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, s
.AfterFirst('=') );
1027 CPPUNIT_ASSERT_EQUAL( "", s
.AfterFirst('!') );
1030 CPPUNIT_ASSERT_EQUAL( LAST_PART
, s
.AfterLast('=') );
1031 CPPUNIT_ASSERT_EQUAL( s
, s
.AfterLast('!') );
1038 void StringTestCase::ScopedBuffers()
1040 // wxString relies on efficient buffers, verify they work as they should
1042 const char *literal
= "Hello World!";
1044 // non-owned buffer points to the string passed to it
1045 wxScopedCharBuffer sbuf
= wxScopedCharBuffer::CreateNonOwned(literal
);
1046 CPPUNIT_ASSERT( sbuf
.data() == literal
);
1048 // a copy of scoped non-owned buffer still points to the same string
1049 wxScopedCharBuffer
sbuf2(sbuf
);
1050 CPPUNIT_ASSERT( sbuf
.data() == sbuf2
.data() );
1052 // but assigning it to wxCharBuffer makes a full copy
1053 wxCharBuffer
buf(sbuf
);
1054 CPPUNIT_ASSERT( buf
.data() != literal
);
1055 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1057 wxCharBuffer buf2
= sbuf
;
1058 CPPUNIT_ASSERT( buf2
.data() != literal
);
1059 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1061 // Check that extending the buffer keeps it NUL-terminated.
1064 wxCharBuffer
buf3(len
);
1065 CPPUNIT_ASSERT_EQUAL('\0', buf3
.data()[len
]);
1069 CPPUNIT_ASSERT_EQUAL('\0', buf4
.data()[len
]);
1071 wxCharBuffer
buf5(5);
1073 CPPUNIT_ASSERT_EQUAL('\0', buf5
.data()[len
]);