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 #if wxUSE_UNICODE_UTF8
196 // This string has 3 characters (<h>, <e'> and <l>), not 4 when using UTF-8
198 if ( wxConvLibc
.IsUTF8() )
200 wxString
s3("h\xc3\xa9llo", 4);
201 CPPUNIT_ASSERT_EQUAL( 3, s3
.length() );
202 CPPUNIT_ASSERT_EQUAL( 'l', (char)s3
[2] );
204 #endif // wxUSE_UNICODE_UTF8
207 static const char *s
= "?really!";
208 const char *start
= wxStrchr(s
, 'r');
209 const char *end
= wxStrchr(s
, '!');
210 CPPUNIT_ASSERT_EQUAL( "really", wxString(start
, end
) );
212 // test if creating string from NULL C pointer works:
213 CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL
) );
216 void StringTestCase::StaticConstructors()
218 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
219 CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
220 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
221 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
222 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
224 // FIXME: this doesn't work currently but should!
225 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
228 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
229 CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
230 CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
231 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
232 CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
234 CPPUNIT_ASSERT_EQUAL( 2, wxString::FromUTF8("h\xc3\xa9llo", 3).length() );
237 //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
240 void StringTestCase::Extraction()
242 wxString
s(wxT("Hello, world!"));
244 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , wxT("Hello, world!") ) == 0 );
245 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , wxT("Hello") ) == 0 );
246 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , wxT("world!") ) == 0 );
247 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 );
248 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , wxT("lo, world!") ) == 0 );
249 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , wxT("lo, w") ) == 0 );
250 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , wxT("lo, world!") ) == 0 );
253 static const char *germanUTF8
= "Oberfl\303\244che";
254 wxString
strUnicode(wxString::FromUTF8(germanUTF8
));
256 CPPUNIT_ASSERT( strUnicode
.Mid(0, 10) == strUnicode
);
257 CPPUNIT_ASSERT( strUnicode
.Mid(7, 2) == "ch" );
258 #endif // wxUSE_UNICODE
262 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
263 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
265 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
267 TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true );
268 TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true );
269 TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true );
270 TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false );
271 TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true );
272 TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false );
273 TEST_STARTS_WITH( wxT("Hi"), wxT(""), false );
275 #undef TEST_STARTS_WITH
277 rest
= "Hello world";
278 CPPUNIT_ASSERT( rest
.StartsWith("Hello ", &rest
) );
279 CPPUNIT_ASSERT_EQUAL("world", rest
);
281 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
282 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
284 CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
286 TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true );
287 TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true );
288 TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true );
289 TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true );
290 TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true );
291 TEST_ENDS_WITH( wxT("very long string"), wxT(""), false );
292 TEST_ENDS_WITH( wxT("?"), wxT(""), false );
293 TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false );
294 TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false );
296 #undef TEST_ENDS_WITH
299 void StringTestCase::Trim()
301 #define TEST_TRIM( str , dir , result ) \
302 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
304 TEST_TRIM( wxT(" Test "), true, wxT(" Test") );
305 TEST_TRIM( wxT(" "), true, wxT("") );
306 TEST_TRIM( wxT(" "), true, wxT("") );
307 TEST_TRIM( wxT(""), true, wxT("") );
309 TEST_TRIM( wxT(" Test "), false, wxT("Test ") );
310 TEST_TRIM( wxT(" "), false, wxT("") );
311 TEST_TRIM( wxT(" "), false, wxT("") );
312 TEST_TRIM( wxT(""), false, wxT("") );
317 void StringTestCase::Find()
319 #define TEST_FIND( str , start , result ) \
320 CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
322 TEST_FIND( wxT("Well, hello world"), 0, 1 );
323 TEST_FIND( wxT("Well, hello world"), 6, 7 );
324 TEST_FIND( wxT("Well, hello world"), 9, wxString::npos
);
329 void StringTestCase::Replace()
331 #define TEST_REPLACE( original , pos , len , replacement , result ) \
333 wxString s = original; \
334 s.replace( pos , len , replacement ); \
335 CPPUNIT_ASSERT_EQUAL( result, s ); \
338 TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") );
339 TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") );
340 TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") );
341 TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") );
342 TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") );
345 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
347 wxString s(o,olen); \
348 s.replace( pos , len , replacement ); \
349 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
352 TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
353 wxT("null\0dhar"), 9 );
355 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
357 wxString s(o,olen); \
358 s.Replace( olds, news, all ); \
359 CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
362 TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
363 wxT("null\0dehar"), 10 );
365 TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
366 wxT("null\0char"), 9 );
368 TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
369 TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
370 TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
371 TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
372 TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
374 #undef TEST_WXREPLACE
375 #undef TEST_NULLCHARREPLACE
379 void StringTestCase::Match()
381 #define TEST_MATCH( s1 , s2 , result ) \
382 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
384 TEST_MATCH( wxT("foobar"), wxT("foo*"), true );
385 TEST_MATCH( wxT("foobar"), wxT("*oo*"), true );
386 TEST_MATCH( wxT("foobar"), wxT("*bar"), true );
387 TEST_MATCH( wxT("foobar"), wxT("??????"), true );
388 TEST_MATCH( wxT("foobar"), wxT("f??b*"), true );
389 TEST_MATCH( wxT("foobar"), wxT("f?b*"), false );
390 TEST_MATCH( wxT("foobar"), wxT("*goo*"), false );
391 TEST_MATCH( wxT("foobar"), wxT("*foo"), false );
392 TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true );
393 TEST_MATCH( wxT(""), wxT("*"), true );
394 TEST_MATCH( wxT(""), wxT("?"), false );
400 void StringTestCase::CaseChanges()
402 wxString
s1(wxT("Hello!"));
408 CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u
);
409 CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l
);
415 CPPUNIT_ASSERT_EQUAL( "", s2u
);
416 CPPUNIT_ASSERT_EQUAL( "", s2l
);
419 wxString
s3("good bye");
420 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
.Capitalize() );
421 s3
.MakeCapitalized();
422 CPPUNIT_ASSERT_EQUAL( "Good bye", s3
);
424 CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
426 CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
429 void StringTestCase::Compare()
431 wxString s1
= wxT("AHH");
432 wxString eq
= wxT("AHH");
433 wxString neq1
= wxT("HAH");
434 wxString neq2
= wxT("AH");
435 wxString neq3
= wxT("AHHH");
436 wxString neq4
= wxT("AhH");
438 CPPUNIT_ASSERT( s1
== eq
);
439 CPPUNIT_ASSERT( s1
!= neq1
);
440 CPPUNIT_ASSERT( s1
!= neq2
);
441 CPPUNIT_ASSERT( s1
!= neq3
);
442 CPPUNIT_ASSERT( s1
!= neq4
);
444 CPPUNIT_ASSERT( s1
== wxT("AHH") );
445 CPPUNIT_ASSERT( s1
!= wxT("no") );
446 CPPUNIT_ASSERT( s1
< wxT("AZ") );
447 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
448 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
449 CPPUNIT_ASSERT( s1
> wxT("AA") );
450 CPPUNIT_ASSERT( s1
>= wxT("AA") );
451 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
453 // test comparison with C strings in Unicode build (must work in ANSI as
455 CPPUNIT_ASSERT( s1
== "AHH" );
456 CPPUNIT_ASSERT( s1
!= "no" );
457 CPPUNIT_ASSERT( s1
< "AZ" );
458 CPPUNIT_ASSERT( s1
<= "AZ" );
459 CPPUNIT_ASSERT( s1
<= "AHH" );
460 CPPUNIT_ASSERT( s1
> "AA" );
461 CPPUNIT_ASSERT( s1
>= "AA" );
462 CPPUNIT_ASSERT( s1
>= "AHH" );
464 // wxString _s1 = wxT("A\0HH");
465 // wxString _eq = wxT("A\0HH");
466 // wxString _neq1 = wxT("H\0AH");
467 // wxString _neq2 = wxT("A\0H");
468 // wxString _neq3 = wxT("A\0HHH");
469 // wxString _neq4 = wxT("A\0hH");
472 neq1
.insert(1,1,'\0');
473 neq2
.insert(1,1,'\0');
474 neq3
.insert(1,1,'\0');
475 neq4
.insert(1,1,'\0');
477 CPPUNIT_ASSERT( s1
== eq
);
478 CPPUNIT_ASSERT( s1
!= neq1
);
479 CPPUNIT_ASSERT( s1
!= neq2
);
480 CPPUNIT_ASSERT( s1
!= neq3
);
481 CPPUNIT_ASSERT( s1
!= neq4
);
483 CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
484 CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
485 CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
488 void StringTestCase::CompareNoCase()
490 wxString s1
= wxT("AHH");
491 wxString eq
= wxT("AHH");
492 wxString eq2
= wxT("AhH");
493 wxString eq3
= wxT("ahh");
494 wxString neq
= wxT("HAH");
495 wxString neq2
= wxT("AH");
496 wxString neq3
= wxT("AHHH");
498 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
499 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
501 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
502 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
503 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
505 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
506 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
507 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
510 // wxString _s1 = wxT("A\0HH");
511 // wxString _eq = wxT("A\0HH");
512 // wxString _eq2 = wxT("A\0hH");
513 // wxString _eq3 = wxT("a\0hh");
514 // wxString _neq = wxT("H\0AH");
515 // wxString _neq2 = wxT("A\0H");
516 // wxString _neq3 = wxT("A\0HHH");
520 eq2
.insert(1,1,'\0');
521 eq3
.insert(1,1,'\0');
522 neq
.insert(1,1,'\0');
523 neq2
.insert(1,1,'\0');
524 neq3
.insert(1,1,'\0');
526 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
527 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
528 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
530 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
531 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
532 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
534 CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
535 CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
536 CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
539 void StringTestCase::Contains()
541 static const struct ContainsData
544 const wxChar
*needle
;
548 { wxT(""), wxT(""), true },
549 { wxT(""), wxT("foo"), false },
550 { wxT("foo"), wxT(""), true },
551 { wxT("foo"), wxT("f"), true },
552 { wxT("foo"), wxT("o"), true },
553 { wxT("foo"), wxT("oo"), true },
554 { wxT("foo"), wxT("ooo"), false },
555 { wxT("foo"), wxT("oooo"), false },
556 { wxT("foo"), wxT("fooo"), false },
559 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
561 const ContainsData
& cd
= containsData
[n
];
562 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
566 // flags used in ToLongData.flags
571 Number_Unsigned
= 2, // if not specified, works for signed conversion
572 Number_Signed
= 4, // if not specified, works for unsigned
573 Number_LongLong
= 8, // only for long long tests
574 Number_Long
= 16 // only for long tests
577 static const struct ToLongData
584 #endif // wxLongLong_t
587 long LValue() const { return value
; }
588 unsigned long ULValue() const { return value
; }
590 wxLongLong_t
LLValue() const { return value
; }
591 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
592 #endif // wxLongLong_t
594 bool IsOk() const { return !(flags
& Number_Invalid
); }
597 { wxT("1"), 1, Number_Ok
},
598 { wxT("0"), 0, Number_Ok
},
599 { wxT("a"), 0, Number_Invalid
},
600 { wxT("12345"), 12345, Number_Ok
},
601 { wxT("--1"), 0, Number_Invalid
},
603 { wxT("-1"), -1, Number_Signed
| Number_Long
},
604 // this is surprising but consistent with strtoul() behaviour
605 { wxT("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
607 // this must overflow, even with 64 bit long
608 { wxT("922337203685477580711"), 0, Number_Invalid
},
611 { wxT("2147483648"), wxLL(2147483648), Number_LongLong
},
612 { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
613 { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
615 #endif // wxLongLong_t
618 void StringTestCase::ToLong()
621 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
623 const ToLongData
& ld
= longData
[n
];
625 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
628 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
629 // should behave the same for our test data set:
631 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCLong(&l
) );
633 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
635 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
637 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
640 // special case: check that the output is not modified if the parsing
643 CPPUNIT_ASSERT( !wxString("foo").ToLong(&l
) );
644 CPPUNIT_ASSERT_EQUAL( 17, l
);
646 // also check that it is modified if we did parse something successfully in
647 // the beginning of the string
648 CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l
) );
649 CPPUNIT_ASSERT_EQUAL( 9, l
);
652 void StringTestCase::ToULong()
655 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
657 const ToLongData
& ld
= longData
[n
];
659 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
662 // NOTE: unless you're using some exotic locale, ToCLong and ToLong
663 // should behave the same for our test data set:
665 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToCULong(&ul
) );
667 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
669 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
671 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
677 void StringTestCase::ToLongLong()
680 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
682 const ToLongData
& ld
= longData
[n
];
684 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
687 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
689 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
693 void StringTestCase::ToULongLong()
696 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
698 const ToLongData
& ld
= longData
[n
];
700 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
703 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
705 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
709 #endif // wxLongLong_t
711 void StringTestCase::ToDouble()
714 static const struct ToDoubleData
721 { wxT("1"), 1, true },
722 { wxT("1.23"), 1.23, true },
723 { wxT(".1"), .1, true },
724 { wxT("1."), 1, true },
725 { wxT("1.."), 0, false },
726 { wxT("0"), 0, true },
727 { wxT("a"), 0, false },
728 { wxT("12345"), 12345, true },
729 { wxT("-1"), -1, true },
730 { wxT("--1"), 0, false },
731 { wxT("-3E-5"), -3E-5, true },
732 { wxT("-3E-abcde5"), 0, false },
735 // test ToCDouble() first:
738 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
740 const ToDoubleData
& ld
= doubleData
[n
];
741 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToCDouble(&d
) );
743 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
747 // test ToDouble() now:
748 // NOTE: for the test to be reliable, we need to set the locale explicitly
749 // so that we know the decimal point character to use
751 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
752 return; // you should have french support installed to continue this test!
756 // don't load default catalog, it may be unavailable:
757 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
759 static const struct ToDoubleData doubleData2
[] =
761 { wxT("1"), 1, true },
762 { wxT("1,23"), 1.23, true },
763 { wxT(",1"), .1, true },
764 { wxT("1,"), 1, true },
765 { wxT("1,,"), 0, false },
766 { wxT("0"), 0, true },
767 { wxT("a"), 0, false },
768 { wxT("12345"), 12345, true },
769 { wxT("-1"), -1, true },
770 { wxT("--1"), 0, false },
771 { wxT("-3E-5"), -3E-5, true },
772 { wxT("-3E-abcde5"), 0, false },
775 for ( n
= 0; n
< WXSIZEOF(doubleData2
); n
++ )
777 const ToDoubleData
& ld
= doubleData2
[n
];
778 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
780 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
784 void StringTestCase::FromDouble()
786 static const struct FromDoubleTestData
793 { 1.23, -1, "1.23" },
794 // NB: there are no standards about the minimum exponent width
795 // and newer MSVC versions use 3 digits as minimum exponent
796 // width while GNU libc uses 2 digits as minimum width...
797 #ifdef wxUSING_VC_CRT_IO
798 { -3e-10, -1, "-3e-010" },
800 { -3e-10, -1, "-3e-10" },
802 { -0.45678, -1, "-0.45678" },
803 { 1.2345678, 0, "1" },
804 { 1.2345678, 1, "1.2" },
805 { 1.2345678, 2, "1.23" },
806 { 1.2345678, 3, "1.235" },
809 for ( unsigned n
= 0; n
< WXSIZEOF(testData
); n
++ )
811 const FromDoubleTestData
& td
= testData
[n
];
812 CPPUNIT_ASSERT_EQUAL( td
.str
, wxString::FromCDouble(td
.value
, td
.prec
) );
815 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
819 CPPUNIT_ASSERT( locale
.Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
) );
821 for ( unsigned m
= 0; m
< WXSIZEOF(testData
); m
++ )
823 const FromDoubleTestData
& td
= testData
[m
];
825 wxString
str(td
.str
);
826 str
.Replace(".", ",");
827 CPPUNIT_ASSERT_EQUAL( str
, wxString::FromDouble(td
.value
, td
.prec
) );
831 void StringTestCase::StringBuf()
833 // check that buffer can be used to write into the string
835 wxStrcpy(wxStringBuffer(s
, 10), wxT("foo"));
837 CPPUNIT_ASSERT_EQUAL(3, s
.length());
838 CPPUNIT_ASSERT(wxT('f') == s
[0u]);
839 CPPUNIT_ASSERT(wxT('o') == s
[1]);
840 CPPUNIT_ASSERT(wxT('o') == s
[2]);
843 // also check that the buffer initially contains the original string
845 wxStringBuffer
buf(s
, 10);
846 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
847 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
848 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
849 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
853 wxStringBufferLength
buf(s
, 10);
854 CPPUNIT_ASSERT_EQUAL( wxT('f'), buf
[0] );
855 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[1] );
856 CPPUNIT_ASSERT_EQUAL( wxT('o'), buf
[2] );
857 CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf
[3] );
859 // and check that it can be used to write only the specified number of
860 // characters to the string
861 wxStrcpy(buf
, wxT("barrbaz"));
865 CPPUNIT_ASSERT_EQUAL(4, s
.length());
866 CPPUNIT_ASSERT(wxT('b') == s
[0u]);
867 CPPUNIT_ASSERT(wxT('a') == s
[1]);
868 CPPUNIT_ASSERT(wxT('r') == s
[2]);
869 CPPUNIT_ASSERT(wxT('r') == s
[3]);
871 // check that creating buffer of length smaller than string works, i.e. at
872 // least doesn't crash (it would if we naively copied the entire original
873 // string contents in the buffer)
874 *wxStringBuffer(s
, 1) = '!';
877 void StringTestCase::UTF8Buf()
880 // "czech" in Czech ("cestina"):
881 static const char *textUTF8
= "\304\215e\305\241tina";
882 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
885 wxStrcpy(wxUTF8StringBuffer(s
, 9), textUTF8
);
886 CPPUNIT_ASSERT(s
== textUTF16
);
889 wxUTF8StringBufferLength
buf(s
, 20);
890 wxStrcpy(buf
, textUTF8
);
893 CPPUNIT_ASSERT(s
== wxString(textUTF16
, 0, 3));
894 #endif // wxUSE_UNICODE
899 void StringTestCase::CStrDataTernaryOperator()
901 DoCStrDataTernaryOperator(true);
902 DoCStrDataTernaryOperator(false);
905 template<typename T
> bool CheckStr(const wxString
& expected
, T s
)
907 return expected
== wxString(s
);
910 void StringTestCase::DoCStrDataTernaryOperator(bool cond
)
912 // test compilation of wxCStrData when used with operator?: (the asserts
913 // are not very important, we're testing if the code compiles at all):
917 const wchar_t *wcStr
= L
"foo";
918 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : wcStr
)) );
919 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : L
"foo")) );
920 CPPUNIT_ASSERT( CheckStr(s
, (cond
? wcStr
: s
.c_str())) );
921 CPPUNIT_ASSERT( CheckStr(s
, (cond
? L
"foo" : s
.c_str())) );
923 const char *mbStr
= "foo";
924 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : mbStr
)) );
925 CPPUNIT_ASSERT( CheckStr(s
, (cond
? s
.c_str() : "foo")) );
926 CPPUNIT_ASSERT( CheckStr(s
, (cond
? mbStr
: s
.c_str())) );
927 CPPUNIT_ASSERT( CheckStr(s
, (cond
? "foo" : s
.c_str())) );
930 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? empty
.c_str() : wxEmptyString
)) );
931 CPPUNIT_ASSERT( CheckStr(empty
, (cond
? wxEmptyString
: empty
.c_str())) );
934 void StringTestCase::CStrDataOperators()
938 CPPUNIT_ASSERT( s
.c_str()[0] == 'h' );
939 CPPUNIT_ASSERT( s
.c_str()[1] == 'e' );
941 // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
942 // the final character results in an assert failure (with debug CRT)
943 //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
945 CPPUNIT_ASSERT( *s
.c_str() == 'h' );
946 CPPUNIT_ASSERT( *(s
.c_str() + 2) == 'l' );
947 //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
950 bool CheckStrChar(const wxString
& expected
, char *s
)
951 { return CheckStr(expected
, s
); }
952 bool CheckStrWChar(const wxString
& expected
, wchar_t *s
)
953 { return CheckStr(expected
, s
); }
954 bool CheckStrConstChar(const wxString
& expected
, const char *s
)
955 { return CheckStr(expected
, s
); }
956 bool CheckStrConstWChar(const wxString
& expected
, const wchar_t *s
)
957 { return CheckStr(expected
, s
); }
959 void StringTestCase::CStrDataImplicitConversion()
963 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.c_str()) );
964 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.c_str()) );
966 // implicit conversion of wxString is not available in STL build
968 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
) );
969 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
) );
973 void StringTestCase::ExplicitConversion()
977 CPPUNIT_ASSERT( CheckStr(s
, s
.mb_str()) );
978 CPPUNIT_ASSERT( CheckStrConstChar(s
, s
.mb_str()) );
979 CPPUNIT_ASSERT( CheckStrChar(s
, s
.char_str()) );
981 CPPUNIT_ASSERT( CheckStr(s
, s
.wc_str()) );
982 CPPUNIT_ASSERT( CheckStrConstWChar(s
, s
.wc_str()) );
983 CPPUNIT_ASSERT( CheckStrWChar(s
, s
.wchar_str()) );
986 void StringTestCase::IndexedAccess()
989 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
991 // this tests for a possible bug in UTF-8 based wxString implementation:
992 // the 3rd character of the underlying byte string is going to change, but
993 // the 3rd character of wxString should remain the same
995 CPPUNIT_ASSERT_EQUAL( 'r', (char)s
[2] );
998 void StringTestCase::BeforeAndAfter()
1000 // Construct a string with 2 equal signs in it by concatenating its three
1001 // parts: before the first "=", in between the two "="s and after the last
1002 // one. This allows to avoid duplicating the string contents (which has to
1003 // be different for Unicode and ANSI builds) in the tests below.
1005 #define FIRST_PART L"letter"
1006 #define MIDDLE_PART L"\xe9;\xe7a"
1007 #define LAST_PART L"l\xe0"
1008 #else // !wxUSE_UNICODE
1009 #define FIRST_PART "letter"
1010 #define MIDDLE_PART "e;ca"
1011 #define LAST_PART "la"
1012 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1014 const wxString
s(FIRST_PART
wxT("=") MIDDLE_PART
wxT("=") LAST_PART
);
1018 CPPUNIT_ASSERT_EQUAL( FIRST_PART
, s
.BeforeFirst('=', &r
) );
1019 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, r
);
1021 CPPUNIT_ASSERT_EQUAL( s
, s
.BeforeFirst('!', &r
) );
1022 CPPUNIT_ASSERT_EQUAL( "", r
);
1025 CPPUNIT_ASSERT_EQUAL( FIRST_PART
wxT("=") MIDDLE_PART
, s
.BeforeLast('=', &r
) );
1026 CPPUNIT_ASSERT_EQUAL( LAST_PART
, r
);
1028 CPPUNIT_ASSERT_EQUAL( "", s
.BeforeLast('!', &r
) );
1029 CPPUNIT_ASSERT_EQUAL( s
, r
);
1032 CPPUNIT_ASSERT_EQUAL( MIDDLE_PART
wxT("=") LAST_PART
, s
.AfterFirst('=') );
1033 CPPUNIT_ASSERT_EQUAL( "", s
.AfterFirst('!') );
1036 CPPUNIT_ASSERT_EQUAL( LAST_PART
, s
.AfterLast('=') );
1037 CPPUNIT_ASSERT_EQUAL( s
, s
.AfterLast('!') );
1044 void StringTestCase::ScopedBuffers()
1046 // wxString relies on efficient buffers, verify they work as they should
1048 const char *literal
= "Hello World!";
1050 // non-owned buffer points to the string passed to it
1051 wxScopedCharBuffer sbuf
= wxScopedCharBuffer::CreateNonOwned(literal
);
1052 CPPUNIT_ASSERT( sbuf
.data() == literal
);
1054 // a copy of scoped non-owned buffer still points to the same string
1055 wxScopedCharBuffer
sbuf2(sbuf
);
1056 CPPUNIT_ASSERT( sbuf
.data() == sbuf2
.data() );
1058 // but assigning it to wxCharBuffer makes a full copy
1059 wxCharBuffer
buf(sbuf
);
1060 CPPUNIT_ASSERT( buf
.data() != literal
);
1061 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1063 wxCharBuffer buf2
= sbuf
;
1064 CPPUNIT_ASSERT( buf2
.data() != literal
);
1065 CPPUNIT_ASSERT_EQUAL( literal
, buf
.data() );
1067 // Check that extending the buffer keeps it NUL-terminated.
1070 wxCharBuffer
buf3(len
);
1071 CPPUNIT_ASSERT_EQUAL('\0', buf3
.data()[len
]);
1075 CPPUNIT_ASSERT_EQUAL('\0', buf4
.data()[len
]);
1077 wxCharBuffer
buf5(5);
1079 CPPUNIT_ASSERT_EQUAL('\0', buf5
.data()[len
]);