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