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