]>
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 ) \
170 ( s.StartsWith( prefix, &rest ) == result ) && \
171 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
174 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
175 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
176 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
177 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
178 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
179 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
180 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
182 #undef TEST_STARTS_WITH
185 void StringTestCase::Find()
187 #define TEST_FIND( str , start , result ) \
188 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
190 TEST_FIND( _T("Well, hello world"), 0, 1 );
191 TEST_FIND( _T("Well, hello world"), 6, 7 );
192 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
197 void StringTestCase::Replace()
199 #define TEST_REPLACE( original , pos , len , replacement , result ) \
201 wxString s = original; \
202 s.replace( pos , len , replacement ); \
203 CPPUNIT_ASSERT( s == result ); \
206 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
207 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
208 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
209 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
210 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
213 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
215 wxString s(o,olen); \
216 s.replace( pos , len , replacement ); \
217 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
220 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
221 _T("null\0dhar"), 9 );
223 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
225 wxString s(o,olen); \
226 s.Replace( olds, news, all ); \
227 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
230 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
231 _T("null\0dehar"), 10 );
233 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
234 _T("null\0char"), 9 );
236 #undef TEST_WXREPLACE
237 #undef TEST_NULLCHARREPLACE
241 void StringTestCase::Match()
243 #define TEST_MATCH( s1 , s2 , result ) \
244 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
246 TEST_MATCH( _T("foobar"), _T("foo*"), true );
247 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
248 TEST_MATCH( _T("foobar"), _T("*bar"), true );
249 TEST_MATCH( _T("foobar"), _T("??????"), true );
250 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
251 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
252 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
253 TEST_MATCH( _T("foobar"), _T("*foo"), false );
254 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
255 TEST_MATCH( _T(""), _T("*"), true );
256 TEST_MATCH( _T(""), _T("?"), false );
262 void StringTestCase::CaseChanges()
264 wxString
s1(_T("Hello!"));
273 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
274 CPPUNIT_ASSERT( s1l
== _T("hello!") );
275 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
276 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
279 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
282 // try upper casing 8bit strings
283 const wchar_t capital_ya
[] = { 0x42f, 0 },
284 small_ya
[] = { 0x44f, 0 };
286 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
287 sLower(wxConvLibc
.cWC2MB(small_ya
));
289 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
290 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
292 #endif // !wxUSE_UNICODE
295 void StringTestCase::Compare()
297 wxString s1
= wxT("AHH");
298 wxString eq
= wxT("AHH");
299 wxString neq1
= wxT("HAH");
300 wxString neq2
= wxT("AH");
301 wxString neq3
= wxT("AHHH");
302 wxString neq4
= wxT("AhH");
304 CPPUNIT_ASSERT( s1
== eq
);
305 CPPUNIT_ASSERT( s1
!= neq1
);
306 CPPUNIT_ASSERT( s1
!= neq2
);
307 CPPUNIT_ASSERT( s1
!= neq3
);
308 CPPUNIT_ASSERT( s1
!= neq4
);
310 // wxString _s1 = wxT("A\0HH");
311 // wxString _eq = wxT("A\0HH");
312 // wxString _neq1 = wxT("H\0AH");
313 // wxString _neq2 = wxT("A\0H");
314 // wxString _neq3 = wxT("A\0HHH");
315 // wxString _neq4 = wxT("A\0hH");
318 neq1
.insert(1,1,'\0');
319 neq2
.insert(1,1,'\0');
320 neq3
.insert(1,1,'\0');
321 neq4
.insert(1,1,'\0');
323 CPPUNIT_ASSERT( s1
== eq
);
324 CPPUNIT_ASSERT( s1
!= neq1
);
325 CPPUNIT_ASSERT( s1
!= neq2
);
326 CPPUNIT_ASSERT( s1
!= neq3
);
327 CPPUNIT_ASSERT( s1
!= neq4
);
330 void StringTestCase::CompareNoCase()
332 wxString s1
= wxT("AHH");
333 wxString eq
= wxT("AHH");
334 wxString eq2
= wxT("AhH");
335 wxString eq3
= wxT("ahh");
336 wxString neq
= wxT("HAH");
337 wxString neq2
= wxT("AH");
338 wxString neq3
= wxT("AHHH");
340 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
341 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
343 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
344 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
345 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
347 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
348 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
349 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
352 // wxString _s1 = wxT("A\0HH");
353 // wxString _eq = wxT("A\0HH");
354 // wxString _eq2 = wxT("A\0hH");
355 // wxString _eq3 = wxT("a\0hh");
356 // wxString _neq = wxT("H\0AH");
357 // wxString _neq2 = wxT("A\0H");
358 // wxString _neq3 = wxT("A\0HHH");
362 eq2
.insert(1,1,'\0');
363 eq3
.insert(1,1,'\0');
364 neq
.insert(1,1,'\0');
365 neq2
.insert(1,1,'\0');
366 neq3
.insert(1,1,'\0');
368 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
369 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
370 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
372 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
373 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
374 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
377 void StringTestCase::ToLong()
380 static const struct ToLongData
387 { _T("1"), 1, true },
388 { _T("0"), 0, true },
389 { _T("a"), 0, false },
390 { _T("12345"), 12345, true },
391 { _T("-1"), -1, true },
392 { _T("--1"), 0, false },
396 for ( n
= 0; n
< WXSIZEOF(longData
); n
++ )
398 const ToLongData
& ld
= longData
[n
];
399 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToLong(&l
) );
401 CPPUNIT_ASSERT_EQUAL( ld
.value
, l
);
405 void StringTestCase::ToULong()
408 static const struct ToULongData
415 { _T("1"), 1, true },
416 { _T("0"), 0, true },
417 { _T("a"), 0, false },
418 { _T("12345"), 12345, true },
419 // this is surprizing but consistent with strtoul() behaviour
420 { _T("-1"), ULONG_MAX
, true },
424 for ( n
= 0; n
< WXSIZEOF(ulongData
); n
++ )
426 const ToULongData
& uld
= ulongData
[n
];
427 CPPUNIT_ASSERT_EQUAL( uld
.ok
, wxString(uld
.str
).ToULong(&ul
) );
429 CPPUNIT_ASSERT_EQUAL( uld
.value
, ul
);
433 void StringTestCase::ToDouble()
436 static const struct ToDoubleData
443 { _T("1"), 1, true },
444 { _T("1.23"), 1.23, true },
445 { _T(".1"), .1, true },
446 { _T("1."), 1, true },
447 { _T("1.."), 0, false },
448 { _T("0"), 0, true },
449 { _T("a"), 0, false },
450 { _T("12345"), 12345, true },
451 { _T("-1"), -1, true },
452 { _T("--1"), 0, false },
455 // we need to use decimal point, not comma or whatever is its value for the
457 wxSetlocale(LC_ALL
, _T("C"));
460 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
462 const ToDoubleData
& ld
= doubleData
[n
];
463 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
465 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);