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
);
40 CPPUNIT_TEST( ConstructorsWithConversion
);
41 CPPUNIT_TEST( Conversion
);
42 CPPUNIT_TEST( ConversionUTF7
);
43 CPPUNIT_TEST( ConversionUTF8
);
44 #endif // wxUSE_WCHAR_T
45 CPPUNIT_TEST( Extraction
);
47 CPPUNIT_TEST( Replace
);
48 CPPUNIT_TEST( Match
);
49 CPPUNIT_TEST( CaseChanges
);
50 CPPUNIT_TEST( Compare
);
51 CPPUNIT_TEST( CompareNoCase
);
52 CPPUNIT_TEST( ToLong
);
53 CPPUNIT_TEST( ToULong
);
54 CPPUNIT_TEST( ToDouble
);
55 CPPUNIT_TEST_SUITE_END();
62 void ConstructorsWithConversion();
64 void ConversionUTF7();
65 void ConversionUTF8();
66 #endif // wxUSE_WCHAR_T
79 // test if converting s using the given encoding gives ws and vice versa
81 // if either of the first 2 arguments is NULL, the conversion is supposed
83 void DoTestConversion(const char *s
, const wchar_t *w
, wxCSConv
& conv
);
84 #endif // wxUSE_WCHAR_T
86 DECLARE_NO_COPY_CLASS(StringTestCase
)
89 // register in the unnamed registry so that these tests are run by default
90 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
92 // also include in it's own registry so that these tests can be run alone
93 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
95 StringTestCase::StringTestCase()
99 void StringTestCase::String()
107 for (int i
= 0; i
< 2; ++i
)
111 c
= _T("! How'ya doin'?");
114 c
= _T("Hello world! What's up?");
115 CPPUNIT_ASSERT( c
!= a
);
119 void StringTestCase::PChar()
125 for (int i
= 0; i
< 2; ++i
)
127 wxStrcpy (a
, _T("Hello"));
128 wxStrcpy (b
, _T(" world"));
129 wxStrcpy (c
, _T("! How'ya doin'?"));
132 wxStrcpy (c
, _T("Hello world! What's up?"));
133 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
137 void StringTestCase::Format()
140 s1
.Printf(_T("%03d"), 18);
141 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
142 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
143 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
145 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
146 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
148 const size_t len
= lengths
[n
];
150 wxString
s(_T('Z'), len
);
151 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
155 void StringTestCase::Constructors()
157 #define TEST_CTOR(args, res) \
160 CPPUNIT_ASSERT( s == res ); \
163 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
164 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
165 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
167 static const wxChar
*s
= _T("?really!");
168 const wxChar
*start
= wxStrchr(s
, _T('r'));
169 const wxChar
*end
= wxStrchr(s
, _T('!'));
170 TEST_CTOR((start
, end
), _T("really"));
174 void StringTestCase::ConstructorsWithConversion()
176 // the string "Déjà" in UTF-8 and wchar_t:
177 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
178 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
179 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
180 const char *utf8
= (char *)utf8Buf
;
181 const char *utf8sub
= (char *)utf8subBuf
;
183 wxString
s1(utf8
, wxConvUTF8
);
184 wxString
s2(wchar
, wxConvUTF8
);
187 CPPUNIT_ASSERT( s1
== wchar
);
188 CPPUNIT_ASSERT( s2
== wchar
);
190 CPPUNIT_ASSERT( s1
== utf8
);
191 CPPUNIT_ASSERT( s2
== utf8
);
194 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
195 wxString
s3(utf8
, wxConvUTF8
, 4);
196 wxString
s4(wchar
, wxConvUTF8
, 3);
198 CPPUNIT_ASSERT( s3
== sub
);
199 CPPUNIT_ASSERT( s4
== sub
);
202 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
203 wxConvUTF8
) == wxT("") ); //should stop at pos 35
207 void StringTestCase::Conversion()
210 wxString
szTheString(L
"The\0String", wxConvLibc
, 10);
211 wxCharBuffer theBuffer
= szTheString
.mb_str();
213 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String", 11) == 0 );
215 wxString
szTheString2("The\0String", wxConvLocal
, 10);
216 CPPUNIT_ASSERT( szTheString2
.length() == 11 );
217 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String", 11) == 0 );
219 wxString
szTheString(wxT("TheString"));
220 szTheString
.insert(3, 1, '\0');
221 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
223 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
225 wxString
szLocalTheString(wxT("TheString"));
226 szLocalTheString
.insert(3, 1, '\0');
227 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
229 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
234 // in case wcscmp is missing
236 static int wx_wcscmp(const wchar_t *s1
, const wchar_t *s2
)
238 while (*s1
== *s2
&& *s1
!= 0)
248 StringTestCase::DoTestConversion(const char *s
,
255 wxCharBuffer
buf(wxString(ws
).mb_str(conv
));
257 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
259 #else // wxUSE_UNICODE
262 wxWCharBuffer
wbuf(wxString(s
).wc_str(conv
));
265 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
267 CPPUNIT_ASSERT( !*wbuf
);
269 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
272 struct StringConversionData
278 void StringTestCase::ConversionUTF7()
280 static const StringConversionData utf7data
[] =
284 //\u isn't recognized on MSVC 6
285 #if !defined(_MSC_VER)
286 #if !defined(__GNUC__) || (__GNUC__ >= 3)
287 { "+AKM-", L
"\u00a3" },
290 // Windows accepts invalid UTF-7 strings and so does our UTF-7
291 // conversion code -- this is wrong IMO but the way it is for now
293 // notice that converting "+" still behaves as expected because the
294 // result is just an empty string, i.e. the same as if there were an
295 // error, but converting "a+" results in "a" while it really should
301 wxCSConv
conv(_T("utf-7"));
302 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
304 const StringConversionData
& d
= utf7data
[n
];
305 DoTestConversion(d
.str
, d
.wcs
, conv
);
309 void StringTestCase::ConversionUTF8()
311 static const StringConversionData utf8data
[] =
313 //\u isn't recognized on MSVC 6
314 #if !defined(_MSC_VER)
315 #if !defined(__GNUC__) || (__GNUC__ >= 3)
316 { "\xc2\xa3", L
"\u00a3" },
322 wxCSConv
conv(_T("utf-8"));
323 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
325 const StringConversionData
& d
= utf8data
[n
];
326 DoTestConversion(d
.str
, d
.wcs
, conv
);
330 #endif // wxUSE_WCHAR_T
333 void StringTestCase::Extraction()
335 wxString
s(_T("Hello, world!"));
337 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
338 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
339 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
340 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
341 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
342 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
343 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
347 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
349 ( s.StartsWith( prefix, &rest ) == result ) && \
350 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
353 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
354 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
355 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
356 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
357 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
358 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
359 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
361 #undef TEST_STARTS_WITH
364 void StringTestCase::Find()
366 #define TEST_FIND( str , start , result ) \
367 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
369 TEST_FIND( _T("Well, hello world"), 0, 1 );
370 TEST_FIND( _T("Well, hello world"), 6, 7 );
371 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
376 void StringTestCase::Replace()
378 #define TEST_REPLACE( original , pos , len , replacement , result ) \
380 wxString s = original; \
381 s.replace( pos , len , replacement ); \
382 CPPUNIT_ASSERT( s == result ); \
385 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
386 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
387 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
388 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
389 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
392 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
394 wxString s(o,olen); \
395 s.replace( pos , len , replacement ); \
396 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
399 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
400 _T("null\0dhar"), 9 );
402 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
404 wxString s(o,olen); \
405 s.Replace( olds, news, all ); \
406 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
409 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
410 _T("null\0dehar"), 10 );
412 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
413 _T("null\0char"), 9 );
415 #undef TEST_WXREPLACE
416 #undef TEST_NULLCHARREPLACE
420 void StringTestCase::Match()
422 #define TEST_MATCH( s1 , s2 , result ) \
423 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
425 TEST_MATCH( _T("foobar"), _T("foo*"), true );
426 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
427 TEST_MATCH( _T("foobar"), _T("*bar"), true );
428 TEST_MATCH( _T("foobar"), _T("??????"), true );
429 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
430 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
431 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
432 TEST_MATCH( _T("foobar"), _T("*foo"), false );
433 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
434 TEST_MATCH( _T(""), _T("*"), true );
435 TEST_MATCH( _T(""), _T("?"), false );
441 void StringTestCase::CaseChanges()
443 wxString
s1(_T("Hello!"));
452 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
453 CPPUNIT_ASSERT( s1l
== _T("hello!") );
454 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
455 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
458 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
461 // try upper casing 8bit strings
462 wxString
sUpper("\xdf"),
465 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
466 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
468 #endif // !wxUSE_UNICODE
471 void StringTestCase::Compare()
473 wxString s1
= wxT("AHH");
474 wxString eq
= wxT("AHH");
475 wxString neq1
= wxT("HAH");
476 wxString neq2
= wxT("AH");
477 wxString neq3
= wxT("AHHH");
478 wxString neq4
= wxT("AhH");
480 CPPUNIT_ASSERT( s1
== eq
);
481 CPPUNIT_ASSERT( s1
!= neq1
);
482 CPPUNIT_ASSERT( s1
!= neq2
);
483 CPPUNIT_ASSERT( s1
!= neq3
);
484 CPPUNIT_ASSERT( s1
!= neq4
);
486 // wxString _s1 = wxT("A\0HH");
487 // wxString _eq = wxT("A\0HH");
488 // wxString _neq1 = wxT("H\0AH");
489 // wxString _neq2 = wxT("A\0H");
490 // wxString _neq3 = wxT("A\0HHH");
491 // wxString _neq4 = wxT("A\0hH");
494 neq1
.insert(1,1,'\0');
495 neq2
.insert(1,1,'\0');
496 neq3
.insert(1,1,'\0');
497 neq4
.insert(1,1,'\0');
499 CPPUNIT_ASSERT( s1
== eq
);
500 CPPUNIT_ASSERT( s1
!= neq1
);
501 CPPUNIT_ASSERT( s1
!= neq2
);
502 CPPUNIT_ASSERT( s1
!= neq3
);
503 CPPUNIT_ASSERT( s1
!= neq4
);
506 void StringTestCase::CompareNoCase()
508 wxString s1
= wxT("AHH");
509 wxString eq
= wxT("AHH");
510 wxString eq2
= wxT("AhH");
511 wxString eq3
= wxT("ahh");
512 wxString neq
= wxT("HAH");
513 wxString neq2
= wxT("AH");
514 wxString neq3
= wxT("AHHH");
516 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
517 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
519 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
520 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
521 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
523 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
524 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
525 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
528 // wxString _s1 = wxT("A\0HH");
529 // wxString _eq = wxT("A\0HH");
530 // wxString _eq2 = wxT("A\0hH");
531 // wxString _eq3 = wxT("a\0hh");
532 // wxString _neq = wxT("H\0AH");
533 // wxString _neq2 = wxT("A\0H");
534 // wxString _neq3 = wxT("A\0HHH");
538 eq2
.insert(1,1,'\0');
539 eq3
.insert(1,1,'\0');
540 neq
.insert(1,1,'\0');
541 neq2
.insert(1,1,'\0');
542 neq3
.insert(1,1,'\0');
544 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
545 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
546 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
548 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
549 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
550 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
553 void StringTestCase::ToLong()
556 static const struct ToLongData
563 { _T("1"), 1, true },
564 { _T("0"), 0, true },
565 { _T("a"), 0, false },
566 { _T("12345"), 12345, true },
567 { _T("-1"), -1, true },
568 { _T("--1"), 0, false },
572 for ( n
= 0; n
< WXSIZEOF(longData
); n
++ )
574 const ToLongData
& ld
= longData
[n
];
575 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToLong(&l
) );
577 CPPUNIT_ASSERT_EQUAL( ld
.value
, l
);
581 void StringTestCase::ToULong()
584 static const struct ToULongData
591 { _T("1"), 1, true },
592 { _T("0"), 0, true },
593 { _T("a"), 0, false },
594 { _T("12345"), 12345, true },
595 // this is surprizing but consistent with strtoul() behaviour
596 { _T("-1"), ULONG_MAX
, true },
600 for ( n
= 0; n
< WXSIZEOF(ulongData
); n
++ )
602 const ToULongData
& uld
= ulongData
[n
];
603 CPPUNIT_ASSERT_EQUAL( uld
.ok
, wxString(uld
.str
).ToULong(&ul
) );
605 CPPUNIT_ASSERT_EQUAL( uld
.value
, ul
);
609 void StringTestCase::ToDouble()
612 static const struct ToDoubleData
619 { _T("1"), 1, true },
620 { _T("1.23"), 1.23, true },
621 { _T(".1"), .1, true },
622 { _T("1."), 1, true },
623 { _T("1.."), 0, false },
624 { _T("0"), 0, true },
625 { _T("a"), 0, false },
626 { _T("12345"), 12345, true },
627 { _T("-1"), -1, true },
628 { _T("--1"), 0, false },
631 // we need to use decimal point, not comma or whatever is its value for the
633 wxSetlocale(LC_ALL
, _T("C"));
636 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
638 const ToDoubleData
& ld
= doubleData
[n
];
639 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
641 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);