]>
Commit | Line | Data |
---|---|---|
cf1014a2 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/arrays/arrays.cpp | |
3 | // Purpose: wxArray unit test | |
be2c4488 | 4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba |
cf1014a2 VS |
5 | // Created: 2004-04-01 |
6 | // RCS-ID: $Id$ | |
be2c4488 | 7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba |
cf1014a2 VS |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
cf1014a2 VS |
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/dynarray.h" | |
25 | ||
cf1014a2 VS |
26 | // ---------------------------------------------------------------------------- |
27 | // helpers for testing values and sizes | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | #define COMPARE_VALUE( array , index , value ) ( array.Item( index ) == value ) | |
31 | ||
32 | #define COMPARE_2_VALUES( array , p0 , p1 ) \ | |
33 | COMPARE_VALUE( array , 0 , p0 ) && \ | |
34 | COMPARE_VALUE( array , 1 , p1 ) | |
35 | ||
36 | #define COMPARE_3_VALUES( array , p0 , p1 , p2 ) \ | |
37 | COMPARE_2_VALUES( array , p0 , p1 ) && \ | |
38 | COMPARE_VALUE( array , 2 , p2 ) | |
39 | ||
40 | #define COMPARE_4_VALUES( array , p0 , p1 , p2 , p3 ) \ | |
41 | COMPARE_3_VALUES( array , p0 , p1 , p2 ) && \ | |
42 | COMPARE_VALUE( array , 3 , p3 ) | |
43 | ||
44 | #define COMPARE_5_VALUES( array , p0 , p1 , p2 , p3 , p4 ) \ | |
45 | COMPARE_4_VALUES( array , p0 , p1 , p2 , p3 ) && \ | |
46 | COMPARE_VALUE( array , 4 , p4 ) | |
47 | ||
48 | #define COMPARE_6_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 ) \ | |
49 | COMPARE_5_VALUES( array , p0 , p1 , p2 , p3 , p4 ) && \ | |
50 | COMPARE_VALUE( array , 5 , p5 ) | |
51 | ||
52 | #define COMPARE_7_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 ) \ | |
53 | COMPARE_6_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 ) && \ | |
54 | COMPARE_VALUE( array , 6 , p6 ) | |
55 | ||
56 | #define COMPARE_8_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 ) \ | |
57 | COMPARE_7_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 ) && \ | |
58 | COMPARE_VALUE( array , 7 , p7 ) | |
59 | ||
60 | #define COMPARE_9_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 ) \ | |
61 | COMPARE_8_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 ) && \ | |
62 | COMPARE_VALUE( array , 8 , p8 ) | |
63 | ||
64 | #define COMPARE_10_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 ) \ | |
65 | COMPARE_9_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 ) && \ | |
66 | COMPARE_VALUE( array , 9 , p9 ) | |
67 | ||
68 | #define COMPARE_COUNT( array , n ) \ | |
69 | ( array.GetCount() == n ) && \ | |
70 | ( array.Last() == array.Item( n - 1 ) ) | |
71 | ||
72 | // ---------------------------------------------------------------------------- | |
73 | // helpers for testing wxObjArray | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
76 | class Bar | |
77 | { | |
78 | public: | |
79 | Bar(const wxString& name) : m_name(name) { ms_bars++; } | |
80 | Bar(const Bar& bar) : m_name(bar.m_name) { ms_bars++; } | |
81 | ~Bar() { ms_bars--; } | |
82 | ||
83 | static size_t GetNumber() { return ms_bars; } | |
84 | ||
86501081 | 85 | const wxChar *GetName() const { return m_name.c_str(); } |
cf1014a2 VS |
86 | |
87 | private: | |
88 | wxString m_name; | |
89 | ||
90 | static size_t ms_bars; | |
91 | }; | |
92 | ||
93 | size_t Bar::ms_bars = 0; | |
94 | ||
95 | WX_DECLARE_OBJARRAY(Bar, ArrayBars); | |
96 | #include "wx/arrimpl.cpp" | |
93a800a9 | 97 | WX_DEFINE_OBJARRAY(ArrayBars) |
cf1014a2 VS |
98 | |
99 | // ---------------------------------------------------------------------------- | |
100 | // helpers for sorting arrays and comparing items | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | int wxCMPFUNC_CONV StringLenCompare(const wxString& first, | |
104 | const wxString& second) | |
105 | { | |
106 | return first.length() - second.length(); | |
107 | } | |
108 | ||
109 | #define DEFINE_COMPARE(name, T) \ | |
110 | \ | |
111 | int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \ | |
112 | { \ | |
113 | return first - second; \ | |
114 | } \ | |
115 | \ | |
116 | int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \ | |
117 | { \ | |
118 | return *first - *second; \ | |
119 | } \ | |
120 | \ | |
121 | int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \ | |
122 | { \ | |
123 | return *second - *first; \ | |
124 | } \ | |
125 | ||
126 | typedef unsigned short ushort; | |
127 | ||
93a800a9 VZ |
128 | DEFINE_COMPARE(Char, char) |
129 | DEFINE_COMPARE(UShort, ushort) | |
130 | DEFINE_COMPARE(Int, int) | |
cf1014a2 | 131 | |
1ffc8d7a VZ |
132 | WX_DEFINE_ARRAY_CHAR(char, wxArrayChar); |
133 | WX_DEFINE_SORTED_ARRAY_CHAR(char, wxSortedArrayCharNoCmp); | |
134 | WX_DEFINE_SORTED_ARRAY_CMP_CHAR(char, CharCompareValues, wxSortedArrayChar); | |
135 | ||
cf1014a2 VS |
136 | WX_DEFINE_ARRAY_SHORT(ushort, wxArrayUShort); |
137 | WX_DEFINE_SORTED_ARRAY_SHORT(ushort, wxSortedArrayUShortNoCmp); | |
138 | WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort, UShortCompareValues, wxSortedArrayUShort); | |
1ffc8d7a | 139 | |
cf1014a2 VS |
140 | WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt); |
141 | ||
35d5da67 VZ |
142 | struct Item |
143 | { | |
144 | Item(int n_ = 0) : n(n_) { } | |
145 | ||
146 | int n; | |
147 | }; | |
148 | ||
149 | WX_DEFINE_ARRAY_PTR(Item *, ItemPtrArray); | |
150 | ||
cf1014a2 VS |
151 | // ---------------------------------------------------------------------------- |
152 | // test class | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | class ArraysTestCase : public CppUnit::TestCase | |
156 | { | |
157 | public: | |
158 | ArraysTestCase() { } | |
159 | ||
160 | private: | |
161 | CPPUNIT_TEST_SUITE( ArraysTestCase ); | |
162 | CPPUNIT_TEST( wxStringArrayTest ); | |
0287ae5c | 163 | CPPUNIT_TEST( SortedArray ); |
abbb59e8 VZ |
164 | CPPUNIT_TEST( wxStringArraySplitTest ); |
165 | CPPUNIT_TEST( wxStringArrayJoinTest ); | |
166 | CPPUNIT_TEST( wxStringArraySplitJoinTest ); | |
167 | ||
cf1014a2 VS |
168 | CPPUNIT_TEST( wxObjArrayTest ); |
169 | CPPUNIT_TEST( wxArrayUShortTest ); | |
170 | CPPUNIT_TEST( wxArrayIntTest ); | |
1ffc8d7a | 171 | CPPUNIT_TEST( wxArrayCharTest ); |
cf1014a2 | 172 | CPPUNIT_TEST( TestSTL ); |
7788fc40 | 173 | CPPUNIT_TEST( Alloc ); |
0c61ab6f | 174 | CPPUNIT_TEST( Clear ); |
91276868 | 175 | CPPUNIT_TEST( Swap ); |
23f32cd7 | 176 | CPPUNIT_TEST( IndexFromEnd ); |
cf1014a2 VS |
177 | CPPUNIT_TEST_SUITE_END(); |
178 | ||
179 | void wxStringArrayTest(); | |
0287ae5c | 180 | void SortedArray(); |
abbb59e8 VZ |
181 | void wxStringArraySplitTest(); |
182 | void wxStringArrayJoinTest(); | |
183 | void wxStringArraySplitJoinTest(); | |
cf1014a2 VS |
184 | void wxObjArrayTest(); |
185 | void wxArrayUShortTest(); | |
186 | void wxArrayIntTest(); | |
1ffc8d7a | 187 | void wxArrayCharTest(); |
cf1014a2 | 188 | void TestSTL(); |
7788fc40 | 189 | void Alloc(); |
0c61ab6f | 190 | void Clear(); |
91276868 | 191 | void Swap(); |
23f32cd7 | 192 | void IndexFromEnd(); |
cf1014a2 VS |
193 | |
194 | DECLARE_NO_COPY_CLASS(ArraysTestCase) | |
195 | }; | |
196 | ||
197 | // register in the unnamed registry so that these tests are run by default | |
198 | CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase ); | |
199 | ||
e3778b4d | 200 | // also include in its own registry so that these tests can be run alone |
cf1014a2 VS |
201 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" ); |
202 | ||
203 | void ArraysTestCase::wxStringArrayTest() | |
204 | { | |
205 | wxArrayString a1; | |
9a83f860 VZ |
206 | a1.Add(wxT("thermit")); |
207 | a1.Add(wxT("condor")); | |
208 | a1.Add(wxT("lion"), 3); | |
209 | a1.Add(wxT("dog")); | |
210 | a1.Add(wxT("human")); | |
211 | a1.Add(wxT("alligator")); | |
212 | ||
213 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , wxT("thermit") , | |
214 | wxT("condor") , | |
215 | wxT("lion") , | |
216 | wxT("lion") , | |
217 | wxT("lion") , | |
218 | wxT("dog") , | |
219 | wxT("human") , | |
220 | wxT("alligator") ) ); | |
cf1014a2 VS |
221 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) ); |
222 | ||
223 | wxArrayString a2(a1); | |
224 | ||
9a83f860 VZ |
225 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , wxT("thermit") , |
226 | wxT("condor") , | |
227 | wxT("lion") , | |
228 | wxT("lion") , | |
229 | wxT("lion") , | |
230 | wxT("dog") , | |
231 | wxT("human") , | |
232 | wxT("alligator") ) ); | |
cf1014a2 VS |
233 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) ); |
234 | ||
235 | wxSortedArrayString a3(a1); | |
236 | ||
9a83f860 VZ |
237 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , wxT("alligator") , |
238 | wxT("condor") , | |
239 | wxT("dog") , | |
240 | wxT("human") , | |
241 | wxT("lion") , | |
242 | wxT("lion") , | |
243 | wxT("lion") , | |
244 | wxT("thermit") ) ); | |
cf1014a2 VS |
245 | CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) ); |
246 | ||
247 | wxSortedArrayString a4; | |
248 | for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it) | |
249 | a4.Add(*it); | |
250 | ||
9a83f860 VZ |
251 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , wxT("alligator") , |
252 | wxT("condor") , | |
253 | wxT("dog") , | |
254 | wxT("human") , | |
255 | wxT("lion") , | |
256 | wxT("lion") , | |
257 | wxT("lion") , | |
258 | wxT("thermit") ) ); | |
cf1014a2 VS |
259 | CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) ); |
260 | ||
261 | a1.RemoveAt(2,3); | |
262 | ||
9a83f860 VZ |
263 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("thermit") , |
264 | wxT("condor") , | |
265 | wxT("dog") , | |
266 | wxT("human") , | |
267 | wxT("alligator") ) ); | |
cf1014a2 VS |
268 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); |
269 | ||
270 | a2 = a1; | |
271 | ||
9a83f860 VZ |
272 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , wxT("thermit") , |
273 | wxT("condor") , | |
274 | wxT("dog") , | |
275 | wxT("human") , | |
276 | wxT("alligator") ) ); | |
cf1014a2 VS |
277 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) ); |
278 | ||
279 | a1.Sort(false); | |
280 | ||
9a83f860 VZ |
281 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("alligator") , |
282 | wxT("condor") , | |
283 | wxT("dog") , | |
284 | wxT("human") , | |
285 | wxT("thermit") ) ); | |
cf1014a2 VS |
286 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); |
287 | ||
288 | a1.Sort(true); | |
289 | ||
9a83f860 VZ |
290 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("thermit") , |
291 | wxT("human") , | |
292 | wxT("dog") , | |
293 | wxT("condor") , | |
294 | wxT("alligator") ) ); | |
cf1014a2 VS |
295 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); |
296 | ||
297 | a1.Sort(&StringLenCompare); | |
298 | ||
9a83f860 VZ |
299 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("dog") , |
300 | wxT("human") , | |
301 | wxT("condor") , | |
302 | wxT("thermit") , | |
303 | wxT("alligator") ) ); | |
cf1014a2 | 304 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); |
9a83f860 VZ |
305 | CPPUNIT_ASSERT( a1.Index( wxT("dog") ) == 0 ); |
306 | CPPUNIT_ASSERT( a1.Index( wxT("human") ) == 1 ); | |
307 | CPPUNIT_ASSERT( a1.Index( wxT("humann") ) == wxNOT_FOUND ); | |
308 | CPPUNIT_ASSERT( a1.Index( wxT("condor") ) == 2 ); | |
309 | CPPUNIT_ASSERT( a1.Index( wxT("thermit") ) == 3 ); | |
310 | CPPUNIT_ASSERT( a1.Index( wxT("alligator") ) == 4 ); | |
555d0d7e | 311 | |
23f32cd7 VS |
312 | CPPUNIT_ASSERT( a1.Index( wxT("dog"), /*bCase=*/true, /*fromEnd=*/true ) == 0 ); |
313 | CPPUNIT_ASSERT( a1.Index( wxT("human"), /*bCase=*/true, /*fromEnd=*/true ) == 1 ); | |
314 | CPPUNIT_ASSERT( a1.Index( wxT("humann"), /*bCase=*/true, /*fromEnd=*/true ) == wxNOT_FOUND ); | |
315 | CPPUNIT_ASSERT( a1.Index( wxT("condor"), /*bCase=*/true, /*fromEnd=*/true ) == 2 ); | |
316 | CPPUNIT_ASSERT( a1.Index( wxT("thermit"), /*bCase=*/true, /*fromEnd=*/true ) == 3 ); | |
317 | CPPUNIT_ASSERT( a1.Index( wxT("alligator"), /*bCase=*/true, /*fromEnd=*/true ) == 4 ); | |
318 | ||
555d0d7e MB |
319 | wxArrayString a5; |
320 | ||
9a83f860 VZ |
321 | CPPUNIT_ASSERT( a5.Add( wxT("x"), 1 ) == 0 ); |
322 | CPPUNIT_ASSERT( a5.Add( wxT("a"), 3 ) == 1 ); | |
555d0d7e | 323 | |
9a83f860 VZ |
324 | CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, wxT("x") , |
325 | wxT("a") , | |
326 | wxT("a") , | |
327 | wxT("a") ) ); | |
d11c9d86 VZ |
328 | |
329 | a5.assign(a1.end(), a1.end()); | |
330 | CPPUNIT_ASSERT( a5.empty() ); | |
331 | ||
332 | a5.assign(a1.begin(), a1.end()); | |
333 | CPPUNIT_ASSERT( a5 == a1 ); | |
334 | ||
335 | #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN | |
336 | const wxString months[] = { "Jan", "Feb", "Mar" }; | |
337 | a5.assign(months, months + WXSIZEOF(months)); | |
338 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(months), a5.size() ); | |
339 | CPPUNIT_ASSERT( COMPARE_3_VALUES(a5, "Jan", "Feb", "Mar") ); | |
340 | #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN | |
3d156e93 VZ |
341 | |
342 | a5.clear(); | |
343 | CPPUNIT_ASSERT_EQUAL( 0, a5.size() ); | |
344 | ||
345 | a5.resize(7, "Foo"); | |
346 | CPPUNIT_ASSERT_EQUAL( 7, a5.size() ); | |
347 | CPPUNIT_ASSERT_EQUAL( "Foo", a5[3] ); | |
348 | ||
349 | a5.resize(3); | |
350 | CPPUNIT_ASSERT_EQUAL( 3, a5.size() ); | |
351 | CPPUNIT_ASSERT_EQUAL( "Foo", a5[2] ); | |
cf1014a2 VS |
352 | } |
353 | ||
0287ae5c VZ |
354 | void ArraysTestCase::SortedArray() |
355 | { | |
356 | wxSortedArrayString a; | |
357 | a.Add("d"); | |
358 | a.Add("c"); | |
359 | CPPUNIT_ASSERT_EQUAL( 0, a.Index("c") ); | |
360 | ||
361 | a.push_back("b"); | |
362 | a.push_back("a"); | |
363 | CPPUNIT_ASSERT_EQUAL( 0, a.Index("a") ); | |
364 | } | |
365 | ||
abbb59e8 VZ |
366 | void ArraysTestCase::wxStringArraySplitTest() |
367 | { | |
368 | // test wxSplit: | |
369 | ||
370 | { | |
371 | wxString str = wxT(",,,,first,second,third,,"); | |
372 | const wxChar *expected[] = | |
373 | { wxT(""), wxT(""), wxT(""), wxT(""), wxT("first"), | |
374 | wxT("second"), wxT("third"), wxT(""), wxT("") }; | |
375 | ||
376 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
377 | wxArrayString realarr(wxSplit(str, wxT(','))); | |
378 | CPPUNIT_ASSERT( exparr == realarr ); | |
379 | } | |
380 | ||
381 | { | |
382 | wxString str = wxT(",\\,first,second,third,"); | |
383 | const wxChar *expected[] = | |
384 | { wxT(""), wxT(",first"), wxT("second"), wxT("third"), wxT("") }; | |
385 | const wxChar *expected2[] = | |
386 | { wxT(""), wxT("\\"), wxT("first"), wxT("second"), wxT("third"), wxT("") }; | |
387 | ||
388 | // escaping on: | |
389 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
390 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
391 | CPPUNIT_ASSERT( exparr == realarr ); | |
392 | ||
393 | // escaping turned off: | |
394 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
395 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
396 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
397 | } | |
398 | ||
399 | { | |
400 | // test is escape characters placed before non-separator character are | |
401 | // just ignored as they should: | |
402 | wxString str = wxT(",\\,,fir\\st,se\\cond\\,,\\third"); | |
403 | const wxChar *expected[] = | |
404 | { wxT(""), wxT(","), wxT("fir\\st"), wxT("se\\cond,"), wxT("\\third") }; | |
405 | const wxChar *expected2[] = | |
406 | { wxT(""), wxT("\\"), wxT(""), wxT("fir\\st"), wxT("se\\cond\\"), | |
407 | wxT(""), wxT("\\third") }; | |
408 | ||
409 | // escaping on: | |
410 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
411 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
412 | CPPUNIT_ASSERT( exparr == realarr ); | |
413 | ||
414 | // escaping turned off: | |
415 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
416 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
417 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
418 | } | |
419 | } | |
420 | ||
421 | void ArraysTestCase::wxStringArrayJoinTest() | |
422 | { | |
423 | // test wxJoin: | |
424 | ||
425 | { | |
426 | const wxChar *arr[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") }; | |
427 | wxString expected = wxT("first,,second,third"); | |
428 | ||
429 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
430 | wxString result = wxJoin(arrstr, wxT(',')); | |
431 | CPPUNIT_ASSERT( expected == result ); | |
432 | } | |
433 | ||
434 | { | |
435 | const wxChar *arr[] = { wxT("first, word"), wxT(",,second"), wxT("third,,") }; | |
436 | wxString expected = wxT("first\\, word,\\,\\,second,third\\,\\,"); | |
437 | wxString expected2 = wxT("first, word,,,second,third,,"); | |
438 | ||
439 | // escaping on: | |
440 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
441 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
442 | CPPUNIT_ASSERT( expected == result ); | |
443 | ||
444 | // escaping turned off: | |
445 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
446 | CPPUNIT_ASSERT( expected2 == result2 ); | |
447 | } | |
448 | ||
449 | { | |
450 | // test is escape characters placed in the original array are just ignored as they should: | |
451 | const wxChar *arr[] = { wxT("first\\, wo\\rd"), wxT("seco\\nd"), wxT("\\third\\") }; | |
452 | wxString expected = wxT("first\\\\, wo\\rd,seco\\nd,\\third\\"); | |
453 | wxString expected2 = wxT("first\\, wo\\rd,seco\\nd,\\third\\"); | |
454 | ||
455 | // escaping on: | |
456 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
457 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
458 | CPPUNIT_ASSERT( expected == result ); | |
459 | ||
460 | // escaping turned off: | |
461 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
462 | CPPUNIT_ASSERT( expected2 == result2 ); | |
463 | } | |
464 | } | |
465 | ||
466 | void ArraysTestCase::wxStringArraySplitJoinTest() | |
467 | { | |
468 | wxChar separators[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'), | |
469 | wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'), | |
470 | wxT('\''), wxT('\n'), wxT('!'), wxT('-') }; | |
471 | ||
472 | // test with a string: split it and then rejoin it: | |
473 | ||
474 | wxString str = wxT("This is a long, long test; if wxSplit and wxJoin do work ") | |
475 | wxT("correctly, then splitting and joining this 'text' _multiple_ ") | |
476 | wxT("times shouldn't cause any loss of content.\n") | |
477 | wxT("This is some latex code: ") | |
478 | wxT("\\func{wxString}{wxJoin}{") | |
479 | wxT("\\param{const wxArray String\\&}{ arr}, ") | |
480 | wxT("\\param{const wxChar}{ sep}, ") | |
481 | wxT("\\param{const wxChar}{ escape = '\\'}}.\n") | |
482 | wxT("This is some HTML code: ") | |
483 | wxT("<html><head><meta http-equiv=\"content-type\">") | |
484 | wxT("<title>Initial page of Mozilla Firefox</title>") | |
485 | wxT("</meta></head></html>"); | |
486 | ||
5098c258 VZ |
487 | size_t i; |
488 | for (i = 0; i < WXSIZEOF(separators); i++) | |
abbb59e8 VZ |
489 | { |
490 | wxArrayString arr = wxSplit(str, separators[i]); | |
491 | CPPUNIT_ASSERT( str == wxJoin(arr, separators[i]) ); | |
492 | } | |
493 | ||
494 | ||
495 | // test with an array: join it and then resplit it: | |
496 | ||
497 | const wxChar *arr[] = | |
498 | { | |
499 | wxT("first, second!"), wxT("this is the third!!"), | |
500 | wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "), | |
501 | wxT("_sixth__token__with_underscores"), wxT("The! Last! One!") | |
502 | }; | |
503 | wxArrayString theArr(WXSIZEOF(arr), arr); | |
504 | ||
5098c258 | 505 | for (i = 0; i < WXSIZEOF(separators); i++) |
abbb59e8 VZ |
506 | { |
507 | wxString string = wxJoin(theArr, separators[i]); | |
508 | CPPUNIT_ASSERT( theArr == wxSplit(string, separators[i]) ); | |
509 | } | |
510 | ||
511 | wxArrayString emptyArray; | |
9a83f860 | 512 | wxString string = wxJoin(emptyArray, wxT(';')); |
abbb59e8 VZ |
513 | CPPUNIT_ASSERT( string.empty() ); |
514 | ||
9a83f860 | 515 | CPPUNIT_ASSERT( wxSplit(string, wxT(';')).empty() ); |
abbb59e8 | 516 | |
9a83f860 | 517 | CPPUNIT_ASSERT_EQUAL( 2, wxSplit(wxT(";"), wxT(';')).size() ); |
abbb59e8 VZ |
518 | } |
519 | ||
cf1014a2 VS |
520 | void ArraysTestCase::wxObjArrayTest() |
521 | { | |
522 | { | |
523 | ArrayBars bars; | |
9a83f860 | 524 | Bar bar(wxT("first bar in general, second bar in array (two copies!)")); |
cf1014a2 | 525 | |
7ee34f2b VZ |
526 | CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() ); |
527 | CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() ); | |
cf1014a2 | 528 | |
9a83f860 | 529 | bars.Add(new Bar(wxT("first bar in array"))); |
0c61ab6f | 530 | bars.Add(bar, 2); |
cf1014a2 | 531 | |
7ee34f2b VZ |
532 | CPPUNIT_ASSERT_EQUAL( 3, bars.GetCount() ); |
533 | CPPUNIT_ASSERT_EQUAL( 4, Bar::GetNumber() ); | |
cf1014a2 VS |
534 | |
535 | bars.RemoveAt(1, bars.GetCount() - 1); | |
536 | ||
7ee34f2b VZ |
537 | CPPUNIT_ASSERT_EQUAL( 1, bars.GetCount() ); |
538 | CPPUNIT_ASSERT_EQUAL( 2, Bar::GetNumber() ); | |
cf1014a2 VS |
539 | |
540 | bars.Empty(); | |
541 | ||
7ee34f2b VZ |
542 | CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() ); |
543 | CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() ); | |
cf1014a2 | 544 | } |
7ee34f2b | 545 | CPPUNIT_ASSERT_EQUAL( 0, Bar::GetNumber() ); |
cf1014a2 VS |
546 | } |
547 | ||
548 | #define TestArrayOf(name) \ | |
549 | \ | |
550 | void ArraysTestCase::wxArray ## name ## Test() \ | |
551 | { \ | |
552 | wxArray##name a; \ | |
553 | a.Add(1); \ | |
554 | a.Add(17,2); \ | |
555 | a.Add(5,3); \ | |
556 | a.Add(3,4); \ | |
557 | \ | |
558 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \ | |
559 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
560 | \ | |
561 | a.Sort(name ## Compare); \ | |
562 | \ | |
563 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \ | |
564 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
565 | \ | |
566 | a.Sort(name ## RevCompare); \ | |
567 | \ | |
568 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \ | |
569 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
570 | \ | |
571 | wxSortedArray##name b; \ | |
572 | \ | |
573 | b.Add(1); \ | |
574 | b.Add(17); \ | |
575 | b.Add(5); \ | |
576 | b.Add(3); \ | |
577 | \ | |
578 | CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \ | |
579 | CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \ | |
10dcbe63 MB |
580 | CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \ |
581 | CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \ | |
582 | CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \ | |
583 | CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \ | |
584 | CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \ | |
585 | CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \ | |
586 | CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \ | |
cf1014a2 VS |
587 | } |
588 | ||
93a800a9 | 589 | TestArrayOf(UShort) |
cf1014a2 | 590 | |
93a800a9 | 591 | TestArrayOf(Char) |
1ffc8d7a | 592 | |
93a800a9 | 593 | TestArrayOf(Int) |
cf1014a2 | 594 | |
7788fc40 VZ |
595 | void ArraysTestCase::Alloc() |
596 | { | |
597 | wxArrayInt a; | |
598 | a.Add(17); | |
599 | a.Add(9); | |
10d256d6 | 600 | CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() ); |
7788fc40 VZ |
601 | |
602 | a.Alloc(1000); | |
603 | ||
10d256d6 | 604 | CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() ); |
7788fc40 VZ |
605 | CPPUNIT_ASSERT_EQUAL( 17, a[0] ); |
606 | CPPUNIT_ASSERT_EQUAL( 9, a[1] ); | |
607 | } | |
608 | ||
0c61ab6f VZ |
609 | void ArraysTestCase::Clear() |
610 | { | |
611 | ItemPtrArray items; | |
612 | ||
613 | WX_CLEAR_ARRAY(items); | |
614 | CPPUNIT_ASSERT_EQUAL( 0, items.size() ); | |
615 | ||
616 | items.push_back(new Item(17)); | |
617 | items.push_back(new Item(71)); | |
618 | CPPUNIT_ASSERT_EQUAL( 2, items.size() ); | |
619 | ||
620 | WX_CLEAR_ARRAY(items); | |
621 | CPPUNIT_ASSERT_EQUAL( 0, items.size() ); | |
622 | } | |
623 | ||
ff2201cc VZ |
624 | namespace |
625 | { | |
626 | ||
627 | template <typename A, typename T> | |
628 | void DoTestSwap(T v1, T v2, T v3, | |
629 | A * WXUNUSED(dummyUglyVC6Workaround)) | |
91276868 | 630 | { |
ff2201cc | 631 | A a1, a2; |
91276868 VZ |
632 | a1.swap(a2); |
633 | CPPUNIT_ASSERT( a1.empty() && a2.empty() ); | |
634 | ||
ff2201cc | 635 | a1.push_back(v1); |
91276868 VZ |
636 | a1.swap(a2); |
637 | CPPUNIT_ASSERT( a1.empty() ); | |
638 | CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); | |
639 | ||
ff2201cc VZ |
640 | a1.push_back(v2); |
641 | a1.push_back(v3); | |
91276868 VZ |
642 | a2.swap(a1); |
643 | CPPUNIT_ASSERT_EQUAL( 1, a1.size() ); | |
644 | CPPUNIT_ASSERT_EQUAL( 2, a2.size() ); | |
ff2201cc VZ |
645 | CPPUNIT_ASSERT_EQUAL( v1, a1[0] ); |
646 | CPPUNIT_ASSERT_EQUAL( v3, a2[1] ); | |
91276868 VZ |
647 | |
648 | a1.swap(a2); | |
649 | CPPUNIT_ASSERT_EQUAL( 2, a1.size() ); | |
650 | CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); | |
651 | } | |
652 | ||
ff2201cc VZ |
653 | } // anonymous namespace |
654 | ||
655 | void ArraysTestCase::Swap() | |
656 | { | |
657 | DoTestSwap("Foo", "Bar", "Baz", (wxArrayString *)NULL); | |
fecddbaa | 658 | |
ff2201cc | 659 | DoTestSwap(1, 10, 100, (wxArrayInt *)NULL); |
8112fbf0 | 660 | DoTestSwap(6, 28, 496, (wxArrayLong *)NULL); |
ff2201cc VZ |
661 | } |
662 | ||
cf1014a2 VS |
663 | void ArraysTestCase::TestSTL() |
664 | { | |
665 | wxArrayInt list1; | |
666 | wxArrayInt::iterator it, en; | |
667 | wxArrayInt::reverse_iterator rit, ren; | |
668 | int i; | |
a059db11 VZ |
669 | static const int COUNT = 5; |
670 | ||
671 | for ( i = 0; i < COUNT; ++i ) | |
cf1014a2 VS |
672 | list1.push_back(i); |
673 | ||
a059db11 | 674 | CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT ); |
10d256d6 | 675 | CPPUNIT_ASSERT_EQUAL( COUNT, list1.size() ); |
a059db11 | 676 | |
cf1014a2 VS |
677 | for ( it = list1.begin(), en = list1.end(), i = 0; |
678 | it != en; ++it, ++i ) | |
679 | { | |
680 | CPPUNIT_ASSERT( *it == i ); | |
681 | } | |
682 | ||
a059db11 VZ |
683 | CPPUNIT_ASSERT_EQUAL( COUNT, i ); |
684 | ||
685 | for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT; | |
cf1014a2 VS |
686 | rit != ren; ++rit, --i ) |
687 | { | |
a059db11 | 688 | CPPUNIT_ASSERT( *rit == i-1 ); |
cf1014a2 VS |
689 | } |
690 | ||
a059db11 VZ |
691 | CPPUNIT_ASSERT_EQUAL( 0, i ); |
692 | ||
cf1014a2 VS |
693 | CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) && |
694 | *list1.begin() == *(list1.rend()-1) ); | |
695 | ||
696 | it = list1.begin()+1; | |
697 | rit = list1.rbegin()+1; | |
698 | CPPUNIT_ASSERT( *list1.begin() == *(it-1) && | |
699 | *list1.rbegin() == *(rit-1) ); | |
700 | ||
a059db11 | 701 | CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) ); |
cf1014a2 VS |
702 | |
703 | list1.erase(list1.begin()); | |
704 | list1.erase(list1.end()-1); | |
705 | ||
706 | for ( it = list1.begin(), en = list1.end(), i = 1; | |
707 | it != en; ++it, ++i ) | |
708 | { | |
709 | CPPUNIT_ASSERT( *it == i ); | |
710 | } | |
35d5da67 VZ |
711 | |
712 | ||
713 | ItemPtrArray items; | |
714 | items.push_back(new Item(17)); | |
715 | CPPUNIT_ASSERT_EQUAL( 17, (*(items.rbegin()))->n ); | |
716 | CPPUNIT_ASSERT_EQUAL( 17, (**items.begin()).n ); | |
cf1014a2 | 717 | } |
23f32cd7 VS |
718 | |
719 | void ArraysTestCase::IndexFromEnd() | |
720 | { | |
721 | wxArrayInt a; | |
722 | a.push_back(10); | |
723 | a.push_back(1); | |
724 | a.push_back(42); | |
725 | ||
726 | CPPUNIT_ASSERT_EQUAL( 0, a.Index(10) ); | |
727 | CPPUNIT_ASSERT_EQUAL( 1, a.Index(1) ); | |
728 | CPPUNIT_ASSERT_EQUAL( 2, a.Index(42) ); | |
729 | CPPUNIT_ASSERT_EQUAL( 0, a.Index(10, /*bFromEnd=*/true) ); | |
730 | CPPUNIT_ASSERT_EQUAL( 1, a.Index(1, /*bFromEnd=*/true) ); | |
731 | CPPUNIT_ASSERT_EQUAL( 2, a.Index(42, /*bFromEnd=*/true) ); | |
732 | } |