File: lists.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (211 lines) | stat: -rw-r--r-- 5,740 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/lists/lists.cpp
// Purpose:     wxList unit test
// Author:      Vadim Zeitlin, Mattia Barbon
// Created:     2004-12-08
// Copyright:   (c) 2004 Vadim Zeitlin, Mattia Barbon
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif // WX_PRECOMP

#include "wx/list.h"

// --------------------------------------------------------------------------
// test class
// --------------------------------------------------------------------------

class ListsTestCase : public CppUnit::TestCase
{
public:
    ListsTestCase() { }

private:
    CPPUNIT_TEST_SUITE( ListsTestCase );
        CPPUNIT_TEST( wxListTest );
        CPPUNIT_TEST( wxStdListTest );
        CPPUNIT_TEST( wxListCtorTest );
    CPPUNIT_TEST_SUITE_END();

    void wxListTest();
    void wxStdListTest();
    void wxListCtorTest();

    DECLARE_NO_COPY_CLASS(ListsTestCase)
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( ListsTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListsTestCase, "ListsTestCase" );

class Baz // Foo is already taken in the hash test
{
public:
    Baz(const wxString& name) : m_name(name) { ms_bars++; }
    Baz(const Baz& bar) : m_name(bar.m_name) { ms_bars++; }
   ~Baz() { ms_bars--; }

   static size_t GetNumber() { return ms_bars; }

   const wxChar *GetName() const { return m_name.c_str(); }

private:
   wxString m_name;

   static size_t ms_bars;
};

size_t Baz::ms_bars = 0;

#include "wx/list.h"

WX_DECLARE_LIST(Baz, wxListBazs);
#include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxListBazs)

WX_DECLARE_LIST(int, wxListInt);
WX_DEFINE_LIST(wxListInt)

void ListsTestCase::wxListTest()
{
    wxListInt list1;
    int dummy[5];
    size_t i;

    for ( i = 0; i < WXSIZEOF(dummy); ++i )
        list1.Append(dummy + i);

    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(dummy), list1.GetCount() );
    CPPUNIT_ASSERT_EQUAL( dummy + 3, list1.Item(3)->GetData() );
    CPPUNIT_ASSERT( list1.Find(dummy + 4) );

    wxListInt::compatibility_iterator node;
    for ( i = 0, node = list1.GetFirst(); node; ++i, node = node->GetNext() )
    {
        CPPUNIT_ASSERT_EQUAL( dummy + i, node->GetData() );
    }

    CPPUNIT_ASSERT_EQUAL( i, list1.GetCount() );

    list1.Insert(dummy + 0);
    list1.Insert(1, dummy + 1);
    list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);

    for ( i = 0, node = list1.GetFirst(); i < 3; ++i, node = node->GetNext() )
    {
        int* t = node->GetData();
        CPPUNIT_ASSERT_EQUAL( dummy + i, t );
    }
}

void ListsTestCase::wxStdListTest()
{
    wxListInt list1;
    wxListInt::iterator it, en;
    wxListInt::reverse_iterator rit, ren;
    int i;
    for ( i = 0; i < 5; ++i )
        list1.push_back(i + &i);

    for ( it = list1.begin(), en = list1.end(), i = 0;
          it != en; ++it, ++i )
    {
        CPPUNIT_ASSERT( *it == i + &i );
    }

    for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
          rit != ren; ++rit, --i )
    {
        CPPUNIT_ASSERT( *rit == i + &i );
    }

    CPPUNIT_ASSERT( *list1.rbegin() == *--list1.end() &&
                    *list1.begin() == *--list1.rend() );
    CPPUNIT_ASSERT( *list1.begin() == *--++list1.begin() &&
                    *list1.rbegin() == *--++list1.rbegin() );

    CPPUNIT_ASSERT( list1.front() == &i && list1.back() == &i + 4 );

    list1.erase(list1.begin());
    list1.erase(--list1.end());

    for ( it = list1.begin(), en = list1.end(), i = 1;
          it != en; ++it, ++i )
    {
        CPPUNIT_ASSERT( *it == i + &i );
    }

    list1.clear();
    CPPUNIT_ASSERT( list1.empty() );

    it = list1.insert(list1.end(), (int *)1);
    CPPUNIT_ASSERT_EQUAL( (int *)1, *it );
    CPPUNIT_ASSERT( it == list1.begin() );
    CPPUNIT_ASSERT_EQUAL( (int *)1, list1.front() );

    it = list1.insert(list1.end(), (int *)2);
    CPPUNIT_ASSERT_EQUAL( (int *)2, *it );
    CPPUNIT_ASSERT( ++it == list1.end() );
    CPPUNIT_ASSERT_EQUAL( (int *)2, list1.back() );

    it = list1.begin();
    wxListInt::iterator it2 = list1.insert(++it, (int *)3);
    CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );

    it = list1.begin();
    it = list1.erase(++it, list1.end());
    CPPUNIT_ASSERT_EQUAL( 1, list1.size() );
    CPPUNIT_ASSERT( it == list1.end() );

    wxListInt list2;
    list2.push_back((int *)3);
    list2.push_back((int *)4);
    list1.insert(list1.begin(), list2.begin(), list2.end());
    CPPUNIT_ASSERT_EQUAL( 3, list1.size() );
    CPPUNIT_ASSERT_EQUAL( (int *)3, list1.front() );

    list1.insert(list1.end(), list2.begin(), list2.end());
    CPPUNIT_ASSERT_EQUAL( 5, list1.size() );
    CPPUNIT_ASSERT_EQUAL( (int *)4, list1.back() );
}

void ListsTestCase::wxListCtorTest()
{
    {
        wxListBazs list1;
        list1.Append(new Baz(wxT("first")));
        list1.Append(new Baz(wxT("second")));

        CPPUNIT_ASSERT( list1.GetCount() == 2 );
        CPPUNIT_ASSERT( Baz::GetNumber() == 2 );

        wxListBazs list2;
        list2 = list1;

        CPPUNIT_ASSERT( list1.GetCount() == 2 );
        CPPUNIT_ASSERT( list2.GetCount() == 2 );
        CPPUNIT_ASSERT( Baz::GetNumber() == 2 );

#if !wxUSE_STL
        list1.DeleteContents(true);
#else
        WX_CLEAR_LIST(wxListBazs, list1);
#endif
    }

    CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
}