]> git.saurik.com Git - wxWidgets.git/blame - tests/arrays/arrays.cpp
Return an empty string from GetValue if the textctrl is set to the
[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
85 const wxChar *GetName() const { return m_name; }
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
142// ----------------------------------------------------------------------------
143// test class
144// ----------------------------------------------------------------------------
145
146class ArraysTestCase : public CppUnit::TestCase
147{
148public:
149 ArraysTestCase() { }
150
151private:
152 CPPUNIT_TEST_SUITE( ArraysTestCase );
153 CPPUNIT_TEST( wxStringArrayTest );
154 CPPUNIT_TEST( wxObjArrayTest );
155 CPPUNIT_TEST( wxArrayUShortTest );
156 CPPUNIT_TEST( wxArrayIntTest );
1ffc8d7a 157 CPPUNIT_TEST( wxArrayCharTest );
cf1014a2 158 CPPUNIT_TEST( TestSTL );
7788fc40 159 CPPUNIT_TEST( Alloc );
cf1014a2
VS
160 CPPUNIT_TEST_SUITE_END();
161
162 void wxStringArrayTest();
163 void wxObjArrayTest();
164 void wxArrayUShortTest();
165 void wxArrayIntTest();
1ffc8d7a 166 void wxArrayCharTest();
cf1014a2 167 void TestSTL();
7788fc40 168 void Alloc();
cf1014a2
VS
169
170 DECLARE_NO_COPY_CLASS(ArraysTestCase)
171};
172
173// register in the unnamed registry so that these tests are run by default
174CPPUNIT_TEST_SUITE_REGISTRATION( ArraysTestCase );
175
176// also include in it's own registry so that these tests can be run alone
177CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArraysTestCase, "ArraysTestCase" );
178
179void ArraysTestCase::wxStringArrayTest()
180{
181 wxArrayString a1;
182 a1.Add(_T("thermit"));
183 a1.Add(_T("condor"));
184 a1.Add(_T("lion"), 3);
185 a1.Add(_T("dog"));
186 a1.Add(_T("human"));
187 a1.Add(_T("alligator"));
188
189 CPPUNIT_ASSERT( COMPARE_8_VALUES( a1 , _T("thermit") ,
190 _T("condor") ,
191 _T("lion") ,
192 _T("lion") ,
193 _T("lion") ,
194 _T("dog") ,
195 _T("human") ,
196 _T("alligator") ) );
197 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 8 ) );
198
199 wxArrayString a2(a1);
200
e3f2384e 201 CPPUNIT_ASSERT( COMPARE_8_VALUES( a2 , _T("thermit") ,
cf1014a2
VS
202 _T("condor") ,
203 _T("lion") ,
204 _T("lion") ,
205 _T("lion") ,
206 _T("dog") ,
207 _T("human") ,
208 _T("alligator") ) );
209 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 8 ) );
210
211 wxSortedArrayString a3(a1);
212
e3f2384e 213 CPPUNIT_ASSERT( COMPARE_8_VALUES( a3 , _T("alligator") ,
cf1014a2
VS
214 _T("condor") ,
215 _T("dog") ,
216 _T("human") ,
217 _T("lion") ,
218 _T("lion") ,
219 _T("lion") ,
220 _T("thermit") ) );
221 CPPUNIT_ASSERT( COMPARE_COUNT( a3 , 8 ) );
222
223 wxSortedArrayString a4;
224 for (wxArrayString::iterator it = a1.begin(), en = a1.end(); it != en; ++it)
225 a4.Add(*it);
226
e3f2384e 227 CPPUNIT_ASSERT( COMPARE_8_VALUES( a4 , _T("alligator") ,
cf1014a2
VS
228 _T("condor") ,
229 _T("dog") ,
230 _T("human") ,
231 _T("lion") ,
232 _T("lion") ,
233 _T("lion") ,
234 _T("thermit") ) );
235 CPPUNIT_ASSERT( COMPARE_COUNT( a4 , 8 ) );
236
237 a1.RemoveAt(2,3);
238
239 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") ,
240 _T("condor") ,
241 _T("dog") ,
242 _T("human") ,
243 _T("alligator") ) );
244 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
245
246 a2 = a1;
247
248 CPPUNIT_ASSERT( COMPARE_5_VALUES( a2 , _T("thermit") ,
249 _T("condor") ,
250 _T("dog") ,
251 _T("human") ,
252 _T("alligator") ) );
253 CPPUNIT_ASSERT( COMPARE_COUNT( a2 , 5 ) );
254
255 a1.Sort(false);
256
257 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("alligator") ,
258 _T("condor") ,
259 _T("dog") ,
260 _T("human") ,
261 _T("thermit") ) );
262 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
263
264 a1.Sort(true);
265
266 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("thermit") ,
267 _T("human") ,
268 _T("dog") ,
269 _T("condor") ,
270 _T("alligator") ) );
271 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
272
273 a1.Sort(&StringLenCompare);
274
275 CPPUNIT_ASSERT( COMPARE_5_VALUES( a1 , _T("dog") ,
276 _T("human") ,
277 _T("condor") ,
278 _T("thermit") ,
279 _T("alligator") ) );
280 CPPUNIT_ASSERT( COMPARE_COUNT( a1 , 5 ) );
e3f2384e
WS
281 CPPUNIT_ASSERT( a1.Index( _T("dog") ) == 0 );
282 CPPUNIT_ASSERT( a1.Index( _T("human") ) == 1 );
283 CPPUNIT_ASSERT( a1.Index( _T("humann") ) == wxNOT_FOUND );
284 CPPUNIT_ASSERT( a1.Index( _T("condor") ) == 2 );
285 CPPUNIT_ASSERT( a1.Index( _T("thermit") ) == 3 );
286 CPPUNIT_ASSERT( a1.Index( _T("alligator") ) == 4 );
555d0d7e
MB
287
288 wxArrayString a5;
289
290 CPPUNIT_ASSERT( a5.Add( _T("x"), 1 ) == 0 );
291 CPPUNIT_ASSERT( a5.Add( _T("a"), 3 ) == 1 );
292
293 CPPUNIT_ASSERT( COMPARE_4_VALUES( a5, _T("x") ,
294 _T("a") ,
295 _T("a") ,
296 _T("a") ) );
cf1014a2
VS
297}
298
299void ArraysTestCase::wxObjArrayTest()
300{
301 {
302 ArrayBars bars;
303 Bar bar(_T("first bar in general, second bar in array (two copies!)"));
304
305 CPPUNIT_ASSERT( bars.GetCount() == 0 );
306 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
307
308 bars.Add(new Bar(_T("first bar in array")));
309 bars.Add(bar,2);
310
311 CPPUNIT_ASSERT( bars.GetCount() == 3 );
312 CPPUNIT_ASSERT( Bar::GetNumber() == 4 );
313
314 bars.RemoveAt(1, bars.GetCount() - 1);
315
316 CPPUNIT_ASSERT( bars.GetCount() == 1 );
317 CPPUNIT_ASSERT( Bar::GetNumber() == 2 );
318
319 bars.Empty();
320
321 CPPUNIT_ASSERT( bars.GetCount() == 0 );
322 CPPUNIT_ASSERT( Bar::GetNumber() == 1 );
323 }
324 CPPUNIT_ASSERT( Bar::GetNumber() == 0 );
325}
326
327#define TestArrayOf(name) \
328 \
329void ArraysTestCase::wxArray ## name ## Test() \
330{ \
331 wxArray##name a; \
332 a.Add(1); \
333 a.Add(17,2); \
334 a.Add(5,3); \
335 a.Add(3,4); \
336 \
337 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,17,17,5,5,5,3,3,3,3) ); \
338 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
339 \
340 a.Sort(name ## Compare); \
341 \
342 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,1,3,3,3,3,5,5,5,17,17) ); \
343 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
344 \
345 a.Sort(name ## RevCompare); \
346 \
347 CPPUNIT_ASSERT( COMPARE_10_VALUES(a,17,17,5,5,5,3,3,3,3,1) ); \
348 CPPUNIT_ASSERT( COMPARE_COUNT( a , 10 ) ); \
349 \
350 wxSortedArray##name b; \
351 \
352 b.Add(1); \
353 b.Add(17); \
354 b.Add(5); \
355 b.Add(3); \
356 \
357 CPPUNIT_ASSERT( COMPARE_4_VALUES(b,1,3,5,17) ); \
358 CPPUNIT_ASSERT( COMPARE_COUNT( b , 4 ) ); \
10dcbe63
MB
359 CPPUNIT_ASSERT( b.Index( 0 ) == wxNOT_FOUND ); \
360 CPPUNIT_ASSERT( b.Index( 1 ) == 0 ); \
361 CPPUNIT_ASSERT( b.Index( 3 ) == 1 ); \
362 CPPUNIT_ASSERT( b.Index( 4 ) == wxNOT_FOUND ); \
363 CPPUNIT_ASSERT( b.Index( 5 ) == 2 ); \
364 CPPUNIT_ASSERT( b.Index( 6 ) == wxNOT_FOUND ); \
365 CPPUNIT_ASSERT( b.Index( 17 ) == 3 ); \
cf1014a2
VS
366}
367
368TestArrayOf(UShort);
369
1ffc8d7a
VZ
370TestArrayOf(Char);
371
cf1014a2
VS
372TestArrayOf(Int);
373
7788fc40
VZ
374void ArraysTestCase::Alloc()
375{
376 wxArrayInt a;
377 a.Add(17);
378 a.Add(9);
7d3b9f3a 379 CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() );
7788fc40
VZ
380
381 a.Alloc(1000);
382
7d3b9f3a 383 CPPUNIT_ASSERT_EQUAL( size_t(2), a.GetCount() );
7788fc40
VZ
384 CPPUNIT_ASSERT_EQUAL( 17, a[0] );
385 CPPUNIT_ASSERT_EQUAL( 9, a[1] );
386}
387
cf1014a2
VS
388void ArraysTestCase::TestSTL()
389{
390 wxArrayInt list1;
391 wxArrayInt::iterator it, en;
392 wxArrayInt::reverse_iterator rit, ren;
393 int i;
a059db11
VZ
394 static const int COUNT = 5;
395
396 for ( i = 0; i < COUNT; ++i )
cf1014a2
VS
397 list1.push_back(i);
398
a059db11
VZ
399 CPPUNIT_ASSERT( list1.capacity() >= (size_t)COUNT );
400 CPPUNIT_ASSERT_EQUAL( (size_t)COUNT, list1.size() );
401
cf1014a2
VS
402 for ( it = list1.begin(), en = list1.end(), i = 0;
403 it != en; ++it, ++i )
404 {
405 CPPUNIT_ASSERT( *it == i );
406 }
407
a059db11
VZ
408 CPPUNIT_ASSERT_EQUAL( COUNT, i );
409
410 for ( rit = list1.rbegin(), ren = list1.rend(), i = COUNT;
cf1014a2
VS
411 rit != ren; ++rit, --i )
412 {
a059db11 413 CPPUNIT_ASSERT( *rit == i-1 );
cf1014a2
VS
414 }
415
a059db11
VZ
416 CPPUNIT_ASSERT_EQUAL( 0, i );
417
cf1014a2
VS
418 CPPUNIT_ASSERT( *list1.rbegin() == *(list1.end()-1) &&
419 *list1.begin() == *(list1.rend()-1) );
420
421 it = list1.begin()+1;
422 rit = list1.rbegin()+1;
423 CPPUNIT_ASSERT( *list1.begin() == *(it-1) &&
424 *list1.rbegin() == *(rit-1) );
425
a059db11 426 CPPUNIT_ASSERT( ( list1.front() == 0 ) && ( list1.back() == COUNT - 1 ) );
cf1014a2
VS
427
428 list1.erase(list1.begin());
429 list1.erase(list1.end()-1);
430
431 for ( it = list1.begin(), en = list1.end(), i = 1;
432 it != en; ++it, ++i )
433 {
434 CPPUNIT_ASSERT( *it == i );
435 }
436}