]>
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 it's 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
); }
61 SelfPointingObject
*m_self
;
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 class VectorsTestCase
: public CppUnit::TestCase
74 CPPUNIT_TEST_SUITE( VectorsTestCase
);
75 CPPUNIT_TEST( PushPopTest
);
76 CPPUNIT_TEST( Insert
);
77 CPPUNIT_TEST( Erase
);
78 CPPUNIT_TEST( Iterators
);
79 CPPUNIT_TEST( Objects
);
80 CPPUNIT_TEST( NonPODs
);
81 CPPUNIT_TEST_SUITE_END();
90 DECLARE_NO_COPY_CLASS(VectorsTestCase
)
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( VectorsTestCase
);
96 // also include in it's own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VectorsTestCase
, "VectorsTestCase" );
99 void VectorsTestCase::PushPopTest()
103 CPPUNIT_ASSERT( v
.size() == 0 );
105 CPPUNIT_ASSERT( v
.size() == 1 );
107 CPPUNIT_ASSERT( v
.size() == 2 );
109 CPPUNIT_ASSERT( v
.size() == 3 );
111 CPPUNIT_ASSERT( v
[0] == 1 );
112 CPPUNIT_ASSERT( v
[1] == 2 );
113 CPPUNIT_ASSERT( v
[2] == 42 );
116 CPPUNIT_ASSERT( v
.size() == 2 );
117 CPPUNIT_ASSERT( v
[0] == 1 );
118 CPPUNIT_ASSERT( v
[1] == 2 );
121 CPPUNIT_ASSERT( v
.size() == 1 );
122 CPPUNIT_ASSERT( v
[0] == 1 );
125 CPPUNIT_ASSERT( v
.size() == 0 );
126 CPPUNIT_ASSERT( v
.empty() );
128 wxVector
<char> vEmpty
;
131 void VectorsTestCase::Insert()
135 v
.insert(v
.end(), 'a');
136 CPPUNIT_ASSERT( v
.size() == 1 );
137 CPPUNIT_ASSERT( v
[0] == 'a' );
139 v
.insert(v
.end(), 'b');
140 CPPUNIT_ASSERT( v
.size() == 2 );
141 CPPUNIT_ASSERT( v
[0] == 'a' );
142 CPPUNIT_ASSERT( v
[1] == 'b' );
144 v
.insert(v
.begin(), '0');
145 CPPUNIT_ASSERT( v
.size() == 3 );
146 CPPUNIT_ASSERT( v
[0] == '0' );
147 CPPUNIT_ASSERT( v
[1] == 'a' );
148 CPPUNIT_ASSERT( v
[2] == 'b' );
150 v
.insert(v
.begin() + 2, 'X');
151 CPPUNIT_ASSERT( v
.size() == 4 );
152 CPPUNIT_ASSERT( v
[0] == '0' );
153 CPPUNIT_ASSERT( v
[1] == 'a' );
154 CPPUNIT_ASSERT( v
[2] == 'X' );
155 CPPUNIT_ASSERT( v
[3] == 'b' );
158 void VectorsTestCase::Erase()
166 CPPUNIT_ASSERT( v
.size() == 4 );
168 v
.erase(v
.begin(), v
.end()-1);
169 CPPUNIT_ASSERT( v
.size() == 1 );
170 CPPUNIT_ASSERT( v
[0] == 4 );
177 CPPUNIT_ASSERT( v
.size() == 4 );
180 CPPUNIT_ASSERT( v
.size() == 3 );
181 CPPUNIT_ASSERT( v
[0] == 2 );
182 CPPUNIT_ASSERT( v
[1] == 3 );
183 CPPUNIT_ASSERT( v
[2] == 4 );
186 void VectorsTestCase::Iterators()
195 for ( wxVector
<int>::iterator i
= v
.begin(); i
!= v
.end(); ++i
, ++value
)
197 CPPUNIT_ASSERT_EQUAL( value
, *i
);
201 void VectorsTestCase::Objects()
203 wxVector
<CountedObject
> v
;
204 v
.push_back(CountedObject(1));
205 v
.push_back(CountedObject(2));
206 v
.push_back(CountedObject(3));
209 CPPUNIT_ASSERT_EQUAL( 2, v
.size() );
210 CPPUNIT_ASSERT_EQUAL( 2, CountedObject::GetCount() );
213 CPPUNIT_ASSERT_EQUAL( 0, CountedObject::GetCount() );
216 void VectorsTestCase::NonPODs()
218 wxVector
<SelfPointingObject
> v
;
219 v
.push_back(SelfPointingObject());
220 v
.push_back(SelfPointingObject());
221 v
.push_back(SelfPointingObject());
226 // try the same with wxString, which is not POD, but is implemented in
227 // a movable way (this won't assert, but would crash or show some memory
228 // problems under Valgrind if wxString couldn't be safely moved with
230 wxVector
<wxString
> vs
;
233 vs
.push_back("three");
235 vs
.erase(vs
.begin());