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