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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
24 #include "wx/tokenzr.h"
26 #include "wx/cppunit.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class StringTestCase
: public CppUnit::TestCase
38 CPPUNIT_TEST_SUITE( StringTestCase
);
39 CPPUNIT_TEST( String
);
40 CPPUNIT_TEST( PChar
);
41 CPPUNIT_TEST( Format
);
42 CPPUNIT_TEST( Constructors
);
43 CPPUNIT_TEST( Extraction
);
45 CPPUNIT_TEST( Tokenizer
);
46 CPPUNIT_TEST( Replace
);
47 CPPUNIT_TEST( Match
);
48 CPPUNIT_TEST( CaseChanges
);
49 CPPUNIT_TEST_SUITE_END();
57 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
63 DECLARE_NO_COPY_CLASS(StringTestCase
)
66 // register in the unnamed registry so that these tests are run by default
67 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
69 // also include in it's own registry so that these tests can be run alone
70 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
72 StringTestCase::StringTestCase()
76 void StringTestCase::String()
84 for (int i
= 0; i
< 2; ++i
)
88 c
= _T("! How'ya doin'?");
91 c
= _T("Hello world! What's up?");
92 CPPUNIT_ASSERT( c
!= a
);
96 void StringTestCase::PChar()
102 for (int i
= 0; i
< 2; ++i
)
104 wxStrcpy (a
, _T("Hello"));
105 wxStrcpy (b
, _T(" world"));
106 wxStrcpy (c
, _T("! How'ya doin'?"));
109 wxStrcpy (c
, _T("Hello world! What's up?"));
110 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
114 void StringTestCase::Format()
117 s1
.Printf(_T("%03d"), 18);
118 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
119 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
120 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
123 void StringTestCase::Constructors()
125 #define TEST_CTOR(args, res) \
128 CPPUNIT_ASSERT( s == res ); \
131 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
132 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
133 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
135 static const wxChar
*s
= _T("?really!");
136 const wxChar
*start
= wxStrchr(s
, _T('r'));
137 const wxChar
*end
= wxStrchr(s
, _T('!'));
138 TEST_CTOR((start
, end
), _T("really"));
141 void StringTestCase::Extraction()
143 wxString
s(_T("Hello, world!"));
145 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
146 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
147 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
148 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
149 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
150 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
151 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
155 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
157 ( s.StartsWith( prefix, &rest ) == result ) && \
158 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
161 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
162 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
163 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
164 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
165 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
166 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
167 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
169 #undef TEST_STARTS_WITH
172 void StringTestCase::Find()
174 #define TEST_FIND( str , start , result ) \
175 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
177 TEST_FIND( _T("Well, hello world"), 0, 1 );
178 TEST_FIND( _T("Well, hello world"), 6, 7 );
179 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
184 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
186 wxStringTokenizer
tkz( str
, delims
, mode
);
187 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
189 wxChar
*buf
, *s
= NULL
, *last
;
191 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
193 buf
= new wxChar
[wxStrlen(str
) + 1];
195 s
= wxStrtok(buf
, delims
, &last
);
203 while ( tkz
.HasMoreTokens() )
205 wxString token
= tkz
.GetNextToken();
208 CPPUNIT_ASSERT( token
== s
);
209 s
= wxStrtok(NULL
, delims
, &last
);
214 CPPUNIT_ASSERT( count2
== count
);
221 void StringTestCase::Tokenizer()
223 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
224 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
225 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT
);
226 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT
);
227 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT
);
228 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT
);
229 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
230 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
231 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
232 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
233 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
234 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
235 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
238 void StringTestCase::Replace()
240 #define TEST_REPLACE( original , pos , len , replacement , result ) \
242 wxString s = original; \
243 s.replace( pos , len , replacement ); \
244 CPPUNIT_ASSERT( s == result ); \
247 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
248 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
249 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
250 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
251 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
256 void StringTestCase::Match()
258 #define TEST_MATCH( s1 , s2 , result ) \
259 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
261 TEST_MATCH( _T("foobar"), _T("foo*"), true );
262 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
263 TEST_MATCH( _T("foobar"), _T("*bar"), true );
264 TEST_MATCH( _T("foobar"), _T("??????"), true );
265 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
266 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
267 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
268 TEST_MATCH( _T("foobar"), _T("*foo"), false );
269 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
270 TEST_MATCH( _T(""), _T("*"), true );
271 TEST_MATCH( _T(""), _T("?"), false );
277 void StringTestCase::CaseChanges()
279 wxString
s1(_T("Hello!"));
288 CPPUNIT_ASSERT( s1u
== _T("HELLO!") );
289 CPPUNIT_ASSERT( s1l
== _T("hello!") );
290 CPPUNIT_ASSERT( s2u
== wxEmptyString
);
291 CPPUNIT_ASSERT( s2l
== wxEmptyString
);