]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
fac42532758fedd7a30017cf5c47d7a1712ec131
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/strings.cpp
3 // Purpose: wxString unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class StringTestCase
: public CppUnit::TestCase
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
);
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
);
51 CPPUNIT_TEST( ToLongLong
);
52 CPPUNIT_TEST( ToULongLong
);
53 #endif // wxLongLong_t
54 CPPUNIT_TEST( ToDouble
);
55 CPPUNIT_TEST( WriteBuf
);
56 CPPUNIT_TEST_SUITE_END();
76 #endif // wxLongLong_t
80 DECLARE_NO_COPY_CLASS(StringTestCase
)
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
86 // also include in it's own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
89 StringTestCase::StringTestCase()
93 void StringTestCase::String()
101 for (int i
= 0; i
< 2; ++i
)
105 c
= _T("! How'ya doin'?");
108 c
= _T("Hello world! What's up?");
109 CPPUNIT_ASSERT( c
!= a
);
113 void StringTestCase::PChar()
119 for (int i
= 0; i
< 2; ++i
)
121 wxStrcpy (a
, _T("Hello"));
122 wxStrcpy (b
, _T(" world"));
123 wxStrcpy (c
, _T("! How'ya doin'?"));
126 wxStrcpy (c
, _T("Hello world! What's up?"));
127 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
131 void StringTestCase::Format()
134 s1
.Printf(_T("%03d"), 18);
135 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
136 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
137 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
139 static const size_t lengths
[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
140 for ( size_t n
= 0; n
< WXSIZEOF(lengths
); n
++ )
142 const size_t len
= lengths
[n
];
144 wxString
s(_T('Z'), len
);
145 CPPUNIT_ASSERT_EQUAL( len
, wxString::Format(_T("%s"), s
.c_str()).length());
149 void StringTestCase::Constructors()
151 #define TEST_CTOR(args, res) \
154 CPPUNIT_ASSERT( s == res ); \
157 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
158 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
159 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
161 static const wxChar
*s
= _T("?really!");
162 const wxChar
*start
= wxStrchr(s
, _T('r'));
163 const wxChar
*end
= wxStrchr(s
, _T('!'));
164 TEST_CTOR((start
, end
), _T("really"));
168 void StringTestCase::Extraction()
170 wxString
s(_T("Hello, world!"));
172 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
173 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
174 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
175 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
176 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
177 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
178 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
182 #define TEST_STARTS_WITH(prefix, correct_rest, result) \
183 CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \
185 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
187 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
188 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
189 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
190 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
191 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
192 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
193 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
195 #undef TEST_STARTS_WITH
197 #define TEST_ENDS_WITH(suffix, correct_rest, result) \
198 CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \
200 CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest)
202 TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true );
203 TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true );
204 TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true );
205 TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true );
206 TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true );
207 TEST_ENDS_WITH( _T("very long string"), _T(""), false );
208 TEST_ENDS_WITH( _T("?"), _T(""), false );
209 TEST_ENDS_WITH( _T("Hello, world"), _T(""), false );
210 TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false );
212 #undef TEST_ENDS_WITH
215 void StringTestCase::Trim()
217 #define TEST_TRIM( str , dir , result ) \
218 CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
220 TEST_TRIM( _T(" Test "), true, _T(" Test") );
221 TEST_TRIM( _T(" "), true, _T("") );
222 TEST_TRIM( _T(" "), true, _T("") );
223 TEST_TRIM( _T(""), true, _T("") );
225 TEST_TRIM( _T(" Test "), false, _T("Test ") );
226 TEST_TRIM( _T(" "), false, _T("") );
227 TEST_TRIM( _T(" "), false, _T("") );
228 TEST_TRIM( _T(""), false, _T("") );
233 void StringTestCase::Find()
235 #define TEST_FIND( str , start , result ) \
236 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
238 TEST_FIND( _T("Well, hello world"), 0, 1 );
239 TEST_FIND( _T("Well, hello world"), 6, 7 );
240 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
245 void StringTestCase::Replace()
247 #define TEST_REPLACE( original , pos , len , replacement , result ) \
249 wxString s = original; \
250 s.replace( pos , len , replacement ); \
251 CPPUNIT_ASSERT( s == result ); \
254 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
255 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
256 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
257 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
258 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
261 #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
263 wxString s(o,olen); \
264 s.replace( pos , len , replacement ); \
265 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
268 TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"),
269 _T("null\0dhar"), 9 );
271 #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
273 wxString s(o,olen); \
274 s.Replace( olds, news, all ); \
275 CPPUNIT_ASSERT( s == wxString(r,rlen) ); \
278 TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true,
279 _T("null\0dehar"), 10 );
281 TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true,
282 _T("null\0char"), 9 );
284 #undef TEST_WXREPLACE
285 #undef TEST_NULLCHARREPLACE
289 void StringTestCase::Match()
291 #define TEST_MATCH( s1 , s2 , result ) \
292 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
294 TEST_MATCH( _T("foobar"), _T("foo*"), true );
295 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
296 TEST_MATCH( _T("foobar"), _T("*bar"), true );
297 TEST_MATCH( _T("foobar"), _T("??????"), true );
298 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
299 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
300 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
301 TEST_MATCH( _T("foobar"), _T("*foo"), false );
302 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
303 TEST_MATCH( _T(""), _T("*"), true );
304 TEST_MATCH( _T(""), _T("?"), false );
310 void StringTestCase::CaseChanges()
312 wxString
s1(_T("Hello!"));
321 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
322 CPPUNIT_ASSERT( s1l
== _T("hello!") );
323 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
324 CPPUNIT_ASSERT( s2l
== wxEmptyString
);
327 wxLocale
locRu(wxLANGUAGE_RUSSIAN
, 0 /* flags */);
330 // try upper casing 8bit strings
331 const wchar_t capital_ya
[] = { 0x42f, 0 },
332 small_ya
[] = { 0x44f, 0 };
334 wxString
sUpper(wxConvLibc
.cWC2MB(capital_ya
)),
335 sLower(wxConvLibc
.cWC2MB(small_ya
));
337 CPPUNIT_ASSERT( sUpper
.Lower() == sLower
);
338 CPPUNIT_ASSERT( sLower
.Upper() == sUpper
);
340 #endif // !wxUSE_UNICODE
343 void StringTestCase::Compare()
345 wxString s1
= wxT("AHH");
346 wxString eq
= wxT("AHH");
347 wxString neq1
= wxT("HAH");
348 wxString neq2
= wxT("AH");
349 wxString neq3
= wxT("AHHH");
350 wxString neq4
= wxT("AhH");
352 CPPUNIT_ASSERT( s1
== eq
);
353 CPPUNIT_ASSERT( s1
!= neq1
);
354 CPPUNIT_ASSERT( s1
!= neq2
);
355 CPPUNIT_ASSERT( s1
!= neq3
);
356 CPPUNIT_ASSERT( s1
!= neq4
);
358 CPPUNIT_ASSERT( s1
== wxT("AHH") );
359 CPPUNIT_ASSERT( s1
!= wxT("no") );
360 CPPUNIT_ASSERT( s1
< wxT("AZ") );
361 CPPUNIT_ASSERT( s1
<= wxT("AZ") );
362 CPPUNIT_ASSERT( s1
<= wxT("AHH") );
363 CPPUNIT_ASSERT( s1
> wxT("AA") );
364 CPPUNIT_ASSERT( s1
>= wxT("AA") );
365 CPPUNIT_ASSERT( s1
>= wxT("AHH") );
367 // test comparison with C strings in Unicode build (must work in ANSI as
369 CPPUNIT_ASSERT( s1
== "AHH" );
370 CPPUNIT_ASSERT( s1
!= "no" );
371 CPPUNIT_ASSERT( s1
< "AZ" );
372 CPPUNIT_ASSERT( s1
<= "AZ" );
373 CPPUNIT_ASSERT( s1
<= "AHH" );
374 CPPUNIT_ASSERT( s1
> "AA" );
375 CPPUNIT_ASSERT( s1
>= "AA" );
376 CPPUNIT_ASSERT( s1
>= "AHH" );
378 // wxString _s1 = wxT("A\0HH");
379 // wxString _eq = wxT("A\0HH");
380 // wxString _neq1 = wxT("H\0AH");
381 // wxString _neq2 = wxT("A\0H");
382 // wxString _neq3 = wxT("A\0HHH");
383 // wxString _neq4 = wxT("A\0hH");
386 neq1
.insert(1,1,'\0');
387 neq2
.insert(1,1,'\0');
388 neq3
.insert(1,1,'\0');
389 neq4
.insert(1,1,'\0');
391 CPPUNIT_ASSERT( s1
== eq
);
392 CPPUNIT_ASSERT( s1
!= neq1
);
393 CPPUNIT_ASSERT( s1
!= neq2
);
394 CPPUNIT_ASSERT( s1
!= neq3
);
395 CPPUNIT_ASSERT( s1
!= neq4
);
398 void StringTestCase::CompareNoCase()
400 wxString s1
= wxT("AHH");
401 wxString eq
= wxT("AHH");
402 wxString eq2
= wxT("AhH");
403 wxString eq3
= wxT("ahh");
404 wxString neq
= wxT("HAH");
405 wxString neq2
= wxT("AH");
406 wxString neq3
= wxT("AHHH");
408 #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
409 #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
411 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
412 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
413 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
415 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
416 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
417 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
420 // wxString _s1 = wxT("A\0HH");
421 // wxString _eq = wxT("A\0HH");
422 // wxString _eq2 = wxT("A\0hH");
423 // wxString _eq3 = wxT("a\0hh");
424 // wxString _neq = wxT("H\0AH");
425 // wxString _neq2 = wxT("A\0H");
426 // wxString _neq3 = wxT("A\0HHH");
430 eq2
.insert(1,1,'\0');
431 eq3
.insert(1,1,'\0');
432 neq
.insert(1,1,'\0');
433 neq2
.insert(1,1,'\0');
434 neq3
.insert(1,1,'\0');
436 CPPUNIT_CNCEQ_ASSERT( s1
, eq
);
437 CPPUNIT_CNCEQ_ASSERT( s1
, eq2
);
438 CPPUNIT_CNCEQ_ASSERT( s1
, eq3
);
440 CPPUNIT_CNCNEQ_ASSERT( s1
, neq
);
441 CPPUNIT_CNCNEQ_ASSERT( s1
, neq2
);
442 CPPUNIT_CNCNEQ_ASSERT( s1
, neq3
);
445 void StringTestCase::Contains()
447 static const struct ContainsData
450 const wxChar
*needle
;
454 { _T(""), _T(""), true },
455 { _T(""), _T("foo"), false },
456 { _T("foo"), _T(""), true },
457 { _T("foo"), _T("f"), true },
458 { _T("foo"), _T("o"), true },
459 { _T("foo"), _T("oo"), true },
460 { _T("foo"), _T("ooo"), false },
461 { _T("foo"), _T("oooo"), false },
462 { _T("foo"), _T("fooo"), false },
465 for ( size_t n
= 0; n
< WXSIZEOF(containsData
); n
++ )
467 const ContainsData
& cd
= containsData
[n
];
468 CPPUNIT_ASSERT_EQUAL( cd
.contains
, wxString(cd
.hay
).Contains(cd
.needle
) );
472 // flags used in ToLongData.flags
477 Number_Unsigned
= 2, // if not specified, works for signed conversion
478 Number_Signed
= 4, // if not specified, works for unsigned
479 Number_LongLong
= 8, // only for long long tests
480 Number_Long
= 16, // only for long tests
483 static const struct ToLongData
490 #endif // wxLongLong_t
493 long LValue() const { return value
; }
494 unsigned long ULValue() const { return value
; }
496 wxLongLong_t
LLValue() const { return value
; }
497 wxULongLong_t
ULLValue() const { return (wxULongLong_t
)value
; }
498 #endif // wxLongLong_t
500 bool IsOk() const { return !(flags
& Number_Invalid
); }
503 { _T("1"), 1, Number_Ok
},
504 { _T("0"), 0, Number_Ok
},
505 { _T("a"), 0, Number_Invalid
},
506 { _T("12345"), 12345, Number_Ok
},
507 { _T("--1"), 0, Number_Invalid
},
509 { _T("-1"), -1, Number_Signed
| Number_Long
},
510 // this is surprizing but consistent with strtoul() behaviour
511 { _T("-1"), ULONG_MAX
, Number_Unsigned
| Number_Long
},
513 // this must overflow, even with 64 bit long
514 { _T("922337203685477580711"), 0, Number_Invalid
},
517 { _T("2147483648"), wxLL(2147483648), Number_LongLong
},
518 { _T("-2147483648"), wxLL(-2147483648), Number_LongLong
| Number_Signed
},
519 { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong
|
521 #endif // wxLongLong_t
524 void StringTestCase::ToLong()
527 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
529 const ToLongData
& ld
= longData
[n
];
531 if ( ld
.flags
& (Number_LongLong
| Number_Unsigned
) )
534 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLong(&l
) );
536 CPPUNIT_ASSERT_EQUAL( ld
.LValue(), l
);
540 void StringTestCase::ToULong()
543 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
545 const ToLongData
& ld
= longData
[n
];
547 if ( ld
.flags
& (Number_LongLong
| Number_Signed
) )
550 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULong(&ul
) );
552 CPPUNIT_ASSERT_EQUAL( ld
.ULValue(), ul
);
558 void StringTestCase::ToLongLong()
561 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
563 const ToLongData
& ld
= longData
[n
];
565 if ( ld
.flags
& (Number_Long
| Number_Unsigned
) )
568 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToLongLong(&l
) );
570 CPPUNIT_ASSERT_EQUAL( ld
.LLValue(), l
);
574 void StringTestCase::ToULongLong()
577 for ( size_t n
= 0; n
< WXSIZEOF(longData
); n
++ )
579 const ToLongData
& ld
= longData
[n
];
581 if ( ld
.flags
& (Number_Long
| Number_Signed
) )
584 CPPUNIT_ASSERT_EQUAL( ld
.IsOk(), wxString(ld
.str
).ToULongLong(&ul
) );
586 CPPUNIT_ASSERT_EQUAL( ld
.ULLValue(), ul
);
590 #endif // wxLongLong_t
592 void StringTestCase::ToDouble()
595 static const struct ToDoubleData
602 { _T("1"), 1, true },
603 { _T("1.23"), 1.23, true },
604 { _T(".1"), .1, true },
605 { _T("1."), 1, true },
606 { _T("1.."), 0, false },
607 { _T("0"), 0, true },
608 { _T("a"), 0, false },
609 { _T("12345"), 12345, true },
610 { _T("-1"), -1, true },
611 { _T("--1"), 0, false },
614 // we need to use decimal point, not comma or whatever is its value for the
616 wxSetlocale(LC_ALL
, _T("C"));
619 for ( n
= 0; n
< WXSIZEOF(doubleData
); n
++ )
621 const ToDoubleData
& ld
= doubleData
[n
];
622 CPPUNIT_ASSERT_EQUAL( ld
.ok
, wxString(ld
.str
).ToDouble(&d
) );
624 CPPUNIT_ASSERT_EQUAL( ld
.value
, d
);
628 void StringTestCase::WriteBuf()
631 wxStrcpy(wxStringBuffer(s
, 10), _T("foo"));
633 CPPUNIT_ASSERT_EQUAL(_T('f'), s
[0u]);
634 CPPUNIT_ASSERT_EQUAL(_T('o'), s
[1]);
635 CPPUNIT_ASSERT_EQUAL(_T('o'), s
[2]);
636 CPPUNIT_ASSERT_EQUAL((size_t)3, s
.length());
640 wxStringBufferLength
buf(s
, 10);
641 wxStrcpy(buf
, _T("barrbaz"));
645 CPPUNIT_ASSERT_EQUAL(_T('b'), s
[0u]);
646 CPPUNIT_ASSERT_EQUAL(_T('a'), s
[1]);
647 CPPUNIT_ASSERT_EQUAL(_T('r'), s
[2]);
648 CPPUNIT_ASSERT_EQUAL(_T('r'), s
[3]);
649 CPPUNIT_ASSERT_EQUAL((size_t)4, s
.length());
651 CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s
) );