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