]> git.saurik.com Git - wxWidgets.git/blob - tests/lists/lists.cpp
Minor change to wxString pretty-printer for gdb.
[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 its 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 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() );
159
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() );
164
165 it = list1.begin();
166 wxListInt::iterator it2 = list1.insert(++it, (int *)3);
167 CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );
168
169 it = list1.begin();
170 it = list1.erase(++it, list1.end());
171 CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
172 CPPUNIT_ASSERT( it == list1.end() );
173
174 wxListInt list2;
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() );
180
181 list1.insert(list1.end(), list2.begin(), list2.end());
182 CPPUNIT_ASSERT_EQUAL( 5, list1.size() );
183 CPPUNIT_ASSERT_EQUAL( (int *)4, list1.back() );
184 }
185
186 void ListsTestCase::wxListCtorTest()
187 {
188 {
189 wxListBazs list1;
190 list1.Append(new Baz(wxT("first")));
191 list1.Append(new Baz(wxT("second")));
192
193 CPPUNIT_ASSERT( list1.GetCount() == 2 );
194 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
195
196 wxListBazs list2;
197 list2 = list1;
198
199 CPPUNIT_ASSERT( list1.GetCount() == 2 );
200 CPPUNIT_ASSERT( list2.GetCount() == 2 );
201 CPPUNIT_ASSERT( Baz::GetNumber() == 2 );
202
203 #if !wxUSE_STL
204 list1.DeleteContents(true);
205 #else
206 WX_CLEAR_LIST(wxListBazs, list1);
207 #endif
208 }
209
210 CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
211 }
212