]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/strings.cpp
added Galician translation
[wxWidgets.git] / tests / strings / strings.cpp
CommitLineData
1cd53e88
VS
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
8899b155 14#include "testprec.h"
1cd53e88
VS
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif // WX_PRECOMP
23
1cd53e88
VS
24// ----------------------------------------------------------------------------
25// test class
26// ----------------------------------------------------------------------------
27
28class StringTestCase : public CppUnit::TestCase
29{
30public:
31 StringTestCase();
32
33private:
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 );
1cd53e88
VS
41 CPPUNIT_TEST( Replace );
42 CPPUNIT_TEST( Match );
bd7f096d 43 CPPUNIT_TEST( CaseChanges );
dcb68102
RN
44 CPPUNIT_TEST( Compare );
45 CPPUNIT_TEST( CompareNoCase );
4f7ee81a
VZ
46 CPPUNIT_TEST( ToLong );
47 CPPUNIT_TEST( ToULong );
48 CPPUNIT_TEST( ToDouble );
1cd53e88
VS
49 CPPUNIT_TEST_SUITE_END();
50
51 void String();
52 void PChar();
53 void Format();
54 void Constructors();
55 void Extraction();
56 void Find();
1cd53e88
VS
57 void Replace();
58 void Match();
bd7f096d 59 void CaseChanges();
dcb68102
RN
60 void Compare();
61 void CompareNoCase();
4f7ee81a
VZ
62 void ToLong();
63 void ToULong();
64 void ToDouble();
1cd53e88
VS
65
66 DECLARE_NO_COPY_CLASS(StringTestCase)
67};
68
69// register in the unnamed registry so that these tests are run by default
70CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
71
72// also include in it's own registry so that these tests can be run alone
73CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
74
75StringTestCase::StringTestCase()
76{
77}
78
79void 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
99void 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
117void 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()) );
b03cd7e6
VZ
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 }
1cd53e88
VS
133}
134
135void 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
265d5cce 153
1cd53e88
VS
154void 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
3affcd07
VZ
168 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
169 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
170 if ( result ) \
171 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
1cd53e88
VS
172
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 );
180
181 #undef TEST_STARTS_WITH
3affcd07
VZ
182
183 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
184 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
185 if ( result ) \
186 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
187
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 );
197
198 #undef TEST_ENDS_WITH
1cd53e88
VS
199}
200
201void StringTestCase::Find()
202{
203 #define TEST_FIND( str , start , result ) \
204 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
205
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 );
209
210 #undef TEST_FIND
211}
212
1cd53e88
VS
213void StringTestCase::Replace()
214{
215 #define TEST_REPLACE( original , pos , len , replacement , result ) \
216 { \
217 wxString s = original; \
218 s.replace( pos , len , replacement ); \
219 CPPUNIT_ASSERT( s == result ); \
220 }
221
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") );
227
7634e443
RN
228
229 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
230 { \
231 wxString s(o,olen); \
232 s.replace( pos , len , replacement ); \
233 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
234 }
235
236 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
237 _T("null\0dhar"), 9 );
238
239 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
240 { \
241 wxString s(o,olen); \
242 s.Replace( olds, news, all ); \
243 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
244 }
245
2df0258e
RN
246 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
247 _T("null\0dehar"), 10 );
7634e443 248
4629f07e
RN
249 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
250 _T("null\0char"), 9 );
251
7634e443
RN
252 #undef TEST_WXREPLACE
253 #undef TEST_NULLCHARREPLACE
1cd53e88
VS
254 #undef TEST_REPLACE
255}
256
257void StringTestCase::Match()
258{
259 #define TEST_MATCH( s1 , s2 , result ) \
260 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
261
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 );
273
274 #undef TEST_MATCH
275}
276
bd7f096d
VS
277
278void StringTestCase::CaseChanges()
279{
280 wxString s1(_T("Hello!"));
281 wxString s1u(s1);
282 wxString s1l(s1);
283 s1u.MakeUpper();
284 s1l.MakeLower();
285 wxString s2u, s2l;
286 s2u.MakeUpper();
287 s2l.MakeLower();
288
289 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
290 CPPUNIT_ASSERT( s1l == _T("hello!") );
291 CPPUNIT_ASSERT( s2u == wxEmptyString );
292 CPPUNIT_ASSERT( s2l == wxEmptyString );
329b43f6
VZ
293
294#if !wxUSE_UNICODE
295 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
296 if ( locRu.IsOk() )
297 {
298 // try upper casing 8bit strings
b26ed94e
MW
299 const wchar_t capital_ya[] = { 0x42f, 0 },
300 small_ya[] = { 0x44f, 0 };
301
302 wxString sUpper(wxConvLibc.cWC2MB(capital_ya)),
303 sLower(wxConvLibc.cWC2MB(small_ya));
329b43f6
VZ
304
305 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
306 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
307 }
308#endif // !wxUSE_UNICODE
bd7f096d 309}
dcb68102
RN
310
311void StringTestCase::Compare()
312{
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");
0c924034 319
dcb68102
RN
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 );
325
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");
332 s1.insert(1,1,'\0');
333 eq.insert(1,1,'\0');
334 neq1.insert(1,1,'\0');
335 neq2.insert(1,1,'\0');
336 neq3.insert(1,1,'\0');
337 neq4.insert(1,1,'\0');
0c924034 338
dcb68102
RN
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 );
344}
345
346void StringTestCase::CompareNoCase()
347{
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");
0c924034 355
dcb68102
RN
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)
358
359 CPPUNIT_CNCEQ_ASSERT( s1, eq );
360 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
361 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
362
363 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
364 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
365 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
366
367
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");
0c924034 375
dcb68102
RN
376 s1.insert(1,1,'\0');
377 eq.insert(1,1,'\0');
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');
383
384 CPPUNIT_CNCEQ_ASSERT( s1, eq );
385 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
386 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
387
388 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
389 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
390 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
0c924034
WS
391}
392
4f7ee81a
VZ
393void StringTestCase::ToLong()
394{
395 long l;
396 static const struct ToLongData
397 {
398 const wxChar *str;
399 long value;
400 bool ok;
401 } longData[] =
402 {
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 },
409 };
410
411 size_t n;
412 for ( n = 0; n < WXSIZEOF(longData); n++ )
413 {
414 const ToLongData& ld = longData[n];
415 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) );
416 if ( ld.ok )
417 CPPUNIT_ASSERT_EQUAL( ld.value, l );
418 }
419}
420
421void StringTestCase::ToULong()
422{
423 unsigned long ul;
424 static const struct ToULongData
425 {
426 const wxChar *str;
427 unsigned long value;
428 bool ok;
429 } ulongData[] =
430 {
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 },
437 };
438
439 size_t n;
440 for ( n = 0; n < WXSIZEOF(ulongData); n++ )
441 {
442 const ToULongData& uld = ulongData[n];
443 CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) );
444 if ( uld.ok )
445 CPPUNIT_ASSERT_EQUAL( uld.value, ul );
446 }
447}
448
449void StringTestCase::ToDouble()
450{
451 double d;
452 static const struct ToDoubleData
453 {
454 const wxChar *str;
455 double value;
456 bool ok;
457 } doubleData[] =
458 {
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 },
469 };
470
471 // we need to use decimal point, not comma or whatever is its value for the
472 // current locale
473 wxSetlocale(LC_ALL, _T("C"));
474
475 size_t n;
476 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
477 {
478 const ToDoubleData& ld = doubleData[n];
479 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
480 if ( ld.ok )
481 CPPUNIT_ASSERT_EQUAL( ld.value, d );
482 }
483}