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_SUITE_END();
56 void SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
);
61 DECLARE_NO_COPY_CLASS(StringTestCase
)
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase
);
67 // also include in it's own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase
, "StringTestCase" );
70 StringTestCase::StringTestCase()
74 void StringTestCase::String()
82 for (int i
= 0; i
< 2; ++i
)
86 c
= _T("! How'ya doin'?");
89 c
= _T("Hello world! What's up?");
90 CPPUNIT_ASSERT( c
!= a
);
94 void StringTestCase::PChar()
100 for (int i
= 0; i
< 2; ++i
)
102 wxStrcpy (a
, _T("Hello"));
103 wxStrcpy (b
, _T(" world"));
104 wxStrcpy (c
, _T("! How'ya doin'?"));
107 wxStrcpy (c
, _T("Hello world! What's up?"));
108 CPPUNIT_ASSERT( wxStrcmp (c
, a
) != 0 );
112 void StringTestCase::Format()
115 s1
.Printf(_T("%03d"), 18);
116 CPPUNIT_ASSERT( s1
== wxString::Format(_T("%03d"), 18) );
117 s2
.Printf(_T("Number 18: %s\n"), s1
.c_str());
118 CPPUNIT_ASSERT( s2
== wxString::Format(_T("Number 18: %s\n"), s1
.c_str()) );
121 void StringTestCase::Constructors()
123 #define TEST_CTOR(args, res) \
126 CPPUNIT_ASSERT( s == res ); \
129 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
130 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
131 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
133 static const wxChar
*s
= _T("?really!");
134 const wxChar
*start
= wxStrchr(s
, _T('r'));
135 const wxChar
*end
= wxStrchr(s
, _T('!'));
136 TEST_CTOR((start
, end
), _T("really"));
139 void StringTestCase::Extraction()
141 wxString
s(_T("Hello, world!"));
143 CPPUNIT_ASSERT( wxStrcmp( s
.c_str() , _T("Hello, world!") ) == 0 );
144 CPPUNIT_ASSERT( wxStrcmp( s
.Left(5).c_str() , _T("Hello") ) == 0 );
145 CPPUNIT_ASSERT( wxStrcmp( s
.Right(6).c_str() , _T("world!") ) == 0 );
146 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
147 CPPUNIT_ASSERT( wxStrcmp( s
.Mid(3).c_str() , _T("lo, world!") ) == 0 );
148 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
149 CPPUNIT_ASSERT( wxStrcmp( s
.substr(3).c_str() , _T("lo, world!") ) == 0 );
153 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
155 ( s.StartsWith( prefix, &rest ) == result ) && \
156 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
159 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
160 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
161 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
162 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
163 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
164 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
165 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
167 #undef TEST_STARTS_WITH
170 void StringTestCase::Find()
172 #define TEST_FIND( str , start , result ) \
173 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
175 TEST_FIND( _T("Well, hello world"), 0, 1 );
176 TEST_FIND( _T("Well, hello world"), 6, 7 );
177 TEST_FIND( _T("Well, hello world"), 9, wxString::npos
);
182 void StringTestCase::SingleTokenizerTest( wxChar
*str
, wxChar
*delims
, size_t count
, wxStringTokenizerMode mode
)
184 wxStringTokenizer
tkz( str
, delims
, mode
);
185 CPPUNIT_ASSERT( tkz
.CountTokens() == count
);
187 wxChar
*buf
, *s
= NULL
, *last
;
189 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
191 buf
= new wxChar
[wxStrlen(str
) + 1];
193 s
= wxStrtok(buf
, delims
, &last
);
201 while ( tkz
.HasMoreTokens() )
203 wxString token
= tkz
.GetNextToken();
206 CPPUNIT_ASSERT( token
== s
);
207 s
= wxStrtok(NULL
, delims
, &last
);
212 CPPUNIT_ASSERT( count2
== count
);
219 void StringTestCase::Tokenizer()
221 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT
);
222 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT
);
223 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, 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(",!"), 3, wxTOKEN_DEFAULT
);
227 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
);
228 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT
);
229 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4, wxTOKEN_DEFAULT
);
230 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
);
231 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
);
232 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT
);
233 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
);
236 void StringTestCase::Replace()
238 #define TEST_REPLACE( original , pos , len , replacement , result ) \
240 wxString s = original; \
241 s.replace( pos , len , replacement ); \
242 CPPUNIT_ASSERT( s == result ); \
245 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
246 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
247 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
248 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
249 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
254 void StringTestCase::Match()
256 #define TEST_MATCH( s1 , s2 , result ) \
257 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
259 TEST_MATCH( _T("foobar"), _T("foo*"), true );
260 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
261 TEST_MATCH( _T("foobar"), _T("*bar"), true );
262 TEST_MATCH( _T("foobar"), _T("??????"), true );
263 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
264 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
265 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
266 TEST_MATCH( _T("foobar"), _T("*foo"), false );
267 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
268 TEST_MATCH( _T(""), _T("*"), true );
269 TEST_MATCH( _T(""), _T("?"), false );