1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/lists/lists.cpp
3 // Purpose: wxList unit test
4 // Author: Vadim Zeitlin, Mattia Barbon
6 // Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 // --------------------------------------------------------------------------
27 // --------------------------------------------------------------------------
29 class ListsTestCase
: public CppUnit::TestCase
35 CPPUNIT_TEST_SUITE( ListsTestCase
);
36 CPPUNIT_TEST( wxListTest
);
37 CPPUNIT_TEST( wxStdListTest
);
38 CPPUNIT_TEST( wxListCtorTest
);
39 CPPUNIT_TEST_SUITE_END();
43 void wxListCtorTest();
45 DECLARE_NO_COPY_CLASS(ListsTestCase
)
48 // register in the unnamed registry so that these tests are run by default
49 CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase
);
51 // also include in its own registry so that these tests can be run alone
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase
, "ListsTestCase" );
54 class Baz
// Foo is already taken in the hash test
57 Baz(const wxString
& name
) : m_name(name
) { ms_bars
++; }
58 Baz(const Baz
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
61 static size_t GetNumber() { return ms_bars
; }
63 const wxChar
*GetName() const { return m_name
.c_str(); }
68 static size_t ms_bars
;
71 size_t Baz::ms_bars
= 0;
75 WX_DECLARE_LIST(Baz
, wxListBazs
);
76 #include "wx/listimpl.cpp"
77 WX_DEFINE_LIST(wxListBazs
)
79 WX_DECLARE_LIST(int, wxListInt
);
80 WX_DEFINE_LIST(wxListInt
)
82 void ListsTestCase::wxListTest()
88 for ( i
= 0; i
< WXSIZEOF(dummy
); ++i
)
89 list1
.Append(dummy
+ i
);
91 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy
), list1
.GetCount() );
92 CPPUNIT_ASSERT_EQUAL( dummy
+ 3, list1
.Item(3)->GetData() );
93 CPPUNIT_ASSERT( list1
.Find(dummy
+ 4) );
95 wxListInt::compatibility_iterator node
;
96 for ( i
= 0, node
= list1
.GetFirst(); node
; ++i
, node
= node
->GetNext() )
98 CPPUNIT_ASSERT_EQUAL( dummy
+ i
, node
->GetData() );
101 CPPUNIT_ASSERT_EQUAL( i
, list1
.GetCount() );
103 list1
.Insert(dummy
+ 0);
104 list1
.Insert(1, dummy
+ 1);
105 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
107 for ( i
= 0, node
= list1
.GetFirst(); i
< 3; ++i
, node
= node
->GetNext() )
109 int* t
= node
->GetData();
110 CPPUNIT_ASSERT_EQUAL( dummy
+ i
, t
);
114 void ListsTestCase::wxStdListTest()
117 wxListInt::iterator it
, en
;
118 wxListInt::reverse_iterator rit
, ren
;
120 for ( i
= 0; i
< 5; ++i
)
121 list1
.push_back(i
+ &i
);
123 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
124 it
!= en
; ++it
, ++i
)
126 CPPUNIT_ASSERT( *it
== i
+ &i
);
129 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
130 rit
!= ren
; ++rit
, --i
)
132 CPPUNIT_ASSERT( *rit
== i
+ &i
);
135 CPPUNIT_ASSERT( *list1
.rbegin() == *--list1
.end() &&
136 *list1
.begin() == *--list1
.rend() );
137 CPPUNIT_ASSERT( *list1
.begin() == *--++list1
.begin() &&
138 *list1
.rbegin() == *--++list1
.rbegin() );
140 CPPUNIT_ASSERT( list1
.front() == &i
&& list1
.back() == &i
+ 4 );
142 list1
.erase(list1
.begin());
143 list1
.erase(--list1
.end());
145 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
146 it
!= en
; ++it
, ++i
)
148 CPPUNIT_ASSERT( *it
== i
+ &i
);
152 CPPUNIT_ASSERT( list1
.empty() );
154 it
= list1
.insert(list1
.end(), (int *)1);
155 CPPUNIT_ASSERT_EQUAL( (int *)1, *it
);
156 CPPUNIT_ASSERT( it
== list1
.begin() );
157 CPPUNIT_ASSERT_EQUAL( (int *)1, list1
.front() );
159 it
= list1
.insert(list1
.end(), (int *)2);
160 CPPUNIT_ASSERT_EQUAL( (int *)2, *it
);
161 CPPUNIT_ASSERT( ++it
== list1
.end() );
162 CPPUNIT_ASSERT_EQUAL( (int *)2, list1
.back() );
165 wxListInt::iterator it2
= list1
.insert(++it
, (int *)3);
166 CPPUNIT_ASSERT_EQUAL( (int *)3, *it2
);
169 it
= list1
.erase(++it
, list1
.end());
170 CPPUNIT_ASSERT_EQUAL( 1, list1
.size() );
171 CPPUNIT_ASSERT( it
== list1
.end() );
174 list2
.push_back((int *)3);
175 list2
.push_back((int *)4);
176 list1
.insert(list1
.begin(), list2
.begin(), list2
.end());
177 CPPUNIT_ASSERT_EQUAL( 3, list1
.size() );
178 CPPUNIT_ASSERT_EQUAL( (int *)3, list1
.front() );
180 list1
.insert(list1
.end(), list2
.begin(), list2
.end());
181 CPPUNIT_ASSERT_EQUAL( 5, list1
.size() );
182 CPPUNIT_ASSERT_EQUAL( (int *)4, list1
.back() );
185 void ListsTestCase::wxListCtorTest()
189 list1
.Append(new Baz(wxT("first")));
190 list1
.Append(new Baz(wxT("second")));
192 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
193 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
198 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
199 CPPUNIT_ASSERT( list2
.GetCount() == 2 );
200 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
203 list1
.DeleteContents(true);
205 WX_CLEAR_LIST(wxListBazs
, list1
);
209 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );