]> git.saurik.com Git - wxWidgets.git/blob - tests/vectors/vectors.cpp
make sure the quit item is only shown where appropriate on osx
[wxWidgets.git] / tests / vectors / vectors.cpp
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
26 // ----------------------------------------------------------------------------
27 // simple class capable of detecting leaks of its objects
28 // ----------------------------------------------------------------------------
29
30 class CountedObject
31 {
32 public:
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
41 private:
42 static int ms_count;
43
44 int m_n;
45 };
46
47 int CountedObject::ms_count = 0;
48
49 // ----------------------------------------------------------------------------
50 // simple class capable of checking its "this" pointer validity
51 // ----------------------------------------------------------------------------
52
53 class SelfPointingObject
54 {
55 public:
56 SelfPointingObject() { m_self = this; }
57 SelfPointingObject(const SelfPointingObject&) { m_self = this; }
58 ~SelfPointingObject() { CPPUNIT_ASSERT( this == m_self ); }
59
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
64 private:
65 SelfPointingObject *m_self;
66 };
67
68 // ----------------------------------------------------------------------------
69 // test class
70 // ----------------------------------------------------------------------------
71
72 class VectorsTestCase : public CppUnit::TestCase
73 {
74 public:
75 VectorsTestCase() {}
76
77 private:
78 CPPUNIT_TEST_SUITE( VectorsTestCase );
79 CPPUNIT_TEST( PushPopTest );
80 CPPUNIT_TEST( Insert );
81 CPPUNIT_TEST( Erase );
82 CPPUNIT_TEST( Iterators );
83 CPPUNIT_TEST( Objects );
84 CPPUNIT_TEST( NonPODs );
85 CPPUNIT_TEST( Resize );
86 CPPUNIT_TEST( Swap );
87 CPPUNIT_TEST( Sort );
88 CPPUNIT_TEST_SUITE_END();
89
90 void PushPopTest();
91 void Insert();
92 void Erase();
93 void Iterators();
94 void Objects();
95 void NonPODs();
96 void Resize();
97 void Swap();
98 void Sort();
99
100 DECLARE_NO_COPY_CLASS(VectorsTestCase)
101 };
102
103 // register in the unnamed registry so that these tests are run by default
104 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase );
105
106 // also include in it's own registry so that these tests can be run alone
107 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase, "VectorsTestCase" );
108
109 void VectorsTestCase::PushPopTest()
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 );
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
141 void 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
168 void 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 );
194 }
195
196 void 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 }
210
211 void 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());
219 CPPUNIT_ASSERT_EQUAL( 2, v.size() );
220 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
221
222 v.clear();
223 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
224 }
225
226 void 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 }
248
249 void 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
273 void 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
294
295 void 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 }