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
.c_str(); }
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
);
144 Item(int n_
= 0) : n(n_
) { }
149 WX_DEFINE_ARRAY_PTR(Item
*, ItemPtrArray
);
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 class ArraysTestCase
: public CppUnit::TestCase
161 CPPUNIT_TEST_SUITE( ArraysTestCase
);
162 CPPUNIT_TEST( wxStringArrayTest
);
163 CPPUNIT_TEST( wxStringArraySplitTest
);
164 CPPUNIT_TEST( wxStringArrayJoinTest
);
165 CPPUNIT_TEST( wxStringArraySplitJoinTest
);
167 CPPUNIT_TEST( wxObjArrayTest
);
168 CPPUNIT_TEST( wxArrayUShortTest
);
169 CPPUNIT_TEST( wxArrayIntTest
);
170 CPPUNIT_TEST( wxArrayCharTest
);
171 CPPUNIT_TEST( TestSTL
);
172 CPPUNIT_TEST( Alloc
);
173 CPPUNIT_TEST( Swap
);
174 CPPUNIT_TEST_SUITE_END();
176 void wxStringArrayTest();
177 void wxStringArraySplitTest();
178 void wxStringArrayJoinTest();
179 void wxStringArraySplitJoinTest();
180 void wxObjArrayTest();
181 void wxArrayUShortTest();
182 void wxArrayIntTest();
183 void wxArrayCharTest();
188 DECLARE_NO_COPY_CLASS(ArraysTestCase
)
191 // register in the unnamed registry so that these tests are run by default
192 CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase
);
194 // also include in it's own registry so that these tests can be run alone
195 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase
, "ArraysTestCase" );
197 void ArraysTestCase::wxStringArrayTest()
200 a1
.Add(_T("thermit"));
201 a1
.Add(_T("condor"));
202 a1
.Add(_T("lion"), 3);
205 a1
.Add(_T("alligator"));
207 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1
, _T("thermit") ,
215 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 8 ) );
217 wxArrayString
a2(a1
);
219 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2
, _T("thermit") ,
227 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 8 ) );
229 wxSortedArrayString
a3(a1
);
231 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3
, _T("alligator") ,
239 CPPUNIT_ASSERT( COMPARE_COUNT( a3
, 8 ) );
241 wxSortedArrayString a4
;
242 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end(); it
!= en
; ++it
)
245 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4
, _T("alligator") ,
253 CPPUNIT_ASSERT( COMPARE_COUNT( a4
, 8 ) );
257 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
262 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
266 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2
, _T("thermit") ,
271 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 5 ) );
275 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("alligator") ,
280 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
284 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
289 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
291 a1
.Sort(&StringLenCompare
);
293 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("dog") ,
298 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
299 CPPUNIT_ASSERT( a1
.Index( _T("dog") ) == 0 );
300 CPPUNIT_ASSERT( a1
.Index( _T("human") ) == 1 );
301 CPPUNIT_ASSERT( a1
.Index( _T("humann") ) == wxNOT_FOUND
);
302 CPPUNIT_ASSERT( a1
.Index( _T("condor") ) == 2 );
303 CPPUNIT_ASSERT( a1
.Index( _T("thermit") ) == 3 );
304 CPPUNIT_ASSERT( a1
.Index( _T("alligator") ) == 4 );
308 CPPUNIT_ASSERT( a5
.Add( _T("x"), 1 ) == 0 );
309 CPPUNIT_ASSERT( a5
.Add( _T("a"), 3 ) == 1 );
311 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5
, _T("x") ,
317 void ArraysTestCase::wxStringArraySplitTest()
322 wxString str
= wxT(",,,,first,second,third,,");
323 const wxChar
*expected
[] =
324 { wxT(""), wxT(""), wxT(""), wxT(""), wxT("first"),
325 wxT("second"), wxT("third"), wxT(""), wxT("") };
327 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
328 wxArrayString
realarr(wxSplit(str
, wxT(',')));
329 CPPUNIT_ASSERT( exparr
== realarr
);
333 wxString str
= wxT(",\\,first,second,third,");
334 const wxChar
*expected
[] =
335 { wxT(""), wxT(",first"), wxT("second"), wxT("third"), wxT("") };
336 const wxChar
*expected2
[] =
337 { wxT(""), wxT("\\"), wxT("first"), wxT("second"), wxT("third"), wxT("") };
340 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
341 wxArrayString
realarr(wxSplit(str
, wxT(','), wxT('\\')));
342 CPPUNIT_ASSERT( exparr
== realarr
);
344 // escaping turned off:
345 wxArrayString
exparr2(WXSIZEOF(expected2
), expected2
);
346 wxArrayString
realarr2(wxSplit(str
, wxT(','), wxT('\0')));
347 CPPUNIT_ASSERT( exparr2
== realarr2
);
351 // test is escape characters placed before non-separator character are
352 // just ignored as they should:
353 wxString str
= wxT(",\\,,fir\\st,se\\cond\\,,\\third");
354 const wxChar
*expected
[] =
355 { wxT(""), wxT(","), wxT("fir\\st"), wxT("se\\cond,"), wxT("\\third") };
356 const wxChar
*expected2
[] =
357 { wxT(""), wxT("\\"), wxT(""), wxT("fir\\st"), wxT("se\\cond\\"),
358 wxT(""), wxT("\\third") };
361 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
362 wxArrayString
realarr(wxSplit(str
, wxT(','), wxT('\\')));
363 CPPUNIT_ASSERT( exparr
== realarr
);
365 // escaping turned off:
366 wxArrayString
exparr2(WXSIZEOF(expected2
), expected2
);
367 wxArrayString
realarr2(wxSplit(str
, wxT(','), wxT('\0')));
368 CPPUNIT_ASSERT( exparr2
== realarr2
);
372 void ArraysTestCase::wxStringArrayJoinTest()
377 const wxChar
*arr
[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") };
378 wxString expected
= wxT("first,,second,third");
380 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
381 wxString result
= wxJoin(arrstr
, wxT(','));
382 CPPUNIT_ASSERT( expected
== result
);
386 const wxChar
*arr
[] = { wxT("first, word"), wxT(",,second"), wxT("third,,") };
387 wxString expected
= wxT("first\\, word,\\,\\,second,third\\,\\,");
388 wxString expected2
= wxT("first, word,,,second,third,,");
391 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
392 wxString result
= wxJoin(arrstr
, wxT(','), wxT('\\'));
393 CPPUNIT_ASSERT( expected
== result
);
395 // escaping turned off:
396 wxString result2
= wxJoin(arrstr
, wxT(','), wxT('\0'));
397 CPPUNIT_ASSERT( expected2
== result2
);
401 // test is escape characters placed in the original array are just ignored as they should:
402 const wxChar
*arr
[] = { wxT("first\\, wo\\rd"), wxT("seco\\nd"), wxT("\\third\\") };
403 wxString expected
= wxT("first\\\\, wo\\rd,seco\\nd,\\third\\");
404 wxString expected2
= wxT("first\\, wo\\rd,seco\\nd,\\third\\");
407 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
408 wxString result
= wxJoin(arrstr
, wxT(','), wxT('\\'));
409 CPPUNIT_ASSERT( expected
== result
);
411 // escaping turned off:
412 wxString result2
= wxJoin(arrstr
, wxT(','), wxT('\0'));
413 CPPUNIT_ASSERT( expected2
== result2
);
417 void ArraysTestCase::wxStringArraySplitJoinTest()
419 wxChar separators
[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'),
420 wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'),
421 wxT('\''), wxT('\n'), wxT('!'), wxT('-') };
423 // test with a string: split it and then rejoin it:
425 wxString str
= wxT("This is a long, long test; if wxSplit and wxJoin do work ")
426 wxT("correctly, then splitting and joining this 'text' _multiple_ ")
427 wxT("times shouldn't cause any loss of content.\n")
428 wxT("This is some latex code: ")
429 wxT("\\func{wxString}{wxJoin}{")
430 wxT("\\param{const wxArray String\\&}{ arr}, ")
431 wxT("\\param{const wxChar}{ sep}, ")
432 wxT("\\param{const wxChar}{ escape = '\\'}}.\n")
433 wxT("This is some HTML code: ")
434 wxT("<html><head><meta http-equiv=\"content-type\">")
435 wxT("<title>Initial page of Mozilla Firefox</title>")
436 wxT("</meta></head></html>");
439 for (i
= 0; i
< WXSIZEOF(separators
); i
++)
441 wxArrayString arr
= wxSplit(str
, separators
[i
]);
442 CPPUNIT_ASSERT( str
== wxJoin(arr
, separators
[i
]) );
446 // test with an array: join it and then resplit it:
448 const wxChar
*arr
[] =
450 wxT("first, second!"), wxT("this is the third!!"),
451 wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "),
452 wxT("_sixth__token__with_underscores"), wxT("The! Last! One!")
454 wxArrayString
theArr(WXSIZEOF(arr
), arr
);
456 for (i
= 0; i
< WXSIZEOF(separators
); i
++)
458 wxString string
= wxJoin(theArr
, separators
[i
]);
459 CPPUNIT_ASSERT( theArr
== wxSplit(string
, separators
[i
]) );
462 wxArrayString emptyArray
;
463 wxString string
= wxJoin(emptyArray
, _T(';'));
464 CPPUNIT_ASSERT( string
.empty() );
466 CPPUNIT_ASSERT( wxSplit(string
, _T(';')).empty() );
468 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxSplit(_T(";"), _T(';')).size() );
471 void ArraysTestCase::wxObjArrayTest()
475 Bar
bar(_T("first bar in general, second bar in array (two copies!)"));
477 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
478 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
480 bars
.Add(new Bar(_T("first bar in array")));
483 CPPUNIT_ASSERT( bars
.GetCount() == 3 );
484 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
486 bars
.RemoveAt(1, bars
.GetCount() - 1);
488 CPPUNIT_ASSERT( bars
.GetCount() == 1 );
489 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
493 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
494 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
496 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
499 #define TestArrayOf(name) \
501 void ArraysTestCase::wxArray ## name ## Test() \
509 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
510 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
512 a.Sort(name ## Compare); \
514 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
515 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
517 a.Sort(name ## RevCompare); \
519 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
520 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
522 wxSortedArray##name b; \
529 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
530 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
531 CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \
532 CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \
533 CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \
534 CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \
535 CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \
536 CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \
537 CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \
546 void ArraysTestCase::Alloc()
551 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
555 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
556 CPPUNIT_ASSERT_EQUAL( 17, a
[0] );
557 CPPUNIT_ASSERT_EQUAL( 9, a
[1] );
563 template <typename A
, typename T
>
564 void DoTestSwap(T v1
, T v2
, T v3
,
565 A
* WXUNUSED(dummyUglyVC6Workaround
))
569 CPPUNIT_ASSERT( a1
.empty() && a2
.empty() );
573 CPPUNIT_ASSERT( a1
.empty() );
574 CPPUNIT_ASSERT_EQUAL( 1, a2
.size() );
579 CPPUNIT_ASSERT_EQUAL( 1, a1
.size() );
580 CPPUNIT_ASSERT_EQUAL( 2, a2
.size() );
581 CPPUNIT_ASSERT_EQUAL( v1
, a1
[0] );
582 CPPUNIT_ASSERT_EQUAL( v3
, a2
[1] );
585 CPPUNIT_ASSERT_EQUAL( 2, a1
.size() );
586 CPPUNIT_ASSERT_EQUAL( 1, a2
.size() );
589 } // anonymous namespace
591 void ArraysTestCase::Swap()
593 DoTestSwap("Foo", "Bar", "Baz", (wxArrayString
*)NULL
);
594 DoTestSwap(1, 10, 100, (wxArrayInt
*)NULL
);
595 DoTestSwap(6, 28, 496, (wxArrayLong
*)NULL
);
598 void ArraysTestCase::TestSTL()
601 wxArrayInt::iterator it
, en
;
602 wxArrayInt::reverse_iterator rit
, ren
;
604 static const int COUNT
= 5;
606 for ( i
= 0; i
< COUNT
; ++i
)
609 CPPUNIT_ASSERT( list1
.capacity() >= (size_t)COUNT
);
610 CPPUNIT_ASSERT_EQUAL( (size_t)COUNT
, list1
.size() );
612 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
613 it
!= en
; ++it
, ++i
)
615 CPPUNIT_ASSERT( *it
== i
);
618 CPPUNIT_ASSERT_EQUAL( COUNT
, i
);
620 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= COUNT
;
621 rit
!= ren
; ++rit
, --i
)
623 CPPUNIT_ASSERT( *rit
== i
-1 );
626 CPPUNIT_ASSERT_EQUAL( 0, i
);
628 CPPUNIT_ASSERT( *list1
.rbegin() == *(list1
.end()-1) &&
629 *list1
.begin() == *(list1
.rend()-1) );
631 it
= list1
.begin()+1;
632 rit
= list1
.rbegin()+1;
633 CPPUNIT_ASSERT( *list1
.begin() == *(it
-1) &&
634 *list1
.rbegin() == *(rit
-1) );
636 CPPUNIT_ASSERT( ( list1
.front() == 0 ) && ( list1
.back() == COUNT
- 1 ) );
638 list1
.erase(list1
.begin());
639 list1
.erase(list1
.end()-1);
641 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
642 it
!= en
; ++it
, ++i
)
644 CPPUNIT_ASSERT( *it
== i
);
649 items
.push_back(new Item(17));
650 CPPUNIT_ASSERT_EQUAL( 17, (*(items
.rbegin()))->n
);
651 CPPUNIT_ASSERT_EQUAL( 17, (**items
.begin()).n
);