]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
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( Extraction
);
42 CPPUNIT_TEST( Replace
);
43 CPPUNIT_TEST( Match
);
44 CPPUNIT_TEST( CaseChanges
);
45 CPPUNIT_TEST( Compare
);
46 CPPUNIT_TEST( CompareNoCase
);
47 CPPUNIT_TEST( Contains
);
48 CPPUNIT_TEST( ToLong
);
49 CPPUNIT_TEST( ToULong
);
50 CPPUNIT_TEST( ToDouble
);
51 CPPUNIT_TEST_SUITE_END();
70 DECLARE_NO_COPY_CLASS(StringTestCase
)
73 // register in the unnamed registry so that these tests are run by default
74 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
76 // also include in it's own registry so that these tests can be run alone
77 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
79 StringTestCase::StringTestCase()
83 void StringTestCase::String()
91 for (int i
= 0; i
< 2; ++i
)
95 c
= _T("! How'ya doin'?");
98 c
= _T("Hello world! What's up?");
99 CPPUNIT_ASSERT( c
!= a
);
103 void StringTestCase::PChar()
109 for (int i
= 0; i
< 2; ++i
)
111 wxStrcpy (a
, _T("Hello"));
112 wxStrcpy (b
, _T(" world"));
113 wxStrcpy (c
, _T("! How'ya doin'?"));
116 wxStrcpy (c
, _T("Hello world! What's up?"));
117 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
121 void StringTestCase::Format()
124 s1
.Printf(_T("%03d"), 18);
125 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
126 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
127 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
129 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
130 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
132 const size_t len
= lengths
[n
];
134 wxString
s(_T('Z'), len
);
135 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
139 void StringTestCase::Constructors()
141 #define TEST_CTOR(args, res) \
144 CPPUNIT_ASSERT( s == res ); \
147 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
148 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
149 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
151 static const wxChar
*s
= _T("?really!");
152 const wxChar
*start
= wxStrchr(s
, _T('r'));
153 const wxChar
*end
= wxStrchr(s
, _T('!'));
154 TEST_CTOR((start
, end
), _T("really"));
158 void StringTestCase::Extraction()
160 wxString
s(_T("Hello, world!"));
162 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
163 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
164 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
165 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
166 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
167 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
168 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
172 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
173 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
175 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
177 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
178 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
179 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
180 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
181 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
182 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
183 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
185 #undef TEST_STARTS_WITH
187 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
188 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
190 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
192 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
193 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
194 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
195 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
196 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
197 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
198 TEST_ENDS_WITH( _T("?"), _T(""), false );
199 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
200 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
202 #undef TEST_ENDS_WITH
205 void StringTestCase::Trim()
207 #define TEST_TRIM( str , dir , result ) \
208 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
210 TEST_TRIM( _T(" Test "), true, _T(" Test") );
211 TEST_TRIM( _T(" "), true, _T("") );
212 TEST_TRIM( _T(" "), true, _T("") );
213 TEST_TRIM( _T(""), true, _T("") );
215 TEST_TRIM( _T(" Test "), false, _T("Test ") );
216 TEST_TRIM( _T(" "), false, _T("") );
217 TEST_TRIM( _T(" "), false, _T("") );
218 TEST_TRIM( _T(""), false, _T("") );
223 void StringTestCase::Find()
225 #define TEST_FIND( str , start , result ) \
226 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
228 TEST_FIND( _T("Well, hello world"), 0, 1 );
229 TEST_FIND( _T("Well, hello world"), 6, 7 );
230 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
235 void StringTestCase::Replace()
237 #define TEST_REPLACE( original , pos , len , replacement , result ) \
239 wxString s = original; \
240 s.replace( pos , len , replacement ); \
241 CPPUNIT_ASSERT( s == result ); \
244 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
245 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
246 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
247 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
248 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
251 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
253 wxString s(o,olen); \
254 s.replace( pos , len , replacement ); \
255 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
258 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
259 _T("null\0dhar"), 9 );
261 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
263 wxString s(o,olen); \
264 s.Replace( olds, news, all ); \
265 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
268 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
269 _T("null\0dehar"), 10 );
271 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
272 _T("null\0char"), 9 );
274 #undef TEST_WXREPLACE
275 #undef TEST_NULLCHARREPLACE
279 void StringTestCase::Match()
281 #define TEST_MATCH( s1 , s2 , result ) \
282 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
284 TEST_MATCH( _T("foobar"), _T("foo*"), true );
285 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
286 TEST_MATCH( _T("foobar"), _T("*bar"), true );
287 TEST_MATCH( _T("foobar"), _T("??????"), true );
288 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
289 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
290 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
291 TEST_MATCH( _T("foobar"), _T("*foo"), false );
292 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
293 TEST_MATCH( _T(""), _T("*"), true );
294 TEST_MATCH( _T(""), _T("?"), false );
300 void StringTestCase::CaseChanges()
302 wxString
s1(_T("Hello!"));
311 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
312 CPPUNIT_ASSERT( s1l
== _T("hello!") );
313 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
314 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
317 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
320 // try upper casing 8bit strings
321 const wchar_t capital_ya
[] = { 0x42f, 0 },
322 small_ya
[] = { 0x44f, 0 };
324 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
325 sLower(wxConvLibc
.cWC2MB(small_ya
));
327 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
328 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
330 #endif // !wxUSE_UNICODE
333 void StringTestCase::Compare()
335 wxString s1
= wxT("AHH");
336 wxString eq
= wxT("AHH");
337 wxString neq1
= wxT("HAH");
338 wxString neq2
= wxT("AH");
339 wxString neq3
= wxT("AHHH");
340 wxString neq4
= wxT("AhH");
342 CPPUNIT_ASSERT( s1
== eq
);
343 CPPUNIT_ASSERT( s1
!= neq1
);
344 CPPUNIT_ASSERT( s1
!= neq2
);
345 CPPUNIT_ASSERT( s1
!= neq3
);
346 CPPUNIT_ASSERT( s1
!= neq4
);
348 // wxString _s1 = wxT("A\0HH");
349 // wxString _eq = wxT("A\0HH");
350 // wxString _neq1 = wxT("H\0AH");
351 // wxString _neq2 = wxT("A\0H");
352 // wxString _neq3 = wxT("A\0HHH");
353 // wxString _neq4 = wxT("A\0hH");
356 neq1
.insert(1,1,'\0');
357 neq2
.insert(1,1,'\0');
358 neq3
.insert(1,1,'\0');
359 neq4
.insert(1,1,'\0');
361 CPPUNIT_ASSERT( s1
== eq
);
362 CPPUNIT_ASSERT( s1
!= neq1
);
363 CPPUNIT_ASSERT( s1
!= neq2
);
364 CPPUNIT_ASSERT( s1
!= neq3
);
365 CPPUNIT_ASSERT( s1
!= neq4
);
368 void StringTestCase::CompareNoCase()
370 wxString s1
= wxT("AHH");
371 wxString eq
= wxT("AHH");
372 wxString eq2
= wxT("AhH");
373 wxString eq3
= wxT("ahh");
374 wxString neq
= wxT("HAH");
375 wxString neq2
= wxT("AH");
376 wxString neq3
= wxT("AHHH");
378 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
379 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
381 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
382 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
383 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
385 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
386 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
387 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
390 // wxString _s1 = wxT("A\0HH");
391 // wxString _eq = wxT("A\0HH");
392 // wxString _eq2 = wxT("A\0hH");
393 // wxString _eq3 = wxT("a\0hh");
394 // wxString _neq = wxT("H\0AH");
395 // wxString _neq2 = wxT("A\0H");
396 // wxString _neq3 = wxT("A\0HHH");
400 eq2
.insert(1,1,'\0');
401 eq3
.insert(1,1,'\0');
402 neq
.insert(1,1,'\0');
403 neq2
.insert(1,1,'\0');
404 neq3
.insert(1,1,'\0');
406 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
407 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
408 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
410 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
411 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
412 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
415 void StringTestCase::Contains()
417 static const struct ContainsData
420 const wxChar
*needle
;
424 { _T(""), _T(""), true },
425 { _T(""), _T("foo"), false },
426 { _T("foo"), _T(""), true },
427 { _T("foo"), _T("f"), true },
428 { _T("foo"), _T("o"), true },
429 { _T("foo"), _T("oo"), true },
430 { _T("foo"), _T("ooo"), false },
431 { _T("foo"), _T("oooo"), false },
432 { _T("foo"), _T("fooo"), false },
435 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
437 const ContainsData
& cd
= containsData
[n
];
438 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
442 void StringTestCase::ToLong()
444 static const struct ToLongData
451 { _T("1"), 1, true },
452 { _T("0"), 0, true },
453 { _T("a"), 0, false },
454 { _T("12345"), 12345, true },
455 { _T("-1"), -1, true },
456 { _T("--1"), 0, false },
460 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
462 const ToLongData
& ld
= longData
[n
];
463 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToLong(&l
) );
465 CPPUNIT_ASSERT_EQUAL( ld
.value
, l
);
469 void StringTestCase::ToULong()
472 static const struct ToULongData
479 { _T("1"), 1, true },
480 { _T("0"), 0, true },
481 { _T("a"), 0, false },
482 { _T("12345"), 12345, true },
483 // this is surprizing but consistent with strtoul() behaviour
484 { _T("-1"), ULONG_MAX
, true },
488 for ( n
= 0; n
< WXSIZEOF(ulongData
); n
++ )
490 const ToULongData
& uld
= ulongData
[n
];
491 CPPUNIT_ASSERT_EQUAL( uld
.ok
, wxString(uld
.str
).ToULong(&ul
) );
493 CPPUNIT_ASSERT_EQUAL( uld
.value
, ul
);
497 void StringTestCase::ToDouble()
500 static const struct ToDoubleData
507 { _T("1"), 1, true },
508 { _T("1.23"), 1.23, true },
509 { _T(".1"), .1, true },
510 { _T("1."), 1, true },
511 { _T("1.."), 0, false },
512 { _T("0"), 0, true },
513 { _T("a"), 0, false },
514 { _T("12345"), 12345, true },
515 { _T("-1"), -1, true },
516 { _T("--1"), 0, false },
519 // we need to use decimal point, not comma or whatever is its value for the
521 wxSetlocale(LC_ALL
, _T("C"));
524 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
526 const ToDoubleData
& ld
= doubleData
[n
];
527 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
529 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);