]> git.saurik.com Git - wxWidgets.git/blame - tests/arrays/arrays.cpp
No changes whatsoever, just remove trailing whitespace.
[wxWidgets.git] / tests / arrays / arrays.cpp
CommitLineData
cf1014a2
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/arrays/arrays.cpp
3// Purpose: wxArray unit test
be2c4488 4// Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
cf1014a2
VS
5// Created: 2004-04-01
6// RCS-ID: $Id$
be2c4488 7// Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
cf1014a2
VS
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
8899b155 14#include "testprec.h"
cf1014a2
VS
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
cf1014a2
VS
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
76class Bar
77{
78public:
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
86501081 85 const wxChar *GetName() const { return m_name.c_str(); }
cf1014a2
VS
86
87private:
88 wxString m_name;
89
90 static size_t ms_bars;
91};
92
93size_t Bar::ms_bars = 0;
94
95WX_DECLARE_OBJARRAY(Bar, ArrayBars);
96#include "wx/arrimpl.cpp"
93a800a9 97WX_DEFINE_OBJARRAY(ArrayBars)
cf1014a2
VS
98
99// ----------------------------------------------------------------------------
100// helpers for sorting arrays and comparing items
101// ----------------------------------------------------------------------------
102
103int 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 \
111int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
112{ \
113 return first - second; \
114} \
115 \
116int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
117{ \
118 return *first - *second; \
119} \
120 \
121int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
122{ \
123 return *second - *first; \
124} \
125
126typedef unsigned short ushort;
127
93a800a9
VZ
128DEFINE_COMPARE(Char, char)
129DEFINE_COMPARE(UShort, ushort)
130DEFINE_COMPARE(Int, int)
cf1014a2 131
1ffc8d7a
VZ
132WX_DEFINE_ARRAY_CHAR(char, wxArrayChar);
133WX_DEFINE_SORTED_ARRAY_CHAR(char, wxSortedArrayCharNoCmp);
134WX_DEFINE_SORTED_ARRAY_CMP_CHAR(char, CharCompareValues, wxSortedArrayChar);
135
cf1014a2
VS
136WX_DEFINE_ARRAY_SHORT(ushort, wxArrayUShort);
137WX_DEFINE_SORTED_ARRAY_SHORT(ushort, wxSortedArrayUShortNoCmp);
138WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort, UShortCompareValues, wxSortedArrayUShort);
1ffc8d7a 139
cf1014a2
VS
140WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt);
141
35d5da67
VZ
142struct Item
143{
144 Item(int n_ = 0) : n(n_) { }
145
146 int n;
147};
148
149WX_DEFINE_ARRAY_PTR(Item *, ItemPtrArray);
150
cf1014a2
VS
151// ----------------------------------------------------------------------------
152// test class
153// ----------------------------------------------------------------------------
154
155class ArraysTestCase : public CppUnit::TestCase
156{
157public:
158 ArraysTestCase() { }
159
160private:
161 CPPUNIT_TEST_SUITE( ArraysTestCase );
162 CPPUNIT_TEST( wxStringArrayTest );
abbb59e8
VZ
163 CPPUNIT_TEST( wxStringArraySplitTest );
164 CPPUNIT_TEST( wxStringArrayJoinTest );
165 CPPUNIT_TEST( wxStringArraySplitJoinTest );
166
cf1014a2
VS
167 CPPUNIT_TEST( wxObjArrayTest );
168 CPPUNIT_TEST( wxArrayUShortTest );
169 CPPUNIT_TEST( wxArrayIntTest );
1ffc8d7a 170 CPPUNIT_TEST( wxArrayCharTest );
cf1014a2 171 CPPUNIT_TEST( TestSTL );
7788fc40 172 CPPUNIT_TEST( Alloc );
0c61ab6f 173 CPPUNIT_TEST( Clear );
91276868 174 CPPUNIT_TEST( Swap );
cf1014a2
VS
175 CPPUNIT_TEST_SUITE_END();
176
177 void wxStringArrayTest();
abbb59e8
VZ
178 void wxStringArraySplitTest();
179 void wxStringArrayJoinTest();
180 void wxStringArraySplitJoinTest();
cf1014a2
VS
181 void wxObjArrayTest();
182 void wxArrayUShortTest();
183 void wxArrayIntTest();
1ffc8d7a 184 void wxArrayCharTest();
cf1014a2 185 void TestSTL();
7788fc40 186 void Alloc();
0c61ab6f 187 void Clear();
91276868 188 void Swap();
cf1014a2
VS
189
190 DECLARE_NO_COPY_CLASS(ArraysTestCase)
191};
192
193// register in the unnamed registry so that these tests are run by default
194CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase );
195
196// also include in it's own registry so that these tests can be run alone
197CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" );
198
199void ArraysTestCase::wxStringArrayTest()
200{
201 wxArrayString a1;
9a83f860
VZ
202 a1.Add(wxT("thermit"));
203 a1.Add(wxT("condor"));
204 a1.Add(wxT("lion"), 3);
205 a1.Add(wxT("dog"));
206 a1.Add(wxT("human"));
207 a1.Add(wxT("alligator"));
208
209 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , wxT("thermit") ,
210 wxT("condor") ,
211 wxT("lion") ,
212 wxT("lion") ,
213 wxT("lion") ,
214 wxT("dog") ,
215 wxT("human") ,
216 wxT("alligator") ) );
cf1014a2
VS
217 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) );
218
219 wxArrayString a2(a1);
220
9a83f860
VZ
221 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , wxT("thermit") ,
222 wxT("condor") ,
223 wxT("lion") ,
224 wxT("lion") ,
225 wxT("lion") ,
226 wxT("dog") ,
227 wxT("human") ,
228 wxT("alligator") ) );
cf1014a2
VS
229 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) );
230
231 wxSortedArrayString a3(a1);
232
9a83f860
VZ
233 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , wxT("alligator") ,
234 wxT("condor") ,
235 wxT("dog") ,
236 wxT("human") ,
237 wxT("lion") ,
238 wxT("lion") ,
239 wxT("lion") ,
240 wxT("thermit") ) );
cf1014a2
VS
241 CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) );
242
243 wxSortedArrayString a4;
244 for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it)
245 a4.Add(*it);
246
9a83f860
VZ
247 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , wxT("alligator") ,
248 wxT("condor") ,
249 wxT("dog") ,
250 wxT("human") ,
251 wxT("lion") ,
252 wxT("lion") ,
253 wxT("lion") ,
254 wxT("thermit") ) );
cf1014a2
VS
255 CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) );
256
257 a1.RemoveAt(2,3);
258
9a83f860
VZ
259 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("thermit") ,
260 wxT("condor") ,
261 wxT("dog") ,
262 wxT("human") ,
263 wxT("alligator") ) );
cf1014a2
VS
264 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
265
266 a2 = a1;
267
9a83f860
VZ
268 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , wxT("thermit") ,
269 wxT("condor") ,
270 wxT("dog") ,
271 wxT("human") ,
272 wxT("alligator") ) );
cf1014a2
VS
273 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) );
274
275 a1.Sort(false);
276
9a83f860
VZ
277 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("alligator") ,
278 wxT("condor") ,
279 wxT("dog") ,
280 wxT("human") ,
281 wxT("thermit") ) );
cf1014a2
VS
282 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
283
284 a1.Sort(true);
285
9a83f860
VZ
286 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("thermit") ,
287 wxT("human") ,
288 wxT("dog") ,
289 wxT("condor") ,
290 wxT("alligator") ) );
cf1014a2
VS
291 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
292
293 a1.Sort(&StringLenCompare);
294
9a83f860
VZ
295 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , wxT("dog") ,
296 wxT("human") ,
297 wxT("condor") ,
298 wxT("thermit") ,
299 wxT("alligator") ) );
cf1014a2 300 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
9a83f860
VZ
301 CPPUNIT_ASSERT( a1.Index( wxT("dog") ) == 0 );
302 CPPUNIT_ASSERT( a1.Index( wxT("human") ) == 1 );
303 CPPUNIT_ASSERT( a1.Index( wxT("humann") ) == wxNOT_FOUND );
304 CPPUNIT_ASSERT( a1.Index( wxT("condor") ) == 2 );
305 CPPUNIT_ASSERT( a1.Index( wxT("thermit") ) == 3 );
306 CPPUNIT_ASSERT( a1.Index( wxT("alligator") ) == 4 );
555d0d7e
MB
307
308 wxArrayString a5;
309
9a83f860
VZ
310 CPPUNIT_ASSERT( a5.Add( wxT("x"), 1 ) == 0 );
311 CPPUNIT_ASSERT( a5.Add( wxT("a"), 3 ) == 1 );
555d0d7e 312
9a83f860
VZ
313 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, wxT("x") ,
314 wxT("a") ,
315 wxT("a") ,
316 wxT("a") ) );
d11c9d86
VZ
317
318 a5.assign(a1.end(), a1.end());
319 CPPUNIT_ASSERT( a5.empty() );
320
321 a5.assign(a1.begin(), a1.end());
322 CPPUNIT_ASSERT( a5 == a1 );
323
324#ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
325 const wxString months[] = { "Jan", "Feb", "Mar" };
326 a5.assign(months, months + WXSIZEOF(months));
327 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(months), a5.size() );
328 CPPUNIT_ASSERT( COMPARE_3_VALUES(a5, "Jan", "Feb", "Mar") );
329#endif // wxHAS_VECTOR_TEMPLATE_ASSIGN
3d156e93
VZ
330
331 a5.clear();
332 CPPUNIT_ASSERT_EQUAL( 0, a5.size() );
333
334 a5.resize(7, "Foo");
335 CPPUNIT_ASSERT_EQUAL( 7, a5.size() );
336 CPPUNIT_ASSERT_EQUAL( "Foo", a5[3] );
337
338 a5.resize(3);
339 CPPUNIT_ASSERT_EQUAL( 3, a5.size() );
340 CPPUNIT_ASSERT_EQUAL( "Foo", a5[2] );
cf1014a2
VS
341}
342
abbb59e8
VZ
343void ArraysTestCase::wxStringArraySplitTest()
344{
345 // test wxSplit:
346
347 {
348 wxString str = wxT(",,,,first,second,third,,");
349 const wxChar *expected[] =
350 { wxT(""), wxT(""), wxT(""), wxT(""), wxT("first"),
351 wxT("second"), wxT("third"), wxT(""), wxT("") };
352
353 wxArrayString exparr(WXSIZEOF(expected), expected);
354 wxArrayString realarr(wxSplit(str, wxT(',')));
355 CPPUNIT_ASSERT( exparr == realarr );
356 }
357
358 {
359 wxString str = wxT(",\\,first,second,third,");
360 const wxChar *expected[] =
361 { wxT(""), wxT(",first"), wxT("second"), wxT("third"), wxT("") };
362 const wxChar *expected2[] =
363 { wxT(""), wxT("\\"), wxT("first"), wxT("second"), wxT("third"), wxT("") };
364
365 // escaping on:
366 wxArrayString exparr(WXSIZEOF(expected), expected);
367 wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\')));
368 CPPUNIT_ASSERT( exparr == realarr );
369
370 // escaping turned off:
371 wxArrayString exparr2(WXSIZEOF(expected2), expected2);
372 wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0')));
373 CPPUNIT_ASSERT( exparr2 == realarr2 );
374 }
375
376 {
377 // test is escape characters placed before non-separator character are
378 // just ignored as they should:
379 wxString str = wxT(",\\,,fir\\st,se\\cond\\,,\\third");
380 const wxChar *expected[] =
381 { wxT(""), wxT(","), wxT("fir\\st"), wxT("se\\cond,"), wxT("\\third") };
382 const wxChar *expected2[] =
383 { wxT(""), wxT("\\"), wxT(""), wxT("fir\\st"), wxT("se\\cond\\"),
384 wxT(""), wxT("\\third") };
385
386 // escaping on:
387 wxArrayString exparr(WXSIZEOF(expected), expected);
388 wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\')));
389 CPPUNIT_ASSERT( exparr == realarr );
390
391 // escaping turned off:
392 wxArrayString exparr2(WXSIZEOF(expected2), expected2);
393 wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0')));
394 CPPUNIT_ASSERT( exparr2 == realarr2 );
395 }
396}
397
398void ArraysTestCase::wxStringArrayJoinTest()
399{
400 // test wxJoin:
401
402 {
403 const wxChar *arr[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") };
404 wxString expected = wxT("first,,second,third");
405
406 wxArrayString arrstr(WXSIZEOF(arr), arr);
407 wxString result = wxJoin(arrstr, wxT(','));
408 CPPUNIT_ASSERT( expected == result );
409 }
410
411 {
412 const wxChar *arr[] = { wxT("first, word"), wxT(",,second"), wxT("third,,") };
413 wxString expected = wxT("first\\, word,\\,\\,second,third\\,\\,");
414 wxString expected2 = wxT("first, word,,,second,third,,");
415
416 // escaping on:
417 wxArrayString arrstr(WXSIZEOF(arr), arr);
418 wxString result = wxJoin(arrstr, wxT(','), wxT('\\'));
419 CPPUNIT_ASSERT( expected == result );
420
421 // escaping turned off:
422 wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0'));
423 CPPUNIT_ASSERT( expected2 == result2 );
424 }
425
426 {
427 // test is escape characters placed in the original array are just ignored as they should:
428 const wxChar *arr[] = { wxT("first\\, wo\\rd"), wxT("seco\\nd"), wxT("\\third\\") };
429 wxString expected = wxT("first\\\\, wo\\rd,seco\\nd,\\third\\");
430 wxString expected2 = wxT("first\\, wo\\rd,seco\\nd,\\third\\");
431
432 // escaping on:
433 wxArrayString arrstr(WXSIZEOF(arr), arr);
434 wxString result = wxJoin(arrstr, wxT(','), wxT('\\'));
435 CPPUNIT_ASSERT( expected == result );
436
437 // escaping turned off:
438 wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0'));
439 CPPUNIT_ASSERT( expected2 == result2 );
440 }
441}
442
443void ArraysTestCase::wxStringArraySplitJoinTest()
444{
445 wxChar separators[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'),
446 wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'),
447 wxT('\''), wxT('\n'), wxT('!'), wxT('-') };
448
449 // test with a string: split it and then rejoin it:
450
451 wxString str = wxT("This is a long, long test; if wxSplit and wxJoin do work ")
452 wxT("correctly, then splitting and joining this 'text' _multiple_ ")
453 wxT("times shouldn't cause any loss of content.\n")
454 wxT("This is some latex code: ")
455 wxT("\\func{wxString}{wxJoin}{")
456 wxT("\\param{const wxArray String\\&}{ arr}, ")
457 wxT("\\param{const wxChar}{ sep}, ")
458 wxT("\\param{const wxChar}{ escape = '\\'}}.\n")
459 wxT("This is some HTML code: ")
460 wxT("<html><head><meta http-equiv=\"content-type\">")
461 wxT("<title>Initial page of Mozilla Firefox</title>")
462 wxT("</meta></head></html>");
463
5098c258
VZ
464 size_t i;
465 for (i = 0; i < WXSIZEOF(separators); i++)
abbb59e8
VZ
466 {
467 wxArrayString arr = wxSplit(str, separators[i]);
468 CPPUNIT_ASSERT( str == wxJoin(arr, separators[i]) );
469 }
470
471
472 // test with an array: join it and then resplit it:
473
474 const wxChar *arr[] =
475 {
476 wxT("first, second!"), wxT("this is the third!!"),
477 wxT("\nThat's the fourth token\n\n"), wxT(" - fifth\ndummy\ntoken - "),
478 wxT("_sixth__token__with_underscores"), wxT("The! Last! One!")
479 };
480 wxArrayString theArr(WXSIZEOF(arr), arr);
481
5098c258 482 for (i = 0; i < WXSIZEOF(separators); i++)
abbb59e8
VZ
483 {
484 wxString string = wxJoin(theArr, separators[i]);
485 CPPUNIT_ASSERT( theArr == wxSplit(string, separators[i]) );
486 }
487
488 wxArrayString emptyArray;
9a83f860 489 wxString string = wxJoin(emptyArray, wxT(';'));
abbb59e8
VZ
490 CPPUNIT_ASSERT( string.empty() );
491
9a83f860 492 CPPUNIT_ASSERT( wxSplit(string, wxT(';')).empty() );
abbb59e8 493
9a83f860 494 CPPUNIT_ASSERT_EQUAL( 2, wxSplit(wxT(";"), wxT(';')).size() );
abbb59e8
VZ
495}
496
cf1014a2
VS
497void ArraysTestCase::wxObjArrayTest()
498{
499 {
500 ArrayBars bars;
9a83f860 501 Bar bar(wxT("first bar in general, second bar in array (two copies!)"));
cf1014a2 502
7ee34f2b
VZ
503 CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() );
504 CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() );
cf1014a2 505
9a83f860 506 bars.Add(new Bar(wxT("first bar in array")));
0c61ab6f 507 bars.Add(bar, 2);
cf1014a2 508
7ee34f2b
VZ
509 CPPUNIT_ASSERT_EQUAL( 3, bars.GetCount() );
510 CPPUNIT_ASSERT_EQUAL( 4, Bar::GetNumber() );
cf1014a2
VS
511
512 bars.RemoveAt(1, bars.GetCount() - 1);
513
7ee34f2b
VZ
514 CPPUNIT_ASSERT_EQUAL( 1, bars.GetCount() );
515 CPPUNIT_ASSERT_EQUAL( 2, Bar::GetNumber() );
cf1014a2
VS
516
517 bars.Empty();
518
7ee34f2b
VZ
519 CPPUNIT_ASSERT_EQUAL( 0, bars.GetCount() );
520 CPPUNIT_ASSERT_EQUAL( 1, Bar::GetNumber() );
cf1014a2 521 }
7ee34f2b 522 CPPUNIT_ASSERT_EQUAL( 0, Bar::GetNumber() );
cf1014a2
VS
523}
524
525#define TestArrayOf(name) \
526 \
527void ArraysTestCase::wxArray ## name ## Test() \
528{ \
529 wxArray##name a; \
530 a.Add(1); \
531 a.Add(17,2); \
532 a.Add(5,3); \
533 a.Add(3,4); \
534 \
535 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
536 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
537 \
538 a.Sort(name ## Compare); \
539 \
540 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
541 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
542 \
543 a.Sort(name ## RevCompare); \
544 \
545 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
546 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
547 \
548 wxSortedArray##name b; \
549 \
550 b.Add(1); \
551 b.Add(17); \
552 b.Add(5); \
553 b.Add(3); \
554 \
555 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
556 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
10dcbe63
MB
557 CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \
558 CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \
559 CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \
560 CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \
561 CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \
562 CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \
563 CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \
cf1014a2
VS
564}
565
93a800a9 566TestArrayOf(UShort)
cf1014a2 567
93a800a9 568TestArrayOf(Char)
1ffc8d7a 569
93a800a9 570TestArrayOf(Int)
cf1014a2 571
7788fc40
VZ
572void ArraysTestCase::Alloc()
573{
574 wxArrayInt a;
575 a.Add(17);
576 a.Add(9);
10d256d6 577 CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() );
7788fc40
VZ
578
579 a.Alloc(1000);
580
10d256d6 581 CPPUNIT_ASSERT_EQUAL( 2, a.GetCount() );
7788fc40
VZ
582 CPPUNIT_ASSERT_EQUAL( 17, a[0] );
583 CPPUNIT_ASSERT_EQUAL( 9, a[1] );
584}
585
0c61ab6f
VZ
586void ArraysTestCase::Clear()
587{
588 ItemPtrArray items;
589
590 WX_CLEAR_ARRAY(items);
591 CPPUNIT_ASSERT_EQUAL( 0, items.size() );
592
593 items.push_back(new Item(17));
594 items.push_back(new Item(71));
595 CPPUNIT_ASSERT_EQUAL( 2, items.size() );
596
597 WX_CLEAR_ARRAY(items);
598 CPPUNIT_ASSERT_EQUAL( 0, items.size() );
599}
600
ff2201cc
VZ
601namespace
602{
603
604template <typename A, typename T>
605void DoTestSwap(T v1, T v2, T v3,
606 A * WXUNUSED(dummyUglyVC6Workaround))
91276868 607{
ff2201cc 608 A a1, a2;
91276868
VZ
609 a1.swap(a2);
610 CPPUNIT_ASSERT( a1.empty() && a2.empty() );
611
ff2201cc 612 a1.push_back(v1);
91276868
VZ
613 a1.swap(a2);
614 CPPUNIT_ASSERT( a1.empty() );
615 CPPUNIT_ASSERT_EQUAL( 1, a2.size() );
616
ff2201cc
VZ
617 a1.push_back(v2);
618 a1.push_back(v3);
91276868
VZ
619 a2.swap(a1);
620 CPPUNIT_ASSERT_EQUAL( 1, a1.size() );
621 CPPUNIT_ASSERT_EQUAL( 2, a2.size() );
ff2201cc
VZ
622 CPPUNIT_ASSERT_EQUAL( v1, a1[0] );
623 CPPUNIT_ASSERT_EQUAL( v3, a2[1] );
91276868
VZ
624
625 a1.swap(a2);
626 CPPUNIT_ASSERT_EQUAL( 2, a1.size() );
627 CPPUNIT_ASSERT_EQUAL( 1, a2.size() );
628}
629
ff2201cc
VZ
630} // anonymous namespace
631
632void ArraysTestCase::Swap()
633{
634 DoTestSwap("Foo", "Bar", "Baz", (wxArrayString *)NULL);
fecddbaa 635
ff2201cc 636 DoTestSwap(1, 10, 100, (wxArrayInt *)NULL);
8112fbf0 637 DoTestSwap(6, 28, 496, (wxArrayLong *)NULL);
ff2201cc
VZ
638}
639
cf1014a2
VS
640void ArraysTestCase::TestSTL()
641{
642 wxArrayInt list1;
643 wxArrayInt::iterator it, en;
644 wxArrayInt::reverse_iterator rit, ren;
645 int i;
a059db11
VZ
646 static const int COUNT = 5;
647
648 for ( i = 0; i < COUNT; ++i )
cf1014a2
VS
649 list1.push_back(i);
650
a059db11 651 CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT );
10d256d6 652 CPPUNIT_ASSERT_EQUAL( COUNT, list1.size() );
a059db11 653
cf1014a2
VS
654 for ( it = list1.begin(), en = list1.end(), i = 0;
655 it != en; ++it, ++i )
656 {
657 CPPUNIT_ASSERT( *it == i );
658 }
659
a059db11
VZ
660 CPPUNIT_ASSERT_EQUAL( COUNT, i );
661
662 for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT;
cf1014a2
VS
663 rit != ren; ++rit, --i )
664 {
a059db11 665 CPPUNIT_ASSERT( *rit == i-1 );
cf1014a2
VS
666 }
667
a059db11
VZ
668 CPPUNIT_ASSERT_EQUAL( 0, i );
669
cf1014a2
VS
670 CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) &&
671 *list1.begin() == *(list1.rend()-1) );
672
673 it = list1.begin()+1;
674 rit = list1.rbegin()+1;
675 CPPUNIT_ASSERT( *list1.begin() == *(it-1) &&
676 *list1.rbegin() == *(rit-1) );
677
a059db11 678 CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) );
cf1014a2
VS
679
680 list1.erase(list1.begin());
681 list1.erase(list1.end()-1);
682
683 for ( it = list1.begin(), en = list1.end(), i = 1;
684 it != en; ++it, ++i )
685 {
686 CPPUNIT_ASSERT( *it == i );
687 }
35d5da67
VZ
688
689
690 ItemPtrArray items;
691 items.push_back(new Item(17));
692 CPPUNIT_ASSERT_EQUAL( 17, (*(items.rbegin()))->n );
693 CPPUNIT_ASSERT_EQUAL( 17, (**items.begin()).n );
cf1014a2 694}