]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/strings.cpp
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[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 "wx/wxprec.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 #include "wx/cppunit.h"
27
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class StringTestCase : public CppUnit::TestCase
33 {
34 public:
35 StringTestCase();
36
37 private:
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 );
44 CPPUNIT_TEST( Find );
45 CPPUNIT_TEST( Tokenizer );
46 CPPUNIT_TEST( Replace );
47 CPPUNIT_TEST( Match );
48 CPPUNIT_TEST_SUITE_END();
49
50 void String();
51 void PChar();
52 void Format();
53 void Constructors();
54 void Extraction();
55 void Find();
56 void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
57 void Tokenizer();
58 void Replace();
59 void Match();
60
61 DECLARE_NO_COPY_CLASS(StringTestCase)
62 };
63
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
66
67 // also include in it's own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
69
70 StringTestCase::StringTestCase()
71 {
72 }
73
74 void StringTestCase::String()
75 {
76 wxString a, b, c;
77
78 a.reserve (128);
79 b.reserve (128);
80 c.reserve (128);
81
82 for (int i = 0; i < 2; ++i)
83 {
84 a = _T("Hello");
85 b = _T(" world");
86 c = _T("! How'ya doin'?");
87 a += b;
88 a += c;
89 c = _T("Hello world! What's up?");
90 CPPUNIT_ASSERT( c != a );
91 }
92 }
93
94 void StringTestCase::PChar()
95 {
96 wxChar a [128];
97 wxChar b [128];
98 wxChar c [128];
99
100 for (int i = 0; i < 2; ++i)
101 {
102 wxStrcpy (a, _T("Hello"));
103 wxStrcpy (b, _T(" world"));
104 wxStrcpy (c, _T("! How'ya doin'?"));
105 wxStrcat (a, b);
106 wxStrcat (a, c);
107 wxStrcpy (c, _T("Hello world! What's up?"));
108 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
109 }
110 }
111
112 void StringTestCase::Format()
113 {
114 wxString s1,s2;
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()) );
119 }
120
121 void StringTestCase::Constructors()
122 {
123 #define TEST_CTOR(args, res) \
124 { \
125 wxString s args ; \
126 CPPUNIT_ASSERT( s == res ); \
127 }
128
129 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
130 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
131 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
132
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"));
137 }
138
139 void StringTestCase::Extraction()
140 {
141 wxString s(_T("Hello, world!"));
142
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 );
150
151 wxString rest;
152
153 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
154 CPPUNIT_ASSERT( \
155 ( s.StartsWith( prefix, &rest ) == result ) && \
156 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
157 )
158
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 );
166
167 #undef TEST_STARTS_WITH
168 }
169
170 void StringTestCase::Find()
171 {
172 #define TEST_FIND( str , start , result ) \
173 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
174
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 );
178
179 #undef TEST_FIND
180 }
181
182 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
183 {
184 wxStringTokenizer tkz( str, delims, mode);
185 CPPUNIT_ASSERT( tkz.CountTokens() == count );
186
187 wxChar *buf, *s = NULL, *last;
188
189 if ( tkz.GetMode() == wxTOKEN_STRTOK )
190 {
191 buf = new wxChar[wxStrlen(str) + 1];
192 wxStrcpy(buf, str);
193 s = wxStrtok(buf, delims, &last);
194 }
195 else
196 {
197 buf = NULL;
198 }
199
200 size_t count2 = 0;
201 while ( tkz.HasMoreTokens() )
202 {
203 wxString token = tkz.GetNextToken();
204 if ( buf )
205 {
206 CPPUNIT_ASSERT( token == s );
207 s = wxStrtok(NULL, delims, &last);
208 }
209 count2++;
210 }
211
212 CPPUNIT_ASSERT( count2 == count );
213 if ( buf )
214 {
215 delete [] buf;
216 }
217 }
218
219 void StringTestCase::Tokenizer()
220 {
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 );
234 }
235
236 void StringTestCase::Replace()
237 {
238 #define TEST_REPLACE( original , pos , len , replacement , result ) \
239 { \
240 wxString s = original; \
241 s.replace( pos , len , replacement ); \
242 CPPUNIT_ASSERT( s == result ); \
243 }
244
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") );
250
251 #undef TEST_REPLACE
252 }
253
254 void StringTestCase::Match()
255 {
256 #define TEST_MATCH( s1 , s2 , result ) \
257 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
258
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 );
270
271 #undef TEST_MATCH
272 }
273