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
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 class ArraysTestCase
: public CppUnit::TestCase
152 CPPUNIT_TEST_SUITE( ArraysTestCase
);
153 CPPUNIT_TEST( wxStringArrayTest
);
154 CPPUNIT_TEST( wxStringArraySplitTest
);
155 CPPUNIT_TEST( wxStringArrayJoinTest
);
156 CPPUNIT_TEST( wxStringArraySplitJoinTest
);
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();
166 void wxStringArrayTest();
167 void wxStringArraySplitTest();
168 void wxStringArrayJoinTest();
169 void wxStringArraySplitJoinTest();
170 void wxObjArrayTest();
171 void wxArrayUShortTest();
172 void wxArrayIntTest();
173 void wxArrayCharTest();
177 DECLARE_NO_COPY_CLASS(ArraysTestCase
)
180 // register in the unnamed registry so that these tests are run by default
181 CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase
);
183 // also include in it's own registry so that these tests can be run alone
184 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase
, "ArraysTestCase" );
186 void ArraysTestCase::wxStringArrayTest()
189 a1
.Add(_T("thermit"));
190 a1
.Add(_T("condor"));
191 a1
.Add(_T("lion"), 3);
194 a1
.Add(_T("alligator"));
196 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1
, _T("thermit") ,
204 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 8 ) );
206 wxArrayString
a2(a1
);
208 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2
, _T("thermit") ,
216 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 8 ) );
218 wxSortedArrayString
a3(a1
);
220 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3
, _T("alligator") ,
228 CPPUNIT_ASSERT( COMPARE_COUNT( a3
, 8 ) );
230 wxSortedArrayString a4
;
231 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end(); it
!= en
; ++it
)
234 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4
, _T("alligator") ,
242 CPPUNIT_ASSERT( COMPARE_COUNT( a4
, 8 ) );
246 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
251 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
255 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2
, _T("thermit") ,
260 CPPUNIT_ASSERT( COMPARE_COUNT( a2
, 5 ) );
264 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("alligator") ,
269 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
273 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("thermit") ,
278 CPPUNIT_ASSERT( COMPARE_COUNT( a1
, 5 ) );
280 a1
.Sort(&StringLenCompare
);
282 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1
, _T("dog") ,
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 );
297 CPPUNIT_ASSERT( a5
.Add( _T("x"), 1 ) == 0 );
298 CPPUNIT_ASSERT( a5
.Add( _T("a"), 3 ) == 1 );
300 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5
, _T("x") ,
306 void ArraysTestCase::wxStringArraySplitTest()
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("") };
316 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
317 wxArrayString
realarr(wxSplit(str
, wxT(',')));
318 CPPUNIT_ASSERT( exparr
== realarr
);
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("") };
329 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
330 wxArrayString
realarr(wxSplit(str
, wxT(','), wxT('\\')));
331 CPPUNIT_ASSERT( exparr
== realarr
);
333 // escaping turned off:
334 wxArrayString
exparr2(WXSIZEOF(expected2
), expected2
);
335 wxArrayString
realarr2(wxSplit(str
, wxT(','), wxT('\0')));
336 CPPUNIT_ASSERT( exparr2
== realarr2
);
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") };
350 wxArrayString
exparr(WXSIZEOF(expected
), expected
);
351 wxArrayString
realarr(wxSplit(str
, wxT(','), wxT('\\')));
352 CPPUNIT_ASSERT( exparr
== realarr
);
354 // escaping turned off:
355 wxArrayString
exparr2(WXSIZEOF(expected2
), expected2
);
356 wxArrayString
realarr2(wxSplit(str
, wxT(','), wxT('\0')));
357 CPPUNIT_ASSERT( exparr2
== realarr2
);
361 void ArraysTestCase::wxStringArrayJoinTest()
366 const wxChar
*arr
[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") };
367 wxString expected
= wxT("first,,second,third");
369 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
370 wxString result
= wxJoin(arrstr
, wxT(','));
371 CPPUNIT_ASSERT( expected
== result
);
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,,");
380 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
381 wxString result
= wxJoin(arrstr
, wxT(','), wxT('\\'));
382 CPPUNIT_ASSERT( expected
== result
);
384 // escaping turned off:
385 wxString result2
= wxJoin(arrstr
, wxT(','), wxT('\0'));
386 CPPUNIT_ASSERT( expected2
== result2
);
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\\");
396 wxArrayString
arrstr(WXSIZEOF(arr
), arr
);
397 wxString result
= wxJoin(arrstr
, wxT(','), wxT('\\'));
398 CPPUNIT_ASSERT( expected
== result
);
400 // escaping turned off:
401 wxString result2
= wxJoin(arrstr
, wxT(','), wxT('\0'));
402 CPPUNIT_ASSERT( expected2
== result2
);
406 void ArraysTestCase::wxStringArraySplitJoinTest()
408 wxChar separators
[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'),
409 wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'),
410 wxT('\''), wxT('\n'), wxT('!'), wxT('-') };
412 // test with a string: split it and then rejoin it:
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>");
428 for (i
= 0; i
< WXSIZEOF(separators
); i
++)
430 wxArrayString arr
= wxSplit(str
, separators
[i
]);
431 CPPUNIT_ASSERT( str
== wxJoin(arr
, separators
[i
]) );
435 // test with an array: join it and then resplit it:
437 const wxChar
*arr
[] =
439 wxT("first, second!"), wxT("this is the third!!"),
440 wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "),
441 wxT("_sixth__token__with_underscores"), wxT("The! Last! One!")
443 wxArrayString
theArr(WXSIZEOF(arr
), arr
);
445 for (i
= 0; i
< WXSIZEOF(separators
); i
++)
447 wxString string
= wxJoin(theArr
, separators
[i
]);
448 CPPUNIT_ASSERT( theArr
== wxSplit(string
, separators
[i
]) );
451 wxArrayString emptyArray
;
452 wxString string
= wxJoin(emptyArray
, _T(';'));
453 CPPUNIT_ASSERT( string
.empty() );
455 CPPUNIT_ASSERT( wxSplit(string
, _T(';')).empty() );
457 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxSplit(_T(";"), _T(';')).size() );
460 void ArraysTestCase::wxObjArrayTest()
464 Bar
bar(_T("first bar in general, second bar in array (two copies!)"));
466 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
467 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
469 bars
.Add(new Bar(_T("first bar in array")));
472 CPPUNIT_ASSERT( bars
.GetCount() == 3 );
473 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
475 bars
.RemoveAt(1, bars
.GetCount() - 1);
477 CPPUNIT_ASSERT( bars
.GetCount() == 1 );
478 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
482 CPPUNIT_ASSERT( bars
.GetCount() == 0 );
483 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
485 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
488 #define TestArrayOf(name) \
490 void ArraysTestCase::wxArray ## name ## Test() \
498 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
499 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
501 a.Sort(name ## Compare); \
503 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
504 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
506 a.Sort(name ## RevCompare); \
508 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
509 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
511 wxSortedArray##name b; \
518 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
519 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
520 CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \
521 CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \
522 CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \
523 CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \
524 CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \
525 CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \
526 CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \
535 void ArraysTestCase::Alloc()
540 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
544 CPPUNIT_ASSERT_EQUAL( size_t(2), a
.GetCount() );
545 CPPUNIT_ASSERT_EQUAL( 17, a
[0] );
546 CPPUNIT_ASSERT_EQUAL( 9, a
[1] );
549 void ArraysTestCase::TestSTL()
552 wxArrayInt::iterator it
, en
;
553 wxArrayInt::reverse_iterator rit
, ren
;
555 static const int COUNT
= 5;
557 for ( i
= 0; i
< COUNT
; ++i
)
560 CPPUNIT_ASSERT( list1
.capacity() >= (size_t)COUNT
);
561 CPPUNIT_ASSERT_EQUAL( (size_t)COUNT
, list1
.size() );
563 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
564 it
!= en
; ++it
, ++i
)
566 CPPUNIT_ASSERT( *it
== i
);
569 CPPUNIT_ASSERT_EQUAL( COUNT
, i
);
571 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= COUNT
;
572 rit
!= ren
; ++rit
, --i
)
574 CPPUNIT_ASSERT( *rit
== i
-1 );
577 CPPUNIT_ASSERT_EQUAL( 0, i
);
579 CPPUNIT_ASSERT( *list1
.rbegin() == *(list1
.end()-1) &&
580 *list1
.begin() == *(list1
.rend()-1) );
582 it
= list1
.begin()+1;
583 rit
= list1
.rbegin()+1;
584 CPPUNIT_ASSERT( *list1
.begin() == *(it
-1) &&
585 *list1
.rbegin() == *(rit
-1) );
587 CPPUNIT_ASSERT( ( list1
.front() == 0 ) && ( list1
.back() == COUNT
- 1 ) );
589 list1
.erase(list1
.begin());
590 list1
.erase(list1
.end()-1);
592 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
593 it
!= en
; ++it
, ++i
)
595 CPPUNIT_ASSERT( *it
== i
);