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