added ToLong/ULong/Double() tests
[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 #include "wx/tokenzr.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class StringTestCase : public CppUnit::TestCase
31 {
32 public:
33 StringTestCase();
34
35 private:
36 CPPUNIT_TEST_SUITE( StringTestCase );
37 CPPUNIT_TEST( String );
38 CPPUNIT_TEST( PChar );
39 CPPUNIT_TEST( Format );
40 CPPUNIT_TEST( Constructors );
41 #if wxUSE_WCHAR_T
42 CPPUNIT_TEST( ConstructorsWithConversion );
43 CPPUNIT_TEST( Conversion );
44 CPPUNIT_TEST( ConversionUTF7 );
45 CPPUNIT_TEST( ConversionUTF8 );
46 #endif // wxUSE_WCHAR_T
47 CPPUNIT_TEST( Extraction );
48 CPPUNIT_TEST( Find );
49 CPPUNIT_TEST( Tokenizer );
50 CPPUNIT_TEST( TokenizerGetPosition );
51 CPPUNIT_TEST( Replace );
52 CPPUNIT_TEST( Match );
53 CPPUNIT_TEST( CaseChanges );
54 CPPUNIT_TEST( Compare );
55 CPPUNIT_TEST( CompareNoCase );
56 CPPUNIT_TEST( ToLong );
57 CPPUNIT_TEST( ToULong );
58 CPPUNIT_TEST( ToDouble );
59 CPPUNIT_TEST_SUITE_END();
60
61 void String();
62 void PChar();
63 void Format();
64 void Constructors();
65 #if wxUSE_WCHAR_T
66 void ConstructorsWithConversion();
67 void Conversion();
68 void ConversionUTF7();
69 void ConversionUTF8();
70 #endif // wxUSE_WCHAR_T
71 void Extraction();
72 void Find();
73 void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
74 void Tokenizer();
75 void TokenizerGetPosition();
76 void Replace();
77 void Match();
78 void CaseChanges();
79 void Compare();
80 void CompareNoCase();
81 void ToLong();
82 void ToULong();
83 void ToDouble();
84
85 #if wxUSE_WCHAR_T
86 // test if converting s using the given encoding gives ws and vice versa
87 //
88 // if either of the first 2 arguments is NULL, the conversion is supposed
89 // to fail
90 void DoTestConversion(const char *s, const wchar_t *w, wxCSConv& conv);
91 #endif // wxUSE_WCHAR_T
92
93 DECLARE_NO_COPY_CLASS(StringTestCase)
94 };
95
96 // register in the unnamed registry so that these tests are run by default
97 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
98
99 // also include in it's own registry so that these tests can be run alone
100 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
101
102 StringTestCase::StringTestCase()
103 {
104 }
105
106 void StringTestCase::String()
107 {
108 wxString a, b, c;
109
110 a.reserve (128);
111 b.reserve (128);
112 c.reserve (128);
113
114 for (int i = 0; i < 2; ++i)
115 {
116 a = _T("Hello");
117 b = _T(" world");
118 c = _T("! How'ya doin'?");
119 a += b;
120 a += c;
121 c = _T("Hello world! What's up?");
122 CPPUNIT_ASSERT( c != a );
123 }
124 }
125
126 void StringTestCase::PChar()
127 {
128 wxChar a [128];
129 wxChar b [128];
130 wxChar c [128];
131
132 for (int i = 0; i < 2; ++i)
133 {
134 wxStrcpy (a, _T("Hello"));
135 wxStrcpy (b, _T(" world"));
136 wxStrcpy (c, _T("! How'ya doin'?"));
137 wxStrcat (a, b);
138 wxStrcat (a, c);
139 wxStrcpy (c, _T("Hello world! What's up?"));
140 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
141 }
142 }
143
144 void StringTestCase::Format()
145 {
146 wxString s1,s2;
147 s1.Printf(_T("%03d"), 18);
148 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
149 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
150 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
151 }
152
153 void StringTestCase::Constructors()
154 {
155 #define TEST_CTOR(args, res) \
156 { \
157 wxString s args ; \
158 CPPUNIT_ASSERT( s == res ); \
159 }
160
161 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
162 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
163 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
164
165 static const wxChar *s = _T("?really!");
166 const wxChar *start = wxStrchr(s, _T('r'));
167 const wxChar *end = wxStrchr(s, _T('!'));
168 TEST_CTOR((start, end), _T("really"));
169 }
170
171 #if wxUSE_WCHAR_T
172 void StringTestCase::ConstructorsWithConversion()
173 {
174 // the string "Déjà" in UTF-8 and wchar_t:
175 const unsigned char utf8Buf[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
176 const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
177 const unsigned char utf8subBuf[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
178 const char *utf8 = (char *)utf8Buf;
179 const char *utf8sub = (char *)utf8subBuf;
180
181 wxString s1(utf8, wxConvUTF8);
182 wxString s2(wchar, wxConvUTF8);
183
184 #if wxUSE_UNICODE
185 CPPUNIT_ASSERT( s1 == wchar );
186 CPPUNIT_ASSERT( s2 == wchar );
187 #else
188 CPPUNIT_ASSERT( s1 == utf8 );
189 CPPUNIT_ASSERT( s2 == utf8 );
190 #endif
191
192 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
193 wxString s3(utf8, wxConvUTF8, 4);
194 wxString s4(wchar, wxConvUTF8, 3);
195
196 CPPUNIT_ASSERT( s3 == sub );
197 CPPUNIT_ASSERT( s4 == sub );
198
199 #if wxUSE_UNICODE
200 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
201 wxConvUTF8) == wxT("") ); //should stop at pos 35
202 #endif
203 }
204
205 void StringTestCase::Conversion()
206 {
207 #if wxUSE_UNICODE
208 wxString szTheString(L"The\0String", wxConvLibc, 10);
209 wxCharBuffer theBuffer = szTheString.mb_str();
210
211 CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String", 11) == 0 );
212
213 wxString szTheString2("The\0String", wxConvLocal, 10);
214 CPPUNIT_ASSERT( szTheString2.length() == 11 );
215 CPPUNIT_ASSERT( wxTmemcmp(szTheString2.c_str(), L"The\0String", 11) == 0 );
216 #else
217 wxString szTheString(wxT("TheString"));
218 szTheString.insert(3, 1, '\0');
219 wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
220
221 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
222
223 wxString szLocalTheString(wxT("TheString"));
224 szLocalTheString.insert(3, 1, '\0');
225 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
226
227 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
228 #endif
229 }
230
231 // in case wcscmp is missing
232 //
233 static int wx_wcscmp(const wchar_t *s1, const wchar_t *s2)
234 {
235 for (;;) {
236 if (*s1 != *s2)
237 return *s1 - *s2;
238 if (*s1 == 0)
239 break;
240 s1++;
241 s2++;
242 }
243 return 0;
244 }
245
246 void
247 StringTestCase::DoTestConversion(const char *s,
248 const wchar_t *ws,
249 wxCSConv& conv)
250 {
251 #if wxUSE_UNICODE
252 if ( ws )
253 {
254 wxCharBuffer buf(wxString(ws).mb_str(conv));
255
256 CPPUNIT_ASSERT( strcmp(buf, s) == 0 );
257 }
258 #else // wxUSE_UNICODE
259 if ( s )
260 {
261 wxWCharBuffer wbuf(wxString(s).wc_str(conv));
262
263 if ( ws )
264 CPPUNIT_ASSERT( wx_wcscmp(wbuf, ws) == 0 );
265 else
266 CPPUNIT_ASSERT( !*wbuf );
267 }
268 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
269 }
270
271 struct StringConversionData
272 {
273 const char *str;
274 const wchar_t *wcs;
275 };
276
277 void StringTestCase::ConversionUTF7()
278 {
279 static const StringConversionData utf7data[] =
280 {
281 { "+-", L"+" },
282 { "+--", L"+-" },
283 //\u isn't recognized on MSVC 6
284 #if !defined(_MSC_VER)
285 #if !defined(__GNUC__) || (__GNUC__ >= 3)
286 { "+AKM-", L"\u00a3" },
287 #endif
288 #endif
289 // Windows accepts invalid UTF-7 strings and so does our UTF-7
290 // conversion code -- this is wrong IMO but the way it is for now
291 //
292 // notice that converting "+" still behaves as expected because the
293 // result is just an empty string, i.e. the same as if there were an
294 // error, but converting "a+" results in "a" while it really should
295 // fail
296 { "+", NULL },
297 { "a+", L"a" },
298 };
299
300 wxCSConv conv(_T("utf-7"));
301 for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
302 {
303 const StringConversionData& d = utf7data[n];
304 DoTestConversion(d.str, d.wcs, conv);
305 }
306 }
307
308 void StringTestCase::ConversionUTF8()
309 {
310 static const StringConversionData utf8data[] =
311 {
312 //\u isn't recognized on MSVC 6
313 #if !defined(_MSC_VER)
314 #if !defined(__GNUC__) || (__GNUC__ >= 3)
315 { "\xc2\xa3", L"\u00a3" },
316 #endif
317 #endif
318 { "\xc2", NULL },
319 };
320
321 wxCSConv conv(_T("utf-8"));
322 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
323 {
324 const StringConversionData& d = utf8data[n];
325 DoTestConversion(d.str, d.wcs, conv);
326 }
327 }
328
329 #endif // wxUSE_WCHAR_T
330
331
332 void StringTestCase::Extraction()
333 {
334 wxString s(_T("Hello, world!"));
335
336 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
337 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
338 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
339 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
340 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
341 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
342 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
343
344 wxString rest;
345
346 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
347 CPPUNIT_ASSERT( \
348 ( s.StartsWith( prefix, &rest ) == result ) && \
349 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
350 )
351
352 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
353 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
354 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
355 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
356 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
357 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
358 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
359
360 #undef TEST_STARTS_WITH
361 }
362
363 void StringTestCase::Find()
364 {
365 #define TEST_FIND( str , start , result ) \
366 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
367
368 TEST_FIND( _T("Well, hello world"), 0, 1 );
369 TEST_FIND( _T("Well, hello world"), 6, 7 );
370 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
371
372 #undef TEST_FIND
373 }
374
375 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
376 {
377 wxStringTokenizer tkz( str, delims, mode);
378 CPPUNIT_ASSERT( tkz.CountTokens() == count );
379
380 wxChar *buf, *s = NULL, *last;
381
382 if ( tkz.GetMode() == wxTOKEN_STRTOK )
383 {
384 buf = new wxChar[wxStrlen(str) + 1];
385 wxStrcpy(buf, str);
386 s = wxStrtok(buf, delims, &last);
387 }
388 else
389 {
390 buf = NULL;
391 }
392
393 size_t count2 = 0;
394 while ( tkz.HasMoreTokens() )
395 {
396 wxString token = tkz.GetNextToken();
397 if ( buf )
398 {
399 CPPUNIT_ASSERT( token == s );
400 s = wxStrtok(NULL, delims, &last);
401 }
402 count2++;
403 }
404
405 CPPUNIT_ASSERT( count2 == count );
406 if ( buf )
407 {
408 delete [] buf;
409 }
410 }
411
412 void StringTestCase::Tokenizer()
413 {
414 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT );
415 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT );
416 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT );
417 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT );
418 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT );
419 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT );
420 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL );
421 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT );
422 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT );
423 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY );
424 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL );
425 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT );
426 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS );
427 }
428
429 // call this with the string to tokenize, delimeters to use and the expected
430 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
431 // terminate positions with 0
432 static void DoTokenizerGetPosition(const wxChar *s,
433 const wxChar *delims, int pos, ...)
434 {
435 wxStringTokenizer tkz(s, delims);
436
437 CPPUNIT_ASSERT( tkz.GetPosition() == 0 );
438
439 va_list ap;
440 va_start(ap, pos);
441
442 for ( ;; )
443 {
444 if ( !pos )
445 {
446 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
447 break;
448 }
449
450 tkz.GetNextToken();
451
452 CPPUNIT_ASSERT( tkz.GetPosition() == (size_t)pos );
453
454 pos = va_arg(ap, int);
455 }
456
457 va_end(ap);
458 }
459
460 void StringTestCase::TokenizerGetPosition()
461 {
462 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
463 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
464 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
465 }
466
467 void StringTestCase::Replace()
468 {
469 #define TEST_REPLACE( original , pos , len , replacement , result ) \
470 { \
471 wxString s = original; \
472 s.replace( pos , len , replacement ); \
473 CPPUNIT_ASSERT( s == result ); \
474 }
475
476 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
477 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
478 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
479 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
480 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
481
482
483 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
484 { \
485 wxString s(o,olen); \
486 s.replace( pos , len , replacement ); \
487 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
488 }
489
490 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
491 _T("null\0dhar"), 9 );
492
493 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
494 { \
495 wxString s(o,olen); \
496 s.Replace( olds, news, all ); \
497 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
498 }
499
500 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
501 _T("null\0dehar"), 10 );
502
503 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
504 _T("null\0char"), 9 );
505
506 #undef TEST_WXREPLACE
507 #undef TEST_NULLCHARREPLACE
508 #undef TEST_REPLACE
509 }
510
511 void StringTestCase::Match()
512 {
513 #define TEST_MATCH( s1 , s2 , result ) \
514 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
515
516 TEST_MATCH( _T("foobar"), _T("foo*"), true );
517 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
518 TEST_MATCH( _T("foobar"), _T("*bar"), true );
519 TEST_MATCH( _T("foobar"), _T("??????"), true );
520 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
521 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
522 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
523 TEST_MATCH( _T("foobar"), _T("*foo"), false );
524 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
525 TEST_MATCH( _T(""), _T("*"), true );
526 TEST_MATCH( _T(""), _T("?"), false );
527
528 #undef TEST_MATCH
529 }
530
531
532 void StringTestCase::CaseChanges()
533 {
534 wxString s1(_T("Hello!"));
535 wxString s1u(s1);
536 wxString s1l(s1);
537 s1u.MakeUpper();
538 s1l.MakeLower();
539 wxString s2u, s2l;
540 s2u.MakeUpper();
541 s2l.MakeLower();
542
543 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
544 CPPUNIT_ASSERT( s1l == _T("hello!") );
545 CPPUNIT_ASSERT( s2u == wxEmptyString );
546 CPPUNIT_ASSERT( s2l == wxEmptyString );
547
548 #if !wxUSE_UNICODE
549 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
550 if ( locRu.IsOk() )
551 {
552 // try upper casing 8bit strings
553 wxString sUpper("\xdf"),
554 sLower("\xff");
555
556 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
557 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
558 }
559 #endif // !wxUSE_UNICODE
560 }
561
562 void StringTestCase::Compare()
563 {
564 wxString s1 = wxT("AHH");
565 wxString eq = wxT("AHH");
566 wxString neq1 = wxT("HAH");
567 wxString neq2 = wxT("AH");
568 wxString neq3 = wxT("AHHH");
569 wxString neq4 = wxT("AhH");
570
571 CPPUNIT_ASSERT( s1 == eq );
572 CPPUNIT_ASSERT( s1 != neq1 );
573 CPPUNIT_ASSERT( s1 != neq2 );
574 CPPUNIT_ASSERT( s1 != neq3 );
575 CPPUNIT_ASSERT( s1 != neq4 );
576
577 // wxString _s1 = wxT("A\0HH");
578 // wxString _eq = wxT("A\0HH");
579 // wxString _neq1 = wxT("H\0AH");
580 // wxString _neq2 = wxT("A\0H");
581 // wxString _neq3 = wxT("A\0HHH");
582 // wxString _neq4 = wxT("A\0hH");
583 s1.insert(1,1,'\0');
584 eq.insert(1,1,'\0');
585 neq1.insert(1,1,'\0');
586 neq2.insert(1,1,'\0');
587 neq3.insert(1,1,'\0');
588 neq4.insert(1,1,'\0');
589
590 CPPUNIT_ASSERT( s1 == eq );
591 CPPUNIT_ASSERT( s1 != neq1 );
592 CPPUNIT_ASSERT( s1 != neq2 );
593 CPPUNIT_ASSERT( s1 != neq3 );
594 CPPUNIT_ASSERT( s1 != neq4 );
595 }
596
597 void StringTestCase::CompareNoCase()
598 {
599 wxString s1 = wxT("AHH");
600 wxString eq = wxT("AHH");
601 wxString eq2 = wxT("AhH");
602 wxString eq3 = wxT("ahh");
603 wxString neq = wxT("HAH");
604 wxString neq2 = wxT("AH");
605 wxString neq3 = wxT("AHHH");
606
607 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
608 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
609
610 CPPUNIT_CNCEQ_ASSERT( s1, eq );
611 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
612 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
613
614 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
615 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
616 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
617
618
619 // wxString _s1 = wxT("A\0HH");
620 // wxString _eq = wxT("A\0HH");
621 // wxString _eq2 = wxT("A\0hH");
622 // wxString _eq3 = wxT("a\0hh");
623 // wxString _neq = wxT("H\0AH");
624 // wxString _neq2 = wxT("A\0H");
625 // wxString _neq3 = wxT("A\0HHH");
626
627 s1.insert(1,1,'\0');
628 eq.insert(1,1,'\0');
629 eq2.insert(1,1,'\0');
630 eq3.insert(1,1,'\0');
631 neq.insert(1,1,'\0');
632 neq2.insert(1,1,'\0');
633 neq3.insert(1,1,'\0');
634
635 CPPUNIT_CNCEQ_ASSERT( s1, eq );
636 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
637 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
638
639 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
640 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
641 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
642 }
643
644 void StringTestCase::ToLong()
645 {
646 long l;
647 static const struct ToLongData
648 {
649 const wxChar *str;
650 long value;
651 bool ok;
652 } longData[] =
653 {
654 { _T("1"), 1, true },
655 { _T("0"), 0, true },
656 { _T("a"), 0, false },
657 { _T("12345"), 12345, true },
658 { _T("-1"), -1, true },
659 { _T("--1"), 0, false },
660 };
661
662 size_t n;
663 for ( n = 0; n < WXSIZEOF(longData); n++ )
664 {
665 const ToLongData& ld = longData[n];
666 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) );
667 if ( ld.ok )
668 CPPUNIT_ASSERT_EQUAL( ld.value, l );
669 }
670 }
671
672 void StringTestCase::ToULong()
673 {
674 unsigned long ul;
675 static const struct ToULongData
676 {
677 const wxChar *str;
678 unsigned long value;
679 bool ok;
680 } ulongData[] =
681 {
682 { _T("1"), 1, true },
683 { _T("0"), 0, true },
684 { _T("a"), 0, false },
685 { _T("12345"), 12345, true },
686 // this is surprizing but consistent with strtoul() behaviour
687 { _T("-1"), ULONG_MAX, true },
688 };
689
690 size_t n;
691 for ( n = 0; n < WXSIZEOF(ulongData); n++ )
692 {
693 const ToULongData& uld = ulongData[n];
694 CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) );
695 if ( uld.ok )
696 CPPUNIT_ASSERT_EQUAL( uld.value, ul );
697 }
698 }
699
700 void StringTestCase::ToDouble()
701 {
702 double d;
703 static const struct ToDoubleData
704 {
705 const wxChar *str;
706 double value;
707 bool ok;
708 } doubleData[] =
709 {
710 { _T("1"), 1, true },
711 { _T("1.23"), 1.23, true },
712 { _T(".1"), .1, true },
713 { _T("1."), 1, true },
714 { _T("1.."), 0, false },
715 { _T("0"), 0, true },
716 { _T("a"), 0, false },
717 { _T("12345"), 12345, true },
718 { _T("-1"), -1, true },
719 { _T("--1"), 0, false },
720 };
721
722 // we need to use decimal point, not comma or whatever is its value for the
723 // current locale
724 wxSetlocale(LC_ALL, _T("C"));
725
726 size_t n;
727 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
728 {
729 const ToDoubleData& ld = doubleData[n];
730 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
731 if ( ld.ok )
732 CPPUNIT_ASSERT_EQUAL( ld.value, d );
733 }
734 }