]>
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_SUITE_END();
94 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
97 // register in the unnamed registry so that these tests are run by default
98 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
100 // also include in it's own registry so that these tests can be run alone
101 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
103 void VectorsTestCase::PushPopTest()
107 CPPUNIT_ASSERT( v
.size() == 0 );
109 CPPUNIT_ASSERT( v
.size() == 1 );
111 CPPUNIT_ASSERT( v
.size() == 2 );
113 CPPUNIT_ASSERT( v
.size() == 3 );
115 CPPUNIT_ASSERT( v
[0] == 1 );
116 CPPUNIT_ASSERT( v
[1] == 2 );
117 CPPUNIT_ASSERT( v
[2] == 42 );
120 CPPUNIT_ASSERT( v
.size() == 2 );
121 CPPUNIT_ASSERT( v
[0] == 1 );
122 CPPUNIT_ASSERT( v
[1] == 2 );
125 CPPUNIT_ASSERT( v
.size() == 1 );
126 CPPUNIT_ASSERT( v
[0] == 1 );
129 CPPUNIT_ASSERT( v
.size() == 0 );
130 CPPUNIT_ASSERT( v
.empty() );
132 wxVector
<char> vEmpty
;
135 void VectorsTestCase::Insert()
139 v
.insert(v
.end(), 'a');
140 CPPUNIT_ASSERT( v
.size() == 1 );
141 CPPUNIT_ASSERT( v
[0] == 'a' );
143 v
.insert(v
.end(), 'b');
144 CPPUNIT_ASSERT( v
.size() == 2 );
145 CPPUNIT_ASSERT( v
[0] == 'a' );
146 CPPUNIT_ASSERT( v
[1] == 'b' );
148 v
.insert(v
.begin(), '0');
149 CPPUNIT_ASSERT( v
.size() == 3 );
150 CPPUNIT_ASSERT( v
[0] == '0' );
151 CPPUNIT_ASSERT( v
[1] == 'a' );
152 CPPUNIT_ASSERT( v
[2] == 'b' );
154 v
.insert(v
.begin() + 2, 'X');
155 CPPUNIT_ASSERT( v
.size() == 4 );
156 CPPUNIT_ASSERT( v
[0] == '0' );
157 CPPUNIT_ASSERT( v
[1] == 'a' );
158 CPPUNIT_ASSERT( v
[2] == 'X' );
159 CPPUNIT_ASSERT( v
[3] == 'b' );
162 void VectorsTestCase::Erase()
170 CPPUNIT_ASSERT( v
.size() == 4 );
172 v
.erase(v
.begin(), v
.end()-1);
173 CPPUNIT_ASSERT( v
.size() == 1 );
174 CPPUNIT_ASSERT( v
[0] == 4 );
181 CPPUNIT_ASSERT( v
.size() == 4 );
184 CPPUNIT_ASSERT( v
.size() == 3 );
185 CPPUNIT_ASSERT( v
[0] == 2 );
186 CPPUNIT_ASSERT( v
[1] == 3 );
187 CPPUNIT_ASSERT( v
[2] == 4 );
190 void VectorsTestCase::Iterators()
199 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
201 CPPUNIT_ASSERT_EQUAL( value
, *i
);
205 void VectorsTestCase::Objects()
207 wxVector
<CountedObject
> v
;
208 v
.push_back(CountedObject(1));
209 v
.push_back(CountedObject(2));
210 v
.push_back(CountedObject(3));
213 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
214 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
217 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
220 void VectorsTestCase::NonPODs()
222 wxVector
<SelfPointingObject
> v
;
223 v
.push_back(SelfPointingObject());
224 v
.push_back(SelfPointingObject());
225 v
.push_back(SelfPointingObject());
230 // try the same with wxString, which is not POD, but is implemented in
231 // a movable way (this won't assert, but would crash or show some memory
232 // problems under Valgrind if wxString couldn't be safely moved with
234 wxVector
<wxString
> vs
;
237 vs
.push_back("three");
239 vs
.erase(vs
.begin());