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