summaryrefslogtreecommitdiffstats
path: root/src/util/list-container-test.h
blob: b46ed7b9ef54b84eda02210e22fca9b6b0d800f7 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2010 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <cxxtest/TestSuite.h>

#include <stdarg.h>
#include "util/list-container.h"

#define ARRAY_RANGE(array) (array), (array)+sizeof((array))/sizeof((array)[0])

static bool check_values(Inkscape::Util::ListContainer<int> const &c, unsigned n_values, ...) {
    bool ret = true;
    va_list args;
    va_start(args, n_values);
    Inkscape::Util::ListContainer<int>::const_iterator iter(c.begin());
    while ( n_values && iter != c.end() ) {
        int const value = va_arg(args, int);
        if ( value != *iter ) {
            ret = false;
        }
        if ( n_values == 1 && &c.back() != &*iter ) {
            ret = false;
        }
        n_values--;
        ++iter;
    }
    va_end(args);
    return ret && n_values == 0 && iter == c.end();
}

class ListContainerTest : public CxxTest::TestSuite {
public:
    ListContainerTest()
    {
        Inkscape::GC::init();
    }
    virtual ~ListContainerTest() {}

// createSuite and destroySuite get us per-suite setup and teardown
// without us having to worry about static initialization order, etc.
    static ListContainerTest *createSuite() { return new ListContainerTest(); }
    static void destroySuite( ListContainerTest *suite ) { delete suite; }

    void testRangeConstructor()
    {
        int const values[]={1,2,3,4};
        int const * const values_end=values+4;
        Inkscape::Util::ListContainer<int> container(values, values_end);

        Inkscape::Util::ListContainer<int>::iterator container_iter=container.begin();
        int const * values_iter=values;

        while ( values_iter != values_end && container_iter != container.end() ) {
            TS_ASSERT_EQUALS(*values_iter , *container_iter);
            ++values_iter;
            ++container_iter;
        }

        TS_ASSERT_EQUALS(values_iter , values_end);
        TS_ASSERT_EQUALS(container_iter , container.end());
    }

    void testEqualityTests()
    {
        int const a[] = { 1, 2, 3, 4 };
        int const b[] = { 1, 2, 3, 4 };
        int const c[] = { 1, 2, 3 };
        int const d[] = { 1, 2, 3, 5 };
        Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
        Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
        Inkscape::Util::ListContainer<int> c_c(ARRAY_RANGE(c));
        Inkscape::Util::ListContainer<int> c_d(ARRAY_RANGE(d));

        TS_ASSERT(c_a == c_b);
        TS_ASSERT(!( c_a != c_b ));
        TS_ASSERT(!( c_a == c_c ));
        TS_ASSERT(c_a != c_c);
        TS_ASSERT(!( c_a == c_d ));
        TS_ASSERT(c_a != c_d);
    }

    void testLessThan()
    {
        int const a[] = { 1, 2, 3, 4 };
        int const b[] = { 1, 2, 2, 4 };
        int const c[] = { 1, 2, 4, 4 };
        int const d[] = { 1, 2, 3 };
        Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
        Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
        Inkscape::Util::ListContainer<int> c_c(ARRAY_RANGE(c));
        Inkscape::Util::ListContainer<int> c_d(ARRAY_RANGE(d));
        TS_ASSERT(c_a >= c_b);
        TS_ASSERT(!( c_a < c_b ));
        TS_ASSERT(!( c_a >= c_c ));
        TS_ASSERT(c_a < c_c);
        TS_ASSERT(!( c_a < c_d ));
        TS_ASSERT(c_a >= c_d);
        TS_ASSERT(c_d < c_a);
    }

    void testAssignmentOperator()
    {
        int const a[] = { 1, 2, 3, 4 };
        Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
        Inkscape::Util::ListContainer<int> c_c;
        TS_ASSERT(c_a != c_c);
        c_c = c_a;
        TS_ASSERT(c_a == c_c);
        c_c = c_a;
        TS_ASSERT(c_a == c_c);
    }

    void testFillConstructor()
    {
        Inkscape::Util::ListContainer<int> filled((std::size_t)3, 2);
        TS_ASSERT(check_values(filled, 3, 2, 2, 2));
    }

    void testContainerSize()
    {
        // max_size() and size() return ListContainer<>::size_type which is unsigned int
        Inkscape::Util::ListContainer<int> empty;
        TS_ASSERT(empty.empty());
        TS_ASSERT_EQUALS(empty.size(), 0u);
        int const a[] = { 1, 2, 3 };
        Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
        TS_ASSERT(!c_a.empty());
        TS_ASSERT_EQUALS(c_a.size(), 3u);

        TS_ASSERT_LESS_THAN(0u, empty.max_size());
    }

