]> git.saurik.com Git - wxWidgets.git/blame - tests/arrays/arrays.cpp
Fixed header file conflict.
[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"
97WX_DEFINE_OBJARRAY(ArrayBars);
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
1ffc8d7a 128DEFINE_COMPARE(Char, char);
cf1014a2
VS
129DEFINE_COMPARE(UShort, ushort);
130DEFINE_COMPARE(Int, int);
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 );
91276868 173 CPPUNIT_TEST( Swap );
cf1014a2
VS
174 CPPUNIT_TEST_SUITE_END();
175
176 void wxStringArrayTest();
abbb59e8
VZ
177 void wxStringArraySplitTest();
178 void wxStringArrayJoinTest();
179 void wxStringArraySplitJoinTest();
cf1014a2
VS
180 void wxObjArrayTest();
181 void wxArrayUShortTest();
182 void wxArrayIntTest();
1ffc8d7a 183 void wxArrayCharTest();
cf1014a2 184 void TestSTL();
7788fc40 185 void Alloc();
91276868 186 void Swap();
cf1014a2
VS
187
188 DECLARE_NO_COPY_CLASS(ArraysTestCase)
189};
190
191// register in the unnamed registry so that these tests are run by default
192CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase );
193
194// also include in it's own registry so that these tests can be run alone
195CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" );
196
197void ArraysTestCase::wxStringArrayTest()
198{
199 wxArrayString a1;
200 a1.Add(_T("thermit"));
201 a1.Add(_T("condor"));
202 a1.Add(_T("lion"), 3);
203 a1.Add(_T("dog"));
204 a1.Add(_T("human"));
205 a1.Add(_T("alligator"));
206
207 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , _T("thermit") ,
208 _T("condor") ,
209 _T("lion") ,
210 _T("lion") ,
211 _T("lion") ,
212 _T("dog") ,
213 _T("human") ,
214 _T("alligator") ) );
215 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) );
216
217 wxArrayString a2(a1);
218
e3f2384e 219 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , _T("thermit") ,
cf1014a2
VS
220 _T("condor") ,
221 _T("lion") ,
222 _T("lion") ,
223 _T("lion") ,
224 _T("dog") ,
225 _T("human") ,
226 _T("alligator") ) );
227 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) );
228
229 wxSortedArrayString a3(a1);
230
e3f2384e 231 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , _T("alligator") ,
cf1014a2
VS
232 _T("condor") ,
233 _T("dog") ,
234 _T("human") ,
235 _T("lion") ,
236 _T("lion") ,
237 _T("lion") ,
238 _T("thermit") ) );
239 CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) );
240
241 wxSortedArrayString a4;
242 for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it)
243 a4.Add(*it);
244
e3f2384e 245 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , _T("alligator") ,
cf1014a2
VS
246 _T("condor") ,
247 _T("dog") ,
248 _T("human") ,
249 _T("lion") ,
250 _T("lion") ,
251 _T("lion") ,
252 _T("thermit") ) );
253 CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) );
254
255 a1.RemoveAt(2,3);
256
257 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") ,
258 _T("condor") ,
259 _T("dog") ,
260 _T("human") ,
261 _T("alligator") ) );
262 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
263
264 a2 = a1;
265
266 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , _T("thermit") ,
267 _T("condor") ,
268 _T("dog") ,
269 _T("human") ,
270 _T("alligator") ) );
271 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) );
272
273 a1.Sort(false);
274
275 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("alligator") ,
276 _T("condor") ,
277 _T("dog") ,
278 _T("human") ,
279 _T("thermit") ) );
280 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
281
282 a1.Sort(true);
283
284 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") ,
285 _T("human") ,
286 _T("dog") ,
287 _T("condor") ,
288 _T("alligator") ) );
289 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
290
291 a1.Sort(&StringLenCompare);
292
293 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("dog") ,
294 _T("human") ,
295 _T("condor") ,
296 _T("thermit") ,
297 _T("alligator") ) );
298 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
e3f2384e
WS
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 );
555d0d7e
MB
305
306 wxArrayString a5;
307
308 CPPUNIT_ASSERT( a5.Add( _T("x"), 1 ) == 0 );
309 CPPUNIT_ASSERT( a5.Add( _T("a"), 3 ) == 1 );
310
311 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, _T("x") ,
312 _T("a") ,
313 _T("a") ,
314 _T("a") ) );
cf1014a2
VS
315}
316
abbb59e8
VZ
317void ArraysTestCase::wxStringArraySplitTest()
318{
319 // test wxSplit:
320
321 {
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("") };
326
327 wxArrayString exparr(WXSIZEOF(expected), expected);
328 wxArrayString realarr(wxSplit(str, wxT(',')));
329 CPPUNIT_ASSERT( exparr == realarr );
330 }
331
332 {
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("") };
338
339 // escaping on:
340 wxArrayString exparr(WXSIZEOF(expected), expected);
341 wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\')));
342 CPPUNIT_ASSERT( exparr == realarr );
343
344 // escaping turned off:
345 wxArrayString exparr2(WXSIZEOF(expected2), expected2);
346 wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0')));
347 CPPUNIT_ASSERT( exparr2 == realarr2 );
348 }
349
350 {
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") };
359
360 // escaping on:
361 wxArrayString exparr(WXSIZEOF(expected), expected);
362 wxArrayString realarr(wxSplit(str, wxT(','), wxT('\\')));
363 CPPUNIT_ASSERT( exparr == realarr );
364
365 // escaping turned off:
366 wxArrayString exparr2(WXSIZEOF(expected2), expected2);
367 wxArrayString realarr2(wxSplit(str, wxT(','), wxT('\0')));
368 CPPUNIT_ASSERT( exparr2 == realarr2 );
369 }
370}
371
372void ArraysTestCase::wxStringArrayJoinTest()
373{
374 // test wxJoin:
375
376 {
377 const wxChar *arr[] = { wxT("first"), wxT(""), wxT("second"), wxT("third") };
378 wxString expected = wxT("first,,second,third");
379
380 wxArrayString arrstr(WXSIZEOF(arr), arr);
381 wxString result = wxJoin(arrstr, wxT(','));
382 CPPUNIT_ASSERT( expected == result );
383 }
384
385 {
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,,");
389
390 // escaping on:
391 wxArrayString arrstr(WXSIZEOF(arr), arr);
392 wxString result = wxJoin(arrstr, wxT(','), wxT('\\'));
393 CPPUNIT_ASSERT( expected == result );
394
395 // escaping turned off:
396 wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0'));
397 CPPUNIT_ASSERT( expected2 == result2 );
398 }
399
400 {
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\\");
405
406 // escaping on:
407 wxArrayString arrstr(WXSIZEOF(arr), arr);
408 wxString result = wxJoin(arrstr, wxT(','), wxT('\\'));
409 CPPUNIT_ASSERT( expected == result );
410
411 // escaping turned off:
412 wxString result2 = wxJoin(arrstr, wxT(','), wxT('\0'));
413 CPPUNIT_ASSERT( expected2 == result2 );
414 }
415}
416
417void ArraysTestCase::wxStringArraySplitJoinTest()
418{
419 wxChar separators[] = { wxT('a'), wxT(','), wxT('_'), wxT(' '), wxT('\\'),
420 wxT('&'), wxT('{'), wxT('A'), wxT('<'), wxT('>'),
421 wxT('\''), wxT('\n'), wxT('!'), wxT('-') };
422
423 // test with a string: split it and then rejoin it:
424
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>");
437
5098c258
VZ
438 size_t i;
439 for (i = 0; i < WXSIZEOF(separators); i++)
abbb59e8
VZ
440 {
441 wxArrayString arr = wxSplit(str, separators[i]);
442 CPPUNIT_ASSERT( str == wxJoin(arr, separators[i]) );
443 }
444
445
446 // test with an array: join it and then resplit it:
447
448 const wxChar *arr[] =
449 {
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!")
453 };
454 wxArrayString theArr(WXSIZEOF(arr), arr);
455
5098c258 456 for (i = 0; i < WXSIZEOF(separators); i++)
abbb59e8
VZ
457 {
458 wxString string = wxJoin(theArr, separators[i]);
459 CPPUNIT_ASSERT( theArr == wxSplit(string, separators[i]) );
460 }
461
462 wxArrayString emptyArray;
463 wxString string = wxJoin(emptyArray, _T(';'));
464 CPPUNIT_ASSERT( string.empty() );
465
466 CPPUNIT_ASSERT( wxSplit(string, _T(';')).empty() );
467
468 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxSplit(_T(";"), _T(';')).size() );
469}
470
cf1014a2
VS
471void ArraysTestCase::wxObjArrayTest()
472{
473 {
474 ArrayBars bars;
475 Bar bar(_T("first bar in general, second bar in array (two copies!)"));
476
477 CPPUNIT_ASSERT( bars.GetCount() == 0 );
478 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
479
480 bars.Add(new Bar(_T("first bar in array")));
481 bars.Add(bar,2);
482
483 CPPUNIT_ASSERT( bars.GetCount() == 3 );
484 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
485
486 bars.RemoveAt(1, bars.GetCount() - 1);
487
488 CPPUNIT_ASSERT( bars.GetCount() == 1 );
489 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
490
491 bars.Empty();
492
493 CPPUNIT_ASSERT( bars.GetCount() == 0 );
494 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
495 }
496 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
497}
498
499#define TestArrayOf(name) \
500 \
501void ArraysTestCase::wxArray ## name ## Test() \
502{ \
503 wxArray##name a; \
504 a.Add(1); \
505 a.Add(17,2); \
506 a.Add(5,3); \
507 a.Add(3,4); \
508 \
509 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
510 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
511 \
512 a.Sort(name ## Compare); \
513 \
514 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
515 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
516 \
517 a.Sort(name ## RevCompare); \
518 \
519 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
520 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
521 \
522 wxSortedArray##name b; \
523 \
524 b.Add(1); \
525 b.Add(17); \
526 b.Add(5); \
527 b.Add(3); \
528 \
529 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
530 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
10dcbe63
MB
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 ); \
cf1014a2
VS
538}
539
540TestArrayOf(UShort);
541
1ffc8d7a
VZ
542TestArrayOf(Char);
543
cf1014a2
VS
544TestArrayOf(Int);
545
7788fc40
VZ
546void ArraysTestCase::Alloc()
547{
548 wxArrayInt a;
549 a.Add(17);
550 a.Add(9);
7d3b9f3a 551 CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() );
7788fc40
VZ
552
553 a.Alloc(1000);
554
7d3b9f3a 555 CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() );
7788fc40
VZ
556 CPPUNIT_ASSERT_EQUAL( 17, a[0] );
557 CPPUNIT_ASSERT_EQUAL( 9, a[1] );
558}
559
ff2201cc
VZ
560namespace
561{
562
563template <typename A, typename T>
564void DoTestSwap(T v1, T v2, T v3,
565 A * WXUNUSED(dummyUglyVC6Workaround))
91276868 566{
ff2201cc 567 A a1, a2;
91276868
VZ
568 a1.swap(a2);
569 CPPUNIT_ASSERT( a1.empty() && a2.empty() );
570
ff2201cc 571 a1.push_back(v1);
91276868
VZ
572 a1.swap(a2);
573 CPPUNIT_ASSERT( a1.empty() );
574 CPPUNIT_ASSERT_EQUAL( 1, a2.size() );
575
ff2201cc
VZ
576 a1.push_back(v2);
577 a1.push_back(v3);
91276868
VZ
578 a2.swap(a1);
579 CPPUNIT_ASSERT_EQUAL( 1, a1.size() );
580 CPPUNIT_ASSERT_EQUAL( 2, a2.size() );
ff2201cc
VZ
581 CPPUNIT_ASSERT_EQUAL( v1, a1[0] );
582 CPPUNIT_ASSERT_EQUAL( v3, a2[1] );
91276868
VZ
583
584 a1.swap(a2);
585 CPPUNIT_ASSERT_EQUAL( 2, a1.size() );
586 CPPUNIT_ASSERT_EQUAL( 1, a2.size() );
587}
588
ff2201cc
VZ
589} // anonymous namespace
590
591void ArraysTestCase::Swap()
592{
593 DoTestSwap("Foo", "Bar", "Baz", (wxArrayString *)NULL);
594 DoTestSwap(1, 10, 100, (wxArrayInt *)NULL);
595 DoTestSwap(6, 28, 496, (wxArrayLong *)NULL);
596}
597
cf1014a2
VS
598void ArraysTestCase::TestSTL()
599{
600 wxArrayInt list1;
601 wxArrayInt::iterator it, en;
602 wxArrayInt::reverse_iterator rit, ren;
603 int i;
a059db11
VZ
604 static const int COUNT = 5;
605
606 for ( i = 0; i < COUNT; ++i )
cf1014a2
VS
607 list1.push_back(i);
608
a059db11
VZ
609 CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT );
610 CPPUNIT_ASSERT_EQUAL( (size_t)COUNT, list1.size() );
611
cf1014a2
VS
612 for ( it = list1.begin(), en = list1.end(), i = 0;
613 it != en; ++it, ++i )
614 {
615 CPPUNIT_ASSERT( *it == i );
616 }
617
a059db11
VZ
618 CPPUNIT_ASSERT_EQUAL( COUNT, i );
619
620 for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT;
cf1014a2
VS
621 rit != ren; ++rit, --i )
622 {
a059db11 623 CPPUNIT_ASSERT( *rit == i-1 );
cf1014a2
VS
624 }
625
a059db11
VZ
626 CPPUNIT_ASSERT_EQUAL( 0, i );
627
cf1014a2
VS
628 CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) &&
629 *list1.begin() == *(list1.rend()-1) );
630
631 it = list1.begin()+1;
632 rit = list1.rbegin()+1;
633 CPPUNIT_ASSERT( *list1.begin() == *(it-1) &&
634 *list1.rbegin() == *(rit-1) );
635
a059db11 636 CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) );
cf1014a2
VS
637
638 list1.erase(list1.begin());
639 list1.erase(list1.end()-1);
640
641 for ( it = list1.begin(), en = list1.end(), i = 1;
642 it != en; ++it, ++i )
643 {
644 CPPUNIT_ASSERT( *it == i );
645 }
35d5da67
VZ
646
647
648 ItemPtrArray items;
649 items.push_back(new Item(17));
650 CPPUNIT_ASSERT_EQUAL( 17, (*(items.rbegin()))->n );
651 CPPUNIT_ASSERT_EQUAL( 17, (**items.begin()).n );
cf1014a2 652}