]>
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 ); | |
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 ); |
cf1014a2 VS |
175 | CPPUNIT_TEST_SUITE_END(); |
176 | ||
177 | void wxStringArrayTest(); | |
abbb59e8 VZ |
178 | void wxStringArraySplitTest(); |
179 | void wxStringArrayJoinTest(); | |
180 | void wxStringArraySplitJoinTest(); | |
cf1014a2 VS |
181 | void wxObjArrayTest(); |
182 | void wxArrayUShortTest(); | |
183 | void wxArrayIntTest(); | |
1ffc8d7a | 184 | void wxArrayCharTest(); |
cf1014a2 | 185 | void TestSTL(); |
7788fc40 | 186 | void Alloc(); |
0c61ab6f | 187 | void Clear(); |
91276868 | 188 | void Swap(); |
cf1014a2 VS |
189 | |
190 | DECLARE_NO_COPY_CLASS(ArraysTestCase) | |
191 | }; | |
192 | ||
193 | // register in the unnamed registry so that these tests are run by default | |
194 | CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase ); | |
195 | ||
196 | // also include in it's own registry so that these tests can be run alone | |
197 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" ); | |
198 | ||
199 | void ArraysTestCase::wxStringArrayTest() | |
200 | { | |
201 | wxArrayString a1; | |
202 | a1.Add(_T("thermit")); | |
203 | a1.Add(_T("condor")); | |
204 | a1.Add(_T("lion"), 3); | |
205 | a1.Add(_T("dog")); | |
206 | a1.Add(_T("human")); | |
207 | a1.Add(_T("alligator")); | |
208 | ||
209 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , _T("thermit") , | |
210 | _T("condor") , | |
211 | _T("lion") , | |
212 | _T("lion") , | |
213 | _T("lion") , | |
214 | _T("dog") , | |
215 | _T("human") , | |
216 | _T("alligator") ) ); | |
217 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) ); | |
218 | ||
219 | wxArrayString a2(a1); | |
220 | ||
e3f2384e | 221 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , _T("thermit") , |
cf1014a2 VS |
222 | _T("condor") , |
223 | _T("lion") , | |
224 | _T("lion") , | |
225 | _T("lion") , | |
226 | _T("dog") , | |
227 | _T("human") , | |
228 | _T("alligator") ) ); | |
229 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) ); | |
230 | ||
231 | wxSortedArrayString a3(a1); | |
232 | ||
e3f2384e | 233 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , _T("alligator") , |
cf1014a2 VS |
234 | _T("condor") , |
235 | _T("dog") , | |
236 | _T("human") , | |
237 | _T("lion") , | |
238 | _T("lion") , | |
239 | _T("lion") , | |
240 | _T("thermit") ) ); | |
241 | CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) ); | |
242 | ||
243 | wxSortedArrayString a4; | |
244 | for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it) | |
245 | a4.Add(*it); | |
246 | ||
e3f2384e | 247 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , _T("alligator") , |
cf1014a2 VS |
248 | _T("condor") , |
249 | _T("dog") , | |
250 | _T("human") , | |
251 | _T("lion") , | |
252 | _T("lion") , | |
253 | _T("lion") , | |
254 | _T("thermit") ) ); | |
255 | CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) ); | |
256 | ||
257 | a1.RemoveAt(2,3); | |
258 | ||
259 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
260 | _T("condor") , | |
261 | _T("dog") , | |
262 | _T("human") , | |
263 | _T("alligator") ) ); | |
264 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
265 | ||
266 | a2 = a1; | |
267 | ||
268 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , _T("thermit") , | |
269 | _T("condor") , | |
270 | _T("dog") , | |
271 | _T("human") , | |
272 | _T("alligator") ) ); | |
273 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) ); | |
274 | ||
275 | a1.Sort(false); | |
276 | ||
277 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("alligator") , | |
278 | _T("condor") , | |
279 | _T("dog") , | |
280 | _T("human") , | |
281 | _T("thermit") ) ); | |
282 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
283 | ||
284 | a1.Sort(true); | |
285 | ||
286 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
287 | _T("human") , | |
288 | _T("dog") , | |
289 | _T("condor") , | |
290 | _T("alligator") ) ); | |
291 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
292 | ||
293 | a1.Sort(&StringLenCompare); | |
294 | ||
295 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("dog") , | |
296 | _T("human") , | |
297 | _T("condor") , | |
298 | _T("thermit") , | |
299 | _T("alligator") ) ); | |
300 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
e3f2384e WS |
301 | CPPUNIT_ASSERT( a1.Index( _T("dog") ) == 0 ); |
302 | CPPUNIT_ASSERT( a1.Index( _T("human") ) == 1 ); | |
303 | CPPUNIT_ASSERT( a1.Index( _T("humann") ) == wxNOT_FOUND ); | |
304 | CPPUNIT_ASSERT( a1.Index( _T("condor") ) == 2 ); | |
305 | CPPUNIT_ASSERT( a1.Index( _T("thermit") ) == 3 ); | |
306 | CPPUNIT_ASSERT( a1.Index( _T("alligator") ) == 4 ); | |
555d0d7e MB |
307 | |
308 | wxArrayString a5; | |
309 | ||
310 | CPPUNIT_ASSERT( a5.Add( _T("x"), 1 ) == 0 ); | |
311 | CPPUNIT_ASSERT( a5.Add( _T("a"), 3 ) == 1 ); | |
312 | ||
313 | CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, _T("x") , | |
314 | _T("a") , | |
315 | _T("a") , | |
316 | _T("a") ) ); | |
cf1014a2 VS |
317 | } |
318 | ||
abbb59e8 VZ |
319 | void ArraysTestCase::wxStringArraySplitTest() |
320 | { | |
321 | // test wxSplit: | |
322 | ||
323 | { | |
324 | wxString str = wxT(",,,,first,second,third,,"); | |
325 | const wxChar *expected[] = | |
326 | { wxT(""), wxT(""), wxT(""), wxT(""), wxT("first"), | |
327 | wxT("second"), wxT("third"), wxT(""), wxT("") }; | |
328 | ||
329 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
330 | wxArrayString realarr(wxSplit(str, wxT(','))); | |
331 | CPPUNIT_ASSERT( exparr == realarr ); | |
332 | } | |
333 | ||
334 | { | |
335 | wxString str = wxT(",\\,first,second,third,"); | |
336 | const wxChar *expected[] = | |
337 | { wxT(""), wxT(",first"), wxT("second"), wxT("third"), wxT("") }; | |
338 | const wxChar *expected2[] = | |
339 | { wxT(""), wxT("\\"), wxT("first"), wxT("second"), wxT("third"), wxT("") }; | |
340 | ||
341 | // escaping on: | |
342 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
343 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
344 | CPPUNIT_ASSERT( exparr == realarr ); | |
345 | ||
346 | // escaping turned off: | |
347 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
348 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
349 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
350 | } | |
351 | ||
352 | { | |
353 | // test is escape characters placed before non-separator character are | |
354 | // just ignored as they should: | |
355 | wxString str = wxT(",\\,,fir\\st,se\\cond\\,,\\third"); | |
356 | const wxChar *expected[] = | |
357 | { wxT(""), wxT(","), wxT("fir\\st"), wxT("se\\cond,"), wxT("\\third") }; | |
358 | const wxChar *expected2[] = | |
359 | { wxT(""), wxT("\\"), wxT(""), wxT("fir\\st"), wxT("se\\cond\\"), | |
360 | wxT(""), wxT("\\third") }; | |
361 | ||
362 | // escaping on: | |
363 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
364 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
365 | CPPUNIT_ASSERT( exparr == realarr ); | |
366 | ||
367 | // escaping turned off: | |
368 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
369 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
370 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
371 | } | |
372 | } | |
373 | ||
374 | void ArraysTestCase::wxStringArrayJoinTest() | |
375 | { | |
376 | // test wxJoin: | |
377 | ||
378 | { | |
379 | const wxChar *arr[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") }; | |
380 | wxString expected = wxT("first,,second,third"); | |
381 | ||
382 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
383 | wxString result = wxJoin(arrstr, wxT(',')); | |
384 | CPPUNIT_ASSERT( expected == result ); | |
385 | } | |
386 | ||
387 | { | |
388 | const wxChar *arr[] = { wxT("first, word"), wxT(",,second"), wxT("third,,") }; | |
389 | wxString expected = wxT("first\\, word,\\,\\,second,third\\,\\,"); | |
390 | wxString expected2 = wxT("first, word,,,second,third,,"); | |
391 | ||
392 | // escaping on: | |
393 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
394 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
395 | CPPUNIT_ASSERT( expected == result ); | |
396 | ||
397 | // escaping turned off: | |
398 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
399 | CPPUNIT_ASSERT( expected2 == result2 ); | |
400 | } | |
401 | ||
402 | { | |
403 | // test is escape characters placed in the original array are just ignored as they should: | |
404 | const wxChar *arr[] = { wxT("first\\, wo\\rd"), wxT("seco\\nd"), wxT("\\third\\") }; | |
405 | wxString expected = wxT("first\\\\, wo\\rd,seco\\nd,\\third\\"); | |
406 | wxString expected2 = wxT("first\\, wo\\rd,seco\\nd,\\third\\"); | |
407 | ||
408 | // escaping on: | |
409 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
410 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
411 | CPPUNIT_ASSERT( expected == result ); | |
412 | ||
413 | // escaping turned off: | |
414 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
415 | CPPUNIT_ASSERT( expected2 == result2 ); | |
416 | } | |
417 | } | |
418 | ||
419 | void ArraysTestCase::wxStringArraySplitJoinTest() | |
420 | { | |
421 | wxChar separators[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'), | |
422 | wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'), | |
423 | wxT('\''), wxT('\n'), wxT('!'), wxT('-') }; | |
424 | ||
425 | // test with a string: split it and then rejoin it: | |
426 | ||
427 | wxString str = wxT("This is a long, long test; if wxSplit and wxJoin do work ") | |
428 | wxT("correctly, then splitting and joining this 'text' _multiple_ ") | |
429 | wxT("times shouldn't cause any loss of content.\n") | |
430 | wxT("This is some latex code: ") | |
431 | wxT("\\func{wxString}{wxJoin}{") | |
432 | wxT("\\param{const wxArray String\\&}{ arr}, ") | |
433 | wxT("\\param{const wxChar}{ sep}, ") | |
434 | wxT("\\param{const wxChar}{ escape = '\\'}}.\n") | |
435 | wxT("This is some HTML code: ") | |
436 | wxT("<html><head><meta http-equiv=\"content-type\">") | |
437 | wxT("<title>Initial page of Mozilla Firefox</title>") | |
438 | wxT("</meta></head></html>"); | |
439 | ||
5098c258 VZ |
440 | size_t i; |
441 | for (i = 0; i < WXSIZEOF(separators); i++) | |
abbb59e8 VZ |
442 | { |
443 | wxArrayString arr = wxSplit(str, separators[i]); | |
444 | CPPUNIT_ASSERT( str == wxJoin(arr, separators[i]) ); | |
445 | } | |
446 | ||
447 | ||
448 | // test with an array: join it and then resplit it: | |
449 | ||
450 | const wxChar *arr[] = | |
451 | { | |
452 | wxT("first, second!"), wxT("this is the third!!"), | |
453 | wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "), | |
454 | wxT("_sixth__token__with_underscores"), wxT("The! Last! One!") | |
455 | }; | |
456 | wxArrayString theArr(WXSIZEOF(arr), arr); | |
457 | ||
5098c258 | 458 | for (i = 0; i < WXSIZEOF(separators); i++) |
abbb59e8 VZ |
459 | { |
460 | wxString string = wxJoin(theArr, separators[i]); | |
461 | CPPUNIT_ASSERT( theArr == wxSplit(string, separators[i]) ); | |
462 | } | |
463 | ||
464 | wxArrayString emptyArray; | |
465 | wxString string = wxJoin(emptyArray, _T(';')); | |
466 | CPPUNIT_ASSERT( string.empty() ); | |
467 | ||
468 | CPPUNIT_ASSERT( wxSplit(string, _T(';')).empty() ); | |
469 | ||
10d256d6 | 470 | CPPUNIT_ASSERT_EQUAL( 2, wxSplit(_T(";"), _T(';')).size() ); |
abbb59e8 VZ |
471 | } |
472 | ||
cf1014a2 VS |
473 | void ArraysTestCase::wxObjArrayTest() |
474 | { | |
475 | { | |
476 | ArrayBars bars; | |
477 | Bar bar(_T("first bar in general, second bar in array (two copies!)")); | |
478 | ||
7ee34f2b VZ |
479 | CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() ); |
480 | CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() ); | |
cf1014a2 VS |
481 | |
482 | bars.Add(new Bar(_T("first bar in array"))); | |
0c61ab6f | 483 | bars.Add(bar, 2); |
cf1014a2 | 484 | |
7ee34f2b VZ |
485 | CPPUNIT_ASSERT_EQUAL( 3, bars.GetCount() ); |
486 | CPPUNIT_ASSERT_EQUAL( 4, Bar::GetNumber() ); | |
cf1014a2 VS |
487 | |
488 | bars.RemoveAt(1, bars.GetCount() - 1); | |
489 | ||
7ee34f2b VZ |
490 | CPPUNIT_ASSERT_EQUAL( 1, bars.GetCount() ); |
491 | CPPUNIT_ASSERT_EQUAL( 2, Bar::GetNumber() ); | |
cf1014a2 VS |
492 | |
493 | bars.Empty(); | |
494 | ||
7ee34f2b VZ |
495 | CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() ); |
496 | CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() ); | |
cf1014a2 | 497 | } |
7ee34f2b | 498 | CPPUNIT_ASSERT_EQUAL( 0, Bar::GetNumber() ); |
cf1014a2 VS |
499 | } |
500 | ||
501 | #define TestArrayOf(name) \ | |
502 | \ | |
503 | void ArraysTestCase::wxArray ## name ## Test() \ | |
504 | { \ | |
505 | wxArray##name a; \ | |
506 | a.Add(1); \ | |
507 | a.Add(17,2); \ | |
508 | a.Add(5,3); \ | |
509 | a.Add(3,4); \ | |
510 | \ | |
511 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \ | |
512 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
513 | \ | |
514 | a.Sort(name ## Compare); \ | |
515 | \ | |
516 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \ | |
517 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
518 | \ | |
519 | a.Sort(name ## RevCompare); \ | |
520 | \ | |
521 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \ | |
522 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
523 | \ | |
524 | wxSortedArray##name b; \ | |
525 | \ | |
526 | b.Add(1); \ | |
527 | b.Add(17); \ | |
528 | b.Add(5); \ | |
529 | b.Add(3); \ | |
530 | \ | |
531 | CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \ | |
532 | CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \ | |
10dcbe63 MB |
533 | CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \ |
534 | CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \ | |
535 | CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \ | |
536 | CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \ | |
537 | CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \ | |
538 | CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \ | |
539 | CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \ | |
cf1014a2 VS |
540 | } |
541 | ||
93a800a9 | 542 | TestArrayOf(UShort) |
cf1014a2 | 543 | |
93a800a9 | 544 | TestArrayOf(Char) |
1ffc8d7a | 545 | |
93a800a9 | 546 | TestArrayOf(Int) |
cf1014a2 | 547 | |
7788fc40 VZ |
548 | void ArraysTestCase::Alloc() |
549 | { | |
550 | wxArrayInt a; | |
551 | a.Add(17); | |
552 | a.Add(9); | |
10d256d6 | 553 | CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() ); |
7788fc40 VZ |
554 | |
555 | a.Alloc(1000); | |
556 | ||
10d256d6 | 557 | CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() ); |
7788fc40 VZ |
558 | CPPUNIT_ASSERT_EQUAL( 17, a[0] ); |
559 | CPPUNIT_ASSERT_EQUAL( 9, a[1] ); | |
560 | } | |
561 | ||
0c61ab6f VZ |
562 | void ArraysTestCase::Clear() |
563 | { | |
564 | ItemPtrArray items; | |
565 | ||
566 | WX_CLEAR_ARRAY(items); | |
567 | CPPUNIT_ASSERT_EQUAL( 0, items.size() ); | |
568 | ||
569 | items.push_back(new Item(17)); | |
570 | items.push_back(new Item(71)); | |
571 | CPPUNIT_ASSERT_EQUAL( 2, items.size() ); | |
572 | ||
573 | WX_CLEAR_ARRAY(items); | |
574 | CPPUNIT_ASSERT_EQUAL( 0, items.size() ); | |
575 | } | |
576 | ||
ff2201cc VZ |
577 | namespace |
578 | { | |
579 | ||
580 | template <typename A, typename T> | |
581 | void DoTestSwap(T v1, T v2, T v3, | |
582 | A * WXUNUSED(dummyUglyVC6Workaround)) | |
91276868 | 583 | { |
ff2201cc | 584 | A a1, a2; |
91276868 VZ |
585 | a1.swap(a2); |
586 | CPPUNIT_ASSERT( a1.empty() && a2.empty() ); | |
587 | ||
ff2201cc | 588 | a1.push_back(v1); |
91276868 VZ |
589 | a1.swap(a2); |
590 | CPPUNIT_ASSERT( a1.empty() ); | |
591 | CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); | |
592 | ||
ff2201cc VZ |
593 | a1.push_back(v2); |
594 | a1.push_back(v3); | |
91276868 VZ |
595 | a2.swap(a1); |
596 | CPPUNIT_ASSERT_EQUAL( 1, a1.size() ); | |
597 | CPPUNIT_ASSERT_EQUAL( 2, a2.size() ); | |
ff2201cc VZ |
598 | CPPUNIT_ASSERT_EQUAL( v1, a1[0] ); |
599 | CPPUNIT_ASSERT_EQUAL( v3, a2[1] ); | |
91276868 VZ |
600 | |
601 | a1.swap(a2); | |
602 | CPPUNIT_ASSERT_EQUAL( 2, a1.size() ); | |
603 | CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); | |
604 | } | |
605 | ||
ff2201cc VZ |
606 | } // anonymous namespace |
607 | ||
608 | void ArraysTestCase::Swap() | |
609 | { | |
610 | DoTestSwap("Foo", "Bar", "Baz", (wxArrayString *)NULL); | |
fecddbaa | 611 | |
ff2201cc | 612 | DoTestSwap(1, 10, 100, (wxArrayInt *)NULL); |
8112fbf0 | 613 | DoTestSwap(6, 28, 496, (wxArrayLong *)NULL); |
ff2201cc VZ |
614 | } |
615 | ||
cf1014a2 VS |
616 | void ArraysTestCase::TestSTL() |
617 | { | |
618 | wxArrayInt list1; | |
619 | wxArrayInt::iterator it, en; | |
620 | wxArrayInt::reverse_iterator rit, ren; | |
621 | int i; | |
a059db11 VZ |
622 | static const int COUNT = 5; |
623 | ||
624 | for ( i = 0; i < COUNT; ++i ) | |
cf1014a2 VS |
625 | list1.push_back(i); |
626 | ||
a059db11 | 627 | CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT ); |
10d256d6 | 628 | CPPUNIT_ASSERT_EQUAL( COUNT, list1.size() ); |
a059db11 | 629 | |
cf1014a2 VS |
630 | for ( it = list1.begin(), en = list1.end(), i = 0; |
631 | it != en; ++it, ++i ) | |
632 | { | |
633 | CPPUNIT_ASSERT( *it == i ); | |
634 | } | |
635 | ||
a059db11 VZ |
636 | CPPUNIT_ASSERT_EQUAL( COUNT, i ); |
637 | ||
638 | for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT; | |
cf1014a2 VS |
639 | rit != ren; ++rit, --i ) |
640 | { | |
a059db11 | 641 | CPPUNIT_ASSERT( *rit == i-1 ); |
cf1014a2 VS |
642 | } |
643 | ||
a059db11 VZ |
644 | CPPUNIT_ASSERT_EQUAL( 0, i ); |
645 | ||
cf1014a2 VS |
646 | CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) && |
647 | *list1.begin() == *(list1.rend()-1) ); | |
648 | ||
649 | it = list1.begin()+1; | |
650 | rit = list1.rbegin()+1; | |
651 | CPPUNIT_ASSERT( *list1.begin() == *(it-1) && | |
652 | *list1.rbegin() == *(rit-1) ); | |
653 | ||
a059db11 | 654 | CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) ); |
cf1014a2 VS |
655 | |
656 | list1.erase(list1.begin()); | |
657 | list1.erase(list1.end()-1); | |
658 | ||
659 | for ( it = list1.begin(), en = list1.end(), i = 1; | |
660 | it != en; ++it, ++i ) | |
661 | { | |
662 | CPPUNIT_ASSERT( *it == i ); | |
663 | } | |
35d5da67 VZ |
664 | |
665 | ||
666 | ItemPtrArray items; | |
667 | items.push_back(new Item(17)); | |
668 | CPPUNIT_ASSERT_EQUAL( 17, (*(items.rbegin()))->n ); | |
669 | CPPUNIT_ASSERT_EQUAL( 17, (**items.begin()).n ); | |
cf1014a2 | 670 | } |