]>
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 // ----------------------------------------------------------------------------
24 #include "wx/dynarray.h"
26 // ----------------------------------------------------------------------------
27 // helpers for testing values and sizes
28 // ----------------------------------------------------------------------------
30 #define COMPARE_VALUE( array , index , value ) ( array.Item( index ) == value )
32 #define COMPARE_2_VALUES( array , p0 , p1 ) \
33 COMPARE_VALUE( array , 0 , p0 ) && \
34 COMPARE_VALUE( array , 1 , p1 )
36 #define COMPARE_3_VALUES( array , p0 , p1 , p2 ) \
37 COMPARE_2_VALUES( array , p0 , p1 ) && \
38 COMPARE_VALUE( array , 2 , p2 )
40 #define COMPARE_4_VALUES( array , p0 , p1 , p2 , p3 ) \
41 COMPARE_3_VALUES( array , p0 , p1 , p2 ) && \
42 COMPARE_VALUE( array , 3 , p3 )
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 )
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 )
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 )
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 )
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 )
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 )
68 #define COMPARE_COUNT( array , n ) \
69 ( array.GetCount() == n ) && \
70 ( array.Last() == array.Item( n - 1 ) )
72 // ----------------------------------------------------------------------------
73 // helpers for testing wxObjArray
74 // ----------------------------------------------------------------------------
79 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
80 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
83 static size_t GetNumber() { return ms_bars
; }
85 const wxChar
*GetName() const { return m_name
; }
90 static size_t ms_bars
;
93 size_t Bar::ms_bars
= 0;
95 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
96 #include "wx/arrimpl.cpp"
97 WX_DEFINE_OBJARRAY(ArrayBars
);
99 // ----------------------------------------------------------------------------
100 // helpers for sorting arrays and comparing items
101 // ----------------------------------------------------------------------------
103 int wxCMPFUNC_CONV
StringLenCompare(const wxString
& first
,
104 const wxString
& second
)
106 return first
.length() - second
.length();
109 #define DEFINE_COMPARE(name, T) \
111 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
113 return first - second; \
116 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
118 return *first - *second; \
121 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
123 return *second - *first; \
126 typedef unsigned short ushort;
128 DEFINE_COMPARE(Char
, char);
129 DEFINE_COMPARE(UShort
, ushort
);
130 DEFINE_COMPARE(Int
, int);
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
);
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
);
140 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues
, wxSortedArrayInt
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 class ArraysTestCase
: public CppUnit::TestCase
152 CPPUNIT_TEST_SUITE( ArraysTestCase
);
153 CPPUNIT_TEST( wxStringArrayTest
);
154 CPPUNIT_TEST( wxObjArrayTest
);
155 CPPUNIT_TEST( wxArrayUShortTest
);
156 CPPUNIT_TEST( wxArrayIntTest
);
157 CPPUNIT_TEST( wxArrayCharTest
);
158 CPPUNIT_TEST( TestSTL
);
159 CPPUNIT_TEST( Alloc
);
160 CPPUNIT_TEST_SUITE_END();
162 void wxStringArrayTest();
163 void wxObjArrayTest();
164 void wxArrayUShortTest();
165 void wxArrayIntTest();
166 void wxArrayCharTest();
170 DECLARE_NO_COPY_CLASS(ArraysTestCase
)
173 // register in the unnamed registry so that these tests are run by default
174 CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase
);
176 // also include in it's own registry so that these tests can be run alone
177 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase
, "ArraysTestCase" );
179 void ArraysTestCase::wxStringArrayTest()
182 a1
.Add(_T("thermit"));
183 a1
.Add(_T("condor"));
184 a1
.Add(_T("lion"), 3);
187 a1
.Add(_T("alligator"));
189 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1
, _T("thermit") ,
197 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 8 ) );
199 wxArrayString
a2(a1
);
201 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2
, _T("thermit") ,
209 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 8 ) );
211 wxSortedArrayString
a3(a1
);
213 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3
, _T("alligator") ,
221 CPPUNIT_ASSERT( COMPARE_COUNT( a3
, 8 ) );
223 wxSortedArrayString a4
;
224 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end(); it
!= en
; ++it
)
227 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4
, _T("alligator") ,
235 CPPUNIT_ASSERT( COMPARE_COUNT( a4
, 8 ) );
239 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
244 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
248 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2
, _T("thermit") ,
253 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 5 ) );
257 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("alligator") ,
262 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
266 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
271 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
273 a1
.Sort(&StringLenCompare
);
275 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("dog") ,
280 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
281 CPPUNIT_ASSERT( a1
.Index( _T("dog") ) == 0 );
282 CPPUNIT_ASSERT( a1
.Index( _T("human") ) == 1 );
283 CPPUNIT_ASSERT( a1
.Index( _T("humann") ) == wxNOT_FOUND
);
284 CPPUNIT_ASSERT( a1
.Index( _T("condor") ) == 2 );
285 CPPUNIT_ASSERT( a1
.Index( _T("thermit") ) == 3 );
286 CPPUNIT_ASSERT( a1
.Index( _T("alligator") ) == 4 );
290 CPPUNIT_ASSERT( a5
.Add( _T("x"), 1 ) == 0 );
291 CPPUNIT_ASSERT( a5
.Add( _T("a"), 3 ) == 1 );
293 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5
, _T("x") ,
299 void ArraysTestCase::wxObjArrayTest()
303 Bar
bar(_T("first bar in general, second bar in array (two copies!)"));
305 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
306 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
308 bars
.Add(new Bar(_T("first bar in array")));
311 CPPUNIT_ASSERT( bars
.GetCount() == 3 );
312 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
314 bars
.RemoveAt(1, bars
.GetCount() - 1);
316 CPPUNIT_ASSERT( bars
.GetCount() == 1 );
317 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
321 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
322 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
324 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
327 #define TestArrayOf(name) \
329 void ArraysTestCase::wxArray ## name ## Test() \
337 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
338 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
340 a.Sort(name ## Compare); \
342 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
343 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
345 a.Sort(name ## RevCompare); \
347 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
348 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
350 wxSortedArray##name b; \
357 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
358 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
359 CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \
360 CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \
361 CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \
362 CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \
363 CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \
364 CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \
365 CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \
374 void ArraysTestCase::Alloc()
379 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
383 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
384 CPPUNIT_ASSERT_EQUAL( 17, a
[0] );
385 CPPUNIT_ASSERT_EQUAL( 9, a
[1] );
388 void ArraysTestCase::TestSTL()
391 wxArrayInt::iterator it
, en
;
392 wxArrayInt::reverse_iterator rit
, ren
;
394 static const int COUNT
= 5;
396 for ( i
= 0; i
< COUNT
; ++i
)
399 CPPUNIT_ASSERT( list1
.capacity() >= (size_t)COUNT
);
400 CPPUNIT_ASSERT_EQUAL( (size_t)COUNT
, list1
.size() );
402 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
403 it
!= en
; ++it
, ++i
)
405 CPPUNIT_ASSERT( *it
== i
);
408 CPPUNIT_ASSERT_EQUAL( COUNT
, i
);
410 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= COUNT
;
411 rit
!= ren
; ++rit
, --i
)
413 CPPUNIT_ASSERT( *rit
== i
-1 );
416 CPPUNIT_ASSERT_EQUAL( 0, i
);
418 CPPUNIT_ASSERT( *list1
.rbegin() == *(list1
.end()-1) &&
419 *list1
.begin() == *(list1
.rend()-1) );
421 it
= list1
.begin()+1;
422 rit
= list1
.rbegin()+1;
423 CPPUNIT_ASSERT( *list1
.begin() == *(it
-1) &&
424 *list1
.rbegin() == *(rit
-1) );
426 CPPUNIT_ASSERT( ( list1
.front() == 0 ) && ( list1
.back() == COUNT
- 1 ) );
428 list1
.erase(list1
.begin());
429 list1
.erase(list1
.end()-1);
431 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
432 it
!= en
; ++it
, ++i
)
434 CPPUNIT_ASSERT( *it
== i
);