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 static const char *s
= "?really!";
194 const char *start
= wxStrchr(s
, 'r');
195 const char *end
= wxStrchr(s
, '!');
196 CPPUNIT_ASSERT_EQUAL( "really", wxString(start
, end
) );
198 // test if creating string from NULL C pointer works:
199 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL
) );
202 void StringTestCase::StaticConstructors()
204 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
205 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
206 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
207 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
208 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
210 // FIXME: this doesn't work currently but should!
211 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
214 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
215 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
216 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
217 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
218 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
220 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
223 void StringTestCase::Extraction()
225 wxString
s(wxT("Hello, world!"));
227 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , wxT("Hello, world!") ) == 0 );
228 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , wxT("Hello") ) == 0 );
229 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , wxT("world!") ) == 0 );
230 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 );
231 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , wxT("lo, world!") ) == 0 );
232 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , wxT("lo, w") ) == 0 );
233 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , wxT("lo, world!") ) == 0 );
236 static const char *germanUTF8
= "Oberfl\303\244che";
237 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
239 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
240 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
241 #endif // wxUSE_UNICODE
245 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
246 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
248 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
250 TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true );
251 TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true );
252 TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true );
253 TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false );
254 TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true );
255 TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false );
256 TEST_STARTS_WITH( wxT("Hi"), wxT(""), false );
258 #undef TEST_STARTS_WITH
260 rest
= "Hello world";
261 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
262 CPPUNIT_ASSERT_EQUAL("world", rest
);
264 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
265 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
267 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
269 TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true );
270 TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true );
271 TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true );
272 TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true );
273 TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true );
274 TEST_ENDS_WITH( wxT("very long string"), wxT(""), false );
275 TEST_ENDS_WITH( wxT("?"), wxT(""), false );
276 TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false );
277 TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false );
279 #undef TEST_ENDS_WITH
282 void StringTestCase::Trim()
284 #define TEST_TRIM( str , dir , result ) \
285 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
287 TEST_TRIM( wxT(" Test "), true, wxT(" Test") );
288 TEST_TRIM( wxT(" "), true, wxT("") );
289 TEST_TRIM( wxT(" "), true, wxT("") );
290 TEST_TRIM( wxT(""), true, wxT("") );
292 TEST_TRIM( wxT(" Test "), false, wxT("Test ") );
293 TEST_TRIM( wxT(" "), false, wxT("") );
294 TEST_TRIM( wxT(" "), false, wxT("") );
295 TEST_TRIM( wxT(""), false, wxT("") );
300 void StringTestCase::Find()
302 #define TEST_FIND( str , start , result ) \
303 CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
305 TEST_FIND( wxT("Well, hello world"), 0, 1 );
306 TEST_FIND( wxT("Well, hello world"), 6, 7 );
307 TEST_FIND( wxT("Well, hello world"), 9, wxString::npos
);
312 void StringTestCase::Replace()
314 #define TEST_REPLACE( original , pos , len , replacement , result ) \
316 wxString s = original; \
317 s.replace( pos , len , replacement ); \
318 CPPUNIT_ASSERT_EQUAL( result, s ); \
321 TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") );
322 TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") );
323 TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") );
324 TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") );
325 TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") );
328 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
330 wxString s(o,olen); \
331 s.replace( pos , len , replacement ); \
332 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
335 TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
336 wxT("null\0dhar"), 9 );
338 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
340 wxString s(o,olen); \
341 s.Replace( olds, news, all ); \
342 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
345 TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
346 wxT("null\0dehar"), 10 );
348 TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
349 wxT("null\0char"), 9 );
351 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
352 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
353 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
354 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
355 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
357 #undef TEST_WXREPLACE
358 #undef TEST_NULLCHARREPLACE
362 void StringTestCase::Match()
364 #define TEST_MATCH( s1 , s2 , result ) \
365 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
367 TEST_MATCH( wxT("foobar"), wxT("foo*"), true );
368 TEST_MATCH( wxT("foobar"), wxT("*oo*"), true );
369 TEST_MATCH( wxT("foobar"), wxT("*bar"), true );
370 TEST_MATCH( wxT("foobar"), wxT("??????"), true );
371 TEST_MATCH( wxT("foobar"), wxT("f??b*"), true );
372 TEST_MATCH( wxT("foobar"), wxT("f?b*"), false );
373 TEST_MATCH( wxT("foobar"), wxT("*goo*"), false );
374 TEST_MATCH( wxT("foobar"), wxT("*foo"), false );
375 TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true );
376 TEST_MATCH( wxT(""), wxT("*"), true );
377 TEST_MATCH( wxT(""), wxT("?"), false );
383 void StringTestCase::CaseChanges()
385 wxString
s1(wxT("Hello!"));
391 CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u
);
392 CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l
);
398 CPPUNIT_ASSERT_EQUAL( "", s2u
);
399 CPPUNIT_ASSERT_EQUAL( "", s2l
);
402 wxString
s3("good bye");
403 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
.Capitalize() );
404 s3
.MakeCapitalized();
405 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
);
407 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
409 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
412 void StringTestCase::Compare()
414 wxString s1
= wxT("AHH");
415 wxString eq
= wxT("AHH");
416 wxString neq1
= wxT("HAH");
417 wxString neq2
= wxT("AH");
418 wxString neq3
= wxT("AHHH");
419 wxString neq4
= wxT("AhH");
421 CPPUNIT_ASSERT( s1
== eq
);
422 CPPUNIT_ASSERT( s1
!= neq1
);
423 CPPUNIT_ASSERT( s1
!= neq2
);
424 CPPUNIT_ASSERT( s1
!= neq3
);
425 CPPUNIT_ASSERT( s1
!= neq4
);
427 CPPUNIT_ASSERT( s1
== wxT("AHH") );
428 CPPUNIT_ASSERT( s1
!= wxT("no") );
429 CPPUNIT_ASSERT( s1
< wxT("AZ") );
430 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
431 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
432 CPPUNIT_ASSERT( s1
> wxT("AA") );
433 CPPUNIT_ASSERT( s1
>= wxT("AA") );
434 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
436 // test comparison with C strings in Unicode build (must work in ANSI as
438 CPPUNIT_ASSERT( s1
== "AHH" );
439 CPPUNIT_ASSERT( s1
!= "no" );
440 CPPUNIT_ASSERT( s1
< "AZ" );
441 CPPUNIT_ASSERT( s1
<= "AZ" );
442 CPPUNIT_ASSERT( s1
<= "AHH" );
443 CPPUNIT_ASSERT( s1
> "AA" );
444 CPPUNIT_ASSERT( s1
>= "AA" );
445 CPPUNIT_ASSERT( s1
>= "AHH" );
447 // wxString _s1 = wxT("A\0HH");
448 // wxString _eq = wxT("A\0HH");
449 // wxString _neq1 = wxT("H\0AH");
450 // wxString _neq2 = wxT("A\0H");
451 // wxString _neq3 = wxT("A\0HHH");
452 // wxString _neq4 = wxT("A\0hH");
455 neq1
.insert(1,1,'\0');
456 neq2
.insert(1,1,'\0');
457 neq3
.insert(1,1,'\0');
458 neq4
.insert(1,1,'\0');
460 CPPUNIT_ASSERT( s1
== eq
);
461 CPPUNIT_ASSERT( s1
!= neq1
);
462 CPPUNIT_ASSERT( s1
!= neq2
);
463 CPPUNIT_ASSERT( s1
!= neq3
);
464 CPPUNIT_ASSERT( s1
!= neq4
);
466 CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
467 CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
468 CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
471 void StringTestCase::CompareNoCase()
473 wxString s1
= wxT("AHH");
474 wxString eq
= wxT("AHH");
475 wxString eq2
= wxT("AhH");
476 wxString eq3
= wxT("ahh");
477 wxString neq
= wxT("HAH");
478 wxString neq2
= wxT("AH");
479 wxString neq3
= wxT("AHHH");
481 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
482 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
484 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
485 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
486 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
488 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
489 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
490 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
493 // wxString _s1 = wxT("A\0HH");
494 // wxString _eq = wxT("A\0HH");
495 // wxString _eq2 = wxT("A\0hH");
496 // wxString _eq3 = wxT("a\0hh");
497 // wxString _neq = wxT("H\0AH");
498 // wxString _neq2 = wxT("A\0H");
499 // wxString _neq3 = wxT("A\0HHH");
503 eq2
.insert(1,1,'\0');
504 eq3
.insert(1,1,'\0');
505 neq
.insert(1,1,'\0');
506 neq2
.insert(1,1,'\0');
507 neq3
.insert(1,1,'\0');
509 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
510 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
511 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
513 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
514 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
515 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
517 CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
518 CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
519 CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
522 void StringTestCase::Contains()
524 static const struct ContainsData
527 const wxChar
*needle
;
531 { wxT(""), wxT(""), true },
532 { wxT(""), wxT("foo"), false },
533 { wxT("foo"), wxT(""), true },
534 { wxT("foo"), wxT("f"), true },
535 { wxT("foo"), wxT("o"), true },
536 { wxT("foo"), wxT("oo"), true },
537 { wxT("foo"), wxT("ooo"), false },
538 { wxT("foo"), wxT("oooo"), false },
539 { wxT("foo"), wxT("fooo"), false },
542 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
544 const ContainsData
& cd
= containsData
[n
];
545 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
549 // flags used in ToLongData.flags
554 Number_Unsigned
= 2, // if not specified, works for signed conversion
555 Number_Signed
= 4, // if not specified, works for unsigned
556 Number_LongLong
= 8, // only for long long tests
557 Number_Long
= 16 // only for long tests
560 static const struct ToLongData
567 #endif // wxLongLong_t
570 long LValue() const { return value
; }
571 unsigned long ULValue() const { return value
; }
573 wxLongLong_t
LLValue() const { return value
; }
574 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
575 #endif // wxLongLong_t
577 bool IsOk() const { return !(flags
& Number_Invalid
); }
580 { wxT("1"), 1, Number_Ok
},
581 { wxT("0"), 0, Number_Ok
},
582 { wxT("a"), 0, Number_Invalid
},
583 { wxT("12345"), 12345, Number_Ok
},
584 { wxT("--1"), 0, Number_Invalid
},
586 { wxT("-1"), -1, Number_Signed
| Number_Long
},
587 // this is surprising but consistent with strtoul() behaviour
588 { wxT("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
590 // this must overflow, even with 64 bit long
591 { wxT("922337203685477580711"), 0, Number_Invalid
},
594 { wxT("2147483648"), wxLL(2147483648), Number_LongLong
},
595 { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
596 { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
598 #endif // wxLongLong_t
601 void StringTestCase::ToLong()
604 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
606 const ToLongData
& ld
= longData
[n
];
608 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
611 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
612 // should behave the same for our test data set:
614 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCLong(&l
) );
616 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
618 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
620 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
623 // special case: check that the output is not modified if the parsing
626 CPPUNIT_ASSERT( !wxString("foo").ToLong(&l
) );
627 CPPUNIT_ASSERT_EQUAL( 17, l
);
629 // also check that it is modified if we did parse something successfully in
630 // the beginning of the string
631 CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l
) );
632 CPPUNIT_ASSERT_EQUAL( 9, l
);
635 void StringTestCase::ToULong()
638 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
640 const ToLongData
& ld
= longData
[n
];
642 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
645 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
646 // should behave the same for our test data set:
648 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCULong(&ul
) );
650 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
652 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
654 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
660 void StringTestCase::ToLongLong()
663 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
665 const ToLongData
& ld
= longData
[n
];
667 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
670 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
672 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
676 void StringTestCase::ToULongLong()
679 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
681 const ToLongData
& ld
= longData
[n
];
683 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
686 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
688 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
692 #endif // wxLongLong_t
694 void StringTestCase::ToDouble()
697 static const struct ToDoubleData
704 { wxT("1"), 1, true },
705 { wxT("1.23"), 1.23, true },
706 { wxT(".1"), .1, true },
707 { wxT("1."), 1, true },
708 { wxT("1.."), 0, false },
709 { wxT("0"), 0, true },
710 { wxT("a"), 0, false },
711 { wxT("12345"), 12345, true },
712 { wxT("-1"), -1, true },
713 { wxT("--1"), 0, false },
714 { wxT("-3E-5"), -3E-5, true },
715 { wxT("-3E-abcde5"), 0, false },
718 // test ToCDouble() first:
721 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
723 const ToDoubleData
& ld
= doubleData
[n
];
724 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToCDouble(&d
) );
726 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
730 // test ToDouble() now:
731 // NOTE: for the test to be reliable, we need to set the locale explicitly
732 // so that we know the decimal point character to use
734 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
735 return; // you should have french support installed to continue this test!
739 // don't load default catalog, it may be unavailable:
740 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
742 static const struct ToDoubleData doubleData2
[] =
744 { wxT("1"), 1, true },
745 { wxT("1,23"), 1.23, true },
746 { wxT(",1"), .1, true },
747 { wxT("1,"), 1, true },
748 { wxT("1,,"), 0, false },
749 { wxT("0"), 0, true },
750 { wxT("a"), 0, false },
751 { wxT("12345"), 12345, true },
752 { wxT("-1"), -1, true },
753 { wxT("--1"), 0, false },
754 { wxT("-3E-5"), -3E-5, true },
755 { wxT("-3E-abcde5"), 0, false },
758 for ( n
= 0; n
< WXSIZEOF(doubleData2
); n
++ )
760 const ToDoubleData
& ld
= doubleData2
[n
];
761 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
763 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
767 void StringTestCase::FromDouble()
769 static const struct FromDoubleTestData
776 { 1.23, -1, "1.23" },
777 // NB: there are no standards about the minimum exponent width
778 // and newer MSVC versions use 3 digits as minimum exponent
779 // width while GNU libc uses 2 digits as minimum width...
780 #ifdef wxUSING_VC_CRT_IO
781 { -3e-10, -1, "-3e-010" },
783 { -3e-10, -1, "-3e-10" },
785 { -0.45678, -1, "-0.45678" },
786 { 1.2345678, 0, "1" },
787 { 1.2345678, 1, "1.2" },
788 { 1.2345678, 2, "1.23" },
789 { 1.2345678, 3, "1.235" },
792 for ( unsigned n
= 0; n
< WXSIZEOF(testData
); n
++ )
794 const FromDoubleTestData
& td
= testData
[n
];
795 CPPUNIT_ASSERT_EQUAL( td
.str
, wxString::FromCDouble(td
.value
, td
.prec
) );
798 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
802 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
804 for ( unsigned m
= 0; m
< WXSIZEOF(testData
); m
++ )
806 const FromDoubleTestData
& td
= testData
[m
];
808 wxString
str(td
.str
);
809 str
.Replace(".", ",");
810 CPPUNIT_ASSERT_EQUAL( str
, wxString::FromDouble(td
.value
, td
.prec
) );
814 void StringTestCase::StringBuf()
816 // check that buffer can be used to write into the string
818 wxStrcpy(wxStringBuffer(s
, 10), wxT("foo"));
820 CPPUNIT_ASSERT_EQUAL(3, s
.length());
821 CPPUNIT_ASSERT(wxT('f') == s
[0u]);
822 CPPUNIT_ASSERT(wxT('o') == s
[1]);
823 CPPUNIT_ASSERT(wxT('o') == s
[2]);
826 // also check that the buffer initially contains the original string
828 wxStringBuffer
buf(s
, 10);
829 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
830 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
831 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
832 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
836 wxStringBufferLength
buf(s
, 10);
837 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
838 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
839 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
840 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
842 // and check that it can be used to write only the specified number of
843 // characters to the string
844 wxStrcpy(buf
, wxT("barrbaz"));
848 CPPUNIT_ASSERT_EQUAL(4, s
.length());
849 CPPUNIT_ASSERT(wxT('b') == s
[0u]);
850 CPPUNIT_ASSERT(wxT('a') == s
[1]);
851 CPPUNIT_ASSERT(wxT('r') == s
[2]);
852 CPPUNIT_ASSERT(wxT('r') == s
[3]);
854 // check that creating buffer of length smaller than string works, i.e. at
855 // least doesn't crash (it would if we naively copied the entire original
856 // string contents in the buffer)
857 *wxStringBuffer(s
, 1) = '!';
860 void StringTestCase::UTF8Buf()
863 // "czech" in Czech ("cestina"):
864 static const char *textUTF8
= "\304\215e\305\241tina";
865 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
868 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
869 CPPUNIT_ASSERT(s
== textUTF16
);
872 wxUTF8StringBufferLength
buf(s
, 20);
873 wxStrcpy(buf
, textUTF8
);
876 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
877 #endif // wxUSE_UNICODE
882 void StringTestCase::CStrDataTernaryOperator()
884 DoCStrDataTernaryOperator(true);
885 DoCStrDataTernaryOperator(false);
888 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
890 return expected
== wxString(s
);
893 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
895 // test compilation of wxCStrData when used with operator?: (the asserts
896 // are not very important, we're testing if the code compiles at all):
900 const wchar_t *wcStr
= L
"foo";
901 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
902 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
903 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
904 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
906 const char *mbStr
= "foo";
907 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
908 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
909 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
910 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
913 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
914 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
917 void StringTestCase::CStrDataOperators()
921 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
922 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
924 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
925 // the final character results in an assert failure (with debug CRT)
926 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
928 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
929 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
930 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
933 bool CheckStrChar(const wxString
& expected
, char *s
)
934 { return CheckStr(expected
, s
); }
935 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
936 { return CheckStr(expected
, s
); }
937 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
938 { return CheckStr(expected
, s
); }
939 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
940 { return CheckStr(expected
, s
); }
942 void StringTestCase::CStrDataImplicitConversion()
946 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
947 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
949 // implicit conversion of wxString is not available in STL build
951 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
952 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
956 void StringTestCase::ExplicitConversion()
960 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
961 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
962 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
964 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
965 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
966 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );
969 void StringTestCase::IndexedAccess()
972 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
974 // this tests for a possible bug in UTF-8 based wxString implementation:
975 // the 3rd character of the underlying byte string is going to change, but
976 // the 3rd character of wxString should remain the same
978 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
981 void StringTestCase::BeforeAndAfter()
983 // Construct a string with 2 equal signs in it by concatenating its three
984 // parts: before the first "=", in between the two "="s and after the last
985 // one. This allows to avoid duplicating the string contents (which has to
986 // be different for Unicode and ANSI builds) in the tests below.
988 #define FIRST_PART L"letter"
989 #define MIDDLE_PART L"\xe9;\xe7a"
990 #define LAST_PART L"l\xe0"
991 #else // !wxUSE_UNICODE
992 #define FIRST_PART "letter"
993 #define MIDDLE_PART "e;ca"
994 #define LAST_PART "la"
995 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
997 const wxString
s(FIRST_PART
wxT("=") MIDDLE_PART
wxT("=") LAST_PART
);
1001 CPPUNIT_ASSERT_EQUAL( FIRST_PART
, s
.BeforeFirst('=', &r
) );
1002 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, r
);
1004 CPPUNIT_ASSERT_EQUAL( s
, s
.BeforeFirst('!', &r
) );
1005 CPPUNIT_ASSERT_EQUAL( "", r
);
1008 CPPUNIT_ASSERT_EQUAL( FIRST_PART
wxT("=") MIDDLE_PART
, s
.BeforeLast('=', &r
) );
1009 CPPUNIT_ASSERT_EQUAL( LAST_PART
, r
);
1011 CPPUNIT_ASSERT_EQUAL( "", s
.BeforeLast('!', &r
) );
1012 CPPUNIT_ASSERT_EQUAL( s
, r
);
1015 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, s
.AfterFirst('=') );
1016 CPPUNIT_ASSERT_EQUAL( "", s
.AfterFirst('!') );
1019 CPPUNIT_ASSERT_EQUAL( LAST_PART
, s
.AfterLast('=') );
1020 CPPUNIT_ASSERT_EQUAL( s
, s
.AfterLast('!') );
1027 void StringTestCase::ScopedBuffers()
1029 // wxString relies on efficient buffers, verify they work as they should
1031 const char *literal
= "Hello World!";
1033 // non-owned buffer points to the string passed to it
1034 wxScopedCharBuffer sbuf
= wxScopedCharBuffer::CreateNonOwned(literal
);
1035 CPPUNIT_ASSERT( sbuf
.data() == literal
);
1037 // a copy of scoped non-owned buffer still points to the same string
1038 wxScopedCharBuffer
sbuf2(sbuf
);
1039 CPPUNIT_ASSERT( sbuf
.data() == sbuf2
.data() );
1041 // but assigning it to wxCharBuffer makes a full copy
1042 wxCharBuffer
buf(sbuf
);
1043 CPPUNIT_ASSERT( buf
.data() != literal
);
1044 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1046 wxCharBuffer buf2
= sbuf
;
1047 CPPUNIT_ASSERT( buf2
.data() != literal
);
1048 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1050 // Check that extending the buffer keeps it NUL-terminated.
1053 wxCharBuffer
buf3(len
);
1054 CPPUNIT_ASSERT_EQUAL('\0', buf3
.data()[len
]);
1058 CPPUNIT_ASSERT_EQUAL('\0', buf4
.data()[len
]);
1060 wxCharBuffer
buf5(5);
1062 CPPUNIT_ASSERT_EQUAL('\0', buf5
.data()[len
]);