]>
git.saurik.com Git - wxWidgets.git/blob - tests/vectors/vectors.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/vectors/vectors.cpp
3 // Purpose: wxVector<T> unit test
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 Vaclav Slavik
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/vector.h"
26 // ----------------------------------------------------------------------------
27 // simple class capable of detecting leaks of its objects
28 // ----------------------------------------------------------------------------
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
--; }
37 int GetValue() const { return m_n
; }
39 static int GetCount() { return ms_count
; }
47 int CountedObject::ms_count
= 0;
49 // ----------------------------------------------------------------------------
50 // simple class capable of checking its "this" pointer validity
51 // ----------------------------------------------------------------------------
53 class SelfPointingObject
56 SelfPointingObject() { m_self
= this; }
57 SelfPointingObject(const SelfPointingObject
&) { m_self
= this; }
58 ~SelfPointingObject() { CPPUNIT_ASSERT( this == m_self
); }
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; }
65 SelfPointingObject
*m_self
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class VectorsTestCase
: public CppUnit::TestCase
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
);
87 CPPUNIT_TEST_SUITE_END();
98 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
101 // register in the unnamed registry so that these tests are run by default
102 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
104 // also include in it's own registry so that these tests can be run alone
105 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
107 void VectorsTestCase::PushPopTest()
111 CPPUNIT_ASSERT( v
.size() == 0 );
113 CPPUNIT_ASSERT( v
.size() == 1 );
115 CPPUNIT_ASSERT( v
.size() == 2 );
117 CPPUNIT_ASSERT( v
.size() == 3 );
119 CPPUNIT_ASSERT( v
[0] == 1 );
120 CPPUNIT_ASSERT( v
[1] == 2 );
121 CPPUNIT_ASSERT( v
[2] == 42 );
124 CPPUNIT_ASSERT( v
.size() == 2 );
125 CPPUNIT_ASSERT( v
[0] == 1 );
126 CPPUNIT_ASSERT( v
[1] == 2 );
129 CPPUNIT_ASSERT( v
.size() == 1 );
130 CPPUNIT_ASSERT( v
[0] == 1 );
133 CPPUNIT_ASSERT( v
.size() == 0 );
134 CPPUNIT_ASSERT( v
.empty() );
136 wxVector
<char> vEmpty
;
139 void VectorsTestCase::Insert()
143 v
.insert(v
.end(), 'a');
144 CPPUNIT_ASSERT( v
.size() == 1 );
145 CPPUNIT_ASSERT( v
[0] == 'a' );
147 v
.insert(v
.end(), 'b');
148 CPPUNIT_ASSERT( v
.size() == 2 );
149 CPPUNIT_ASSERT( v
[0] == 'a' );
150 CPPUNIT_ASSERT( v
[1] == 'b' );
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' );
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' );
166 void VectorsTestCase::Erase()
174 CPPUNIT_ASSERT( v
.size() == 4 );
176 v
.erase(v
.begin(), v
.end()-1);
177 CPPUNIT_ASSERT( v
.size() == 1 );
178 CPPUNIT_ASSERT( v
[0] == 4 );
185 CPPUNIT_ASSERT( v
.size() == 4 );
188 CPPUNIT_ASSERT( v
.size() == 3 );
189 CPPUNIT_ASSERT( v
[0] == 2 );
190 CPPUNIT_ASSERT( v
[1] == 3 );
191 CPPUNIT_ASSERT( v
[2] == 4 );
194 void VectorsTestCase::Iterators()
203 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
205 CPPUNIT_ASSERT_EQUAL( value
, *i
);
209 void VectorsTestCase::Objects()
211 wxVector
<CountedObject
> v
;
212 v
.push_back(CountedObject(1));
213 v
.push_back(CountedObject(2));
214 v
.push_back(CountedObject(3));
217 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
218 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
221 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
224 void VectorsTestCase::NonPODs()
226 wxVector
<SelfPointingObject
> v
;
227 v
.push_back(SelfPointingObject());
228 v
.push_back(SelfPointingObject());
229 v
.push_back(SelfPointingObject());
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
238 wxVector
<wxString
> vs
;
241 vs
.push_back("three");
243 vs
.erase(vs
.begin());
247 void VectorsTestCase::Resize()
249 wxVector
<CountedObject
> v
;
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() );
259 CPPUNIT_ASSERT_EQUAL( 1, v
.size() );
260 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
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() );
271 void VectorsTestCase::Swap()
273 wxVector
<int> v1
, v2
;
276 CPPUNIT_ASSERT( v1
.empty() );
277 CPPUNIT_ASSERT_EQUAL( 1, v2
.size() );
278 CPPUNIT_ASSERT_EQUAL( 17, v2
[0] );
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] );
289 CPPUNIT_ASSERT( v1
.empty() );