]>
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
);
86 CPPUNIT_TEST_SUITE_END();
96 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
99 // register in the unnamed registry so that these tests are run by default
100 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
102 // also include in it's own registry so that these tests can be run alone
103 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
105 void VectorsTestCase::PushPopTest()
109 CPPUNIT_ASSERT( v
.size() == 0 );
111 CPPUNIT_ASSERT( v
.size() == 1 );
113 CPPUNIT_ASSERT( v
.size() == 2 );
115 CPPUNIT_ASSERT( v
.size() == 3 );
117 CPPUNIT_ASSERT( v
[0] == 1 );
118 CPPUNIT_ASSERT( v
[1] == 2 );
119 CPPUNIT_ASSERT( v
[2] == 42 );
122 CPPUNIT_ASSERT( v
.size() == 2 );
123 CPPUNIT_ASSERT( v
[0] == 1 );
124 CPPUNIT_ASSERT( v
[1] == 2 );
127 CPPUNIT_ASSERT( v
.size() == 1 );
128 CPPUNIT_ASSERT( v
[0] == 1 );
131 CPPUNIT_ASSERT( v
.size() == 0 );
132 CPPUNIT_ASSERT( v
.empty() );
134 wxVector
<char> vEmpty
;
137 void VectorsTestCase::Insert()
141 v
.insert(v
.end(), 'a');
142 CPPUNIT_ASSERT( v
.size() == 1 );
143 CPPUNIT_ASSERT( v
[0] == 'a' );
145 v
.insert(v
.end(), 'b');
146 CPPUNIT_ASSERT( v
.size() == 2 );
147 CPPUNIT_ASSERT( v
[0] == 'a' );
148 CPPUNIT_ASSERT( v
[1] == 'b' );
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' );
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' );
164 void VectorsTestCase::Erase()
172 CPPUNIT_ASSERT( v
.size() == 4 );
174 v
.erase(v
.begin(), v
.end()-1);
175 CPPUNIT_ASSERT( v
.size() == 1 );
176 CPPUNIT_ASSERT( v
[0] == 4 );
183 CPPUNIT_ASSERT( v
.size() == 4 );
186 CPPUNIT_ASSERT( v
.size() == 3 );
187 CPPUNIT_ASSERT( v
[0] == 2 );
188 CPPUNIT_ASSERT( v
[1] == 3 );
189 CPPUNIT_ASSERT( v
[2] == 4 );
192 void VectorsTestCase::Iterators()
201 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
203 CPPUNIT_ASSERT_EQUAL( value
, *i
);
207 void VectorsTestCase::Objects()
209 wxVector
<CountedObject
> v
;
210 v
.push_back(CountedObject(1));
211 v
.push_back(CountedObject(2));
212 v
.push_back(CountedObject(3));
215 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
216 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
219 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
222 void VectorsTestCase::NonPODs()
224 wxVector
<SelfPointingObject
> v
;
225 v
.push_back(SelfPointingObject());
226 v
.push_back(SelfPointingObject());
227 v
.push_back(SelfPointingObject());
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
236 wxVector
<wxString
> vs
;
239 vs
.push_back("three");
241 vs
.erase(vs
.begin());
245 void VectorsTestCase::Resize()
247 wxVector
<CountedObject
> v
;
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() );
257 CPPUNIT_ASSERT_EQUAL( 1, v
.size() );
258 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
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() );