test wxString's char<->wchar_t ctors
[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 #if wxUSE_WCHAR_T
44 CPPUNIT_TEST( ConstructorsWithConversion );
45 #endif
46 CPPUNIT_TEST( Extraction );
47 CPPUNIT_TEST( Find );
48 CPPUNIT_TEST( Tokenizer );
49 CPPUNIT_TEST( Replace );
50 CPPUNIT_TEST( Match );
51 CPPUNIT_TEST( CaseChanges );
52 CPPUNIT_TEST_SUITE_END();
53
54 void String();
55 void PChar();
56 void Format();
57 void Constructors();
58 #if wxUSE_WCHAR_T
59 void ConstructorsWithConversion();
60 #endif
61 void Extraction();
62 void Find();
63 void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
64 void Tokenizer();
65 void Replace();
66 void Match();
67 void CaseChanges();
68
69 DECLARE_NO_COPY_CLASS(StringTestCase)
70 };
71
72 // register in the unnamed registry so that these tests are run by default
73 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
74
75 // also include in it's own registry so that these tests can be run alone
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
77
78 StringTestCase::StringTestCase()
79 {
80 }
81
82 void StringTestCase::String()
83 {
84 wxString a, b, c;
85
86 a.reserve (128);
87 b.reserve (128);
88 c.reserve (128);
89
90 for (int i = 0; i < 2; ++i)
91 {
92 a = _T("Hello");
93 b = _T(" world");
94 c = _T("! How'ya doin'?");
95 a += b;
96 a += c;
97 c = _T("Hello world! What's up?");
98 CPPUNIT_ASSERT( c != a );
99 }
100 }
101
102 void StringTestCase::PChar()
103 {
104 wxChar a [128];
105 wxChar b [128];
106 wxChar c [128];
107
108 for (int i = 0; i < 2; ++i)
109 {
110 wxStrcpy (a, _T("Hello"));
111 wxStrcpy (b, _T(" world"));
112 wxStrcpy (c, _T("! How'ya doin'?"));
113 wxStrcat (a, b);
114 wxStrcat (a, c);
115 wxStrcpy (c, _T("Hello world! What's up?"));
116 CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
117 }
118 }
119
120 void StringTestCase::Format()
121 {
122 wxString s1,s2;
123 s1.Printf(_T("%03d"), 18);
124 CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) );
125 s2.Printf(_T("Number 18: %s\n"), s1.c_str());
126 CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) );
127 }
128
129 void StringTestCase::Constructors()
130 {
131 #define TEST_CTOR(args, res) \
132 { \
133 wxString s args ; \
134 CPPUNIT_ASSERT( s == res ); \
135 }
136
137 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
138 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
139 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
140
141 static const wxChar *s = _T("?really!");
142 const wxChar *start = wxStrchr(s, _T('r'));
143 const wxChar *end = wxStrchr(s, _T('!'));
144 TEST_CTOR((start, end), _T("really"));
145 }
146
147 #if wxUSE_WCHAR_T
148 void StringTestCase::ConstructorsWithConversion()
149 {
150 // Déj`a in UTF-8 and wchar_t:
151 const char utf8[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
152 const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
153 const char utf8sub[] = {0x44,0xC3,0xA9,0x6A,0}; // "Dej"
154
155 wxString s1(utf8, wxConvUTF8);
156 wxString s2(wchar, wxConvUTF8);
157
158 #if wxUSE_UNICODE
159 CPPUNIT_ASSERT( s1 == wchar );
160 CPPUNIT_ASSERT( s2 == wchar );
161 #else
162 CPPUNIT_ASSERT( s1 == utf8 );
163 CPPUNIT_ASSERT( s2 == utf8 );
164 #endif
165
166 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
167 wxString s3(utf8, wxConvUTF8, 4);
168 wxString s4(wchar, wxConvUTF8, 3);
169
170 CPPUNIT_ASSERT( s3 == sub );
171 CPPUNIT_ASSERT( s4 == sub );
172 }
173 #endif
174
175 void StringTestCase::Extraction()
176 {
177 wxString s(_T("Hello, world!"));
178
179 CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 );
180 CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 );
181 CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 );
182 CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 );
183 CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 );
184 CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 );
185 CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 );
186
187 wxString rest;
188
189 #define TEST_STARTS_WITH( prefix , correct_rest, result ) \
190 CPPUNIT_ASSERT( \
191 ( s.StartsWith( prefix, &rest ) == result ) && \
192 ( ( result == false ) || ( wxStrcmp( correct_rest , rest ) == 0 ) ) \
193 )
194
195 TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true );
196 TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true );
197 TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true );
198 TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false );
199 TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true );
200 TEST_STARTS_WITH( _T("Goodbye"), _T(""), false );
201 TEST_STARTS_WITH( _T("Hi"), _T(""), false );
202
203 #undef TEST_STARTS_WITH
204 }
205
206 void StringTestCase::Find()
207 {
208 #define TEST_FIND( str , start , result ) \
209 CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result );
210
211 TEST_FIND( _T("Well, hello world"), 0, 1 );
212 TEST_FIND( _T("Well, hello world"), 6, 7 );
213 TEST_FIND( _T("Well, hello world"), 9, wxString::npos );
214
215 #undef TEST_FIND
216 }
217
218 void StringTestCase::SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode )
219 {
220 wxStringTokenizer tkz( str, delims, mode);
221 CPPUNIT_ASSERT( tkz.CountTokens() == count );
222
223 wxChar *buf, *s = NULL, *last;
224
225 if ( tkz.GetMode() == wxTOKEN_STRTOK )
226 {
227 buf = new wxChar[wxStrlen(str) + 1];
228 wxStrcpy(buf, str);
229 s = wxStrtok(buf, delims, &last);
230 }
231 else
232 {
233 buf = NULL;
234 }
235
236 size_t count2 = 0;
237 while ( tkz.HasMoreTokens() )
238 {
239 wxString token = tkz.GetNextToken();
240 if ( buf )
241 {
242 CPPUNIT_ASSERT( token == s );
243 s = wxStrtok(NULL, delims, &last);
244 }
245 count2++;
246 }
247
248 CPPUNIT_ASSERT( count2 == count );
249 if ( buf )
250 {
251 delete [] buf;
252 }
253 }
254
255 void StringTestCase::Tokenizer()
256 {
257 SingleTokenizerTest( _T(""), _T(" "), 0, wxTOKEN_DEFAULT );
258 SingleTokenizerTest( _T("Hello, world"), _T(" "), 2, wxTOKEN_DEFAULT );
259 SingleTokenizerTest( _T("Hello, world "), _T(" "), 2, wxTOKEN_DEFAULT );
260 SingleTokenizerTest( _T("Hello, world"), _T(","), 2, wxTOKEN_DEFAULT );
261 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 2, wxTOKEN_DEFAULT );
262 SingleTokenizerTest( _T("Hello,, world!"), _T(",!"), 3, wxTOKEN_DEFAULT );
263 SingleTokenizerTest( _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL );
264 SingleTokenizerTest( _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, wxTOKEN_DEFAULT );
265 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4, wxTOKEN_DEFAULT );
266 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY );
267 SingleTokenizerTest( _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL );
268 SingleTokenizerTest( _T("01/02/99"), _T("/-"), 3, wxTOKEN_DEFAULT );
269 SingleTokenizerTest( _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS );
270 }
271
272 void StringTestCase::Replace()
273 {
274 #define TEST_REPLACE( original , pos , len , replacement , result ) \
275 { \
276 wxString s = original; \
277 s.replace( pos , len , replacement ); \
278 CPPUNIT_ASSERT( s == result ); \
279 }
280
281 TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") );
282 TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") );
283 TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") );
284 TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") );
285 TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") );
286
287 #undef TEST_REPLACE
288 }
289
290 void StringTestCase::Match()
291 {
292 #define TEST_MATCH( s1 , s2 , result ) \
293 CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
294
295 TEST_MATCH( _T("foobar"), _T("foo*"), true );
296 TEST_MATCH( _T("foobar"), _T("*oo*"), true );
297 TEST_MATCH( _T("foobar"), _T("*bar"), true );
298 TEST_MATCH( _T("foobar"), _T("??????"), true );
299 TEST_MATCH( _T("foobar"), _T("f??b*"), true );
300 TEST_MATCH( _T("foobar"), _T("f?b*"), false );
301 TEST_MATCH( _T("foobar"), _T("*goo*"), false );
302 TEST_MATCH( _T("foobar"), _T("*foo"), false );
303 TEST_MATCH( _T("foobarfoo"), _T("*foo"), true );
304 TEST_MATCH( _T(""), _T("*"), true );
305 TEST_MATCH( _T(""), _T("?"), false );
306
307 #undef TEST_MATCH
308 }
309
310
311 void StringTestCase::CaseChanges()
312 {
313 wxString s1(_T("Hello!"));
314 wxString s1u(s1);
315 wxString s1l(s1);
316 s1u.MakeUpper();
317 s1l.MakeLower();
318 wxString s2u, s2l;
319 s2u.MakeUpper();
320 s2l.MakeLower();
321
322 CPPUNIT_ASSERT( s1u == _T("HELLO!") );
323 CPPUNIT_ASSERT( s1l == _T("hello!") );
324 CPPUNIT_ASSERT( s2u == wxEmptyString );
325 CPPUNIT_ASSERT( s2l == wxEmptyString );
326 }