]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/stdstrings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/stdstrings.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/cppunit.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class StdStringTestCase
: public CppUnit::TestCase
36 CPPUNIT_TEST_SUITE( StdStringTestCase
);
37 CPPUNIT_TEST( StdConstructors
);
38 CPPUNIT_TEST( StdAppend
);
39 CPPUNIT_TEST( StdAssign
);
40 CPPUNIT_TEST( StdCompare
);
41 CPPUNIT_TEST( StdErase
);
42 CPPUNIT_TEST( StdFind
);
43 CPPUNIT_TEST( StdFindFirst
);
44 CPPUNIT_TEST( StdFindLast
);
45 CPPUNIT_TEST( StdInsert
);
46 CPPUNIT_TEST( StdReplace
);
47 CPPUNIT_TEST( StdRFind
);
48 CPPUNIT_TEST( StdResize
);
49 CPPUNIT_TEST( StdSubstr
);
50 CPPUNIT_TEST_SUITE_END();
52 void StdConstructors();
66 DECLARE_NO_COPY_CLASS(StdStringTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( StdStringTestCase
);
72 // also include in it's own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStringTestCase
, "StdStringTestCase" );
75 StdStringTestCase::StdStringTestCase()
79 void StdStringTestCase::StdConstructors()
81 wxString
s1(_T("abcdefgh")),
82 s2(_T("abcdefghijklm"), 8),
83 s3(_T("abcdefghijklm")),
87 s7(s3
.begin(), s3
.begin() + 8);
88 wxString
s8(s1
, 4, 8);
90 CPPUNIT_ASSERT( s1
== _T("abcdefgh") );
91 CPPUNIT_ASSERT( s2
== s1
);
92 CPPUNIT_ASSERT( s4
== _T("aaaaaaaa") );
93 CPPUNIT_ASSERT( s5
== _T("abcdefgh") );
94 CPPUNIT_ASSERT( s6
== s1
);
95 CPPUNIT_ASSERT( s7
== s1
);
96 CPPUNIT_ASSERT( s8
== _T("efgh") );
99 void StdStringTestCase::StdAppend()
101 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
103 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
104 s1
.append(_T("def"));
105 s2
.append(_T("defgh"), 3);
106 s3
.append(wxString(_T("abcdef")), 3, 6);
108 s5
.append(3, _T('a'));
109 s6
.append(s1
.begin() + 3, s1
.end());
111 CPPUNIT_ASSERT( s1
== _T("abcdef") );
112 CPPUNIT_ASSERT( s2
== _T("abcdef") );
113 CPPUNIT_ASSERT( s3
== _T("abcdef") );
114 CPPUNIT_ASSERT( s4
== _T("abcabcdef") );
115 CPPUNIT_ASSERT( s5
== _T("abcaaa") );
116 CPPUNIT_ASSERT( s6
== _T("abcdef") );
119 void StdStringTestCase::StdAssign()
121 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
123 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
124 s1
.assign(_T("def"));
125 s2
.assign(_T("defgh"), 3);
126 s3
.assign(wxString(_T("abcdef")), 3, 6);
128 s5
.assign(3, _T('a'));
129 s6
.assign(s1
.begin() + 1, s1
.end());
131 CPPUNIT_ASSERT( s1
== _T("def") );
132 CPPUNIT_ASSERT( s2
== _T("def") );
133 CPPUNIT_ASSERT( s3
== _T("def") );
134 CPPUNIT_ASSERT( s4
== _T("def") );
135 CPPUNIT_ASSERT( s5
== _T("aaa") );
136 CPPUNIT_ASSERT( s6
== _T("ef") );
139 void StdStringTestCase::StdCompare()
141 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
;
146 s4
= _T("abcdefghi");
150 CPPUNIT_ASSERT( s1
.compare(s2
) == 0 );
151 CPPUNIT_ASSERT( s1
.compare(s3
) > 0 );
152 CPPUNIT_ASSERT( s1
.compare(s4
) < 0 );
153 CPPUNIT_ASSERT( s1
.compare(s5
) > 0 );
154 CPPUNIT_ASSERT( s1
.compare(s6
) < 0 );
155 CPPUNIT_ASSERT( s1
.compare(1, 12, s1
) > 0);
156 CPPUNIT_ASSERT( s1
.compare(_T("abcdefgh")) == 0);
157 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh")) == 0);
158 CPPUNIT_ASSERT( s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
161 void StdStringTestCase::StdErase()
163 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
;
168 s4
= _T("abcdefghi");
175 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
176 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
177 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
179 CPPUNIT_ASSERT( s1
== _T("acdefgh") );
180 CPPUNIT_ASSERT( s2
== _T("abcd") );
181 CPPUNIT_ASSERT( s3
== _T("ac") );
182 CPPUNIT_ASSERT( s4
== _T("abcdghi") );
183 CPPUNIT_ASSERT( s7
== _T("zabc") );
184 CPPUNIT_ASSERT( *it
== _T('c') );
185 CPPUNIT_ASSERT( *it2
== _T('g') );
186 CPPUNIT_ASSERT( it3
== s7
.end() );
189 void StdStringTestCase::StdFind()
192 // 01234567890123456789012345
193 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
194 wxString s2
= _T("gAB");
196 CPPUNIT_ASSERT( s1
.find(_T('A')) == 7u );
197 CPPUNIT_ASSERT( s1
.find(_T('A'), 7) == 7u );
198 CPPUNIT_ASSERT( s1
.find(_T('Z')) == wxString::npos
);
199 CPPUNIT_ASSERT( s1
.find(_T('C'), 22) == 25u );
201 CPPUNIT_ASSERT( s1
.find(_T("gAB")) == 6u );
202 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 7) == wxString::npos
);
203 CPPUNIT_ASSERT( s1
.find(_T("gAB"), 6) == 6u );
205 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 2, 3) == 6u );
206 CPPUNIT_ASSERT( s1
.find(_T("gABZZZ"), 7, 3) == wxString::npos
);
208 CPPUNIT_ASSERT( s1
.find(s2
) == 6u );
209 CPPUNIT_ASSERT( s1
.find(s2
, 7) == wxString::npos
);
210 CPPUNIT_ASSERT( s1
.find(s2
, 6) == 6u );
213 void StdStringTestCase::StdFindFirst()
216 // 01234567890123456789012345678901234
217 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
218 wxString s2
= _T("aaaaaa");
220 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a')) == 6u );
221 CPPUNIT_ASSERT( s1
.find_first_not_of(_T('a'), 7) == 7u );
222 CPPUNIT_ASSERT( s2
.find_first_not_of(_T('a')) == wxString::npos
);
224 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 4) == 7u );
225 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abde"), 7) == 7u );
226 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghijkl")) == wxString::npos
);
228 CPPUNIT_ASSERT( s1
.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u );
230 CPPUNIT_ASSERT( s1
.find_first_of(_T('c')) == 7u );
231 CPPUNIT_ASSERT( s1
.find_first_of(_T('v')) == wxString::npos
);
232 CPPUNIT_ASSERT( s1
.find_first_of(_T('c'), 10) == 24u );
234 CPPUNIT_ASSERT( s1
.find_first_of(_T("ijkl")) == 13u );
235 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfg"), 17) == 24u );
236 CPPUNIT_ASSERT( s1
.find_first_of(_T("ddcfga"), 17, 5) == 24u );
239 void StdStringTestCase::StdFindLast()
242 // 01234567890123456789012345678901234
243 wxString s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
244 wxString s2
= _T("aaaaaa");
246 CPPUNIT_ASSERT( s2
.find_last_not_of(_T('a')) == wxString::npos
);
247 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d')) == 33u );
248 CPPUNIT_ASSERT( s1
.find_last_not_of(_T('d'), 25) == 24u );
250 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("bcd")) == 22u );
251 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abc"), 24) == 16u );
253 CPPUNIT_ASSERT( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u );
255 CPPUNIT_ASSERT( s2
.find_last_of(_T('c')) == wxString::npos
);
256 CPPUNIT_ASSERT( s1
.find_last_of(_T('a')) == 22u );
257 CPPUNIT_ASSERT( s1
.find_last_of(_T('b'), 24) == 23u );
259 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklm")) == 16u );
260 CPPUNIT_ASSERT( s1
.find_last_of(_T("ijklma"), 33, 4) == 16u );
261 CPPUNIT_ASSERT( s1
.find_last_of(_T("a"), 17) == 17u );
264 void StdStringTestCase::StdInsert()
266 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
, s10
;
268 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
269 s9
= s10
= _T("cdefg");
271 s1
.insert(1, _T("cc") );
272 s2
.insert(2, _T("cdef"), 3);
274 s4
.insert(2, s10
, 3, 7);
275 s5
.insert(1, 2, _T('c'));
276 s6
.insert(s6
.begin() + 3, _T('X'));
277 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
278 s8
.insert(s8
.begin(), 2, _T('c'));
280 CPPUNIT_ASSERT( s1
== _T("accaaa") );
281 CPPUNIT_ASSERT( s2
== _T("aacdeaa") );
282 CPPUNIT_ASSERT( s3
== _T("aacdefgaa") );
283 CPPUNIT_ASSERT( s4
== _T("aafgaa") );
284 CPPUNIT_ASSERT( s5
== _T("accaaa") );
285 CPPUNIT_ASSERT( s6
== _T("aaaXa") );
286 CPPUNIT_ASSERT( s7
== _T("cdefaaaa") );
287 CPPUNIT_ASSERT( s8
== _T("ccaaaa") );
289 s1
= s2
= s3
= _T("aaaa");
290 s1
.insert(0, _T("ccc"), 2);
291 s2
.insert(4, _T("ccc"), 2);
293 CPPUNIT_ASSERT( s1
== _T("ccaaaa") );
294 CPPUNIT_ASSERT( s2
== _T("aaaacc") );
297 void StdStringTestCase::StdReplace()
299 wxString s1
, s2
, s3
, s4
, s5
, s6
, s7
, s8
, s9
;
301 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
304 s1
.replace(3, 4, _T("rtyu"));
305 s1
.replace(8, 7, _T("opopop"));
306 s2
.replace(10, 12, _T("WWWW"));
307 s3
.replace(1, 5, s9
);
308 s4
.replace(1, 4, s9
, 0, 4);
309 s5
.replace(1, 2, s9
, 1, 12);
310 s6
.replace(0, 123, s9
, 0, 123);
311 s7
.replace(2, 7, s9
);
313 CPPUNIT_ASSERT( s1
== _T("QWErtyuIopopop") );
314 CPPUNIT_ASSERT( s2
== _T("QWERTYUIOPWWWW") );
315 CPPUNIT_ASSERT( s3
== _T("QwertyUIOP") );
316 CPPUNIT_ASSERT( s4
== _T("QwertYUIOP") );
317 CPPUNIT_ASSERT( s5
== _T("QertyRTYUIOP") );
318 CPPUNIT_ASSERT( s6
== s9
);
319 CPPUNIT_ASSERT( s7
== _T("QWwertyP") );
322 void StdStringTestCase::StdRFind()
325 // 01234567890123456789012345
326 wxString s1
= _T("abcdefgABCDEFGabcABCabcABC");
327 wxString s2
= _T("gAB");
328 wxString s3
= _T("ab");
330 CPPUNIT_ASSERT( s1
.rfind(_T('A')) == 23u );
331 CPPUNIT_ASSERT( s1
.rfind(_T('A'), 7) == 7u );
332 CPPUNIT_ASSERT( s1
.rfind(_T('Z')) == wxString::npos
);
333 CPPUNIT_ASSERT( s1
.rfind(_T('C'), 22) == 19u );
335 CPPUNIT_ASSERT( s1
.rfind(_T("cAB")) == 22u );
336 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 15) == wxString::npos
);
337 CPPUNIT_ASSERT( s1
.rfind(_T("cAB"), 21) == 16u );
339 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 7, 3) == 6u );
340 CPPUNIT_ASSERT( s1
.rfind(_T("gABZZZ"), 5, 3) == wxString::npos
);
342 CPPUNIT_ASSERT( s1
.rfind(s2
) == 6u );
343 CPPUNIT_ASSERT( s1
.rfind(s2
, 5) == wxString::npos
);
344 CPPUNIT_ASSERT( s1
.rfind(s2
, 6) == 6u );
345 CPPUNIT_ASSERT( s1
.rfind(s3
, 1) == 0u );
348 void StdStringTestCase::StdResize()
350 wxString s1
, s2
, s3
, s4
;
352 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
356 s3
.resize( 14, _T(' ') );
357 s4
.resize( 14, _T('W') );
359 CPPUNIT_ASSERT( s1
== _T("abcABCdefDEF") );
360 CPPUNIT_ASSERT( s2
== _T("abcABCdefD") );
361 CPPUNIT_ASSERT( s3
== _T("abcABCdefDEF ") );
362 CPPUNIT_ASSERT( s4
== _T("abcABCdefDEFWW") );
365 void StdStringTestCase::StdSubstr()
367 wxString s1
= _T("abcdefgABCDEFG");
369 CPPUNIT_ASSERT( s1
.substr( 0, 14 ) == s1
);
370 CPPUNIT_ASSERT( s1
.substr( 1, 13 ) == _T("bcdefgABCDEFG") );
371 CPPUNIT_ASSERT( s1
.substr( 1, 20 ) == _T("bcdefgABCDEFG") );
372 CPPUNIT_ASSERT( s1
.substr( 14, 30 ) == _T("") );