]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
Some minor tweaks
[wxWidgets.git] / tests / strings / strings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-04-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 // ----------------------------------------------------------------------------
25 // test class
26 // ----------------------------------------------------------------------------
27
28 class StringTestCase : public CppUnit::TestCase
29 {
30 public:
31 StringTestCase();
32
33 private:
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 );
40 CPPUNIT_TEST( Find );
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();
50
51 void String();
52 void PChar();
53 void Format();
54 void Constructors();
55 void Extraction();
56 void Find();
57 void Replace();
58 void Match();
59 void CaseChanges();
60 void Compare();
61 void CompareNoCase();
62 void ToLong();
63 void ToULong();
64 void ToDouble();
65
66 DECLARE_NO_COPY_CLASS(StringTestCase)
67 };
68
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
71
72 // also include in it's own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
74
75 StringTestCase::StringTestCase()
76 {
77 }
78
79 void StringTestCase::String()
80 {
81 wxString a, b, c;
82
83 a.reserve (128);
84 b.reserve (128);
85 c.reserve (128);
86
87 for (int i = 0; i < 2; ++i)
88 {
89 a = _T("Hello");
90 b = _T(" world");
91 c = _T("! How'ya doin'?");
92 a += b;
93 a += c;
94 c = _T("Hello world! What's up?");
95 CPPUNIT_ASSERT( c != a );
96 }
97 }
98
99 void StringTestCase::PChar()
100 {
101 wxChar a [128];
102 wxChar b [128];
103 wxChar c [128];
104
105 for (int i = 0; i < 2; ++i)
106 {
107 wxStrcpy (a, _T("Hello"));
108 wxStrcpy (b, _T(" world"));
109 wxStrcpy (c, _T("! How'ya doin'?"));
110 wxStrcat (a, b);
111 wxStrcat (a, c);
112 wxStrcpy (c, _T("Hello world! What's up?"));
113 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
114 }
115 }
116
117 void StringTestCase::Format()
118 {
119 wxString s1,s2;
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()) );
124
125 static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
126 for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
127 {
128 const size_t len = lengths[n];
129
130 wxString s(_T('Z'), len);
131 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length());
132 }
133 }
134
135 void StringTestCase::Constructors()
136 {
137 #define TEST_CTOR(args, res) \
138 { \
139 wxString s args ; \
140 CPPUNIT_ASSERT( s == res ); \
141 }
142
143 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
144 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
145 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
146
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"));
151 }
152
153
154 void StringTestCase::Extraction()
155 {
156 wxString s(_T("Hello, world!"));
157
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 );
165
166 wxString rest;
167
168 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
169 CPPUNIT_ASSERT( \
170 ( s.StartsWith( prefix, &rest ) == result ) && \
171 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
172 )
173
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 );
181
182 #undef TEST_STARTS_WITH
183 }
184
185 void StringTestCase::Find()
186 {
187 #define TEST_FIND( str , start , result ) \
188 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
189
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 );
193
194 #undef TEST_FIND
195 }
196
197 void StringTestCase::Replace()
198 {
199 #define TEST_REPLACE( original , pos , len , replacement , result ) \
200 { \
201 wxString s = original; \
202 s.replace( pos , len , replacement ); \
203 CPPUNIT_ASSERT( s == result ); \
204 }
205
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") );
211
212
213 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
214 { \
215 wxString s(o,olen); \
216 s.replace( pos , len , replacement ); \
217 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
218 }
219
220 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
221 _T("null\0dhar"), 9 );
222
223 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
224 { \
225 wxString s(o,olen); \
226 s.Replace( olds, news, all ); \
227 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
228 }
229
230 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
231 _T("null\0dehar"), 10 );
232
233 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
234 _T("null\0char"), 9 );
235
236 #undef TEST_WXREPLACE
237 #undef TEST_NULLCHARREPLACE
238 #undef TEST_REPLACE
239 }
240
241 void StringTestCase::Match()
242 {
243 #define TEST_MATCH( s1 , s2 , result ) \
244 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
245
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 );
257
258 #undef TEST_MATCH
259 }
260
261
262 void StringTestCase::CaseChanges()
263 {
264 wxString s1(_T("Hello!"));
265 wxString s1u(s1);
266 wxString s1l(s1);
267 s1u.MakeUpper();
268 s1l.MakeLower();
269 wxString s2u, s2l;
270 s2u.MakeUpper();
271 s2l.MakeLower();
272
273 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
274 CPPUNIT_ASSERT( s1l == _T("hello!") );
275 CPPUNIT_ASSERT( s2u == wxEmptyString );
276 CPPUNIT_ASSERT( s2l == wxEmptyString );
277
278 #if !wxUSE_UNICODE
279 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
280 if ( locRu.IsOk() )
281 {
282 // try upper casing 8bit strings
283 const wchar_t capital_ya[] = { 0x42f, 0 },
284 small_ya[] = { 0x44f, 0 };
285
286 wxString sUpper(wxConvLibc.cWC2MB(capital_ya)),
287 sLower(wxConvLibc.cWC2MB(small_ya));
288
289 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
290 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
291 }
292 #endif // !wxUSE_UNICODE
293 }
294
295 void StringTestCase::Compare()
296 {
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");
303
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 );
309
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");
316 s1.insert(1,1,'\0');
317 eq.insert(1,1,'\0');
318 neq1.insert(1,1,'\0');
319 neq2.insert(1,1,'\0');
320 neq3.insert(1,1,'\0');
321 neq4.insert(1,1,'\0');
322
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 );
328 }
329
330 void StringTestCase::CompareNoCase()
331 {
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");
339
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)
342
343 CPPUNIT_CNCEQ_ASSERT( s1, eq );
344 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
345 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
346
347 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
348 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
349 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
350
351
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");
359
360 s1.insert(1,1,'\0');
361 eq.insert(1,1,'\0');
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');
367
368 CPPUNIT_CNCEQ_ASSERT( s1, eq );
369 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
370 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
371
372 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
373 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
374 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
375 }
376
377 void StringTestCase::ToLong()
378 {
379 long l;
380 static const struct ToLongData
381 {
382 const wxChar *str;
383 long value;
384 bool ok;
385 } longData[] =
386 {
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 },
393 };
394
395 size_t n;
396 for ( n = 0; n < WXSIZEOF(longData); n++ )
397 {
398 const ToLongData& ld = longData[n];
399 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) );
400 if ( ld.ok )
401 CPPUNIT_ASSERT_EQUAL( ld.value, l );
402 }
403 }
404
405 void StringTestCase::ToULong()
406 {
407 unsigned long ul;
408 static const struct ToULongData
409 {
410 const wxChar *str;
411 unsigned long value;
412 bool ok;
413 } ulongData[] =
414 {
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 },
421 };
422
423 size_t n;
424 for ( n = 0; n < WXSIZEOF(ulongData); n++ )
425 {
426 const ToULongData& uld = ulongData[n];
427 CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) );
428 if ( uld.ok )
429 CPPUNIT_ASSERT_EQUAL( uld.value, ul );
430 }
431 }
432
433 void StringTestCase::ToDouble()
434 {
435 double d;
436 static const struct ToDoubleData
437 {
438 const wxChar *str;
439 double value;
440 bool ok;
441 } doubleData[] =
442 {
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 },
453 };
454
455 // we need to use decimal point, not comma or whatever is its value for the
456 // current locale
457 wxSetlocale(LC_ALL, _T("C"));
458
459 size_t n;
460 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
461 {
462 const ToDoubleData& ld = doubleData[n];
463 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
464 if ( ld.ok )
465 CPPUNIT_ASSERT_EQUAL( ld.value, d );
466 }
467 }