]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/strings.cpp
renamed wxstd.po to wxstd.pot so that it uses standard gettext extension for catalog...
[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 );
e6a99197 40 CPPUNIT_TEST( Trim );
1cd53e88 41 CPPUNIT_TEST( Find );
1cd53e88
VS
42 CPPUNIT_TEST( Replace );
43 CPPUNIT_TEST( Match );
bd7f096d 44 CPPUNIT_TEST( CaseChanges );
dcb68102
RN
45 CPPUNIT_TEST( Compare );
46 CPPUNIT_TEST( CompareNoCase );
c4f35063 47 CPPUNIT_TEST( Contains );
4f7ee81a
VZ
48 CPPUNIT_TEST( ToLong );
49 CPPUNIT_TEST( ToULong );
d6718dd1
VZ
50#ifdef wxLongLong_t
51 CPPUNIT_TEST( ToLongLong );
52 CPPUNIT_TEST( ToULongLong );
53#endif // wxLongLong_t
4f7ee81a 54 CPPUNIT_TEST( ToDouble );
db419f1f 55 CPPUNIT_TEST( WriteBuf );
628f87da 56 CPPUNIT_TEST( UTF8Buf );
ef0f1387 57 CPPUNIT_TEST( CStrDataTernaryOperator );
3a69bca1 58 CPPUNIT_TEST( CStrDataOperators );
ef0f1387
VS
59 CPPUNIT_TEST( CStrDataImplicitConversion );
60 CPPUNIT_TEST( ExplicitConversion );
1cd53e88
VS
61 CPPUNIT_TEST_SUITE_END();
62
63 void String();
64 void PChar();
65 void Format();
66 void Constructors();
67 void Extraction();
e6a99197 68 void Trim();
1cd53e88 69 void Find();
1cd53e88
VS
70 void Replace();
71 void Match();
bd7f096d 72 void CaseChanges();
dcb68102
RN
73 void Compare();
74 void CompareNoCase();
c4f35063 75 void Contains();
4f7ee81a
VZ
76 void ToLong();
77 void ToULong();
d6718dd1
VZ
78#ifdef wxLongLong_t
79 void ToLongLong();
80 void ToULongLong();
81#endif // wxLongLong_t
4f7ee81a 82 void ToDouble();
db419f1f 83 void WriteBuf();
628f87da 84 void UTF8Buf();
ef0f1387
VS
85 void CStrDataTernaryOperator();
86 void DoCStrDataTernaryOperator(bool cond);
3a69bca1 87 void CStrDataOperators();
ef0f1387
VS
88 void CStrDataImplicitConversion();
89 void ExplicitConversion();
1cd53e88
VS
90
91 DECLARE_NO_COPY_CLASS(StringTestCase)
92};
93
94// register in the unnamed registry so that these tests are run by default
95CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
96
97// also include in it's own registry so that these tests can be run alone
98CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
99
100StringTestCase::StringTestCase()
101{
102}
103
104void StringTestCase::String()
105{
106 wxString a, b, c;
107
108 a.reserve (128);
109 b.reserve (128);
110 c.reserve (128);
111
112 for (int i = 0; i < 2; ++i)
113 {
114 a = _T("Hello");
115 b = _T(" world");
116 c = _T("! How'ya doin'?");
117 a += b;
118 a += c;
119 c = _T("Hello world! What's up?");
120 CPPUNIT_ASSERT( c != a );
121 }
122}
123
124void StringTestCase::PChar()
125{
126 wxChar a [128];
127 wxChar b [128];
128 wxChar c [128];
129
130 for (int i = 0; i < 2; ++i)
131 {
132 wxStrcpy (a, _T("Hello"));
133 wxStrcpy (b, _T(" world"));
134 wxStrcpy (c, _T("! How'ya doin'?"));
135 wxStrcat (a, b);
136 wxStrcat (a, c);
137 wxStrcpy (c, _T("Hello world! What's up?"));
138 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
139 }
140}
141
142void StringTestCase::Format()
143{
144 wxString s1,s2;
145 s1.Printf(_T("%03d"), 18);
146 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
147 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
148 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
b03cd7e6
VZ
149
150 static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
151 for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
152 {
153 const size_t len = lengths[n];
154
155 wxString s(_T('Z'), len);
156 CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length());
157 }
1cd53e88
VS
158}
159
160void StringTestCase::Constructors()
161{
162 #define TEST_CTOR(args, res) \
163 { \
164 wxString s args ; \
165 CPPUNIT_ASSERT( s == res ); \
166 }
167
168 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
169 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
170 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
171
172 static const wxChar *s = _T("?really!");
173 const wxChar *start = wxStrchr(s, _T('r'));
174 const wxChar *end = wxStrchr(s, _T('!'));
175 TEST_CTOR((start, end), _T("really"));
7652a41d
VS
176
177 // test if creating string from NULL C pointer works:
178 TEST_CTOR(((char*)NULL), "");
1cd53e88
VS
179}
180
265d5cce 181
1cd53e88
VS
182void StringTestCase::Extraction()
183{
184 wxString s(_T("Hello, world!"));
185
186 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
187 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
188 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
189 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
190 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
191 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
192 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
193
478cbb08
VS
194#if wxUSE_UNICODE
195 static const char *germanUTF8 = "Oberfl\303\244che";
196 wxString strUnicode(wxString::FromUTF8(germanUTF8));
197
198 CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode );
199 CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" );
200#endif // wxUSE_UNICODE
201
1cd53e88
VS
202 wxString rest;
203
3affcd07
VZ
204 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
205 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
206 if ( result ) \
c2c87924 207 WX_ASSERT_STR_EQUAL(correct_rest, rest)
1cd53e88
VS
208
209 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
210 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
211 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
212 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
213 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
214 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
215 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
216
217 #undef TEST_STARTS_WITH
3affcd07 218
c8f313b2
VZ
219 rest = "Hello world";
220 CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) );
221 WX_ASSERT_STR_EQUAL("world", rest);
222
3affcd07
VZ
223 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
224 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
225 if ( result ) \
c2c87924 226 WX_ASSERT_STR_EQUAL(correct_rest, rest)
3affcd07
VZ
227
228 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
229 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
230 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
231 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
232 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
233 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
234 TEST_ENDS_WITH( _T("?"), _T(""), false );
235 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
236 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
237
238 #undef TEST_ENDS_WITH
1cd53e88
VS
239}
240
e6a99197
WS
241void StringTestCase::Trim()
242{
243 #define TEST_TRIM( str , dir , result ) \
244 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
245
246 TEST_TRIM( _T(" Test "), true, _T(" Test") );
247 TEST_TRIM( _T(" "), true, _T("") );
248 TEST_TRIM( _T(" "), true, _T("") );
249 TEST_TRIM( _T(""), true, _T("") );
250
251 TEST_TRIM( _T(" Test "), false, _T("Test ") );
252 TEST_TRIM( _T(" "), false, _T("") );
253 TEST_TRIM( _T(" "), false, _T("") );
254 TEST_TRIM( _T(""), false, _T("") );
255
256 #undef TEST_TRIM
257}
258
1cd53e88
VS
259void StringTestCase::Find()
260{
261 #define TEST_FIND( str , start , result ) \
262 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
263
264 TEST_FIND( _T("Well, hello world"), 0, 1 );
265 TEST_FIND( _T("Well, hello world"), 6, 7 );
266 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
267
268 #undef TEST_FIND
269}
270
1cd53e88
VS
271void StringTestCase::Replace()
272{
273 #define TEST_REPLACE( original , pos , len , replacement , result ) \
274 { \
275 wxString s = original; \
276 s.replace( pos , len , replacement ); \
c2c87924 277 WX_ASSERT_STR_EQUAL( result, s ); \
1cd53e88
VS
278 }
279
280 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
281 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
282 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
283 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
284 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
285
7634e443
RN
286
287 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
288 { \
289 wxString s(o,olen); \
290 s.replace( pos , len , replacement ); \
291 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
292 }
293
294 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
295 _T("null\0dhar"), 9 );
296
297 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
298 { \
299 wxString s(o,olen); \
300 s.Replace( olds, news, all ); \
301 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
302 }
303
2df0258e
RN
304 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
305 _T("null\0dehar"), 10 );
7634e443 306
4629f07e
RN
307 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
308 _T("null\0char"), 9 );
309
7634e443
RN
310 #undef TEST_WXREPLACE
311 #undef TEST_NULLCHARREPLACE
1cd53e88
VS
312 #undef TEST_REPLACE
313}
314
315void StringTestCase::Match()
316{
317 #define TEST_MATCH( s1 , s2 , result ) \
318 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
319
320 TEST_MATCH( _T("foobar"), _T("foo*"), true );
321 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
322 TEST_MATCH( _T("foobar"), _T("*bar"), true );
323 TEST_MATCH( _T("foobar"), _T("??????"), true );
324 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
325 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
326 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
327 TEST_MATCH( _T("foobar"), _T("*foo"), false );
328 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
329 TEST_MATCH( _T(""), _T("*"), true );
330 TEST_MATCH( _T(""), _T("?"), false );
331
332 #undef TEST_MATCH
333}
334
bd7f096d
VS
335
336void StringTestCase::CaseChanges()
337{
338 wxString s1(_T("Hello!"));
339 wxString s1u(s1);
340 wxString s1l(s1);
341 s1u.MakeUpper();
342 s1l.MakeLower();
343 wxString s2u, s2l;
344 s2u.MakeUpper();
345 s2l.MakeLower();
346
347 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
348 CPPUNIT_ASSERT( s1l == _T("hello!") );
349 CPPUNIT_ASSERT( s2u == wxEmptyString );
350 CPPUNIT_ASSERT( s2l == wxEmptyString );
329b43f6
VZ
351
352#if !wxUSE_UNICODE
353 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
354 if ( locRu.IsOk() )
355 {
356 // try upper casing 8bit strings
b26ed94e
MW
357 const wchar_t capital_ya[] = { 0x42f, 0 },
358 small_ya[] = { 0x44f, 0 };
359
360 wxString sUpper(wxConvLibc.cWC2MB(capital_ya)),
361 sLower(wxConvLibc.cWC2MB(small_ya));
329b43f6
VZ
362
363 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
364 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
365 }
366#endif // !wxUSE_UNICODE
bd7f096d 367}
dcb68102
RN
368
369void StringTestCase::Compare()
370{
371 wxString s1 = wxT("AHH");
372 wxString eq = wxT("AHH");
373 wxString neq1 = wxT("HAH");
374 wxString neq2 = wxT("AH");
375 wxString neq3 = wxT("AHHH");
376 wxString neq4 = wxT("AhH");
0c924034 377
dcb68102
RN
378 CPPUNIT_ASSERT( s1 == eq );
379 CPPUNIT_ASSERT( s1 != neq1 );
380 CPPUNIT_ASSERT( s1 != neq2 );
381 CPPUNIT_ASSERT( s1 != neq3 );
382 CPPUNIT_ASSERT( s1 != neq4 );
383
d7330233
VS
384 CPPUNIT_ASSERT( s1 == wxT("AHH") );
385 CPPUNIT_ASSERT( s1 != wxT("no") );
386 CPPUNIT_ASSERT( s1 < wxT("AZ") );
387 CPPUNIT_ASSERT( s1 <= wxT("AZ") );
388 CPPUNIT_ASSERT( s1 <= wxT("AHH") );
389 CPPUNIT_ASSERT( s1 > wxT("AA") );
390 CPPUNIT_ASSERT( s1 >= wxT("AA") );
391 CPPUNIT_ASSERT( s1 >= wxT("AHH") );
392
393 // test comparison with C strings in Unicode build (must work in ANSI as
394 // well, of course):
395 CPPUNIT_ASSERT( s1 == "AHH" );
396 CPPUNIT_ASSERT( s1 != "no" );
397 CPPUNIT_ASSERT( s1 < "AZ" );
398 CPPUNIT_ASSERT( s1 <= "AZ" );
399 CPPUNIT_ASSERT( s1 <= "AHH" );
400 CPPUNIT_ASSERT( s1 > "AA" );
401 CPPUNIT_ASSERT( s1 >= "AA" );
402 CPPUNIT_ASSERT( s1 >= "AHH" );
403
dcb68102
RN
404// wxString _s1 = wxT("A\0HH");
405// wxString _eq = wxT("A\0HH");
406// wxString _neq1 = wxT("H\0AH");
407// wxString _neq2 = wxT("A\0H");
408// wxString _neq3 = wxT("A\0HHH");
409// wxString _neq4 = wxT("A\0hH");
410 s1.insert(1,1,'\0');
411 eq.insert(1,1,'\0');
412 neq1.insert(1,1,'\0');
413 neq2.insert(1,1,'\0');
414 neq3.insert(1,1,'\0');
415 neq4.insert(1,1,'\0');
0c924034 416
dcb68102
RN
417 CPPUNIT_ASSERT( s1 == eq );
418 CPPUNIT_ASSERT( s1 != neq1 );
419 CPPUNIT_ASSERT( s1 != neq2 );
420 CPPUNIT_ASSERT( s1 != neq3 );
421 CPPUNIT_ASSERT( s1 != neq4 );
422}
423
424void StringTestCase::CompareNoCase()
425{
426 wxString s1 = wxT("AHH");
427 wxString eq = wxT("AHH");
428 wxString eq2 = wxT("AhH");
429 wxString eq3 = wxT("ahh");
430 wxString neq = wxT("HAH");
431 wxString neq2 = wxT("AH");
432 wxString neq3 = wxT("AHHH");
0c924034 433
dcb68102
RN
434 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
435 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
436
437 CPPUNIT_CNCEQ_ASSERT( s1, eq );
438 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
439 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
440
441 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
442 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
443 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
444
445
446// wxString _s1 = wxT("A\0HH");
447// wxString _eq = wxT("A\0HH");
448// wxString _eq2 = wxT("A\0hH");
449// wxString _eq3 = wxT("a\0hh");
450// wxString _neq = wxT("H\0AH");
451// wxString _neq2 = wxT("A\0H");
452// wxString _neq3 = wxT("A\0HHH");
0c924034 453
dcb68102
RN
454 s1.insert(1,1,'\0');
455 eq.insert(1,1,'\0');
456 eq2.insert(1,1,'\0');
457 eq3.insert(1,1,'\0');
458 neq.insert(1,1,'\0');
459 neq2.insert(1,1,'\0');
460 neq3.insert(1,1,'\0');
461
462 CPPUNIT_CNCEQ_ASSERT( s1, eq );
463 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
464 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
465
466 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
467 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
468 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
0c924034
WS
469}
470
c4f35063
VZ
471void StringTestCase::Contains()
472{
473 static const struct ContainsData
474 {
475 const wxChar *hay;
476 const wxChar *needle;
477 bool contains;
478 } containsData[] =
479 {
480 { _T(""), _T(""), true },
481 { _T(""), _T("foo"), false },
482 { _T("foo"), _T(""), true },
483 { _T("foo"), _T("f"), true },
484 { _T("foo"), _T("o"), true },
485 { _T("foo"), _T("oo"), true },
486 { _T("foo"), _T("ooo"), false },
487 { _T("foo"), _T("oooo"), false },
488 { _T("foo"), _T("fooo"), false },
489 };
490
491 for ( size_t n = 0; n < WXSIZEOF(containsData); n++ )
492 {
493 const ContainsData& cd = containsData[n];
494 CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) );
495 }
496}
497
d6718dd1
VZ
498// flags used in ToLongData.flags
499enum
4f7ee81a 500{
d6718dd1
VZ
501 Number_Ok = 0,
502 Number_Invalid = 1,
503 Number_Unsigned = 2, // if not specified, works for signed conversion
504 Number_Signed = 4, // if not specified, works for unsigned
505 Number_LongLong = 8, // only for long long tests
506 Number_Long = 16, // only for long tests
507};
508
509static const struct ToLongData
510{
511 const wxChar *str;
d6718dd1 512#ifdef wxLongLong_t
efc57671
VZ
513 wxLongLong_t value;
514#else
515 long value;
d6718dd1 516#endif // wxLongLong_t
d6718dd1
VZ
517 int flags;
518
efc57671
VZ
519 long LValue() const { return value; }
520 unsigned long ULValue() const { return value; }
521#ifdef wxLongLong_t
522 wxLongLong_t LLValue() const { return value; }
523 wxULongLong_t ULLValue() const { return (wxULongLong_t)value; }
524#endif // wxLongLong_t
525
d6718dd1
VZ
526 bool IsOk() const { return !(flags & Number_Invalid); }
527} longData[] =
528{
529 { _T("1"), 1, Number_Ok },
530 { _T("0"), 0, Number_Ok },
531 { _T("a"), 0, Number_Invalid },
532 { _T("12345"), 12345, Number_Ok },
533 { _T("--1"), 0, Number_Invalid },
534
535 { _T("-1"), -1, Number_Signed | Number_Long },
536 // this is surprizing but consistent with strtoul() behaviour
537 { _T("-1"), ULONG_MAX, Number_Unsigned | Number_Long },
538
539 // this must overflow, even with 64 bit long
540 { _T("922337203685477580711"), 0, Number_Invalid },
541
542#ifdef wxLongLong_t
543 { _T("2147483648"), wxLL(2147483648), Number_LongLong },
544 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed },
545 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong |
546 Number_Unsigned },
547#endif // wxLongLong_t
548};
549
550void StringTestCase::ToLong()
551{
c4f35063
VZ
552 long l;
553 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a
VZ
554 {
555 const ToLongData& ld = longData[n];
d6718dd1
VZ
556
557 if ( ld.flags & (Number_LongLong | Number_Unsigned) )
558 continue;
559
560 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) );
561 if ( ld.IsOk() )
efc57671 562 CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
4f7ee81a
VZ
563 }
564}
565
566void StringTestCase::ToULong()
567{
568 unsigned long ul;
d6718dd1 569 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 570 {
d6718dd1
VZ
571 const ToLongData& ld = longData[n];
572
573 if ( ld.flags & (Number_LongLong | Number_Signed) )
574 continue;
575
576 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) );
577 if ( ld.IsOk() )
efc57671 578 CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
d6718dd1
VZ
579 }
580}
581
582#ifdef wxLongLong_t
583
584void StringTestCase::ToLongLong()
585{
586 wxLongLong_t l;
587 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 588 {
d6718dd1 589 const ToLongData& ld = longData[n];
4f7ee81a 590
d6718dd1
VZ
591 if ( ld.flags & (Number_Long | Number_Unsigned) )
592 continue;
593
594 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) );
595 if ( ld.IsOk() )
efc57671 596 CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l );
d6718dd1
VZ
597 }
598}
599
600void StringTestCase::ToULongLong()
601{
602 wxULongLong_t ul;
603 for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
4f7ee81a 604 {
d6718dd1
VZ
605 const ToLongData& ld = longData[n];
606
607 if ( ld.flags & (Number_Long | Number_Signed) )
608 continue;
609
610 CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) );
611 if ( ld.IsOk() )
efc57671 612 CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul );
4f7ee81a
VZ
613 }
614}
615
d6718dd1
VZ
616#endif // wxLongLong_t
617
4f7ee81a
VZ
618void StringTestCase::ToDouble()
619{
620 double d;
621 static const struct ToDoubleData
622 {
623 const wxChar *str;
624 double value;
625 bool ok;
626 } doubleData[] =
627 {
628 { _T("1"), 1, true },
629 { _T("1.23"), 1.23, true },
630 { _T(".1"), .1, true },
631 { _T("1."), 1, true },
632 { _T("1.."), 0, false },
633 { _T("0"), 0, true },
634 { _T("a"), 0, false },
635 { _T("12345"), 12345, true },
636 { _T("-1"), -1, true },
637 { _T("--1"), 0, false },
638 };
639
640 // we need to use decimal point, not comma or whatever is its value for the
641 // current locale
10de049c 642 wxSetlocale(LC_ALL, "C");
4f7ee81a
VZ
643
644 size_t n;
645 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
646 {
647 const ToDoubleData& ld = doubleData[n];
648 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
649 if ( ld.ok )
650 CPPUNIT_ASSERT_EQUAL( ld.value, d );
651 }
652}
db419f1f
VZ
653
654void StringTestCase::WriteBuf()
655{
656 wxString s;
657 wxStrcpy(wxStringBuffer(s, 10), _T("foo"));
658
c9f78968
VS
659 CPPUNIT_ASSERT(s[0u] == _T('f') );
660 CPPUNIT_ASSERT(_T('f') == s[0u]);
661 CPPUNIT_ASSERT(_T('o') == s[1]);
662 CPPUNIT_ASSERT(_T('o') == s[2]);
c2c87924 663 WX_ASSERT_SIZET_EQUAL(3, s.length());
db419f1f 664
d8a4b666 665
db419f1f 666 {
d8a4b666
VS
667 wxStringBufferLength buf(s, 10);
668 wxStrcpy(buf, _T("barrbaz"));
669 buf.SetLength(4);
670 }
db419f1f 671
c9f78968
VS
672 CPPUNIT_ASSERT(_T('b') == s[0u]);
673 CPPUNIT_ASSERT(_T('a') == s[1]);
674 CPPUNIT_ASSERT(_T('r') == s[2]);
675 CPPUNIT_ASSERT(_T('r') == s[3]);
c2c87924 676 WX_ASSERT_SIZET_EQUAL(4, s.length());
db419f1f 677
d8a4b666 678 CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) );
db419f1f
VZ
679}
680
628f87da
VS
681void StringTestCase::UTF8Buf()
682{
683#if wxUSE_UNICODE
684 // "czech" in Czech ("cestina"):
685 static const char *textUTF8 = "\304\215e\305\241tina";
686 static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
687
688 wxString s;
689 wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8);
690 CPPUNIT_ASSERT(s == textUTF16);
691
692 {
693 wxUTF8StringBufferLength buf(s, 20);
694 wxStrcpy(buf, textUTF8);
695 buf.SetLength(5);
696 }
697 CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3));
698#endif // wxUSE_UNICODE
699}
700
4ca056ea
VS
701
702
ef0f1387 703void StringTestCase::CStrDataTernaryOperator()
4ca056ea 704{
ef0f1387
VS
705 DoCStrDataTernaryOperator(true);
706 DoCStrDataTernaryOperator(false);
4ca056ea
VS
707}
708
709template<typename T> bool CheckStr(const wxString& expected, T s)
710{
711 return expected == wxString(s);
712}
713
ef0f1387 714void StringTestCase::DoCStrDataTernaryOperator(bool cond)
4ca056ea
VS
715{
716 // test compilation of wxCStrData when used with operator?: (the asserts
717 // are not very important, we're testing if the code compiles at all):
718
719 wxString s("foo");
4ca056ea 720
abc505b4 721 const wchar_t *wcStr = L"foo";
4ca056ea 722 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) );
9ffb659a 723 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) );
4ca056ea 724 CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) );
9ffb659a 725 CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) );
11aac4ba 726
9b59b90c 727 const char *mbStr = "foo";
4ca056ea
VS
728 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) );
729 CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) );
730 CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) );
731 CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) );
92258cc1
VS
732
733 wxString empty("");
734 CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) );
735 CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) );
4ca056ea 736}
ef0f1387 737
3a69bca1
VS
738void StringTestCase::CStrDataOperators()
739{
740 wxString s("hello");
741
742 CPPUNIT_ASSERT( s.c_str()[0] == 'h' );
743 CPPUNIT_ASSERT( s.c_str()[1] == 'e' );
744 CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
745
746 CPPUNIT_ASSERT( *s.c_str() == 'h' );
747 CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' );
748 CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
749}
750
ef0f1387
VS
751bool CheckStrChar(const wxString& expected, char *s)
752 { return CheckStr(expected, s); }
753bool CheckStrWChar(const wxString& expected, wchar_t *s)
754 { return CheckStr(expected, s); }
755bool CheckStrConstChar(const wxString& expected, const char *s)
756 { return CheckStr(expected, s); }
757bool CheckStrConstWChar(const wxString& expected, const wchar_t *s)
758 { return CheckStr(expected, s); }
759
760void StringTestCase::CStrDataImplicitConversion()
761{
762 wxString s("foo");
763
ef0f1387 764 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) );
ef0f1387 765 CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) );
7978bc72
VS
766
767 // implicit conversion of wxString is not available in STL build
768#if !wxUSE_STL
769 CPPUNIT_ASSERT( CheckStrConstWChar(s, s) );
ef0f1387 770 CPPUNIT_ASSERT( CheckStrConstChar(s, s) );
7978bc72 771#endif
ef0f1387
VS
772}
773
774void StringTestCase::ExplicitConversion()
775{
776 wxString s("foo");
777
778 CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) );
779 CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) );
780 CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) );
781
782 CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) );
783 CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) );
784 CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) );
785}