wx_wcscmp is only used in unicode mode so wrap in #if wxUSE_UNICODE to avoid
[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 #if !wxUSE_UNICODE
232 // in case wcscmp is missing
233 //
234 static int wx_wcscmp(const wchar_t *s1, const wchar_t *s2)
235 {
236 while (*s1 == *s2 && *s1 != 0)
237 {
238 s1++;
239 s2++;
240 }
241 return *s1 - *s2;
242 }
243 #endif
244
245 void
246 StringTestCase::DoTestConversion(const char *s,
247 const wchar_t *ws,
248 wxCSConv& conv)
249 {
250 #if wxUSE_UNICODE
251 if ( ws )
252 {
253 wxCharBuffer buf(wxString(ws).mb_str(conv));
254
255 CPPUNIT_ASSERT( strcmp(buf, s) == 0 );
256 }
257 #else // wxUSE_UNICODE
258 if ( s )
259 {
260 wxWCharBuffer wbuf(wxString(s).wc_str(conv));
261
262 if ( ws )
263 CPPUNIT_ASSERT( wx_wcscmp(wbuf, ws) == 0 );
264 else
265 CPPUNIT_ASSERT( !*wbuf );
266 }
267 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
268 }
269
270 struct StringConversionData
271 {
272 const char *str;
273 const wchar_t *wcs;
274 };
275
276 void StringTestCase::ConversionUTF7()
277 {
278 static const StringConversionData utf7data[] =
279 {
280 { "+-", L"+" },
281 { "+--", L"+-" },
282 //\u isn't recognized on MSVC 6
283 #if !defined(_MSC_VER)
284 #if !defined(__GNUC__) || (__GNUC__ >= 3)
285 { "+AKM-", L"\u00a3" },
286 #endif
287 #endif
288 // Windows accepts invalid UTF-7 strings and so does our UTF-7
289 // conversion code -- this is wrong IMO but the way it is for now
290 //
291 // notice that converting "+" still behaves as expected because the
292 // result is just an empty string, i.e. the same as if there were an
293 // error, but converting "a+" results in "a" while it really should
294 // fail
295 { "+", NULL },
296 { "a+", L"a" },
297 };
298
299 wxCSConv conv(_T("utf-7"));
300 for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
301 {
302 const StringConversionData& d = utf7data[n];
303 DoTestConversion(d.str, d.wcs, conv);
304 }
305 }
306
307 void StringTestCase::ConversionUTF8()
308 {
309 static const StringConversionData utf8data[] =
310 {
311 //\u isn't recognized on MSVC 6
312 #if !defined(_MSC_VER)
313 #if !defined(__GNUC__) || (__GNUC__ >= 3)
314 { "\xc2\xa3", L"\u00a3" },
315 #endif
316 #endif
317 { "\xc2", NULL },
318 };
319
320 wxCSConv conv(_T("utf-8"));
321 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
322 {
323 const StringConversionData& d = utf8data[n];
324 DoTestConversion(d.str, d.wcs, conv);
325 }
326 }
327
328 #endif // wxUSE_WCHAR_T
329
330
331 void StringTestCase::Extraction()
332 {
333 wxString s(_T("Hello, world!"));
334
335 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
336 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
337 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
338 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
339 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
340 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
341 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
342
343 wxString rest;
344
345 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
346 CPPUNIT_ASSERT( \
347 ( s.StartsWith( prefix, &rest ) == result ) && \
348 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
349 )
350
351 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
352 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
353 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
354 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
355 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
356 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
357 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
358
359 #undef TEST_STARTS_WITH
360 }
361
362 void StringTestCase::Find()
363 {
364 #define TEST_FIND( str , start , result ) \
365 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
366
367 TEST_FIND( _T("Well, hello world"), 0, 1 );
368 TEST_FIND( _T("Well, hello world"), 6, 7 );
369 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
370
371 #undef TEST_FIND
372 }
373
374 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
375 {
376 wxStringTokenizer tkz( str, delims, mode);
377 CPPUNIT_ASSERT( tkz.CountTokens() == count );
378
379 wxChar *buf, *s = NULL, *last;
380
381 if ( tkz.GetMode() == wxTOKEN_STRTOK )
382 {
383 buf = new wxChar[wxStrlen(str) + 1];
384 wxStrcpy(buf, str);
385 s = wxStrtok(buf, delims, &last);
386 }
387 else
388 {
389 buf = NULL;
390 }
391
392 size_t count2 = 0;
393 while ( tkz.HasMoreTokens() )
394 {
395 wxString token = tkz.GetNextToken();
396 if ( buf )
397 {
398 CPPUNIT_ASSERT( token == s );
399 s = wxStrtok(NULL, delims, &last);
400 }
401 count2++;
402 }
403
404 CPPUNIT_ASSERT( count2 == count );
405 if ( buf )
406 {
407 delete [] buf;
408 }
409 }
410
411 void StringTestCase::Tokenizer()
412 {
413 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT );
414 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, 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(",!"), 3, wxTOKEN_DEFAULT );
419 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL );
420 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT );
421 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT );
422 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY );
423 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL );
424 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT );
425 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS );
426 }
427
428 // call this with the string to tokenize, delimeters to use and the expected
429 // positions (i.e. results of GetPosition()) after each GetNextToken() call,
430 // terminate positions with 0
431 static void DoTokenizerGetPosition(const wxChar *s,
432 const wxChar *delims, int pos, ...)
433 {
434 wxStringTokenizer tkz(s, delims);
435
436 CPPUNIT_ASSERT( tkz.GetPosition() == 0 );
437
438 va_list ap;
439 va_start(ap, pos);
440
441 for ( ;; )
442 {
443 if ( !pos )
444 {
445 CPPUNIT_ASSERT( !tkz.HasMoreTokens() );
446 break;
447 }
448
449 tkz.GetNextToken();
450
451 CPPUNIT_ASSERT( tkz.GetPosition() == (size_t)pos );
452
453 pos = va_arg(ap, int);
454 }
455
456 va_end(ap);
457 }
458
459 void StringTestCase::TokenizerGetPosition()
460 {
461 DoTokenizerGetPosition(_T("foo"), _T("_"), 3, 0);
462 DoTokenizerGetPosition(_T("foo_bar"), _T("_"), 4, 7, 0);
463 DoTokenizerGetPosition(_T("foo_bar_"), _T("_"), 4, 8, 0);
464 }
465
466 void StringTestCase::Replace()
467 {
468 #define TEST_REPLACE( original , pos , len , replacement , result ) \
469 { \
470 wxString s = original; \
471 s.replace( pos , len , replacement ); \
472 CPPUNIT_ASSERT( s == result ); \
473 }
474
475 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
476 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
477 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
478 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
479 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
480
481
482 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
483 { \
484 wxString s(o,olen); \
485 s.replace( pos , len , replacement ); \
486 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
487 }
488
489 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
490 _T("null\0dhar"), 9 );
491
492 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
493 { \
494 wxString s(o,olen); \
495 s.Replace( olds, news, all ); \
496 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
497 }
498
499 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
500 _T("null\0dehar"), 10 );
501
502 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
503 _T("null\0char"), 9 );
504
505 #undef TEST_WXREPLACE
506 #undef TEST_NULLCHARREPLACE
507 #undef TEST_REPLACE
508 }
509
510 void StringTestCase::Match()
511 {
512 #define TEST_MATCH( s1 , s2 , result ) \
513 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
514
515 TEST_MATCH( _T("foobar"), _T("foo*"), true );
516 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
517 TEST_MATCH( _T("foobar"), _T("*bar"), true );
518 TEST_MATCH( _T("foobar"), _T("??????"), true );
519 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
520 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
521 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
522 TEST_MATCH( _T("foobar"), _T("*foo"), false );
523 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
524 TEST_MATCH( _T(""), _T("*"), true );
525 TEST_MATCH( _T(""), _T("?"), false );
526
527 #undef TEST_MATCH
528 }
529
530
531 void StringTestCase::CaseChanges()
532 {
533 wxString s1(_T("Hello!"));
534 wxString s1u(s1);
535 wxString s1l(s1);
536 s1u.MakeUpper();
537 s1l.MakeLower();
538 wxString s2u, s2l;
539 s2u.MakeUpper();
540 s2l.MakeLower();
541
542 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
543 CPPUNIT_ASSERT( s1l == _T("hello!") );
544 CPPUNIT_ASSERT( s2u == wxEmptyString );
545 CPPUNIT_ASSERT( s2l == wxEmptyString );
546
547 #if !wxUSE_UNICODE
548 wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
549 if ( locRu.IsOk() )
550 {
551 // try upper casing 8bit strings
552 wxString sUpper("\xdf"),
553 sLower("\xff");
554
555 CPPUNIT_ASSERT( sUpper.Lower() == sLower );
556 CPPUNIT_ASSERT( sLower.Upper() == sUpper );
557 }
558 #endif // !wxUSE_UNICODE
559 }
560
561 void StringTestCase::Compare()
562 {
563 wxString s1 = wxT("AHH");
564 wxString eq = wxT("AHH");
565 wxString neq1 = wxT("HAH");
566 wxString neq2 = wxT("AH");
567 wxString neq3 = wxT("AHHH");
568 wxString neq4 = wxT("AhH");
569
570 CPPUNIT_ASSERT( s1 == eq );
571 CPPUNIT_ASSERT( s1 != neq1 );
572 CPPUNIT_ASSERT( s1 != neq2 );
573 CPPUNIT_ASSERT( s1 != neq3 );
574 CPPUNIT_ASSERT( s1 != neq4 );
575
576 // wxString _s1 = wxT("A\0HH");
577 // wxString _eq = wxT("A\0HH");
578 // wxString _neq1 = wxT("H\0AH");
579 // wxString _neq2 = wxT("A\0H");
580 // wxString _neq3 = wxT("A\0HHH");
581 // wxString _neq4 = wxT("A\0hH");
582 s1.insert(1,1,'\0');
583 eq.insert(1,1,'\0');
584 neq1.insert(1,1,'\0');
585 neq2.insert(1,1,'\0');
586 neq3.insert(1,1,'\0');
587 neq4.insert(1,1,'\0');
588
589 CPPUNIT_ASSERT( s1 == eq );
590 CPPUNIT_ASSERT( s1 != neq1 );
591 CPPUNIT_ASSERT( s1 != neq2 );
592 CPPUNIT_ASSERT( s1 != neq3 );
593 CPPUNIT_ASSERT( s1 != neq4 );
594 }
595
596 void StringTestCase::CompareNoCase()
597 {
598 wxString s1 = wxT("AHH");
599 wxString eq = wxT("AHH");
600 wxString eq2 = wxT("AhH");
601 wxString eq3 = wxT("ahh");
602 wxString neq = wxT("HAH");
603 wxString neq2 = wxT("AH");
604 wxString neq3 = wxT("AHHH");
605
606 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
607 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
608
609 CPPUNIT_CNCEQ_ASSERT( s1, eq );
610 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
611 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
612
613 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
614 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
615 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
616
617
618 // wxString _s1 = wxT("A\0HH");
619 // wxString _eq = wxT("A\0HH");
620 // wxString _eq2 = wxT("A\0hH");
621 // wxString _eq3 = wxT("a\0hh");
622 // wxString _neq = wxT("H\0AH");
623 // wxString _neq2 = wxT("A\0H");
624 // wxString _neq3 = wxT("A\0HHH");
625
626 s1.insert(1,1,'\0');
627 eq.insert(1,1,'\0');
628 eq2.insert(1,1,'\0');
629 eq3.insert(1,1,'\0');
630 neq.insert(1,1,'\0');
631 neq2.insert(1,1,'\0');
632 neq3.insert(1,1,'\0');
633
634 CPPUNIT_CNCEQ_ASSERT( s1, eq );
635 CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
636 CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
637
638 CPPUNIT_CNCNEQ_ASSERT( s1, neq );
639 CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
640 CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
641 }
642
643 void StringTestCase::ToLong()
644 {
645 long l;
646 static const struct ToLongData
647 {
648 const wxChar *str;
649 long value;
650 bool ok;
651 } longData[] =
652 {
653 { _T("1"), 1, true },
654 { _T("0"), 0, true },
655 { _T("a"), 0, false },
656 { _T("12345"), 12345, true },
657 { _T("-1"), -1, true },
658 { _T("--1"), 0, false },
659 };
660
661 size_t n;
662 for ( n = 0; n < WXSIZEOF(longData); n++ )
663 {
664 const ToLongData& ld = longData[n];
665 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) );
666 if ( ld.ok )
667 CPPUNIT_ASSERT_EQUAL( ld.value, l );
668 }
669 }
670
671 void StringTestCase::ToULong()
672 {
673 unsigned long ul;
674 static const struct ToULongData
675 {
676 const wxChar *str;
677 unsigned long value;
678 bool ok;
679 } ulongData[] =
680 {
681 { _T("1"), 1, true },
682 { _T("0"), 0, true },
683 { _T("a"), 0, false },
684 { _T("12345"), 12345, true },
685 // this is surprizing but consistent with strtoul() behaviour
686 { _T("-1"), ULONG_MAX, true },
687 };
688
689 size_t n;
690 for ( n = 0; n < WXSIZEOF(ulongData); n++ )
691 {
692 const ToULongData& uld = ulongData[n];
693 CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) );
694 if ( uld.ok )
695 CPPUNIT_ASSERT_EQUAL( uld.value, ul );
696 }
697 }
698
699 void StringTestCase::ToDouble()
700 {
701 double d;
702 static const struct ToDoubleData
703 {
704 const wxChar *str;
705 double value;
706 bool ok;
707 } doubleData[] =
708 {
709 { _T("1"), 1, true },
710 { _T("1.23"), 1.23, true },
711 { _T(".1"), .1, true },
712 { _T("1."), 1, true },
713 { _T("1.."), 0, false },
714 { _T("0"), 0, true },
715 { _T("a"), 0, false },
716 { _T("12345"), 12345, true },
717 { _T("-1"), -1, true },
718 { _T("--1"), 0, false },
719 };
720
721 // we need to use decimal point, not comma or whatever is its value for the
722 // current locale
723 wxSetlocale(LC_ALL, _T("C"));
724
725 size_t n;
726 for ( n = 0; n < WXSIZEOF(doubleData); n++ )
727 {
728 const ToDoubleData& ld = doubleData[n];
729 CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
730 if ( ld.ok )
731 CPPUNIT_ASSERT_EQUAL( ld.value, d );
732 }
733 }