]>
git.saurik.com Git - wxWidgets.git/blob - tests/vectors/vectors.cpp
ceeea1661cf539792cfe13b7173d8ca5049ec3bc
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
);
88 CPPUNIT_TEST_SUITE_END();
100 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
103 // register in the unnamed registry so that these tests are run by default
104 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
106 // also include in its own registry so that these tests can be run alone
107 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
109 void VectorsTestCase::PushPopTest()
113 CPPUNIT_ASSERT( v
.size() == 0 );
115 CPPUNIT_ASSERT( v
.size() == 1 );
117 CPPUNIT_ASSERT( v
.size() == 2 );
119 CPPUNIT_ASSERT( v
.size() == 3 );
121 CPPUNIT_ASSERT( v
[0] == 1 );
122 CPPUNIT_ASSERT( v
[1] == 2 );
123 CPPUNIT_ASSERT( v
[2] == 42 );
126 CPPUNIT_ASSERT( v
.size() == 2 );
127 CPPUNIT_ASSERT( v
[0] == 1 );
128 CPPUNIT_ASSERT( v
[1] == 2 );
131 CPPUNIT_ASSERT( v
.size() == 1 );
132 CPPUNIT_ASSERT( v
[0] == 1 );
135 CPPUNIT_ASSERT( v
.size() == 0 );
136 CPPUNIT_ASSERT( v
.empty() );
138 wxVector
<char> vEmpty
;
141 void VectorsTestCase::Insert()
145 v
.insert(v
.end(), 'a');
146 CPPUNIT_ASSERT( v
.size() == 1 );
147 CPPUNIT_ASSERT( v
[0] == 'a' );
149 v
.insert(v
.end(), 'b');
150 CPPUNIT_ASSERT( v
.size() == 2 );
151 CPPUNIT_ASSERT( v
[0] == 'a' );
152 CPPUNIT_ASSERT( v
[1] == 'b' );
154 v
.insert(v
.begin(), '0');
155 CPPUNIT_ASSERT( v
.size() == 3 );
156 CPPUNIT_ASSERT( v
[0] == '0' );
157 CPPUNIT_ASSERT( v
[1] == 'a' );
158 CPPUNIT_ASSERT( v
[2] == 'b' );
160 v
.insert(v
.begin() + 2, 'X');
161 CPPUNIT_ASSERT( v
.size() == 4 );
162 CPPUNIT_ASSERT( v
[0] == '0' );
163 CPPUNIT_ASSERT( v
[1] == 'a' );
164 CPPUNIT_ASSERT( v
[2] == 'X' );
165 CPPUNIT_ASSERT( v
[3] == 'b' );
168 void VectorsTestCase::Erase()
176 CPPUNIT_ASSERT( v
.size() == 4 );
178 v
.erase(v
.begin(), v
.end()-1);
179 CPPUNIT_ASSERT( v
.size() == 1 );
180 CPPUNIT_ASSERT( v
[0] == 4 );
187 CPPUNIT_ASSERT( v
.size() == 4 );
190 CPPUNIT_ASSERT( v
.size() == 3 );
191 CPPUNIT_ASSERT( v
[0] == 2 );
192 CPPUNIT_ASSERT( v
[1] == 3 );
193 CPPUNIT_ASSERT( v
[2] == 4 );
196 void VectorsTestCase::Iterators()
205 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
207 CPPUNIT_ASSERT_EQUAL( value
, *i
);
211 void VectorsTestCase::Objects()
213 wxVector
<CountedObject
> v
;
214 v
.push_back(CountedObject(1));
215 v
.push_back(CountedObject(2));
216 v
.push_back(CountedObject(3));
219 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
220 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
223 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
226 void VectorsTestCase::NonPODs()
228 wxVector
<SelfPointingObject
> v
;
229 v
.push_back(SelfPointingObject());
230 v
.push_back(SelfPointingObject());
231 v
.push_back(SelfPointingObject());
236 // try the same with wxString, which is not POD, but is implemented in
237 // a movable way (this won't assert, but would crash or show some memory
238 // problems under Valgrind if wxString couldn't be safely moved with
240 wxVector
<wxString
> vs
;
243 vs
.push_back("three");
245 vs
.erase(vs
.begin());
249 void VectorsTestCase::Resize()
251 wxVector
<CountedObject
> v
;
254 CPPUNIT_ASSERT_EQUAL( 3, v
.size() );
255 CPPUNIT_ASSERT_EQUAL( 3, CountedObject::GetCount() );
256 CPPUNIT_ASSERT_EQUAL( 0, v
[0].GetValue() );
257 CPPUNIT_ASSERT_EQUAL( 0, v
[1].GetValue() );
258 CPPUNIT_ASSERT_EQUAL( 0, v
[2].GetValue() );
261 CPPUNIT_ASSERT_EQUAL( 1, v
.size() );
262 CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
264 v
.resize(4, CountedObject(17));
265 CPPUNIT_ASSERT_EQUAL( 4, v
.size() );
266 CPPUNIT_ASSERT_EQUAL( 4, CountedObject::GetCount() );
267 CPPUNIT_ASSERT_EQUAL( 0, v
[0].GetValue() );
268 CPPUNIT_ASSERT_EQUAL( 17, v
[1].GetValue() );
269 CPPUNIT_ASSERT_EQUAL( 17, v
[2].GetValue() );
270 CPPUNIT_ASSERT_EQUAL( 17, v
[3].GetValue() );
273 void VectorsTestCase::Swap()
275 wxVector
<int> v1
, v2
;
278 CPPUNIT_ASSERT( v1
.empty() );
279 CPPUNIT_ASSERT_EQUAL( 1, v2
.size() );
280 CPPUNIT_ASSERT_EQUAL( 17, v2
[0] );
284 CPPUNIT_ASSERT_EQUAL( 1, v1
.size() );
285 CPPUNIT_ASSERT_EQUAL( 17, v1
[0] );
286 CPPUNIT_ASSERT_EQUAL( 1, v2
.size() );
287 CPPUNIT_ASSERT_EQUAL( 9, v2
[0] );
291 CPPUNIT_ASSERT( v1
.empty() );
295 void VectorsTestCase::Sort()
313 for (idx
=1; idx
<v
.size(); idx
++)
315 CPPUNIT_ASSERT( v
[idx
-1] <= v
[idx
] );