]>
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
6 // Copyright: (c) 2007 Vaclav Slavik
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/vector.h"
25 // ----------------------------------------------------------------------------
26 // simple class capable of detecting leaks of its objects
27 // ----------------------------------------------------------------------------
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
--; }
36 int GetValue() const { return m_n
; }
38 static int GetCount() { return ms_count
; }
46 int CountedObject::ms_count
= 0;
48 // ----------------------------------------------------------------------------
49 // simple class capable of checking its "this" pointer validity
50 // ----------------------------------------------------------------------------
52 class SelfPointingObject
55 SelfPointingObject() { m_self
= this; }
56 SelfPointingObject(const SelfPointingObject
&) { m_self
= this; }
57 ~SelfPointingObject() { CPPUNIT_ASSERT( this == m_self
); }
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; }
64 SelfPointingObject
*m_self
;
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class VectorsTestCase
: public CppUnit::TestCase
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
);
87 CPPUNIT_TEST_SUITE_END();
99 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
105 // also include in its own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
108 void VectorsTestCase::PushPopTest()
112 CPPUNIT_ASSERT( v
.size() == 0 );
114 CPPUNIT_ASSERT( v
.size() == 1 );
116 CPPUNIT_ASSERT( v
.size() == 2 );
118 CPPUNIT_ASSERT( v
.size() == 3 );
120 CPPUNIT_ASSERT( v
[0] == 1 );
121 CPPUNIT_ASSERT( v
[1] == 2 );
122 CPPUNIT_ASSERT( v
[2] == 42 );
125 CPPUNIT_ASSERT( v
.size() == 2 );
126 CPPUNIT_ASSERT( v
[0] == 1 );
127 CPPUNIT_ASSERT( v
[1] == 2 );
130 CPPUNIT_ASSERT( v
.size() == 1 );
131 CPPUNIT_ASSERT( v
[0] == 1 );
134 CPPUNIT_ASSERT( v
.size() == 0 );
135 CPPUNIT_ASSERT( v
.empty() );
137 wxVector
<char> vEmpty
;
140 void VectorsTestCase::Insert()
144 v
.insert(v
.end(), 'a');
145 CPPUNIT_ASSERT( v
.size() == 1 );
146 CPPUNIT_ASSERT( v
[0] == 'a' );
148 v
.insert(v
.end(), 'b');
149 CPPUNIT_ASSERT( v
.size() == 2 );
150 CPPUNIT_ASSERT( v
[0] == 'a' );
151 CPPUNIT_ASSERT( v
[1] == 'b' );
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' );
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' );
167 void VectorsTestCase::Erase()
175 CPPUNIT_ASSERT( v
.size() == 4 );
177 v
.erase(v
.begin(), v
.end()-1);
178 CPPUNIT_ASSERT( v
.size() == 1 );
179 CPPUNIT_ASSERT( v
[0] == 4 );
186 CPPUNIT_ASSERT( v
.size() == 4 );
189 CPPUNIT_ASSERT( v
.size() == 3 );
190 CPPUNIT_ASSERT( v
[0] == 2 );
191 CPPUNIT_ASSERT( v
[1] == 3 );
192 CPPUNIT_ASSERT( v
[2] == 4 );
195 void VectorsTestCase::Iterators()
204 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
206 CPPUNIT_ASSERT_EQUAL( value
, *i
);
210 void VectorsTestCase::Objects()
212 wxVector
<CountedObject
> v
;
213 v
.push_back(CountedObject(1));
214 v
.push_back(CountedObject(2));
215 v
.push_back(CountedObject(3));
218 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
219 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
222 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
225 void VectorsTestCase::NonPODs()
227 wxVector
<SelfPointingObject
> v
;
228 v
.push_back(SelfPointingObject());
229 v
.push_back(SelfPointingObject());
230 v
.push_back(SelfPointingObject());
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
239 wxVector
<wxString
> vs
;
242 vs
.push_back("three");
244 vs
.erase(vs
.begin());
248 void VectorsTestCase::Resize()
250 wxVector
<CountedObject
> v
;
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() );
260 CPPUNIT_ASSERT_EQUAL( 1, v
.size() );
261 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
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() );
272 void VectorsTestCase::Swap()
274 wxVector
<int> v1
, v2
;
277 CPPUNIT_ASSERT( v1
.empty() );
278 CPPUNIT_ASSERT_EQUAL( 1, v2
.size() );
279 CPPUNIT_ASSERT_EQUAL( 17, v2
[0] );
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] );
290 CPPUNIT_ASSERT( v1
.empty() );
294 void VectorsTestCase::Sort()
312 for (idx
=1; idx
<v
.size(); idx
++)
314 CPPUNIT_ASSERT( v
[idx
-1] <= v
[idx
] );