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