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 list1
.insert(list1
.end(), (int *)1);
156 list1
.insert(list1
.end(), (int *)2);
157 CPPUNIT_ASSERT_EQUAL( (int *)1, list1
.front() );
158 CPPUNIT_ASSERT_EQUAL( (int *)2, list1
.back() );
161 it
= list1
.erase(++it
, list1
.end());
162 CPPUNIT_ASSERT_EQUAL( 1, list1
.size() );
163 CPPUNIT_ASSERT( it
== list1
.end() );
166 list2
.push_back((int *)3);
167 list2
.push_back((int *)4);
168 list1
.insert(list1
.begin(), list2
.begin(), list2
.end());
169 CPPUNIT_ASSERT_EQUAL( 3, list1
.size() );
170 CPPUNIT_ASSERT_EQUAL( (int *)3, list1
.front() );
172 list1
.insert(list1
.end(), list2
.begin(), list2
.end());
173 CPPUNIT_ASSERT_EQUAL( 5, list1
.size() );
174 CPPUNIT_ASSERT_EQUAL( (int *)4, list1
.back() );
177 void ListsTestCase::wxListCtorTest()
181 list1
.Append(new Baz(wxT("first")));
182 list1
.Append(new Baz(wxT("second")));
184 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
185 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
190 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
191 CPPUNIT_ASSERT( list2
.GetCount() == 2 );
192 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
195 list1
.DeleteContents(true);
197 WX_CLEAR_LIST(wxListBazs
, list1
);
201 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );