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