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