1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/lists/lists.cpp
3 // Purpose: wxList unit test
4 // Author: Vadim Zeitlin, Mattia Barbon
7 // Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
26 // --------------------------------------------------------------------------
28 // --------------------------------------------------------------------------
30 class ListsTestCase
: public CppUnit::TestCase
36 CPPUNIT_TEST_SUITE( ListsTestCase
);
37 CPPUNIT_TEST( wxListTest
);
38 CPPUNIT_TEST( wxStdListTest
);
39 CPPUNIT_TEST( wxListCtorTest
);
40 CPPUNIT_TEST_SUITE_END();
44 void wxListCtorTest();
46 DECLARE_NO_COPY_CLASS(ListsTestCase
)
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase
);
52 // also include in it's own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase
, "ListsTestCase" );
55 class Baz
// Foo is already taken in the hash test
58 Baz(const wxString
& name
) : m_name(name
) { ms_bars
++; }
59 Baz(const Baz
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
62 static size_t GetNumber() { return ms_bars
; }
64 const wxChar
*GetName() const { return m_name
.c_str(); }
69 static size_t ms_bars
;
72 size_t Baz::ms_bars
= 0;
76 WX_DECLARE_LIST(Baz
, wxListBazs
);
77 #include "wx/listimpl.cpp"
78 WX_DEFINE_LIST(wxListBazs
)
80 WX_DECLARE_LIST(int, wxListInt
);
81 WX_DEFINE_LIST(wxListInt
)
83 void ListsTestCase::wxListTest()
89 for ( i
= 0; i
< WXSIZEOF(dummy
); ++i
)
90 list1
.Append(dummy
+ i
);
92 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy
), list1
.GetCount() );
93 CPPUNIT_ASSERT_EQUAL( dummy
+ 3, list1
.Item(3)->GetData() );
94 CPPUNIT_ASSERT( list1
.Find(dummy
+ 4) );
96 wxListInt::compatibility_iterator node
;
97 for ( i
= 0, node
= list1
.GetFirst(); node
; ++i
, node
= node
->GetNext() )
99 CPPUNIT_ASSERT_EQUAL( dummy
+ i
, node
->GetData() );
102 CPPUNIT_ASSERT_EQUAL( i
, list1
.GetCount() );
104 list1
.Insert(dummy
+ 0);
105 list1
.Insert(1, dummy
+ 1);
106 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
108 for ( i
= 0, node
= list1
.GetFirst(); i
< 3; ++i
, node
= node
->GetNext() )
110 int* t
= node
->GetData();
111 CPPUNIT_ASSERT_EQUAL( dummy
+ i
, t
);
115 void ListsTestCase::wxStdListTest()
118 wxListInt::iterator it
, en
;
119 wxListInt::reverse_iterator rit
, ren
;
121 for ( i
= 0; i
< 5; ++i
)
122 list1
.push_back(i
+ &i
);
124 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
125 it
!= en
; ++it
, ++i
)
127 CPPUNIT_ASSERT( *it
== i
+ &i
);
130 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
131 rit
!= ren
; ++rit
, --i
)
133 CPPUNIT_ASSERT( *rit
== i
+ &i
);
136 CPPUNIT_ASSERT( *list1
.rbegin() == *--list1
.end() &&
137 *list1
.begin() == *--list1
.rend() );
138 CPPUNIT_ASSERT( *list1
.begin() == *--++list1
.begin() &&
139 *list1
.rbegin() == *--++list1
.rbegin() );
141 CPPUNIT_ASSERT( list1
.front() == &i
&& list1
.back() == &i
+ 4 );
143 list1
.erase(list1
.begin());
144 list1
.erase(--list1
.end());
146 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
147 it
!= en
; ++it
, ++i
)
149 CPPUNIT_ASSERT( *it
== i
+ &i
);
153 CPPUNIT_ASSERT( list1
.empty() );
155 it
= list1
.insert(list1
.end(), (int *)1);
156 CPPUNIT_ASSERT_EQUAL( (int *)1, *it
);
157 CPPUNIT_ASSERT( it
== list1
.begin() );
158 CPPUNIT_ASSERT_EQUAL( (int *)1, list1
.front() );
160 it
= list1
.insert(list1
.end(), (int *)2);
161 CPPUNIT_ASSERT_EQUAL( (int *)2, *it
);
162 CPPUNIT_ASSERT( ++it
== list1
.end() );
163 CPPUNIT_ASSERT_EQUAL( (int *)2, list1
.back() );
166 wxListInt::iterator it2
= list1
.insert(++it
, (int *)3);
167 CPPUNIT_ASSERT_EQUAL( (int *)3, *it2
);
170 it
= list1
.erase(++it
, list1
.end());
171 CPPUNIT_ASSERT_EQUAL( 1, list1
.size() );
172 CPPUNIT_ASSERT( it
== list1
.end() );
175 list2
.push_back((int *)3);
176 list2
.push_back((int *)4);
177 list1
.insert(list1
.begin(), list2
.begin(), list2
.end());
178 CPPUNIT_ASSERT_EQUAL( 3, list1
.size() );
179 CPPUNIT_ASSERT_EQUAL( (int *)3, list1
.front() );
181 list1
.insert(list1
.end(), list2
.begin(), list2
.end());
182 CPPUNIT_ASSERT_EQUAL( 5, list1
.size() );
183 CPPUNIT_ASSERT_EQUAL( (int *)4, list1
.back() );
186 void ListsTestCase::wxListCtorTest()
190 list1
.Append(new Baz(wxT("first")));
191 list1
.Append(new Baz(wxT("second")));
193 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
194 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
199 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
200 CPPUNIT_ASSERT( list2
.GetCount() == 2 );
201 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
204 list1
.DeleteContents(true);
206 WX_CLEAR_LIST(wxListBazs
, list1
);
210 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );