]> git.saurik.com Git - wxWidgets.git/blame - tests/vectors/vectors.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / vectors / vectors.cpp
CommitLineData
e966f815
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/vectors/vectors.cpp
3// Purpose: wxVector<T> unit test
4// Author: Vaclav Slavik
5// Created: 2007-07-07
e966f815
VS
6// Copyright: (c) 2007 Vaclav Slavik
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/vector.h"
24
6712283c
VS
25// ----------------------------------------------------------------------------
26// simple class capable of detecting leaks of its objects
27// ----------------------------------------------------------------------------
28
29class CountedObject
30{
31public:
32 CountedObject(int n = 0) : m_n(n) { ms_count++; }
33 CountedObject(const CountedObject& co) : m_n(co.m_n) { ms_count++; }
34 ~CountedObject() { ms_count--; }
35
36 int GetValue() const { return m_n; }
37
38 static int GetCount() { return ms_count; }
39
40private:
41 static int ms_count;
42
43 int m_n;
44};
45
46int CountedObject::ms_count = 0;
47
48// ----------------------------------------------------------------------------
252ead2e 49// simple class capable of checking its "this" pointer validity
6712283c
VS
50// ----------------------------------------------------------------------------
51
52class SelfPointingObject
53{
54public:
55 SelfPointingObject() { m_self = this; }
56 SelfPointingObject(const SelfPointingObject&) { m_self = this; }
57 ~SelfPointingObject() { CPPUNIT_ASSERT( this == m_self ); }
58
252ead2e
VZ
59 // the assignment operator should not modify our "this" pointer so
60 // implement it just to prevent the default version from doing it
61 SelfPointingObject& operator=(const SelfPointingObject&) { return *this; }
62
6712283c
VS
63private:
64 SelfPointingObject *m_self;
65};
66
67// ----------------------------------------------------------------------------
e966f815 68// test class
6712283c 69// ----------------------------------------------------------------------------
e966f815
VS
70
71class VectorsTestCase : public CppUnit::TestCase
72{
73public:
74 VectorsTestCase() {}
75
76private:
77 CPPUNIT_TEST_SUITE( VectorsTestCase );
4e570340
VS
78 CPPUNIT_TEST( PushPopTest );
79 CPPUNIT_TEST( Insert );
80 CPPUNIT_TEST( Erase );
6757b667 81 CPPUNIT_TEST( Iterators );
6712283c
VS
82 CPPUNIT_TEST( Objects );
83 CPPUNIT_TEST( NonPODs );
e068310a 84 CPPUNIT_TEST( Resize );
dbe0872f 85 CPPUNIT_TEST( Swap );
38723be1 86 CPPUNIT_TEST( Sort );
e966f815
VS
87 CPPUNIT_TEST_SUITE_END();
88
4e570340
VS
89 void PushPopTest();
90 void Insert();
91 void Erase();
6757b667 92 void Iterators();
6712283c
VS
93 void Objects();
94 void NonPODs();
e068310a 95 void Resize();
dbe0872f 96 void Swap();
38723be1 97 void Sort();
e966f815
VS
98
99 DECLARE_NO_COPY_CLASS(VectorsTestCase)
100};
101
102// register in the unnamed registry so that these tests are run by default
103CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase );
104
e3778b4d 105// also include in its own registry so that these tests can be run alone
e966f815
VS
106CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase, "VectorsTestCase" );
107
4e570340 108void VectorsTestCase::PushPopTest()
e966f815
VS
109{
110 wxVector<int> v;
111
112 CPPUNIT_ASSERT( v.size() == 0 );
113 v.push_back(1);
114 CPPUNIT_ASSERT( v.size() == 1 );
115 v.push_back(2);
116 CPPUNIT_ASSERT( v.size() == 2 );
117 v.push_back(42);
118 CPPUNIT_ASSERT( v.size() == 3 );
119
120 CPPUNIT_ASSERT( v[0] == 1 );
121 CPPUNIT_ASSERT( v[1] == 2 );
122 CPPUNIT_ASSERT( v[2] == 42 );
4e570340
VS
123
124 v.pop_back();
125 CPPUNIT_ASSERT( v.size() == 2 );
126 CPPUNIT_ASSERT( v[0] == 1 );
127 CPPUNIT_ASSERT( v[1] == 2 );
128
129 v.pop_back();
130 CPPUNIT_ASSERT( v.size() == 1 );
131 CPPUNIT_ASSERT( v[0] == 1 );
132
133 v.pop_back();
134 CPPUNIT_ASSERT( v.size() == 0 );
135 CPPUNIT_ASSERT( v.empty() );
136
137 wxVector<char> vEmpty;
138}
139
140void VectorsTestCase::Insert()
141{
142 wxVector<char> v;
143
144 v.insert(v.end(), 'a');
145 CPPUNIT_ASSERT( v.size() == 1 );
146 CPPUNIT_ASSERT( v[0] == 'a' );
147
148 v.insert(v.end(), 'b');
149 CPPUNIT_ASSERT( v.size() == 2 );
150 CPPUNIT_ASSERT( v[0] == 'a' );
151 CPPUNIT_ASSERT( v[1] == 'b' );
152
153 v.insert(v.begin(), '0');
154 CPPUNIT_ASSERT( v.size() == 3 );
155 CPPUNIT_ASSERT( v[0] == '0' );
156 CPPUNIT_ASSERT( v[1] == 'a' );
157 CPPUNIT_ASSERT( v[2] == 'b' );
158
159 v.insert(v.begin() + 2, 'X');
160 CPPUNIT_ASSERT( v.size() == 4 );
161 CPPUNIT_ASSERT( v[0] == '0' );
162 CPPUNIT_ASSERT( v[1] == 'a' );
163 CPPUNIT_ASSERT( v[2] == 'X' );
164 CPPUNIT_ASSERT( v[3] == 'b' );
165}
166
167void VectorsTestCase::Erase()
168{
169 wxVector<int> v;
170
171 v.push_back(1);
172 v.push_back(2);
173 v.push_back(3);
174 v.push_back(4);
175 CPPUNIT_ASSERT( v.size() == 4 );
176
177 v.erase(v.begin(), v.end()-1);
178 CPPUNIT_ASSERT( v.size() == 1 );
179 CPPUNIT_ASSERT( v[0] == 4 );
180
181 v.clear();
182 v.push_back(1);
183 v.push_back(2);
184 v.push_back(3);
185 v.push_back(4);
186 CPPUNIT_ASSERT( v.size() == 4 );
187
188 v.erase(v.begin());
189 CPPUNIT_ASSERT( v.size() == 3 );
190 CPPUNIT_ASSERT( v[0] == 2 );
191 CPPUNIT_ASSERT( v[1] == 3 );
192 CPPUNIT_ASSERT( v[2] == 4 );
e966f815 193}
6757b667
VS
194
195void VectorsTestCase::Iterators()
196{
197 wxVector<int> v;
198 v.push_back(1);
199 v.push_back(2);
200 v.push_back(3);
201 v.push_back(4);
202
203 int value = 1;
204 for ( wxVector<int>::iterator i = v.begin(); i != v.end(); ++i, ++value )
205 {
206 CPPUNIT_ASSERT_EQUAL( value, *i );
207 }
208}
6712283c
VS
209
210void VectorsTestCase::Objects()
211{
212 wxVector<CountedObject> v;
213 v.push_back(CountedObject(1));
214 v.push_back(CountedObject(2));
215 v.push_back(CountedObject(3));
216
217 v.erase(v.begin());
1de532f5 218 CPPUNIT_ASSERT_EQUAL( 2, v.size() );
6712283c
VS
219 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
220
221 v.clear();
222 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
223}
224
225void VectorsTestCase::NonPODs()
226{
227 wxVector<SelfPointingObject> v;
228 v.push_back(SelfPointingObject());
229 v.push_back(SelfPointingObject());
230 v.push_back(SelfPointingObject());
231
232 v.erase(v.begin());
233 v.clear();
234
235 // try the same with wxString, which is not POD, but is implemented in
236 // a movable way (this won't assert, but would crash or show some memory
237 // problems under Valgrind if wxString couldn't be safely moved with
238 // memmove()):
239 wxVector<wxString> vs;
240 vs.push_back("one");
241 vs.push_back("two");
242 vs.push_back("three");
243
244 vs.erase(vs.begin());
245 vs.clear();
246}
e068310a
VZ
247
248void VectorsTestCase::Resize()
249{
250 wxVector<CountedObject> v;
251 v.resize(3);
252
253 CPPUNIT_ASSERT_EQUAL( 3, v.size() );
254 CPPUNIT_ASSERT_EQUAL( 3, CountedObject::GetCount() );
255 CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
256 CPPUNIT_ASSERT_EQUAL( 0, v[1].GetValue() );
257 CPPUNIT_ASSERT_EQUAL( 0, v[2].GetValue() );
258
259 v.resize(1);
260 CPPUNIT_ASSERT_EQUAL( 1, v.size() );
261 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
262
263 v.resize(4, CountedObject(17));
264 CPPUNIT_ASSERT_EQUAL( 4, v.size() );
265 CPPUNIT_ASSERT_EQUAL( 4, CountedObject::GetCount() );
266 CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
267 CPPUNIT_ASSERT_EQUAL( 17, v[1].GetValue() );
268 CPPUNIT_ASSERT_EQUAL( 17, v[2].GetValue() );
269 CPPUNIT_ASSERT_EQUAL( 17, v[3].GetValue() );
270}
271
dbe0872f
VZ
272void VectorsTestCase::Swap()
273{
274 wxVector<int> v1, v2;
275 v1.push_back(17);
276 v1.swap(v2);
277 CPPUNIT_ASSERT( v1.empty() );
278 CPPUNIT_ASSERT_EQUAL( 1, v2.size() );
279 CPPUNIT_ASSERT_EQUAL( 17, v2[0] );
280
281 v1.push_back(9);
282 v2.swap(v1);
283 CPPUNIT_ASSERT_EQUAL( 1, v1.size() );
284 CPPUNIT_ASSERT_EQUAL( 17, v1[0] );
285 CPPUNIT_ASSERT_EQUAL( 1, v2.size() );
286 CPPUNIT_ASSERT_EQUAL( 9, v2[0] );
287
288 v2.clear();
289 v1.swap(v2);
290 CPPUNIT_ASSERT( v1.empty() );
291}
292
38723be1
RD
293
294void VectorsTestCase::Sort()
295{
296 size_t idx;
297 wxVector<int> v;
298
299 v.push_back(5);
300 v.push_back(7);
301 v.push_back(2);
302 v.push_back(9);
303 v.push_back(4);
304 v.push_back(1);
305 v.push_back(3);
306 v.push_back(8);
307 v.push_back(0);
308 v.push_back(6);
309
310 wxVectorSort(v);
311
312 for (idx=1; idx<v.size(); idx++)
313 {
314 CPPUNIT_ASSERT( v[idx-1] <= v[idx] );
315 }
316}