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
; }
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
< 5; ++i
)
90 list1
.Append(dummy
+ i
);
92 CPPUNIT_ASSERT( list1
.GetCount() == 5 );
93 CPPUNIT_ASSERT( list1
.Item(3)->GetData() == dummy
+ 3 );
94 CPPUNIT_ASSERT( list1
.Find(dummy
+ 4) );
96 wxListInt::compatibility_iterator node
= list1
.GetFirst();
101 CPPUNIT_ASSERT( node
->GetData() == dummy
+ i
);
102 node
= node
->GetNext();
106 CPPUNIT_ASSERT( size_t(i
) == list1
.GetCount() );
108 list1
.Insert(dummy
+ 0);
109 list1
.Insert(1, dummy
+ 1);
110 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
112 node
= list1
.GetFirst();
117 int* t
= node
->GetData();
118 CPPUNIT_ASSERT( t
== dummy
+ i
);
119 node
= node
->GetNext();
124 void ListsTestCase::wxStdListTest()
127 wxListInt::iterator it
, en
;
128 wxListInt::reverse_iterator rit
, ren
;
130 for ( i
= 0; i
< 5; ++i
)
131 list1
.push_back(i
+ &i
);
133 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
134 it
!= en
; ++it
, ++i
)
136 CPPUNIT_ASSERT( *it
== i
+ &i
);
139 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
140 rit
!= ren
; ++rit
, --i
)
142 CPPUNIT_ASSERT( *rit
== i
+ &i
);
145 CPPUNIT_ASSERT( *list1
.rbegin() == *--list1
.end() &&
146 *list1
.begin() == *--list1
.rend() );
147 CPPUNIT_ASSERT( *list1
.begin() == *--++list1
.begin() &&
148 *list1
.rbegin() == *--++list1
.rbegin() );
150 CPPUNIT_ASSERT( list1
.front() == &i
&& list1
.back() == &i
+ 4 );
152 list1
.erase(list1
.begin());
153 list1
.erase(--list1
.end());
155 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
156 it
!= en
; ++it
, ++i
)
158 CPPUNIT_ASSERT( *it
== i
+ &i
);
162 void ListsTestCase::wxListCtorTest()
166 list1
.Append(new Baz(_T("first")));
167 list1
.Append(new Baz(_T("second")));
169 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
170 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
175 CPPUNIT_ASSERT( list1
.GetCount() == 2 );
176 CPPUNIT_ASSERT( list2
.GetCount() == 2 );
177 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
180 list1
.DeleteContents(true);
182 WX_CLEAR_LIST(wxListBazs
, list1
);
186 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );