]> git.saurik.com Git - wxWidgets.git/blob - tests/lists/lists.cpp
Removing more CodeWarrior project files.
[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.c_str(); }
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 size_t i;
88
89 for ( i = 0; i < WXSIZEOF(dummy); ++i )
90 list1.Append(dummy + i);
91
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) );
95
96 wxListInt::compatibility_iterator node;
97 for ( i = 0, node = list1.GetFirst(); node; ++i, node = node->GetNext() )
98 {
99 CPPUNIT_ASSERT_EQUAL( dummy + i, node->GetData() );
100 }
101
102 CPPUNIT_ASSERT_EQUAL( i, list1.GetCount() );
103
104 list1.Insert(dummy + 0);
105 list1.Insert(1, dummy + 1);
106 list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
107
108 for ( i = 0, node = list1.GetFirst(); i < 3; ++i, node = node->GetNext() )
109 {
110 int* t = node->GetData();
111 CPPUNIT_ASSERT_EQUAL( dummy + i, t );
112 }
113 }
114
115 void ListsTestCase::wxStdListTest()
116 {
117 wxListInt list1;
118 wxListInt::iterator it, en;
119 wxListInt::reverse_iterator rit, ren;
120 int i;
121 for ( i = 0; i < 5; ++i )
122 list1.push_back(i + &i);
123
124 for ( it = list1.begin(), en = list1.end(), i = 0;
125 it != en; ++it, ++i )
126 {
127 CPPUNIT_ASSERT( *it == i + &i );
128 }
129
130 for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
131 rit != ren; ++rit, --i )
132 {
133 CPPUNIT_ASSERT( *rit == i + &i );
134 }
135
136 CPPUNIT_ASSERT( *list1.rbegin() == *--list1.end() &&
137 *list1.begin() == *--list1.rend() );
138 CPPUNIT_ASSERT( *list1.begin() == *--++list1.begin() &&
139 *list1.rbegin() == *--++list1.rbegin() );
140
141 CPPUNIT_ASSERT( list1.front() == &i && list1.back() == &i + 4 );
142
143 list1.erase(list1.begin());
144 list1.erase(--list1.end());
145
146 for ( it = list1.begin(), en = list1.end(), i = 1;
147 it != en; ++it, ++i )
148 {
149 CPPUNIT_ASSERT( *it == i + &i );
150 }
151
152 list1.clear();
153 CPPUNIT_ASSERT( list1.empty() );
154
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() );
159
160 it = list1.begin();
161 it = list1.erase(++it, list1.end());
162 CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
163 CPPUNIT_ASSERT( it == list1.end() );
164
165 wxListInt list2;
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() );
171
172 list1.insert(list1.end(), list2.begin(), list2.end());
173 CPPUNIT_ASSERT_EQUAL( 5, list1.size() );
174 CPPUNIT_ASSERT_EQUAL( (int *)4, list1.back() );
175 }
176
177 void ListsTestCase::wxListCtorTest()
178 {
179 {
180 wxListBazs list1;
181 list1.Append(new Baz(_T("first")));
182 list1.Append(new Baz(_T("second")));
183
184 CPPUNIT_ASSERT( list1.GetCount() == 2 );
185 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
186
187 wxListBazs list2;
188 list2 = list1;
189
190 CPPUNIT_ASSERT( list1.GetCount() == 2 );
191 CPPUNIT_ASSERT( list2.GetCount() == 2 );
192 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
193
194 #if !wxUSE_STL
195 list1.DeleteContents(true);
196 #else
197 WX_CLEAR_LIST(wxListBazs, list1);
198 #endif
199 }
200
201 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
202 }
203