]>
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
);
41 CPPUNIT_TEST( Replace
);
42 CPPUNIT_TEST( Match
);
43 CPPUNIT_TEST( CaseChanges
);
44 CPPUNIT_TEST( Compare
);
45 CPPUNIT_TEST( CompareNoCase
);
46 CPPUNIT_TEST( ToLong
);
47 CPPUNIT_TEST( ToULong
);
48 CPPUNIT_TEST( ToDouble
);
49 CPPUNIT_TEST_SUITE_END();
66 DECLARE_NO_COPY_CLASS(StringTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
72 // also include in it's own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
75 StringTestCase::StringTestCase()
79 void StringTestCase::String()
87 for (int i
= 0; i
< 2; ++i
)
91 c
= _T("! How'ya doin'?");
94 c
= _T("Hello world! What's up?");
95 CPPUNIT_ASSERT( c
!= a
);
99 void StringTestCase::PChar()
105 for (int i
= 0; i
< 2; ++i
)
107 wxStrcpy (a
, _T("Hello"));
108 wxStrcpy (b
, _T(" world"));
109 wxStrcpy (c
, _T("! How'ya doin'?"));
112 wxStrcpy (c
, _T("Hello world! What's up?"));
113 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
117 void StringTestCase::Format()
120 s1
.Printf(_T("%03d"), 18);
121 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
122 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
123 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
125 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
126 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
128 const size_t len
= lengths
[n
];
130 wxString
s(_T('Z'), len
);
131 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
135 void StringTestCase::Constructors()
137 #define TEST_CTOR(args, res) \
140 CPPUNIT_ASSERT( s == res ); \
143 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
144 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
145 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
147 static const wxChar
*s
= _T("?really!");
148 const wxChar
*start
= wxStrchr(s
, _T('r'));
149 const wxChar
*end
= wxStrchr(s
, _T('!'));
150 TEST_CTOR((start
, end
), _T("really"));
154 void StringTestCase::Extraction()
156 wxString
s(_T("Hello, world!"));
158 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
159 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
160 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
161 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
162 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
163 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
164 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
168 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
169 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
171 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
173 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
174 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
175 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
176 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
177 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
178 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
179 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
181 #undef TEST_STARTS_WITH
183 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
184 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
186 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
188 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
189 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
190 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
191 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
192 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
193 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
194 TEST_ENDS_WITH( _T("?"), _T(""), false );
195 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
196 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
198 #undef TEST_ENDS_WITH
201 void StringTestCase::Find()
203 #define TEST_FIND( str , start , result ) \
204 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
206 TEST_FIND( _T("Well, hello world"), 0, 1 );
207 TEST_FIND( _T("Well, hello world"), 6, 7 );
208 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
213 void StringTestCase::Replace()
215 #define TEST_REPLACE( original , pos , len , replacement , result ) \
217 wxString s = original; \
218 s.replace( pos , len , replacement ); \
219 CPPUNIT_ASSERT( s == result ); \
222 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
223 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
224 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
225 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
226 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
229 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
231 wxString s(o,olen); \
232 s.replace( pos , len , replacement ); \
233 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
236 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
237 _T("null\0dhar"), 9 );
239 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
241 wxString s(o,olen); \
242 s.Replace( olds, news, all ); \
243 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
246 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
247 _T("null\0dehar"), 10 );
249 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
250 _T("null\0char"), 9 );
252 #undef TEST_WXREPLACE
253 #undef TEST_NULLCHARREPLACE
257 void StringTestCase::Match()
259 #define TEST_MATCH( s1 , s2 , result ) \
260 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
262 TEST_MATCH( _T("foobar"), _T("foo*"), true );
263 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
264 TEST_MATCH( _T("foobar"), _T("*bar"), true );
265 TEST_MATCH( _T("foobar"), _T("??????"), true );
266 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
267 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
268 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
269 TEST_MATCH( _T("foobar"), _T("*foo"), false );
270 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
271 TEST_MATCH( _T(""), _T("*"), true );
272 TEST_MATCH( _T(""), _T("?"), false );
278 void StringTestCase::CaseChanges()
280 wxString
s1(_T("Hello!"));
289 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
290 CPPUNIT_ASSERT( s1l
== _T("hello!") );
291 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
292 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
295 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
298 // try upper casing 8bit strings
299 const wchar_t capital_ya
[] = { 0x42f, 0 },
300 small_ya
[] = { 0x44f, 0 };
302 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
303 sLower(wxConvLibc
.cWC2MB(small_ya
));
305 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
306 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
308 #endif // !wxUSE_UNICODE
311 void StringTestCase::Compare()
313 wxString s1
= wxT("AHH");
314 wxString eq
= wxT("AHH");
315 wxString neq1
= wxT("HAH");
316 wxString neq2
= wxT("AH");
317 wxString neq3
= wxT("AHHH");
318 wxString neq4
= wxT("AhH");
320 CPPUNIT_ASSERT( s1
== eq
);
321 CPPUNIT_ASSERT( s1
!= neq1
);
322 CPPUNIT_ASSERT( s1
!= neq2
);
323 CPPUNIT_ASSERT( s1
!= neq3
);
324 CPPUNIT_ASSERT( s1
!= neq4
);
326 // wxString _s1 = wxT("A\0HH");
327 // wxString _eq = wxT("A\0HH");
328 // wxString _neq1 = wxT("H\0AH");
329 // wxString _neq2 = wxT("A\0H");
330 // wxString _neq3 = wxT("A\0HHH");
331 // wxString _neq4 = wxT("A\0hH");
334 neq1
.insert(1,1,'\0');
335 neq2
.insert(1,1,'\0');
336 neq3
.insert(1,1,'\0');
337 neq4
.insert(1,1,'\0');
339 CPPUNIT_ASSERT( s1
== eq
);
340 CPPUNIT_ASSERT( s1
!= neq1
);
341 CPPUNIT_ASSERT( s1
!= neq2
);
342 CPPUNIT_ASSERT( s1
!= neq3
);
343 CPPUNIT_ASSERT( s1
!= neq4
);
346 void StringTestCase::CompareNoCase()
348 wxString s1
= wxT("AHH");
349 wxString eq
= wxT("AHH");
350 wxString eq2
= wxT("AhH");
351 wxString eq3
= wxT("ahh");
352 wxString neq
= wxT("HAH");
353 wxString neq2
= wxT("AH");
354 wxString neq3
= wxT("AHHH");
356 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
357 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
359 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
360 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
361 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
363 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
364 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
365 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
368 // wxString _s1 = wxT("A\0HH");
369 // wxString _eq = wxT("A\0HH");
370 // wxString _eq2 = wxT("A\0hH");
371 // wxString _eq3 = wxT("a\0hh");
372 // wxString _neq = wxT("H\0AH");
373 // wxString _neq2 = wxT("A\0H");
374 // wxString _neq3 = wxT("A\0HHH");
378 eq2
.insert(1,1,'\0');
379 eq3
.insert(1,1,'\0');
380 neq
.insert(1,1,'\0');
381 neq2
.insert(1,1,'\0');
382 neq3
.insert(1,1,'\0');
384 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
385 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
386 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
388 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
389 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
390 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
393 void StringTestCase::ToLong()
396 static const struct ToLongData
403 { _T("1"), 1, true },
404 { _T("0"), 0, true },
405 { _T("a"), 0, false },
406 { _T("12345"), 12345, true },
407 { _T("-1"), -1, true },
408 { _T("--1"), 0, false },
412 for ( n
= 0; n
< WXSIZEOF(longData
); n
++ )
414 const ToLongData
& ld
= longData
[n
];
415 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToLong(&l
) );
417 CPPUNIT_ASSERT_EQUAL( ld
.value
, l
);
421 void StringTestCase::ToULong()
424 static const struct ToULongData
431 { _T("1"), 1, true },
432 { _T("0"), 0, true },
433 { _T("a"), 0, false },
434 { _T("12345"), 12345, true },
435 // this is surprizing but consistent with strtoul() behaviour
436 { _T("-1"), ULONG_MAX
, true },
440 for ( n
= 0; n
< WXSIZEOF(ulongData
); n
++ )
442 const ToULongData
& uld
= ulongData
[n
];
443 CPPUNIT_ASSERT_EQUAL( uld
.ok
, wxString(uld
.str
).ToULong(&ul
) );
445 CPPUNIT_ASSERT_EQUAL( uld
.value
, ul
);
449 void StringTestCase::ToDouble()
452 static const struct ToDoubleData
459 { _T("1"), 1, true },
460 { _T("1.23"), 1.23, true },
461 { _T(".1"), .1, true },
462 { _T("1."), 1, true },
463 { _T("1.."), 0, false },
464 { _T("0"), 0, true },
465 { _T("a"), 0, false },
466 { _T("12345"), 12345, true },
467 { _T("-1"), -1, true },
468 { _T("--1"), 0, false },
471 // we need to use decimal point, not comma or whatever is its value for the
473 wxSetlocale(LC_ALL
, _T("C"));
476 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
478 const ToDoubleData
& ld
= doubleData
[n
];
479 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
481 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);