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