]> git.saurik.com Git - wxWidgets.git/blob - tests/lists/lists.cpp
16ea8a3d37ad9f46e2825050c43b0132efe73349
[wxWidgets.git] / tests / lists / lists.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/lists/lists.cpp
3 // Purpose: wxList unit test
4 // Author: Vadim Zeitlin, Mattia Barbon
5 // Created: 2004-12-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Mattia Barbon
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/list.h"
25
26 // --------------------------------------------------------------------------
27 // test class
28 // --------------------------------------------------------------------------
29
30 class ListsTestCase : public CppUnit::TestCase
31 {
32 public:
33 ListsTestCase() { }
34
35 private:
36 CPPUNIT_TEST_SUITE( ListsTestCase );
37 CPPUNIT_TEST( wxListTest );
38 CPPUNIT_TEST( wxStdListTest );
39 CPPUNIT_TEST( wxListCtorTest );
40 CPPUNIT_TEST_SUITE_END();
41
42 void wxListTest();
43 void wxStdListTest();
44 void wxListCtorTest();
45
46 DECLARE_NO_COPY_CLASS(ListsTestCase)
47 };
48
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase );
51
52 // also include in it's own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase, "ListsTestCase" );
54
55 class Baz // Foo is already taken in the hash test
56 {
57 public:
58 Baz(const wxString& name) : m_name(name) { ms_bars++; }
59 Baz(const Baz& bar) : m_name(bar.m_name) { ms_bars++; }
60 ~Baz() { ms_bars--; }
61
62 static size_t GetNumber() { return ms_bars; }
63
64 const wxChar *GetName() const { return m_name; }
65
66 private:
67 wxString m_name;
68
69 static size_t ms_bars;
70 };
71
72 size_t Baz::ms_bars = 0;
73
74 #include "wx/list.h"
75
76 WX_DECLARE_LIST(Baz, wxListBazs);
77 #include "wx/listimpl.cpp"
78 WX_DEFINE_LIST(wxListBazs)
79
80 WX_DECLARE_LIST(int, wxListInt);
81 WX_DEFINE_LIST(wxListInt)
82
83 void ListsTestCase::wxListTest()
84 {
85 wxListInt list1;
86 int dummy[5];
87 int i;
88
89 for ( i = 0; i < 5; ++i )
90 list1.Append(dummy + i);
91
92 CPPUNIT_ASSERT( list1.GetCount() == 5 );
93 CPPUNIT_ASSERT( list1.Item(3)->GetData() == dummy + 3 );
94 CPPUNIT_ASSERT( list1.Find(dummy + 4) );
95
96 wxListInt::compatibility_iterator node = list1.GetFirst();
97 i = 0;
98
99 while (node)
100 {
101 CPPUNIT_ASSERT( node->GetData() == dummy + i );
102 node = node->GetNext();
103 ++i;
104 }
105
106 CPPUNIT_ASSERT( size_t(i) == list1.GetCount() );
107
108 list1.Insert(dummy + 0);
109 list1.Insert(1, dummy + 1);
110 list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
111
112 node = list1.GetFirst();
113 i = 0;
114
115 while (i < 3)
116 {
117 int* t = node->GetData();
118 CPPUNIT_ASSERT( t == dummy + i );
119 node = node->GetNext();
120 ++i;
121 }
122 }
123
124 void ListsTestCase::wxStdListTest()
125 {
126 wxListInt list1;
127 wxListInt::iterator it, en;
128 wxListInt::reverse_iterator rit, ren;
129 int i;
130 for ( i = 0; i < 5; ++i )
131 list1.push_back(i + &i);
132
133 for ( it = list1.begin(), en = list1.end(), i = 0;
134 it != en; ++it, ++i )
135 {
136 CPPUNIT_ASSERT( *it == i + &i );
137 }
138
139 for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
140 rit != ren; ++rit, --i )
141 {
142 CPPUNIT_ASSERT( *rit == i + &i );
143 }
144
145 CPPUNIT_ASSERT( *list1.rbegin() == *--list1.end() &&
146 *list1.begin() == *--list1.rend() );
147 CPPUNIT_ASSERT( *list1.begin() == *--++list1.begin() &&
148 *list1.rbegin() == *--++list1.rbegin() );
149
150 CPPUNIT_ASSERT( list1.front() == &i && list1.back() == &i + 4 );
151
152 list1.erase(list1.begin());
153 list1.erase(--list1.end());
154
155 for ( it = list1.begin(), en = list1.end(), i = 1;
156 it != en; ++it, ++i )
157 {
158 CPPUNIT_ASSERT( *it == i + &i );
159 }
160 }
161
162 void ListsTestCase::wxListCtorTest()
163 {
164 {
165 wxListBazs list1;
166 list1.Append(new Baz(_T("first")));
167 list1.Append(new Baz(_T("second")));
168
169 CPPUNIT_ASSERT( list1.GetCount() == 2 );
170 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
171
172 wxListBazs list2;
173 list2 = list1;
174
175 CPPUNIT_ASSERT( list1.GetCount() == 2 );
176 CPPUNIT_ASSERT( list2.GetCount() == 2 );
177 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
178
179 #if !wxUSE_STL
180 list1.DeleteContents(true);
181 #else
182 WX_CLEAR_LIST(wxListBazs, list1);
183 #endif
184 }
185
186 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
187 }
188