]> git.saurik.com Git - wxWidgets.git/blame - tests/lists/lists.cpp
No changes, just a small optimization in DataStreamTestCase.
[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
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
30class ListsTestCase : public CppUnit::TestCase
31{
32public:
33 ListsTestCase() { }
34
35private:
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
50CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase );
51
e3778b4d 52// also include in its own registry so that these tests can be run alone
7d9cfc54
MB
53CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase, "ListsTestCase" );
54
55class Baz // Foo is already taken in the hash test
56{
57public:
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
86501081 64 const wxChar *GetName() const { return m_name.c_str(); }
7d9cfc54
MB
65
66private:
67 wxString m_name;
68
69 static size_t ms_bars;
70};
71
72size_t Baz::ms_bars = 0;
73
74#include "wx/list.h"
75
76WX_DECLARE_LIST(Baz, wxListBazs);
77#include "wx/listimpl.cpp"
412e0d47 78WX_DEFINE_LIST(wxListBazs)
7d9cfc54
MB
79
80WX_DECLARE_LIST(int, wxListInt);
412e0d47 81WX_DEFINE_LIST(wxListInt)
7d9cfc54
MB
82
83void ListsTestCase::wxListTest()
84{
85 wxListInt list1;
86 int dummy[5];
6124b134 87 size_t i;
7d9cfc54 88
6124b134 89 for ( i = 0; i < WXSIZEOF(dummy); ++i )
7d9cfc54
MB
90 list1.Append(dummy + i);
91
6124b134
VZ
92 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy), list1.GetCount() );
93 CPPUNIT_ASSERT_EQUAL( dummy + 3, list1.Item(3)->GetData() );
7d9cfc54
MB
94 CPPUNIT_ASSERT( list1.Find(dummy + 4) );
95
6124b134
VZ
96 wxListInt::compatibility_iterator node;
97 for ( i = 0, node = list1.GetFirst(); node; ++i, node = node->GetNext() )
7d9cfc54 98 {
6124b134 99 CPPUNIT_ASSERT_EQUAL( dummy + i, node->GetData() );
7d9cfc54
MB
100 }
101
6124b134 102 CPPUNIT_ASSERT_EQUAL( i, list1.GetCount() );
7d9cfc54
MB
103
104 list1.Insert(dummy + 0);
105 list1.Insert(1, dummy + 1);
106 list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
107
6124b134 108 for ( i = 0, node = list1.GetFirst(); i < 3; ++i, node = node->GetNext() )
7d9cfc54
MB
109 {
110 int* t = node->GetData();
6124b134 111 CPPUNIT_ASSERT_EQUAL( dummy + i, t );
7d9cfc54
MB
112 }
113}
114
115void 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 }
b9f9065e
VZ
151
152 list1.clear();
153 CPPUNIT_ASSERT( list1.empty() );
154
8dd13d0f
VZ
155 it = list1.insert(list1.end(), (int *)1);
156 CPPUNIT_ASSERT_EQUAL( (int *)1, *it );
157 CPPUNIT_ASSERT( it == list1.begin() );
b9f9065e 158 CPPUNIT_ASSERT_EQUAL( (int *)1, list1.front() );
8dd13d0f
VZ
159
160 it = list1.insert(list1.end(), (int *)2);
161 CPPUNIT_ASSERT_EQUAL( (int *)2, *it );
162 CPPUNIT_ASSERT( ++it == list1.end() );
b9f9065e 163 CPPUNIT_ASSERT_EQUAL( (int *)2, list1.back() );
1416fc5f 164
8dd13d0f
VZ
165 it = list1.begin();
166 wxListInt::iterator it2 = list1.insert(++it, (int *)3);
167 CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );
168
1416fc5f
VZ
169 it = list1.begin();
170 it = list1.erase(++it, list1.end());
171 CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
172 CPPUNIT_ASSERT( it == list1.end() );
ab13878f
VZ
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() );
7d9cfc54
MB
184}
185
186void ListsTestCase::wxListCtorTest()
187{
188 {
189 wxListBazs list1;
9a83f860
VZ
190 list1.Append(new Baz(wxT("first")));
191 list1.Append(new Baz(wxT("second")));
7d9cfc54
MB
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