    void testAppending()
    {
        Inkscape::Util::ListContainer<int> c;
        c.push_back(1);
        TS_ASSERT(check_values(c, 1, 1));
        c.push_back(2);
        TS_ASSERT(check_values(c, 2, 1, 2));
        c.push_back(3);
        TS_ASSERT(check_values(c, 3, 1, 2, 3));
    }

    void testBulkAppending()
    {
        int const a[] = { 1, 2, 3, 4 };
        int const b[] = { 5, 6, 7 };
        Inkscape::Util::ListContainer<int> c_a(ARRAY_RANGE(a));
        Inkscape::Util::ListContainer<int> c_b(ARRAY_RANGE(b));
        c_a.insert(c_a.end(), c_b.begin(), c_b.end());
        TS_ASSERT(check_values(c_a, 7, 1, 2, 3, 4, 5, 6, 7));
    }

    void testPrepending()
    {
        Inkscape::Util::ListContainer<int> c;
        c.push_front(1);
        TS_ASSERT(check_values(c, 1, 1));
        c.push_front(2);
        TS_ASSERT(check_values(c, 2, 2, 1));
        c.push_front(3);
        TS_ASSERT(check_values(c, 3, 3, 2, 1));
    }

    void testSingleValueInsertion()
    {
        Inkscape::Util::ListContainer<int> c;

        c.insert(c.begin(), 1);
        TS_ASSERT(check_values(c, 1, 1));

        c.insert(c.end(), 2);
        TS_ASSERT(check_values(c, 2, 1, 2));

        c.insert(c.begin(), 3);
        TS_ASSERT(check_values(c, 3, 3, 1, 2));

        Inkscape::Util::ListContainer<int>::iterator pos=c.begin();
        ++pos;
        c.insert(pos, 4);
        TS_ASSERT(check_values(c, 4, 3, 4, 1, 2));
    }

    void testSingleValueErasure()
    {
        int const values[] = { 1, 2, 3, 4 };
        Inkscape::Util::ListContainer<int> c(ARRAY_RANGE(values));

        c.erase(c.begin());
        TS_ASSERT(check_values(c, 3, 2, 3, 4));

        Inkscape::Util::ListContainer<int>::iterator pos=c.begin();
        ++pos;
        c.erase(pos);
        TS_ASSERT(check_values(c, 2, 2, 4));

        pos=c.begin();
        ++pos;
        c.erase(pos);
        TS_ASSERT(check_values(c, 1, 2));

        c.erase(c.begin());
        TS_ASSERT(check_values(c, 0));
    }

    void testPopFront()
    {
        int const full_ary[] = { 1, 2, 3 };
        Inkscape::Util::ListContainer<int> t(ARRAY_RANGE(full_ary));
        TS_ASSERT(check_values(t, 3,  1, 2, 3));
        TS_ASSERT_EQUALS(t.back() , 3);
        t.pop_front();
        TS_ASSERT(check_values(t, 2,  2, 3));
        TS_ASSERT_EQUALS(t.back() , 3);
        t.push_back(23);
        TS_ASSERT(check_values(t, 3,  2, 3, 23));
        TS_ASSERT_EQUALS(t.back() , 23);
        t.pop_front();
        TS_ASSERT(check_values(t, 2,  3, 23));
        TS_ASSERT_EQUALS(t.back() , 23);
        t.pop_front();
        TS_ASSERT(check_values(t, 1,  23));
        TS_ASSERT_EQUALS(t.back() , 23);
        t.pop_front();
        TS_ASSERT(check_values(t, 0));
        t.push_back(42);
        TS_ASSERT(check_values(t, 1,  42));
        TS_ASSERT_EQUALS(t.back() , 42);
    }

    void testEraseAfter()
    {
        int const full_ary[] = { 1, 2, 3, 4 };
        int const exp_ary[] = { 1, 3, 4 };
        Inkscape::Util::ListContainer<int> full_list(ARRAY_RANGE(full_ary));
        Inkscape::Util::ListContainer<int> exp_list(ARRAY_RANGE(exp_ary));
        TS_ASSERT(full_list != exp_list);
        full_list.erase_after(full_list.begin());
        TS_ASSERT(full_list == exp_list);
    }
};

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :