]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/arrays/arrays.cpp | |
3 | // Purpose: wxArray unit test | |
4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba | |
5 | // Created: 2004-04-01 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
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 | ||
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 | ||
85 | const wxChar *GetName() const { return m_name.c_str(); } | |
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" | |
97 | WX_DEFINE_OBJARRAY(ArrayBars); | |
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 | ||
128 | DEFINE_COMPARE(Char, char); | |
129 | DEFINE_COMPARE(UShort, ushort); | |
130 | DEFINE_COMPARE(Int, int); | |
131 | ||
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 | ||
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); | |
139 | ||
140 | WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt); | |
141 | ||
142 | // ---------------------------------------------------------------------------- | |
143 | // test class | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | class ArraysTestCase : public CppUnit::TestCase | |
147 | { | |
148 | public: | |
149 | ArraysTestCase() { } | |
150 | ||
151 | private: | |
152 | CPPUNIT_TEST_SUITE( ArraysTestCase ); | |
153 | CPPUNIT_TEST( wxStringArrayTest ); | |
154 | CPPUNIT_TEST( wxStringArraySplitTest ); | |
155 | CPPUNIT_TEST( wxStringArrayJoinTest ); | |
156 | CPPUNIT_TEST( wxStringArraySplitJoinTest ); | |
157 | ||
158 | CPPUNIT_TEST( wxObjArrayTest ); | |
159 | CPPUNIT_TEST( wxArrayUShortTest ); | |
160 | CPPUNIT_TEST( wxArrayIntTest ); | |
161 | CPPUNIT_TEST( wxArrayCharTest ); | |
162 | CPPUNIT_TEST( TestSTL ); | |
163 | CPPUNIT_TEST( Alloc ); | |
164 | CPPUNIT_TEST_SUITE_END(); | |
165 | ||
166 | void wxStringArrayTest(); | |
167 | void wxStringArraySplitTest(); | |
168 | void wxStringArrayJoinTest(); | |
169 | void wxStringArraySplitJoinTest(); | |
170 | void wxObjArrayTest(); | |
171 | void wxArrayUShortTest(); | |
172 | void wxArrayIntTest(); | |
173 | void wxArrayCharTest(); | |
174 | void TestSTL(); | |
175 | void Alloc(); | |
176 | ||
177 | DECLARE_NO_COPY_CLASS(ArraysTestCase) | |
178 | }; | |
179 | ||
180 | // register in the unnamed registry so that these tests are run by default | |
181 | CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase ); | |
182 | ||
183 | // also include in it's own registry so that these tests can be run alone | |
184 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" ); | |
185 | ||
186 | void ArraysTestCase::wxStringArrayTest() | |
187 | { | |
188 | wxArrayString a1; | |
189 | a1.Add(_T("thermit")); | |
190 | a1.Add(_T("condor")); | |
191 | a1.Add(_T("lion"), 3); | |
192 | a1.Add(_T("dog")); | |
193 | a1.Add(_T("human")); | |
194 | a1.Add(_T("alligator")); | |
195 | ||
196 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , _T("thermit") , | |
197 | _T("condor") , | |
198 | _T("lion") , | |
199 | _T("lion") , | |
200 | _T("lion") , | |
201 | _T("dog") , | |
202 | _T("human") , | |
203 | _T("alligator") ) ); | |
204 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) ); | |
205 | ||
206 | wxArrayString a2(a1); | |
207 | ||
208 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , _T("thermit") , | |
209 | _T("condor") , | |
210 | _T("lion") , | |
211 | _T("lion") , | |
212 | _T("lion") , | |
213 | _T("dog") , | |
214 | _T("human") , | |
215 | _T("alligator") ) ); | |
216 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) ); | |
217 | ||
218 | wxSortedArrayString a3(a1); | |
219 | ||
220 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , _T("alligator") , | |
221 | _T("condor") , | |
222 | _T("dog") , | |
223 | _T("human") , | |
224 | _T("lion") , | |
225 | _T("lion") , | |
226 | _T("lion") , | |
227 | _T("thermit") ) ); | |
228 | CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) ); | |
229 | ||
230 | wxSortedArrayString a4; | |
231 | for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it) | |
232 | a4.Add(*it); | |
233 | ||
234 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , _T("alligator") , | |
235 | _T("condor") , | |
236 | _T("dog") , | |
237 | _T("human") , | |
238 | _T("lion") , | |
239 | _T("lion") , | |
240 | _T("lion") , | |
241 | _T("thermit") ) ); | |
242 | CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) ); | |
243 | ||
244 | a1.RemoveAt(2,3); | |
245 | ||
246 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
247 | _T("condor") , | |
248 | _T("dog") , | |
249 | _T("human") , | |
250 | _T("alligator") ) ); | |
251 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
252 | ||
253 | a2 = a1; | |
254 | ||
255 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , _T("thermit") , | |
256 | _T("condor") , | |
257 | _T("dog") , | |
258 | _T("human") , | |
259 | _T("alligator") ) ); | |
260 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) ); | |
261 | ||
262 | a1.Sort(false); | |
263 | ||
264 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("alligator") , | |
265 | _T("condor") , | |
266 | _T("dog") , | |
267 | _T("human") , | |
268 | _T("thermit") ) ); | |
269 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
270 | ||
271 | a1.Sort(true); | |
272 | ||
273 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
274 | _T("human") , | |
275 | _T("dog") , | |
276 | _T("condor") , | |
277 | _T("alligator") ) ); | |
278 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
279 | ||
280 | a1.Sort(&StringLenCompare); | |
281 | ||
282 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("dog") , | |
283 | _T("human") , | |
284 | _T("condor") , | |
285 | _T("thermit") , | |
286 | _T("alligator") ) ); | |
287 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
288 | CPPUNIT_ASSERT( a1.Index( _T("dog") ) == 0 ); | |
289 | CPPUNIT_ASSERT( a1.Index( _T("human") ) == 1 ); | |
290 | CPPUNIT_ASSERT( a1.Index( _T("humann") ) == wxNOT_FOUND ); | |
291 | CPPUNIT_ASSERT( a1.Index( _T("condor") ) == 2 ); | |
292 | CPPUNIT_ASSERT( a1.Index( _T("thermit") ) == 3 ); | |
293 | CPPUNIT_ASSERT( a1.Index( _T("alligator") ) == 4 ); | |
294 | ||
295 | wxArrayString a5; | |
296 | ||
297 | CPPUNIT_ASSERT( a5.Add( _T("x"), 1 ) == 0 ); | |
298 | CPPUNIT_ASSERT( a5.Add( _T("a"), 3 ) == 1 ); | |
299 | ||
300 | CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, _T("x") , | |
301 | _T("a") , | |
302 | _T("a") , | |
303 | _T("a") ) ); | |
304 | } | |
305 | ||
306 | void ArraysTestCase::wxStringArraySplitTest() | |
307 | { | |
308 | // test wxSplit: | |
309 | ||
310 | { | |
311 | wxString str = wxT(",,,,first,second,third,,"); | |
312 | const wxChar *expected[] = | |
313 | { wxT(""), wxT(""), wxT(""), wxT(""), wxT("first"), | |
314 | wxT("second"), wxT("third"), wxT(""), wxT("") }; | |
315 | ||
316 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
317 | wxArrayString realarr(wxSplit(str, wxT(','))); | |
318 | CPPUNIT_ASSERT( exparr == realarr ); | |
319 | } | |
320 | ||
321 | { | |
322 | wxString str = wxT(",\\,first,second,third,"); | |
323 | const wxChar *expected[] = | |
324 | { wxT(""), wxT(",first"), wxT("second"), wxT("third"), wxT("") }; | |
325 | const wxChar *expected2[] = | |
326 | { wxT(""), wxT("\\"), wxT("first"), wxT("second"), wxT("third"), wxT("") }; | |
327 | ||
328 | // escaping on: | |
329 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
330 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
331 | CPPUNIT_ASSERT( exparr == realarr ); | |
332 | ||
333 | // escaping turned off: | |
334 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
335 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
336 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
337 | } | |
338 | ||
339 | { | |
340 | // test is escape characters placed before non-separator character are | |
341 | // just ignored as they should: | |
342 | wxString str = wxT(",\\,,fir\\st,se\\cond\\,,\\third"); | |
343 | const wxChar *expected[] = | |
344 | { wxT(""), wxT(","), wxT("fir\\st"), wxT("se\\cond,"), wxT("\\third") }; | |
345 | const wxChar *expected2[] = | |
346 | { wxT(""), wxT("\\"), wxT(""), wxT("fir\\st"), wxT("se\\cond\\"), | |
347 | wxT(""), wxT("\\third") }; | |
348 | ||
349 | // escaping on: | |
350 | wxArrayString exparr(WXSIZEOF(expected), expected); | |
351 | wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\'))); | |
352 | CPPUNIT_ASSERT( exparr == realarr ); | |
353 | ||
354 | // escaping turned off: | |
355 | wxArrayString exparr2(WXSIZEOF(expected2), expected2); | |
356 | wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0'))); | |
357 | CPPUNIT_ASSERT( exparr2 == realarr2 ); | |
358 | } | |
359 | } | |
360 | ||
361 | void ArraysTestCase::wxStringArrayJoinTest() | |
362 | { | |
363 | // test wxJoin: | |
364 | ||
365 | { | |
366 | const wxChar *arr[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") }; | |
367 | wxString expected = wxT("first,,second,third"); | |
368 | ||
369 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
370 | wxString result = wxJoin(arrstr, wxT(',')); | |
371 | CPPUNIT_ASSERT( expected == result ); | |
372 | } | |
373 | ||
374 | { | |
375 | const wxChar *arr[] = { wxT("first, word"), wxT(",,second"), wxT("third,,") }; | |
376 | wxString expected = wxT("first\\, word,\\,\\,second,third\\,\\,"); | |
377 | wxString expected2 = wxT("first, word,,,second,third,,"); | |
378 | ||
379 | // escaping on: | |
380 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
381 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
382 | CPPUNIT_ASSERT( expected == result ); | |
383 | ||
384 | // escaping turned off: | |
385 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
386 | CPPUNIT_ASSERT( expected2 == result2 ); | |
387 | } | |
388 | ||
389 | { | |
390 | // test is escape characters placed in the original array are just ignored as they should: | |
391 | const wxChar *arr[] = { wxT("first\\, wo\\rd"), wxT("seco\\nd"), wxT("\\third\\") }; | |
392 | wxString expected = wxT("first\\\\, wo\\rd,seco\\nd,\\third\\"); | |
393 | wxString expected2 = wxT("first\\, wo\\rd,seco\\nd,\\third\\"); | |
394 | ||
395 | // escaping on: | |
396 | wxArrayString arrstr(WXSIZEOF(arr), arr); | |
397 | wxString result = wxJoin(arrstr, wxT(','), wxT('\\')); | |
398 | CPPUNIT_ASSERT( expected == result ); | |
399 | ||
400 | // escaping turned off: | |
401 | wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0')); | |
402 | CPPUNIT_ASSERT( expected2 == result2 ); | |
403 | } | |
404 | } | |
405 | ||
406 | void ArraysTestCase::wxStringArraySplitJoinTest() | |
407 | { | |
408 | wxChar separators[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'), | |
409 | wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'), | |
410 | wxT('\''), wxT('\n'), wxT('!'), wxT('-') }; | |
411 | ||
412 | // test with a string: split it and then rejoin it: | |
413 | ||
414 | wxString str = wxT("This is a long, long test; if wxSplit and wxJoin do work ") | |
415 | wxT("correctly, then splitting and joining this 'text' _multiple_ ") | |
416 | wxT("times shouldn't cause any loss of content.\n") | |
417 | wxT("This is some latex code: ") | |
418 | wxT("\\func{wxString}{wxJoin}{") | |
419 | wxT("\\param{const wxArray String\\&}{ arr}, ") | |
420 | wxT("\\param{const wxChar}{ sep}, ") | |
421 | wxT("\\param{const wxChar}{ escape = '\\'}}.\n") | |
422 | wxT("This is some HTML code: ") | |
423 | wxT("<html><head><meta http-equiv=\"content-type\">") | |
424 | wxT("<title>Initial page of Mozilla Firefox</title>") | |
425 | wxT("</meta></head></html>"); | |
426 | ||
427 | for (size_t i=0; i < WXSIZEOF(separators); i++) | |
428 | { | |
429 | wxArrayString arr = wxSplit(str, separators[i]); | |
430 | CPPUNIT_ASSERT( str == wxJoin(arr, separators[i]) ); | |
431 | } | |
432 | ||
433 | ||
434 | // test with an array: join it and then resplit it: | |
435 | ||
436 | const wxChar *arr[] = | |
437 | { | |
438 | wxT("first, second!"), wxT("this is the third!!"), | |
439 | wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "), | |
440 | wxT("_sixth__token__with_underscores"), wxT("The! Last! One!") | |
441 | }; | |
442 | wxArrayString theArr(WXSIZEOF(arr), arr); | |
443 | ||
444 | for (size_t i=0; i < WXSIZEOF(separators); i++) | |
445 | { | |
446 | wxString string = wxJoin(theArr, separators[i]); | |
447 | CPPUNIT_ASSERT( theArr == wxSplit(string, separators[i]) ); | |
448 | } | |
449 | ||
450 | wxArrayString emptyArray; | |
451 | wxString string = wxJoin(emptyArray, _T(';')); | |
452 | CPPUNIT_ASSERT( string.empty() ); | |
453 | ||
454 | CPPUNIT_ASSERT( wxSplit(string, _T(';')).empty() ); | |
455 | ||
456 | CPPUNIT_ASSERT_EQUAL( (size_t)2, wxSplit(_T(";"), _T(';')).size() ); | |
457 | } | |
458 | ||
459 | void ArraysTestCase::wxObjArrayTest() | |
460 | { | |
461 | { | |
462 | ArrayBars bars; | |
463 | Bar bar(_T("first bar in general, second bar in array (two copies!)")); | |
464 | ||
465 | CPPUNIT_ASSERT( bars.GetCount() == 0 ); | |
466 | CPPUNIT_ASSERT( Bar::GetNumber() == 1 ); | |
467 | ||
468 | bars.Add(new Bar(_T("first bar in array"))); | |
469 | bars.Add(bar,2); | |
470 | ||
471 | CPPUNIT_ASSERT( bars.GetCount() == 3 ); | |
472 | CPPUNIT_ASSERT( Bar::GetNumber() == 4 ); | |
473 | ||
474 | bars.RemoveAt(1, bars.GetCount() - 1); | |
475 | ||
476 | CPPUNIT_ASSERT( bars.GetCount() == 1 ); | |
477 | CPPUNIT_ASSERT( Bar::GetNumber() == 2 ); | |
478 | ||
479 | bars.Empty(); | |
480 | ||
481 | CPPUNIT_ASSERT( bars.GetCount() == 0 ); | |
482 | CPPUNIT_ASSERT( Bar::GetNumber() == 1 ); | |
483 | } | |
484 | CPPUNIT_ASSERT( Bar::GetNumber() == 0 ); | |
485 | } | |
486 | ||
487 | #define TestArrayOf(name) \ | |
488 | \ | |
489 | void ArraysTestCase::wxArray ## name ## Test() \ | |
490 | { \ | |
491 | wxArray##name a; \ | |
492 | a.Add(1); \ | |
493 | a.Add(17,2); \ | |
494 | a.Add(5,3); \ | |
495 | a.Add(3,4); \ | |
496 | \ | |
497 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \ | |
498 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
499 | \ | |
500 | a.Sort(name ## Compare); \ | |
501 | \ | |
502 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \ | |
503 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
504 | \ | |
505 | a.Sort(name ## RevCompare); \ | |
506 | \ | |
507 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \ | |
508 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
509 | \ | |
510 | wxSortedArray##name b; \ | |
511 | \ | |
512 | b.Add(1); \ | |
513 | b.Add(17); \ | |
514 | b.Add(5); \ | |
515 | b.Add(3); \ | |
516 | \ | |
517 | CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \ | |
518 | CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \ | |
519 | CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \ | |
520 | CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \ | |
521 | CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \ | |
522 | CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \ | |
523 | CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \ | |
524 | CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \ | |
525 | CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \ | |
526 | } | |
527 | ||
528 | TestArrayOf(UShort); | |
529 | ||
530 | TestArrayOf(Char); | |
531 | ||
532 | TestArrayOf(Int); | |
533 | ||
534 | void ArraysTestCase::Alloc() | |
535 | { | |
536 | wxArrayInt a; | |
537 | a.Add(17); | |
538 | a.Add(9); | |
539 | CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() ); | |
540 | ||
541 | a.Alloc(1000); | |
542 | ||
543 | CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() ); | |
544 | CPPUNIT_ASSERT_EQUAL( 17, a[0] ); | |
545 | CPPUNIT_ASSERT_EQUAL( 9, a[1] ); | |
546 | } | |
547 | ||
548 | void ArraysTestCase::TestSTL() | |
549 | { | |
550 | wxArrayInt list1; | |
551 | wxArrayInt::iterator it, en; | |
552 | wxArrayInt::reverse_iterator rit, ren; | |
553 | int i; | |
554 | static const int COUNT = 5; | |
555 | ||
556 | for ( i = 0; i < COUNT; ++i ) | |
557 | list1.push_back(i); | |
558 | ||
559 | CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT ); | |
560 | CPPUNIT_ASSERT_EQUAL( (size_t)COUNT, list1.size() ); | |
561 | ||
562 | for ( it = list1.begin(), en = list1.end(), i = 0; | |
563 | it != en; ++it, ++i ) | |
564 | { | |
565 | CPPUNIT_ASSERT( *it == i ); | |
566 | } | |
567 | ||
568 | CPPUNIT_ASSERT_EQUAL( COUNT, i ); | |
569 | ||
570 | for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT; | |
571 | rit != ren; ++rit, --i ) | |
572 | { | |
573 | CPPUNIT_ASSERT( *rit == i-1 ); | |
574 | } | |
575 | ||
576 | CPPUNIT_ASSERT_EQUAL( 0, i ); | |
577 | ||
578 | CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) && | |
579 | *list1.begin() == *(list1.rend()-1) ); | |
580 | ||
581 | it = list1.begin()+1; | |
582 | rit = list1.rbegin()+1; | |
583 | CPPUNIT_ASSERT( *list1.begin() == *(it-1) && | |
584 | *list1.rbegin() == *(rit-1) ); | |
585 | ||
586 | CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) ); | |
587 | ||
588 | list1.erase(list1.begin()); | |
589 | list1.erase(list1.end()-1); | |
590 | ||
591 | for ( it = list1.begin(), en = list1.end(), i = 1; | |
592 | it != en; ++it, ++i ) | |
593 | { | |
594 | CPPUNIT_ASSERT( *it == i ); | |
595 | } | |
596 | } |