]>
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; } | |
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(UShort, ushort); | |
129 | DEFINE_COMPARE(Int, int); | |
130 | ||
131 | WX_DEFINE_ARRAY_SHORT(ushort, wxArrayUShort); | |
132 | WX_DEFINE_SORTED_ARRAY_SHORT(ushort, wxSortedArrayUShortNoCmp); | |
133 | WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort, UShortCompareValues, wxSortedArrayUShort); | |
134 | WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt); | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | // test class | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | class ArraysTestCase : public CppUnit::TestCase | |
141 | { | |
142 | public: | |
143 | ArraysTestCase() { } | |
144 | ||
145 | private: | |
146 | CPPUNIT_TEST_SUITE( ArraysTestCase ); | |
147 | CPPUNIT_TEST( wxStringArrayTest ); | |
148 | CPPUNIT_TEST( wxObjArrayTest ); | |
149 | CPPUNIT_TEST( wxArrayUShortTest ); | |
150 | CPPUNIT_TEST( wxArrayIntTest ); | |
151 | CPPUNIT_TEST( TestSTL ); | |
152 | CPPUNIT_TEST_SUITE_END(); | |
153 | ||
154 | void wxStringArrayTest(); | |
155 | void wxObjArrayTest(); | |
156 | void wxArrayUShortTest(); | |
157 | void wxArrayIntTest(); | |
158 | void TestSTL(); | |
159 | ||
160 | DECLARE_NO_COPY_CLASS(ArraysTestCase) | |
161 | }; | |
162 | ||
163 | // register in the unnamed registry so that these tests are run by default | |
164 | CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase ); | |
165 | ||
166 | // also include in it's own registry so that these tests can be run alone | |
167 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" ); | |
168 | ||
169 | void ArraysTestCase::wxStringArrayTest() | |
170 | { | |
171 | wxArrayString a1; | |
172 | a1.Add(_T("thermit")); | |
173 | a1.Add(_T("condor")); | |
174 | a1.Add(_T("lion"), 3); | |
175 | a1.Add(_T("dog")); | |
176 | a1.Add(_T("human")); | |
177 | a1.Add(_T("alligator")); | |
178 | ||
179 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , _T("thermit") , | |
180 | _T("condor") , | |
181 | _T("lion") , | |
182 | _T("lion") , | |
183 | _T("lion") , | |
184 | _T("dog") , | |
185 | _T("human") , | |
186 | _T("alligator") ) ); | |
187 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) ); | |
188 | ||
189 | wxArrayString a2(a1); | |
190 | ||
191 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , _T("thermit") , | |
192 | _T("condor") , | |
193 | _T("lion") , | |
194 | _T("lion") , | |
195 | _T("lion") , | |
196 | _T("dog") , | |
197 | _T("human") , | |
198 | _T("alligator") ) ); | |
199 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) ); | |
200 | ||
201 | wxSortedArrayString a3(a1); | |
202 | ||
203 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , _T("alligator") , | |
204 | _T("condor") , | |
205 | _T("dog") , | |
206 | _T("human") , | |
207 | _T("lion") , | |
208 | _T("lion") , | |
209 | _T("lion") , | |
210 | _T("thermit") ) ); | |
211 | CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) ); | |
212 | ||
213 | wxSortedArrayString a4; | |
214 | for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it) | |
215 | a4.Add(*it); | |
216 | ||
217 | CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , _T("alligator") , | |
218 | _T("condor") , | |
219 | _T("dog") , | |
220 | _T("human") , | |
221 | _T("lion") , | |
222 | _T("lion") , | |
223 | _T("lion") , | |
224 | _T("thermit") ) ); | |
225 | CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) ); | |
226 | ||
227 | a1.RemoveAt(2,3); | |
228 | ||
229 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
230 | _T("condor") , | |
231 | _T("dog") , | |
232 | _T("human") , | |
233 | _T("alligator") ) ); | |
234 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
235 | ||
236 | a2 = a1; | |
237 | ||
238 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , _T("thermit") , | |
239 | _T("condor") , | |
240 | _T("dog") , | |
241 | _T("human") , | |
242 | _T("alligator") ) ); | |
243 | CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) ); | |
244 | ||
245 | a1.Sort(false); | |
246 | ||
247 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("alligator") , | |
248 | _T("condor") , | |
249 | _T("dog") , | |
250 | _T("human") , | |
251 | _T("thermit") ) ); | |
252 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
253 | ||
254 | a1.Sort(true); | |
255 | ||
256 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") , | |
257 | _T("human") , | |
258 | _T("dog") , | |
259 | _T("condor") , | |
260 | _T("alligator") ) ); | |
261 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
262 | ||
263 | a1.Sort(&StringLenCompare); | |
264 | ||
265 | CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("dog") , | |
266 | _T("human") , | |
267 | _T("condor") , | |
268 | _T("thermit") , | |
269 | _T("alligator") ) ); | |
270 | CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) ); | |
271 | CPPUNIT_ASSERT( a1.Index( _T("dog") ) == 0 ); | |
272 | CPPUNIT_ASSERT( a1.Index( _T("human") ) == 1 ); | |
273 | CPPUNIT_ASSERT( a1.Index( _T("humann") ) == wxNOT_FOUND ); | |
274 | CPPUNIT_ASSERT( a1.Index( _T("condor") ) == 2 ); | |
275 | CPPUNIT_ASSERT( a1.Index( _T("thermit") ) == 3 ); | |
276 | CPPUNIT_ASSERT( a1.Index( _T("alligator") ) == 4 ); | |
277 | ||
278 | wxArrayString a5; | |
279 | ||
280 | CPPUNIT_ASSERT( a5.Add( _T("x"), 1 ) == 0 ); | |
281 | CPPUNIT_ASSERT( a5.Add( _T("a"), 3 ) == 1 ); | |
282 | ||
283 | CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, _T("x") , | |
284 | _T("a") , | |
285 | _T("a") , | |
286 | _T("a") ) ); | |
287 | } | |
288 | ||
289 | void ArraysTestCase::wxObjArrayTest() | |
290 | { | |
291 | { | |
292 | ArrayBars bars; | |
293 | Bar bar(_T("first bar in general, second bar in array (two copies!)")); | |
294 | ||
295 | CPPUNIT_ASSERT( bars.GetCount() == 0 ); | |
296 | CPPUNIT_ASSERT( Bar::GetNumber() == 1 ); | |
297 | ||
298 | bars.Add(new Bar(_T("first bar in array"))); | |
299 | bars.Add(bar,2); | |
300 | ||
301 | CPPUNIT_ASSERT( bars.GetCount() == 3 ); | |
302 | CPPUNIT_ASSERT( Bar::GetNumber() == 4 ); | |
303 | ||
304 | bars.RemoveAt(1, bars.GetCount() - 1); | |
305 | ||
306 | CPPUNIT_ASSERT( bars.GetCount() == 1 ); | |
307 | CPPUNIT_ASSERT( Bar::GetNumber() == 2 ); | |
308 | ||
309 | bars.Empty(); | |
310 | ||
311 | CPPUNIT_ASSERT( bars.GetCount() == 0 ); | |
312 | CPPUNIT_ASSERT( Bar::GetNumber() == 1 ); | |
313 | } | |
314 | CPPUNIT_ASSERT( Bar::GetNumber() == 0 ); | |
315 | } | |
316 | ||
317 | #define TestArrayOf(name) \ | |
318 | \ | |
319 | void ArraysTestCase::wxArray ## name ## Test() \ | |
320 | { \ | |
321 | wxArray##name a; \ | |
322 | a.Add(1); \ | |
323 | a.Add(17,2); \ | |
324 | a.Add(5,3); \ | |
325 | a.Add(3,4); \ | |
326 | \ | |
327 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \ | |
328 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
329 | \ | |
330 | a.Sort(name ## Compare); \ | |
331 | \ | |
332 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \ | |
333 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
334 | \ | |
335 | a.Sort(name ## RevCompare); \ | |
336 | \ | |
337 | CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \ | |
338 | CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \ | |
339 | \ | |
340 | wxSortedArray##name b; \ | |
341 | \ | |
342 | b.Add(1); \ | |
343 | b.Add(17); \ | |
344 | b.Add(5); \ | |
345 | b.Add(3); \ | |
346 | \ | |
347 | CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \ | |
348 | CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \ | |
349 | CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \ | |
350 | CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \ | |
351 | CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \ | |
352 | CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \ | |
353 | CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \ | |
354 | CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \ | |
355 | CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \ | |
356 | } | |
357 | ||
358 | TestArrayOf(UShort); | |
359 | ||
360 | TestArrayOf(Int); | |
361 | ||
362 | void ArraysTestCase::TestSTL() | |
363 | { | |
364 | wxArrayInt list1; | |
365 | wxArrayInt::iterator it, en; | |
366 | wxArrayInt::reverse_iterator rit, ren; | |
367 | int i; | |
368 | static const int COUNT = 5; | |
369 | ||
370 | for ( i = 0; i < COUNT; ++i ) | |
371 | list1.push_back(i); | |
372 | ||
373 | CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT ); | |
374 | CPPUNIT_ASSERT_EQUAL( (size_t)COUNT, list1.size() ); | |
375 | ||
376 | for ( it = list1.begin(), en = list1.end(), i = 0; | |
377 | it != en; ++it, ++i ) | |
378 | { | |
379 | CPPUNIT_ASSERT( *it == i ); | |
380 | } | |
381 | ||
382 | CPPUNIT_ASSERT_EQUAL( COUNT, i ); | |
383 | ||
384 | for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT; | |
385 | rit != ren; ++rit, --i ) | |
386 | { | |
387 | CPPUNIT_ASSERT( *rit == i-1 ); | |
388 | } | |
389 | ||
390 | CPPUNIT_ASSERT_EQUAL( 0, i ); | |
391 | ||
392 | CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) && | |
393 | *list1.begin() == *(list1.rend()-1) ); | |
394 | ||
395 | it = list1.begin()+1; | |
396 | rit = list1.rbegin()+1; | |
397 | CPPUNIT_ASSERT( *list1.begin() == *(it-1) && | |
398 | *list1.rbegin() == *(rit-1) ); | |
399 | ||
400 | CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) ); | |
401 | ||
402 | list1.erase(list1.begin()); | |
403 | list1.erase(list1.end()-1); | |
404 | ||
405 | for ( it = list1.begin(), en = list1.end(), i = 1; | |
406 | it != en; ++it, ++i ) | |
407 | { | |
408 | CPPUNIT_ASSERT( *it == i ); | |
409 | } | |
410 | } |