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