]>
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( ToLong
);
48 CPPUNIT_TEST( ToULong
);
49 CPPUNIT_TEST( ToDouble
);
50 CPPUNIT_TEST_SUITE_END();
68 DECLARE_NO_COPY_CLASS(StringTestCase
)
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
74 // also include in it's own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
77 StringTestCase::StringTestCase()
81 void StringTestCase::String()
89 for (int i
= 0; i
< 2; ++i
)
93 c
= _T("! How'ya doin'?");
96 c
= _T("Hello world! What's up?");
97 CPPUNIT_ASSERT( c
!= a
);
101 void StringTestCase::PChar()
107 for (int i
= 0; i
< 2; ++i
)
109 wxStrcpy (a
, _T("Hello"));
110 wxStrcpy (b
, _T(" world"));
111 wxStrcpy (c
, _T("! How'ya doin'?"));
114 wxStrcpy (c
, _T("Hello world! What's up?"));
115 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
119 void StringTestCase::Format()
122 s1
.Printf(_T("%03d"), 18);
123 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
124 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
125 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
127 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
128 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
130 const size_t len
= lengths
[n
];
132 wxString
s(_T('Z'), len
);
133 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
137 void StringTestCase::Constructors()
139 #define TEST_CTOR(args, res) \
142 CPPUNIT_ASSERT( s == res ); \
145 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
146 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
147 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
149 static const wxChar
*s
= _T("?really!");
150 const wxChar
*start
= wxStrchr(s
, _T('r'));
151 const wxChar
*end
= wxStrchr(s
, _T('!'));
152 TEST_CTOR((start
, end
), _T("really"));
156 void StringTestCase::Extraction()
158 wxString
s(_T("Hello, world!"));
160 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
161 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
162 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
163 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
164 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
165 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
166 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
170 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
171 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
173 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
175 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
176 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
177 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
178 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
179 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
180 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
181 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
183 #undef TEST_STARTS_WITH
185 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
186 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
188 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
190 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
191 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
192 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
193 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
194 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
195 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
196 TEST_ENDS_WITH( _T("?"), _T(""), false );
197 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
198 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
200 #undef TEST_ENDS_WITH
203 void StringTestCase::Trim()
205 #define TEST_TRIM( str , dir , result ) \
206 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
208 TEST_TRIM( _T(" Test "), true, _T(" Test") );
209 TEST_TRIM( _T(" "), true, _T("") );
210 TEST_TRIM( _T(" "), true, _T("") );
211 TEST_TRIM( _T(""), true, _T("") );
213 TEST_TRIM( _T(" Test "), false, _T("Test ") );
214 TEST_TRIM( _T(" "), false, _T("") );
215 TEST_TRIM( _T(" "), false, _T("") );
216 TEST_TRIM( _T(""), false, _T("") );
221 void StringTestCase::Find()
223 #define TEST_FIND( str , start , result ) \
224 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
226 TEST_FIND( _T("Well, hello world"), 0, 1 );
227 TEST_FIND( _T("Well, hello world"), 6, 7 );
228 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
233 void StringTestCase::Replace()
235 #define TEST_REPLACE( original , pos , len , replacement , result ) \
237 wxString s = original; \
238 s.replace( pos , len , replacement ); \
239 CPPUNIT_ASSERT( s == result ); \
242 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
243 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
244 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
245 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
246 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
249 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
251 wxString s(o,olen); \
252 s.replace( pos , len , replacement ); \
253 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
256 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
257 _T("null\0dhar"), 9 );
259 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
261 wxString s(o,olen); \
262 s.Replace( olds, news, all ); \
263 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
266 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
267 _T("null\0dehar"), 10 );
269 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
270 _T("null\0char"), 9 );
272 #undef TEST_WXREPLACE
273 #undef TEST_NULLCHARREPLACE
277 void StringTestCase::Match()
279 #define TEST_MATCH( s1 , s2 , result ) \
280 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
282 TEST_MATCH( _T("foobar"), _T("foo*"), true );
283 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
284 TEST_MATCH( _T("foobar"), _T("*bar"), true );
285 TEST_MATCH( _T("foobar"), _T("??????"), true );
286 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
287 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
288 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
289 TEST_MATCH( _T("foobar"), _T("*foo"), false );
290 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
291 TEST_MATCH( _T(""), _T("*"), true );
292 TEST_MATCH( _T(""), _T("?"), false );
298 void StringTestCase::CaseChanges()
300 wxString
s1(_T("Hello!"));
309 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
310 CPPUNIT_ASSERT( s1l
== _T("hello!") );
311 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
312 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
315 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
318 // try upper casing 8bit strings
319 const wchar_t capital_ya
[] = { 0x42f, 0 },
320 small_ya
[] = { 0x44f, 0 };
322 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
323 sLower(wxConvLibc
.cWC2MB(small_ya
));
325 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
326 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
328 #endif // !wxUSE_UNICODE
331 void StringTestCase::Compare()
333 wxString s1
= wxT("AHH");
334 wxString eq
= wxT("AHH");
335 wxString neq1
= wxT("HAH");
336 wxString neq2
= wxT("AH");
337 wxString neq3
= wxT("AHHH");
338 wxString neq4
= wxT("AhH");
340 CPPUNIT_ASSERT( s1
== eq
);
341 CPPUNIT_ASSERT( s1
!= neq1
);
342 CPPUNIT_ASSERT( s1
!= neq2
);
343 CPPUNIT_ASSERT( s1
!= neq3
);
344 CPPUNIT_ASSERT( s1
!= neq4
);
346 // wxString _s1 = wxT("A\0HH");
347 // wxString _eq = wxT("A\0HH");
348 // wxString _neq1 = wxT("H\0AH");
349 // wxString _neq2 = wxT("A\0H");
350 // wxString _neq3 = wxT("A\0HHH");
351 // wxString _neq4 = wxT("A\0hH");
354 neq1
.insert(1,1,'\0');
355 neq2
.insert(1,1,'\0');
356 neq3
.insert(1,1,'\0');
357 neq4
.insert(1,1,'\0');
359 CPPUNIT_ASSERT( s1
== eq
);
360 CPPUNIT_ASSERT( s1
!= neq1
);
361 CPPUNIT_ASSERT( s1
!= neq2
);
362 CPPUNIT_ASSERT( s1
!= neq3
);
363 CPPUNIT_ASSERT( s1
!= neq4
);
366 void StringTestCase::CompareNoCase()
368 wxString s1
= wxT("AHH");
369 wxString eq
= wxT("AHH");
370 wxString eq2
= wxT("AhH");
371 wxString eq3
= wxT("ahh");
372 wxString neq
= wxT("HAH");
373 wxString neq2
= wxT("AH");
374 wxString neq3
= wxT("AHHH");
376 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
377 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
379 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
380 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
381 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
383 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
384 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
385 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
388 // wxString _s1 = wxT("A\0HH");
389 // wxString _eq = wxT("A\0HH");
390 // wxString _eq2 = wxT("A\0hH");
391 // wxString _eq3 = wxT("a\0hh");
392 // wxString _neq = wxT("H\0AH");
393 // wxString _neq2 = wxT("A\0H");
394 // wxString _neq3 = wxT("A\0HHH");
398 eq2
.insert(1,1,'\0');
399 eq3
.insert(1,1,'\0');
400 neq
.insert(1,1,'\0');
401 neq2
.insert(1,1,'\0');
402 neq3
.insert(1,1,'\0');
404 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
405 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
406 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
408 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
409 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
410 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
413 void StringTestCase::ToLong()
416 static const struct ToLongData
423 { _T("1"), 1, true },
424 { _T("0"), 0, true },
425 { _T("a"), 0, false },
426 { _T("12345"), 12345, true },
427 { _T("-1"), -1, true },
428 { _T("--1"), 0, false },
432 for ( n
= 0; n
< WXSIZEOF(longData
); n
++ )
434 const ToLongData
& ld
= longData
[n
];
435 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToLong(&l
) );
437 CPPUNIT_ASSERT_EQUAL( ld
.value
, l
);
441 void StringTestCase::ToULong()
444 static const struct ToULongData
451 { _T("1"), 1, true },
452 { _T("0"), 0, true },
453 { _T("a"), 0, false },
454 { _T("12345"), 12345, true },
455 // this is surprizing but consistent with strtoul() behaviour
456 { _T("-1"), ULONG_MAX
, true },
460 for ( n
= 0; n
< WXSIZEOF(ulongData
); n
++ )
462 const ToULongData
& uld
= ulongData
[n
];
463 CPPUNIT_ASSERT_EQUAL( uld
.ok
, wxString(uld
.str
).ToULong(&ul
) );
465 CPPUNIT_ASSERT_EQUAL( uld
.value
, ul
);
469 void StringTestCase::ToDouble()
472 static const struct ToDoubleData
479 { _T("1"), 1, true },
480 { _T("1.23"), 1.23, true },
481 { _T(".1"), .1, true },
482 { _T("1."), 1, true },
483 { _T("1.."), 0, false },
484 { _T("0"), 0, true },
485 { _T("a"), 0, false },
486 { _T("12345"), 12345, true },
487 { _T("-1"), -1, true },
488 { _T("--1"), 0, false },
491 // we need to use decimal point, not comma or whatever is its value for the
493 wxSetlocale(LC_ALL
, _T("C"));
496 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
498 const ToDoubleData
& ld
= doubleData
[n
];
499 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
501 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);