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