]>
git.saurik.com Git - wxWidgets.git/blob - tests/arrays/arrays.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/arrays/arrays.cpp
3 // Purpose: wxArray unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
24 #include "wx/dynarray.h"
26 #include "wx/cppunit.h"
28 // ----------------------------------------------------------------------------
29 // helpers for testing values and sizes
30 // ----------------------------------------------------------------------------
32 #define COMPARE_VALUE( array , index , value ) ( array.Item( index ) == value )
34 #define COMPARE_2_VALUES( array , p0 , p1 ) \
35 COMPARE_VALUE( array , 0 , p0 ) && \
36 COMPARE_VALUE( array , 1 , p1 )
38 #define COMPARE_3_VALUES( array , p0 , p1 , p2 ) \
39 COMPARE_2_VALUES( array , p0 , p1 ) && \
40 COMPARE_VALUE( array , 2 , p2 )
42 #define COMPARE_4_VALUES( array , p0 , p1 , p2 , p3 ) \
43 COMPARE_3_VALUES( array , p0 , p1 , p2 ) && \
44 COMPARE_VALUE( array , 3 , p3 )
46 #define COMPARE_5_VALUES( array , p0 , p1 , p2 , p3 , p4 ) \
47 COMPARE_4_VALUES( array , p0 , p1 , p2 , p3 ) && \
48 COMPARE_VALUE( array , 4 , p4 )
50 #define COMPARE_6_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 ) \
51 COMPARE_5_VALUES( array , p0 , p1 , p2 , p3 , p4 ) && \
52 COMPARE_VALUE( array , 5 , p5 )
54 #define COMPARE_7_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 ) \
55 COMPARE_6_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 ) && \
56 COMPARE_VALUE( array , 6 , p6 )
58 #define COMPARE_8_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 ) \
59 COMPARE_7_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 ) && \
60 COMPARE_VALUE( array , 7 , p7 )
62 #define COMPARE_9_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 ) \
63 COMPARE_8_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 ) && \
64 COMPARE_VALUE( array , 8 , p8 )
66 #define COMPARE_10_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 ) \
67 COMPARE_9_VALUES( array , p0 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 ) && \
68 COMPARE_VALUE( array , 9 , p9 )
70 #define COMPARE_COUNT( array , n ) \
71 ( array.GetCount() == n ) && \
72 ( array.Last() == array.Item( n - 1 ) )
74 // ----------------------------------------------------------------------------
75 // helpers for testing wxObjArray
76 // ----------------------------------------------------------------------------
81 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
82 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
85 static size_t GetNumber() { return ms_bars
; }
87 const wxChar
*GetName() const { return m_name
; }
92 static size_t ms_bars
;
95 size_t Bar::ms_bars
= 0;
97 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
98 #include "wx/arrimpl.cpp"
99 WX_DEFINE_OBJARRAY(ArrayBars
);
101 // ----------------------------------------------------------------------------
102 // helpers for sorting arrays and comparing items
103 // ----------------------------------------------------------------------------
105 int wxCMPFUNC_CONV
StringLenCompare(const wxString
& first
,
106 const wxString
& second
)
108 return first
.length() - second
.length();
111 #define DEFINE_COMPARE(name, T) \
113 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
115 return first - second; \
118 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
120 return *first - *second; \
123 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
125 return *second - *first; \
128 typedef unsigned short ushort;
130 DEFINE_COMPARE(UShort
, ushort
);
131 DEFINE_COMPARE(Int
, int);
133 WX_DEFINE_ARRAY_SHORT(ushort
, wxArrayUShort
);
134 WX_DEFINE_SORTED_ARRAY_SHORT(ushort
, wxSortedArrayUShortNoCmp
);
135 WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort
, UShortCompareValues
, wxSortedArrayUShort
);
136 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues
, wxSortedArrayInt
);
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 class ArraysTestCase
: public CppUnit::TestCase
148 CPPUNIT_TEST_SUITE( ArraysTestCase
);
149 CPPUNIT_TEST( wxStringArrayTest
);
150 CPPUNIT_TEST( wxObjArrayTest
);
151 CPPUNIT_TEST( wxArrayUShortTest
);
152 CPPUNIT_TEST( wxArrayIntTest
);
153 CPPUNIT_TEST( TestSTL
);
154 CPPUNIT_TEST_SUITE_END();
156 void wxStringArrayTest();
157 void wxObjArrayTest();
158 void wxArrayUShortTest();
159 void wxArrayIntTest();
162 DECLARE_NO_COPY_CLASS(ArraysTestCase
)
165 // register in the unnamed registry so that these tests are run by default
166 CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase
);
168 // also include in it's own registry so that these tests can be run alone
169 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase
, "ArraysTestCase" );
171 void ArraysTestCase::wxStringArrayTest()
174 a1
.Add(_T("thermit"));
175 a1
.Add(_T("condor"));
176 a1
.Add(_T("lion"), 3);
179 a1
.Add(_T("alligator"));
181 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1
, _T("thermit") ,
189 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 8 ) );
191 wxArrayString
a2(a1
);
193 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2
, _T("thermit") ,
201 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 8 ) );
203 wxSortedArrayString
a3(a1
);
205 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3
, _T("alligator") ,
213 CPPUNIT_ASSERT( COMPARE_COUNT( a3
, 8 ) );
215 wxSortedArrayString a4
;
216 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end(); it
!= en
; ++it
)
219 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4
, _T("alligator") ,
227 CPPUNIT_ASSERT( COMPARE_COUNT( a4
, 8 ) );
231 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
236 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
240 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2
, _T("thermit") ,
245 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 5 ) );
249 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("alligator") ,
254 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
258 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
263 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
265 a1
.Sort(&StringLenCompare
);
267 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("dog") ,
272 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
275 void ArraysTestCase::wxObjArrayTest()
279 Bar
bar(_T("first bar in general, second bar in array (two copies!)"));
281 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
282 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
284 bars
.Add(new Bar(_T("first bar in array")));
287 CPPUNIT_ASSERT( bars
.GetCount() == 3 );
288 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
290 bars
.RemoveAt(1, bars
.GetCount() - 1);
292 CPPUNIT_ASSERT( bars
.GetCount() == 1 );
293 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
297 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
298 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
300 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
303 #define TestArrayOf(name) \
305 void ArraysTestCase::wxArray ## name ## Test() \
313 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
314 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
316 a.Sort(name ## Compare); \
318 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
319 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
321 a.Sort(name ## RevCompare); \
323 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
324 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
326 wxSortedArray##name b; \
333 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
334 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
341 void ArraysTestCase::TestSTL()
344 wxArrayInt::iterator it
, en
;
345 wxArrayInt::reverse_iterator rit
, ren
;
347 for ( i
= 0; i
< 5; ++i
)
350 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
351 it
!= en
; ++it
, ++i
)
353 CPPUNIT_ASSERT( *it
== i
);
356 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
357 rit
!= ren
; ++rit
, --i
)
359 CPPUNIT_ASSERT( *rit
== i
);
362 CPPUNIT_ASSERT( *list1
.rbegin() == *(list1
.end()-1) &&
363 *list1
.begin() == *(list1
.rend()-1) );
365 it
= list1
.begin()+1;
366 rit
= list1
.rbegin()+1;
367 CPPUNIT_ASSERT( *list1
.begin() == *(it
-1) &&
368 *list1
.rbegin() == *(rit
-1) );
370 CPPUNIT_ASSERT( ( list1
.front() == 0 ) && ( list1
.back() == 4 ) );
372 list1
.erase(list1
.begin());
373 list1
.erase(list1
.end()-1);
375 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
376 it
!= en
; ++it
, ++i
)
378 CPPUNIT_ASSERT( *it
== i
);