]>
Commit | Line | Data |
---|---|---|
0a199299 WS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/strings/stdstrings.cpp | |
3 | // Purpose: wxString unit test | |
4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba | |
5 | // Created: 2004-05-07 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
0a199299 WS |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
0a199299 WS |
24 | // ---------------------------------------------------------------------------- |
25 | // test class | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class StdStringTestCase : public CppUnit::TestCase | |
29 | { | |
30 | public: | |
31 | StdStringTestCase(); | |
32 | ||
33 | private: | |
34 | CPPUNIT_TEST_SUITE( StdStringTestCase ); | |
35 | CPPUNIT_TEST( StdConstructors ); | |
36 | CPPUNIT_TEST( StdAppend ); | |
37 | CPPUNIT_TEST( StdAssign ); | |
38 | CPPUNIT_TEST( StdCompare ); | |
39 | CPPUNIT_TEST( StdErase ); | |
40 | CPPUNIT_TEST( StdFind ); | |
41 | CPPUNIT_TEST( StdFindFirst ); | |
42 | CPPUNIT_TEST( StdFindLast ); | |
43 | CPPUNIT_TEST( StdInsert ); | |
44 | CPPUNIT_TEST( StdReplace ); | |
45 | CPPUNIT_TEST( StdRFind ); | |
46 | CPPUNIT_TEST( StdResize ); | |
968f291b | 47 | CPPUNIT_TEST( StdRiter ); |
0a199299 | 48 | CPPUNIT_TEST( StdSubstr ); |
59953bf4 VS |
49 | #if wxUSE_STD_STRING |
50 | CPPUNIT_TEST( StdConversion ); | |
51 | #endif | |
0a199299 WS |
52 | CPPUNIT_TEST_SUITE_END(); |
53 | ||
54 | void StdConstructors(); | |
55 | void StdAppend(); | |
56 | void StdAssign(); | |
57 | void StdCompare(); | |
58 | void StdErase(); | |
59 | void StdFind(); | |
60 | void StdFindFirst(); | |
61 | void StdFindLast(); | |
62 | void StdInsert(); | |
63 | void StdReplace(); | |
64 | void StdRFind(); | |
65 | void StdResize(); | |
968f291b | 66 | void StdRiter(); |
0a199299 | 67 | void StdSubstr(); |
59953bf4 VS |
68 | #if wxUSE_STD_STRING |
69 | void StdConversion(); | |
70 | #endif | |
0a199299 WS |
71 | |
72 | DECLARE_NO_COPY_CLASS(StdStringTestCase) | |
73 | }; | |
74 | ||
75 | // register in the unnamed registry so that these tests are run by default | |
76 | CPPUNIT_TEST_SUITE_REGISTRATION( StdStringTestCase ); | |
77 | ||
78 | // also include in it's own registry so that these tests can be run alone | |
79 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStringTestCase, "StdStringTestCase" ); | |
80 | ||
81 | StdStringTestCase::StdStringTestCase() | |
82 | { | |
83 | } | |
84 | ||
85 | void StdStringTestCase::StdConstructors() | |
86 | { | |
87 | wxString s1(_T("abcdefgh")), | |
88 | s2(_T("abcdefghijklm"), 8), | |
89 | s3(_T("abcdefghijklm")), | |
90 | s4(8, _T('a')); | |
91 | wxString s5(s1), | |
92 | s6(s3, 0, 8), | |
93 | s7(s3.begin(), s3.begin() + 8); | |
94 | wxString s8(s1, 4, 8); | |
95 | ||
96 | CPPUNIT_ASSERT( s1 == _T("abcdefgh") ); | |
97 | CPPUNIT_ASSERT( s2 == s1 ); | |
98 | CPPUNIT_ASSERT( s4 == _T("aaaaaaaa") ); | |
99 | CPPUNIT_ASSERT( s5 == _T("abcdefgh") ); | |
100 | CPPUNIT_ASSERT( s6 == s1 ); | |
101 | CPPUNIT_ASSERT( s7 == s1 ); | |
102 | CPPUNIT_ASSERT( s8 == _T("efgh") ); | |
f2a1b1bd VZ |
103 | |
104 | const char *pc = s1; | |
105 | WX_ASSERT_STR_EQUAL( "bcd", wxString(pc + 1, pc + 4) ); | |
106 | ||
107 | const wchar_t *pw = s2.c_str(); | |
108 | WX_ASSERT_STR_EQUAL( "a", wxString(pw, pw + 1) ); | |
0a199299 WS |
109 | } |
110 | ||
111 | void StdStringTestCase::StdAppend() | |
112 | { | |
113 | wxString s1, s2, s3, s4, s5, s6, s7, s8; | |
114 | ||
f2a1b1bd | 115 | s1 = s2 = s3 = s4 = s5 = s6 = _T("abc"); |
0a199299 WS |
116 | s1.append(_T("def")); |
117 | s2.append(_T("defgh"), 3); | |
118 | s3.append(wxString(_T("abcdef")), 3, 6); | |
119 | s4.append(s1); | |
120 | s5.append(3, _T('a')); | |
d483da3c VS |
121 | s5.append(2, 'x'); |
122 | s5.append(1, (unsigned char)'y'); | |
0a199299 WS |
123 | s6.append(s1.begin() + 3, s1.end()); |
124 | ||
125 | CPPUNIT_ASSERT( s1 == _T("abcdef") ); | |
126 | CPPUNIT_ASSERT( s2 == _T("abcdef") ); | |
127 | CPPUNIT_ASSERT( s3 == _T("abcdef") ); | |
128 | CPPUNIT_ASSERT( s4 == _T("abcabcdef") ); | |
d483da3c | 129 | CPPUNIT_ASSERT( s5 == _T("abcaaaxxy") ); |
0a199299 | 130 | CPPUNIT_ASSERT( s6 == _T("abcdef") ); |
7634e443 | 131 | |
f2a1b1bd VZ |
132 | const char *pc = s1.c_str() + 2; |
133 | s7.append(pc, pc + 4); | |
134 | WX_ASSERT_STR_EQUAL( "cdef", s7 ); | |
135 | ||
136 | const wchar_t *pw = s2.c_str() + 2; | |
137 | s8.append(pw, pw + 4); | |
138 | WX_ASSERT_STR_EQUAL( "cdef", s8 ); | |
139 | ||
7634e443 RN |
140 | s7 = s8 = wxString(_T("null\0time"), 9); |
141 | ||
142 | s7.append(_T("def")); | |
143 | s8.append(_T("defgh"), 3); | |
144 | ||
145 | CPPUNIT_ASSERT( s7 == wxString(_T("null\0timedef"), 12) ); | |
146 | CPPUNIT_ASSERT( s8 == wxString(_T("null\0timedef"), 12) ); | |
0a199299 WS |
147 | } |
148 | ||
149 | void StdStringTestCase::StdAssign() | |
150 | { | |
151 | wxString s1, s2, s3, s4, s5, s6, s7, s8; | |
152 | ||
153 | s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc"); | |
154 | s1.assign(_T("def")); | |
155 | s2.assign(_T("defgh"), 3); | |
156 | s3.assign(wxString(_T("abcdef")), 3, 6); | |
157 | s4.assign(s1); | |
158 | s5.assign(3, _T('a')); | |
159 | s6.assign(s1.begin() + 1, s1.end()); | |
160 | ||
161 | CPPUNIT_ASSERT( s1 == _T("def") ); | |
162 | CPPUNIT_ASSERT( s2 == _T("def") ); | |
163 | CPPUNIT_ASSERT( s3 == _T("def") ); | |
164 | CPPUNIT_ASSERT( s4 == _T("def") ); | |
165 | CPPUNIT_ASSERT( s5 == _T("aaa") ); | |
166 | CPPUNIT_ASSERT( s6 == _T("ef") ); | |
f2a1b1bd VZ |
167 | |
168 | const char *pc = s1; | |
169 | s7.assign(pc, pc + 2); | |
170 | WX_ASSERT_STR_EQUAL( "de", s7 ); | |
171 | ||
172 | const wchar_t *pw = s1.c_str(); | |
173 | s8.assign(pw + 2, pw + 3); | |
174 | WX_ASSERT_STR_EQUAL( "f", s8 ); | |
0a199299 WS |
175 | } |
176 | ||
177 | void StdStringTestCase::StdCompare() | |
178 | { | |
179 | wxString s1, s2, s3, s4, s5, s6, s7, s8; | |
180 | ||
181 | s1 = _T("abcdefgh"); | |
182 | s2 = _T("abcdefgh"); | |
183 | s3 = _T("abc"); | |
184 | s4 = _T("abcdefghi"); | |
185 | s5 = _T("aaa"); | |
186 | s6 = _T("zzz"); | |
187 | ||
188 | CPPUNIT_ASSERT( s1.compare(s2) == 0 ); | |
189 | CPPUNIT_ASSERT( s1.compare(s3) > 0 ); | |
190 | CPPUNIT_ASSERT( s1.compare(s4) < 0 ); | |
191 | CPPUNIT_ASSERT( s1.compare(s5) > 0 ); | |
192 | CPPUNIT_ASSERT( s1.compare(s6) < 0 ); | |
193 | CPPUNIT_ASSERT( s1.compare(1, 12, s1) > 0); | |
194 | CPPUNIT_ASSERT( s1.compare(_T("abcdefgh")) == 0); | |
195 | CPPUNIT_ASSERT( s1.compare(1, 7, _T("bcdefgh")) == 0); | |
196 | CPPUNIT_ASSERT( s1.compare(1, 7, _T("bcdefgh"), 7) == 0); | |
197 | } | |
198 | ||
199 | void StdStringTestCase::StdErase() | |
200 | { | |
201 | wxString s1, s2, s3, s4, s5, s6, s7; | |
202 | ||
203 | s1 = _T("abcdefgh"); | |
204 | s2 = _T("abcdefgh"); | |
205 | s3 = _T("abc"); | |
206 | s4 = _T("abcdefghi"); | |
207 | s5 = _T("aaa"); | |
208 | s6 = _T("zzz"); | |
209 | s7 = _T("zabcdefg"); | |
210 | ||
211 | s1.erase(1, 1); | |
212 | s2.erase(4, 12); | |
213 | wxString::iterator it = s3.erase(s3.begin() + 1); | |
214 | wxString::iterator it2 = s4.erase(s4.begin() + 4, s4.begin() + 6); | |
215 | wxString::iterator it3 = s7.erase(s7.begin() + 4, s7.begin() + 8); | |
216 | ||
217 | CPPUNIT_ASSERT( s1 == _T("acdefgh") ); | |
218 | CPPUNIT_ASSERT( s2 == _T("abcd") ); | |
219 | CPPUNIT_ASSERT( s3 == _T("ac") ); | |
220 | CPPUNIT_ASSERT( s4 == _T("abcdghi") ); | |
221 | CPPUNIT_ASSERT( s7 == _T("zabc") ); | |
222 | CPPUNIT_ASSERT( *it == _T('c') ); | |
223 | CPPUNIT_ASSERT( *it2 == _T('g') ); | |
224 | CPPUNIT_ASSERT( it3 == s7.end() ); | |
225 | } | |
226 | ||
227 | void StdStringTestCase::StdFind() | |
228 | { | |
229 | // 0 1 2 | |
230 | // 01234567890123456789012345 | |
231 | wxString s1 = _T("abcdefgABCDEFGabcABCabcABC"); | |
232 | wxString s2 = _T("gAB"); | |
233 | ||
234 | CPPUNIT_ASSERT( s1.find(_T('A')) == 7u ); | |
235 | CPPUNIT_ASSERT( s1.find(_T('A'), 7) == 7u ); | |
236 | CPPUNIT_ASSERT( s1.find(_T('Z')) == wxString::npos ); | |
237 | CPPUNIT_ASSERT( s1.find(_T('C'), 22) == 25u ); | |
238 | ||
239 | CPPUNIT_ASSERT( s1.find(_T("gAB")) == 6u ); | |
240 | CPPUNIT_ASSERT( s1.find(_T("gAB"), 7) == wxString::npos ); | |
241 | CPPUNIT_ASSERT( s1.find(_T("gAB"), 6) == 6u ); | |
242 | ||
243 | CPPUNIT_ASSERT( s1.find(_T("gABZZZ"), 2, 3) == 6u ); | |
244 | CPPUNIT_ASSERT( s1.find(_T("gABZZZ"), 7, 3) == wxString::npos ); | |
245 | ||
246 | CPPUNIT_ASSERT( s1.find(s2) == 6u ); | |
247 | CPPUNIT_ASSERT( s1.find(s2, 7) == wxString::npos ); | |
248 | CPPUNIT_ASSERT( s1.find(s2, 6) == 6u ); | |
968f291b | 249 | |
dcb68102 RN |
250 | // 0 1 2 |
251 | // 0123456 78901234567 8901234567 | |
252 | //wxString _s1 = _T("abcdefg\0ABCDEFGabc\0ABCabcABC"); | |
253 | //wxString _s2 = _T("g\0AB"); | |
254 | wxString _s1 = _T("abcdefgABCDEFGabcABCabcABC"); | |
255 | wxString _s2 = _T("gAB"); | |
968f291b | 256 | |
dcb68102 RN |
257 | _s1.insert(7, 1, '\0'); |
258 | _s1.insert(18, 1, '\0'); | |
259 | _s2.insert(1, 1, '\0'); | |
968f291b | 260 | |
dcb68102 RN |
261 | CPPUNIT_ASSERT( _s1.find(_T('A')) == 8u ); |
262 | CPPUNIT_ASSERT( _s1.find(_T('A'), 8) == 8u ); | |
263 | CPPUNIT_ASSERT( _s1.find(_T('Z')) == wxString::npos ); | |
264 | CPPUNIT_ASSERT( _s1.find(_T('C'), 22) == 27u ); | |
265 | ||
266 | CPPUNIT_ASSERT( _s1.find(_T("AB")) == 8u ); | |
267 | CPPUNIT_ASSERT( _s1.find(_T("AB"), 26) == wxString::npos ); | |
268 | CPPUNIT_ASSERT( _s1.find(_T("AB"), 23) == 25u ); | |
269 | ||
270 | CPPUNIT_ASSERT( _s1.find(_T("ABZZZ"), 2, 2) == 8u ); | |
271 | CPPUNIT_ASSERT( _s1.find(_T("ABZZZ"), 26, 2) == wxString::npos ); | |
272 | ||
273 | CPPUNIT_ASSERT( _s1.find(_s2) == 6u ); | |
274 | CPPUNIT_ASSERT( _s1.find(_s2, 7) == wxString::npos ); | |
275 | CPPUNIT_ASSERT( _s1.find(_s2, 6) == 6u ); | |
0a199299 WS |
276 | } |
277 | ||
278 | void StdStringTestCase::StdFindFirst() | |
279 | { | |
280 | // 0 1 2 3 | |
281 | // 01234567890123456789012345678901234 | |
282 | wxString s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd"); | |
283 | wxString s2 = _T("aaaaaa"); | |
284 | ||
285 | CPPUNIT_ASSERT( s1.find_first_not_of(_T('a')) == 6u ); | |
286 | CPPUNIT_ASSERT( s1.find_first_not_of(_T('a'), 7) == 7u ); | |
287 | CPPUNIT_ASSERT( s2.find_first_not_of(_T('a')) == wxString::npos ); | |
288 | ||
289 | CPPUNIT_ASSERT( s1.find_first_not_of(_T("abde"), 4) == 7u ); | |
290 | CPPUNIT_ASSERT( s1.find_first_not_of(_T("abde"), 7) == 7u ); | |
291 | CPPUNIT_ASSERT( s1.find_first_not_of(_T("abcdefghijkl")) == wxString::npos ); | |
292 | ||
293 | CPPUNIT_ASSERT( s1.find_first_not_of(_T("abcdefghi"), 0, 4) == 9u ); | |
294 | ||
295 | CPPUNIT_ASSERT( s1.find_first_of(_T('c')) == 7u ); | |
296 | CPPUNIT_ASSERT( s1.find_first_of(_T('v')) == wxString::npos ); | |
297 | CPPUNIT_ASSERT( s1.find_first_of(_T('c'), 10) == 24u ); | |
298 | ||
299 | CPPUNIT_ASSERT( s1.find_first_of(_T("ijkl")) == 13u ); | |
300 | CPPUNIT_ASSERT( s1.find_first_of(_T("ddcfg"), 17) == 24u ); | |
301 | CPPUNIT_ASSERT( s1.find_first_of(_T("ddcfga"), 17, 5) == 24u ); | |
302 | } | |
303 | ||
304 | void StdStringTestCase::StdFindLast() | |
305 | { | |
306 | // 0 1 2 3 | |
307 | // 01234567890123456789012345678901234 | |
308 | wxString s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd"); | |
309 | wxString s2 = _T("aaaaaa"); | |
310 | ||
311 | CPPUNIT_ASSERT( s2.find_last_not_of(_T('a')) == wxString::npos ); | |
312 | CPPUNIT_ASSERT( s1.find_last_not_of(_T('d')) == 33u ); | |
313 | CPPUNIT_ASSERT( s1.find_last_not_of(_T('d'), 25) == 24u ); | |
314 | ||
315 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("bcd")) == 22u ); | |
316 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("abc"), 24) == 16u ); | |
317 | ||
318 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3) == 16u ); | |
319 | ||
320 | CPPUNIT_ASSERT( s2.find_last_of(_T('c')) == wxString::npos ); | |
321 | CPPUNIT_ASSERT( s1.find_last_of(_T('a')) == 22u ); | |
322 | CPPUNIT_ASSERT( s1.find_last_of(_T('b'), 24) == 23u ); | |
323 | ||
324 | CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 16u ); | |
325 | CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 33, 4) == 16u ); | |
326 | CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 17) == 17u ); | |
422d306c RN |
327 | |
328 | ||
329 | ||
330 | // 0 1 2 3 | |
331 | // 012345 67890123456789 01234567890123456 | |
332 | // wxString s1 = _T("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd"); | |
333 | // wxString s2 = _T("aaaaaa\0"); | |
334 | s1.insert(6,1,'\0'); | |
335 | s1.insert(20,1,'\0'); | |
336 | s2.insert(6,1,'\0'); | |
337 | ||
338 | CPPUNIT_ASSERT( s2.find_last_not_of(_T('a')) == 6u ); | |
339 | CPPUNIT_ASSERT( s1.find_last_not_of(_T('d')) == 35u ); | |
340 | CPPUNIT_ASSERT( s1.find_last_not_of(_T('d'), 27) == 26u ); | |
341 | ||
342 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("bcd")) == 24u ); | |
343 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("abc"), 26) == 20u ); | |
344 | ||
345 | CPPUNIT_ASSERT( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 26, 3) == 20u ); | |
346 | ||
347 | CPPUNIT_ASSERT( s2.find_last_of(_T('c')) == wxString::npos ); | |
348 | CPPUNIT_ASSERT( s1.find_last_of(_T('a')) == 24u ); | |
349 | CPPUNIT_ASSERT( s1.find_last_of(_T('b'), 26) == 25u ); | |
350 | ||
351 | CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 17u ); | |
352 | CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 35, 4) == 17u ); | |
353 | CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 18) == 18u ); | |
0a199299 WS |
354 | } |
355 | ||
356 | void StdStringTestCase::StdInsert() | |
357 | { | |
358 | wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10; | |
359 | ||
360 | s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("aaaa"); | |
361 | s9 = s10 = _T("cdefg"); | |
362 | ||
363 | s1.insert(1, _T("cc") ); | |
364 | s2.insert(2, _T("cdef"), 3); | |
365 | s3.insert(2, s10); | |
366 | s4.insert(2, s10, 3, 7); | |
367 | s5.insert(1, 2, _T('c')); | |
368 | s6.insert(s6.begin() + 3, _T('X')); | |
369 | s7.insert(s7.begin(), s9.begin(), s9.end() - 1); | |
370 | s8.insert(s8.begin(), 2, _T('c')); | |
371 | ||
372 | CPPUNIT_ASSERT( s1 == _T("accaaa") ); | |
373 | CPPUNIT_ASSERT( s2 == _T("aacdeaa") ); | |
374 | CPPUNIT_ASSERT( s3 == _T("aacdefgaa") ); | |
375 | CPPUNIT_ASSERT( s4 == _T("aafgaa") ); | |
376 | CPPUNIT_ASSERT( s5 == _T("accaaa") ); | |
377 | CPPUNIT_ASSERT( s6 == _T("aaaXa") ); | |
378 | CPPUNIT_ASSERT( s7 == _T("cdefaaaa") ); | |
379 | CPPUNIT_ASSERT( s8 == _T("ccaaaa") ); | |
380 | ||
381 | s1 = s2 = s3 = _T("aaaa"); | |
382 | s1.insert(0, _T("ccc"), 2); | |
383 | s2.insert(4, _T("ccc"), 2); | |
384 | ||
385 | CPPUNIT_ASSERT( s1 == _T("ccaaaa") ); | |
386 | CPPUNIT_ASSERT( s2 == _T("aaaacc") ); | |
387 | } | |
388 | ||
389 | void StdStringTestCase::StdReplace() | |
390 | { | |
391 | wxString s1, s2, s3, s4, s5, s6, s7, s8, s9; | |
392 | ||
393 | s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("QWERTYUIOP"); | |
394 | s9 = _T("werty"); | |
395 | ||
396 | s1.replace(3, 4, _T("rtyu")); | |
397 | s1.replace(8, 7, _T("opopop")); | |
398 | s2.replace(10, 12, _T("WWWW")); | |
399 | s3.replace(1, 5, s9); | |
400 | s4.replace(1, 4, s9, 0, 4); | |
401 | s5.replace(1, 2, s9, 1, 12); | |
402 | s6.replace(0, 123, s9, 0, 123); | |
403 | s7.replace(2, 7, s9); | |
404 | ||
405 | CPPUNIT_ASSERT( s1 == _T("QWErtyuIopopop") ); | |
406 | CPPUNIT_ASSERT( s2 == _T("QWERTYUIOPWWWW") ); | |
407 | CPPUNIT_ASSERT( s3 == _T("QwertyUIOP") ); | |
408 | CPPUNIT_ASSERT( s4 == _T("QwertYUIOP") ); | |
409 | CPPUNIT_ASSERT( s5 == _T("QertyRTYUIOP") ); | |
410 | CPPUNIT_ASSERT( s6 == s9); | |
411 | CPPUNIT_ASSERT( s7 == _T("QWwertyP") ); | |
412 | } | |
413 | ||
414 | void StdStringTestCase::StdRFind() | |
415 | { | |
416 | // 0 1 2 | |
417 | // 01234567890123456789012345 | |
418 | wxString s1 = _T("abcdefgABCDEFGabcABCabcABC"); | |
419 | wxString s2 = _T("gAB"); | |
420 | wxString s3 = _T("ab"); | |
421 | ||
422 | CPPUNIT_ASSERT( s1.rfind(_T('A')) == 23u ); | |
423 | CPPUNIT_ASSERT( s1.rfind(_T('A'), 7) == 7u ); | |
424 | CPPUNIT_ASSERT( s1.rfind(_T('Z')) == wxString::npos ); | |
425 | CPPUNIT_ASSERT( s1.rfind(_T('C'), 22) == 19u ); | |
426 | ||
427 | CPPUNIT_ASSERT( s1.rfind(_T("cAB")) == 22u ); | |
428 | CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 15) == wxString::npos ); | |
429 | CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 21) == 16u ); | |
430 | ||
431 | CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 7, 3) == 6u ); | |
432 | CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 5, 3) == wxString::npos ); | |
433 | ||
434 | CPPUNIT_ASSERT( s1.rfind(s2) == 6u ); | |
435 | CPPUNIT_ASSERT( s1.rfind(s2, 5) == wxString::npos ); | |
436 | CPPUNIT_ASSERT( s1.rfind(s2, 6) == 6u ); | |
437 | CPPUNIT_ASSERT( s1.rfind(s3, 1) == 0u ); | |
dcb68102 RN |
438 | |
439 | ||
440 | // 0 1 2 | |
441 | // 01234 56789012 345678901234567 | |
442 | // wxString s1 = _T("abcde\0fgABCDE\0FGabcABCabcABC"); | |
443 | // wxString s2 = _T("gAB"); | |
444 | // wxString s3 = _T("ab"); | |
968f291b | 445 | |
dcb68102 RN |
446 | s1.insert(5,1,'\0'); |
447 | s1.insert(13,1,'\0'); | |
448 | ||
449 | CPPUNIT_ASSERT( s1.rfind(_T('A')) == 25u ); | |
450 | CPPUNIT_ASSERT( s1.rfind(_T('A'), 8) == 8u ); | |
451 | CPPUNIT_ASSERT( s1.rfind(_T('Z')) == wxString::npos ); | |
452 | CPPUNIT_ASSERT( s1.rfind(_T('C'), 22) == 21u ); | |
453 | ||
454 | CPPUNIT_ASSERT( s1.rfind(_T("cAB")) == 24u ); | |
455 | CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 15) == wxString::npos ); | |
456 | CPPUNIT_ASSERT( s1.rfind(_T("cAB"), 21) == 18u ); | |
457 | ||
458 | CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 8, 3) == 7u ); | |
459 | CPPUNIT_ASSERT( s1.rfind(_T("gABZZZ"), 5, 3) == wxString::npos ); | |
0a199299 WS |
460 | } |
461 | ||
462 | void StdStringTestCase::StdResize() | |
463 | { | |
464 | wxString s1, s2, s3, s4; | |
465 | ||
466 | s1 = s2 = s3 = s4 = _T("abcABCdefDEF"); | |
467 | ||
468 | s1.resize( 12 ); | |
469 | s2.resize( 10 ); | |
470 | s3.resize( 14, _T(' ') ); | |
471 | s4.resize( 14, _T('W') ); | |
472 | ||
473 | CPPUNIT_ASSERT( s1 == _T("abcABCdefDEF") ); | |
474 | CPPUNIT_ASSERT( s2 == _T("abcABCdefD") ); | |
475 | CPPUNIT_ASSERT( s3 == _T("abcABCdefDEF ") ); | |
476 | CPPUNIT_ASSERT( s4 == _T("abcABCdefDEFWW") ); | |
477 | } | |
478 | ||
968f291b VZ |
479 | void StdStringTestCase::StdRiter() |
480 | { | |
481 | const wxString s(_T("fozbar")); | |
482 | ||
483 | wxString::const_reverse_iterator ri(s.rbegin()); | |
c9f78968 VS |
484 | CPPUNIT_ASSERT( _T('r') == *ri ); |
485 | CPPUNIT_ASSERT( _T('a') == *++ri ); | |
486 | CPPUNIT_ASSERT( _T('r') == *--ri ); | |
968f291b VZ |
487 | |
488 | ri = s.rend(); | |
489 | ri--; | |
c9f78968 | 490 | CPPUNIT_ASSERT( _T('f') == *ri ); |
968f291b VZ |
491 | |
492 | --ri; | |
c9f78968 | 493 | CPPUNIT_ASSERT( _T('o') == *ri ); |
968f291b VZ |
494 | |
495 | wxString::const_iterator i = ri.base(); | |
c9f78968 | 496 | CPPUNIT_ASSERT( _T('z') == *i ); |
968f291b VZ |
497 | } |
498 | ||
0a199299 WS |
499 | void StdStringTestCase::StdSubstr() |
500 | { | |
501 | wxString s1 = _T("abcdefgABCDEFG"); | |
502 | ||
503 | CPPUNIT_ASSERT( s1.substr( 0, 14 ) == s1 ); | |
504 | CPPUNIT_ASSERT( s1.substr( 1, 13 ) == _T("bcdefgABCDEFG") ); | |
505 | CPPUNIT_ASSERT( s1.substr( 1, 20 ) == _T("bcdefgABCDEFG") ); | |
506 | CPPUNIT_ASSERT( s1.substr( 14, 30 ) == _T("") ); | |
dcb68102 | 507 | |
dcb68102 RN |
508 | s1.insert(3,1,'\0'); |
509 | s1.insert(8,1,'\0'); | |
510 | s1.insert(13,1,'\0'); | |
511 | ||
512 | wxString s2 = wxT("bcdefgABCDEFG"); | |
513 | s2.insert(2,1,'\0'); | |
514 | s2.insert(7,1,'\0'); | |
515 | s2.insert(12,1,'\0'); | |
968f291b | 516 | |
dcb68102 RN |
517 | CPPUNIT_ASSERT( s1.substr( 0, 17 ) == s1 ); |
518 | CPPUNIT_ASSERT( s1.substr( 1, 17 ) == s2 ); | |
519 | CPPUNIT_ASSERT( s1.substr( 1, 20 ) == s2 ); | |
520 | CPPUNIT_ASSERT( s1.substr( 17, 30 ) == _T("") ); | |
0a199299 WS |
521 | } |
522 | ||
59953bf4 VS |
523 | #if wxUSE_STD_STRING |
524 | void StdStringTestCase::StdConversion() | |
525 | { | |
526 | std::string strStd("std::string value"); | |
527 | wxStdWideString strStdWide(L"std::wstring value"); | |
528 | ||
529 | wxString s1(strStd); | |
530 | CPPUNIT_ASSERT( s1 == "std::string value" ); | |
531 | ||
532 | wxString s2(strStdWide); | |
533 | CPPUNIT_ASSERT( s2 == "std::wstring value" ); | |
534 | ||
535 | wxString s3; | |
536 | s3 = strStd; | |
537 | CPPUNIT_ASSERT( s3 == "std::string value" ); | |
538 | s3 = strStdWide; | |
539 | CPPUNIT_ASSERT( s3 == "std::wstring value" ); | |
540 | ||
541 | wxString s4("hello"); | |
542 | std::string s5 = s4; | |
543 | CPPUNIT_ASSERT( s5 == "hello" ); | |
544 | ||
545 | wxStdWideString s6 = s4; | |
546 | CPPUNIT_ASSERT( s6 == "hello" ); | |
547 | } | |
548 | #endif // wxUSE_STD_STRING |