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