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