]> git.saurik.com Git - wxWidgets.git/blob - tests/vectors/vectors.cpp
Use _UNICODE instead of UNICODE in wx/msw/winundef.h.
[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_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
98 DECLARE_NO_COPY_CLASS(VectorsTestCase)
99 };
100
101 // register in the unnamed registry so that these tests are run by default
102 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase );
103
104 // also include in it's own registry so that these tests can be run alone
105 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase, "VectorsTestCase" );
106
107 void VectorsTestCase::PushPopTest()
108 {
109 wxVector<int> v;
110
111 CPPUNIT_ASSERT( v.size() == 0 );
112 v.push_back(1);
113 CPPUNIT_ASSERT( v.size() == 1 );
114 v.push_back(2);
115 CPPUNIT_ASSERT( v.size() == 2 );
116 v.push_back(42);
117 CPPUNIT_ASSERT( v.size() == 3 );
118
119 CPPUNIT_ASSERT( v[0] == 1 );
120 CPPUNIT_ASSERT( v[1] == 2 );
121 CPPUNIT_ASSERT( v[2] == 42 );
122
123 v.pop_back();
124 CPPUNIT_ASSERT( v.size() == 2 );
125 CPPUNIT_ASSERT( v[0] == 1 );
126 CPPUNIT_ASSERT( v[1] == 2 );
127
128 v.pop_back();
129 CPPUNIT_ASSERT( v.size() == 1 );
130 CPPUNIT_ASSERT( v[0] == 1 );
131
132 v.pop_back();
133 CPPUNIT_ASSERT( v.size() == 0 );
134 CPPUNIT_ASSERT( v.empty() );
135
136 wxVector<char> vEmpty;
137 }
138
139 void VectorsTestCase::Insert()
140 {
141 wxVector<char> v;
142
143 v.insert(v.end(), 'a');
144 CPPUNIT_ASSERT( v.size() == 1 );
145 CPPUNIT_ASSERT( v[0] == 'a' );
146
147 v.insert(v.end(), 'b');
148 CPPUNIT_ASSERT( v.size() == 2 );
149 CPPUNIT_ASSERT( v[0] == 'a' );
150 CPPUNIT_ASSERT( v[1] == 'b' );
151
152 v.insert(v.begin(), '0');
153 CPPUNIT_ASSERT( v.size() == 3 );
154 CPPUNIT_ASSERT( v[0] == '0' );
155 CPPUNIT_ASSERT( v[1] == 'a' );
156 CPPUNIT_ASSERT( v[2] == 'b' );
157
158 v.insert(v.begin() + 2, 'X');
159 CPPUNIT_ASSERT( v.size() == 4 );
160 CPPUNIT_ASSERT( v[0] == '0' );
161 CPPUNIT_ASSERT( v[1] == 'a' );
162 CPPUNIT_ASSERT( v[2] == 'X' );
163 CPPUNIT_ASSERT( v[3] == 'b' );
164 }
165
166 void VectorsTestCase::Erase()
167 {
168 wxVector<int> v;
169
170 v.push_back(1);
171 v.push_back(2);
172 v.push_back(3);
173 v.push_back(4);
174 CPPUNIT_ASSERT( v.size() == 4 );
175
176 v.erase(v.begin(), v.end()-1);
177 CPPUNIT_ASSERT( v.size() == 1 );
178 CPPUNIT_ASSERT( v[0] == 4 );
179
180 v.clear();
181 v.push_back(1);
182 v.push_back(2);
183 v.push_back(3);
184 v.push_back(4);
185 CPPUNIT_ASSERT( v.size() == 4 );
186
187 v.erase(v.begin());
188 CPPUNIT_ASSERT( v.size() == 3 );
189 CPPUNIT_ASSERT( v[0] == 2 );
190 CPPUNIT_ASSERT( v[1] == 3 );
191 CPPUNIT_ASSERT( v[2] == 4 );
192 }
193
194 void VectorsTestCase::Iterators()
195 {
196 wxVector<int> v;
197 v.push_back(1);
198 v.push_back(2);
199 v.push_back(3);
200 v.push_back(4);
201
202 int value = 1;
203 for ( wxVector<int>::iterator i = v.begin(); i != v.end(); ++i, ++value )
204 {
205 CPPUNIT_ASSERT_EQUAL( value, *i );
206 }
207 }
208
209 void VectorsTestCase::Objects()
210 {
211 wxVector<CountedObject> v;
212 v.push_back(CountedObject(1));
213 v.push_back(CountedObject(2));
214 v.push_back(CountedObject(3));
215
216 v.erase(v.begin());
217 CPPUNIT_ASSERT_EQUAL( 2, v.size() );
218 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
219
220 v.clear();
221 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
222 }
223
224 void VectorsTestCase::NonPODs()
225 {
226 wxVector<SelfPointingObject> v;
227 v.push_back(SelfPointingObject());
228 v.push_back(SelfPointingObject());
229 v.push_back(SelfPointingObject());
230
231 v.erase(v.begin());
232 v.clear();
233
234 // try the same with wxString, which is not POD, but is implemented in
235 // a movable way (this won't assert, but would crash or show some memory
236 // problems under Valgrind if wxString couldn't be safely moved with
237 // memmove()):
238 wxVector<wxString> vs;
239 vs.push_back("one");
240 vs.push_back("two");
241 vs.push_back("three");
242
243 vs.erase(vs.begin());
244 vs.clear();
245 }
246
247 void VectorsTestCase::Resize()
248 {
249 wxVector<CountedObject> v;
250 v.resize(3);
251
252 CPPUNIT_ASSERT_EQUAL( 3, v.size() );
253 CPPUNIT_ASSERT_EQUAL( 3, CountedObject::GetCount() );
254 CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
255 CPPUNIT_ASSERT_EQUAL( 0, v[1].GetValue() );
256 CPPUNIT_ASSERT_EQUAL( 0, v[2].GetValue() );
257
258 v.resize(1);
259 CPPUNIT_ASSERT_EQUAL( 1, v.size() );
260 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
261
262 v.resize(4, CountedObject(17));
263 CPPUNIT_ASSERT_EQUAL( 4, v.size() );
264 CPPUNIT_ASSERT_EQUAL( 4, CountedObject::GetCount() );
265 CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
266 CPPUNIT_ASSERT_EQUAL( 17, v[1].GetValue() );
267 CPPUNIT_ASSERT_EQUAL( 17, v[2].GetValue() );
268 CPPUNIT_ASSERT_EQUAL( 17, v[3].GetValue() );
269 }
270
271 void VectorsTestCase::Swap()
272 {
273 wxVector<int> v1, v2;
274 v1.push_back(17);
275 v1.swap(v2);
276 CPPUNIT_ASSERT( v1.empty() );
277 CPPUNIT_ASSERT_EQUAL( 1, v2.size() );
278 CPPUNIT_ASSERT_EQUAL( 17, v2[0] );
279
280 v1.push_back(9);
281 v2.swap(v1);
282 CPPUNIT_ASSERT_EQUAL( 1, v1.size() );
283 CPPUNIT_ASSERT_EQUAL( 17, v1[0] );
284 CPPUNIT_ASSERT_EQUAL( 1, v2.size() );
285 CPPUNIT_ASSERT_EQUAL( 9, v2[0] );
286
287 v2.clear();
288 v1.swap(v2);
289 CPPUNIT_ASSERT( v1.empty() );
290 }
291