summaryrefslogtreecommitdiff
path: root/o3tl/qa/test-sorted_vector.cxx
blob: 3de3f005f6c62ac86c8bde7a67e1ca5f2775a53c (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
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <memory>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

#include <o3tl/sorted_vector.hxx>

using namespace ::o3tl;


// helper class
class SwContent
{
public:
    int x;

    explicit SwContent(int x_) : x(x_) {}

    bool operator<( const SwContent &rCmp) const
    {
        return x < rCmp.x;
    }
};

class sorted_vector_test : public CppUnit::TestFixture
{
public:
    void testBasics()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent> > aVec;

        // create 4 test elements
        std::unique_ptr<SwContent> p1( new SwContent(1) );
        std::unique_ptr<SwContent> p2( new SwContent(2) );
        SwContent *p3 = new SwContent(3);
        std::unique_ptr<SwContent> p4( new SwContent(4) );

        // insert p3, p1 -> not present -> second is true
        CPPUNIT_ASSERT( aVec.insert(p3).second );
        CPPUNIT_ASSERT( aVec.insert(p1.get()).second );
        // insert p3 again -> already present -> second is false
        CPPUNIT_ASSERT( !aVec.insert(p3).second );

        // 2 element should be present
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(2), aVec.size() );

        // check the order -> should be p1, p3
        // by index access
        CPPUNIT_ASSERT_EQUAL( p1.get(), aVec[0] );
        CPPUNIT_ASSERT_EQUAL( p3, aVec[1] );
        // by begin, end
        CPPUNIT_ASSERT_EQUAL( p1.get(), *aVec.begin() );
        CPPUNIT_ASSERT_EQUAL( p3, *(aVec.end()-1) );
        // by front, back
        CPPUNIT_ASSERT_EQUAL( p1.get(), aVec.front() );
        CPPUNIT_ASSERT_EQUAL( p3, aVec.back() );

        // find elements
        CPPUNIT_ASSERT( aVec.find(p1.get()) != aVec.end() );
        CPPUNIT_ASSERT_EQUAL( static_cast<std::ptrdiff_t>(0), aVec.find(p1.get()) - aVec.begin() );
        CPPUNIT_ASSERT( aVec.find(p3) != aVec.end() );
        CPPUNIT_ASSERT_EQUAL( static_cast<std::ptrdiff_t>(1), aVec.find(p3) - aVec.begin() );
        CPPUNIT_ASSERT( bool(aVec.find(p2.get()) == aVec.end()) );
        CPPUNIT_ASSERT( bool(aVec.find(p4.get()) == aVec.end()) );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1.get()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.size() );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.erase(p2.get()) );

        aVec.DeleteAndDestroyAll();
    }

    void testErase()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent> > aVec;
        SwContent *p1 = new SwContent(1);
        SwContent *p2 = new SwContent(2);
        SwContent *p3 = new SwContent(3);
        std::unique_ptr<SwContent> p4( new SwContent(4) );

        aVec.insert(p1);
        aVec.insert(p2);
        aVec.insert(p3);

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(2), aVec.size() );

        aVec.erase(1);
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.size() );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.erase(p4.get()) );

        aVec.clear();
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.size() );

        aVec.insert(p1);
        aVec.insert(p2);
        aVec.insert(p3);
        aVec.DeleteAndDestroyAll();
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.size() );
    }

    void testInsertRange()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent> > aVec1;
        std::unique_ptr<SwContent> p1( new SwContent(1) );
        std::unique_ptr<SwContent> p2( new SwContent(2) );
        std::unique_ptr<SwContent> p3( new SwContent(3) );

        aVec1.insert(p1.get());
        aVec1.insert(p2.get());
        aVec1.insert(p3.get());

        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent> > aVec2;
        aVec2.insert( aVec1 );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(3), aVec2.size() );
    }

    void testLowerBound()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent> > aVec;
        std::unique_ptr<SwContent> p1( new SwContent(1) );
        std::unique_ptr<SwContent> p2( new SwContent(2) );
        std::unique_ptr<SwContent> p3( new SwContent(3) );
        std::unique_ptr<SwContent> p4( new SwContent(4) );

        aVec.insert(p1.get());
        aVec.insert(p2.get());
        aVec.insert(p3.get());

        CPPUNIT_ASSERT( bool(aVec.lower_bound(p1.get()) == aVec.begin()) );
        CPPUNIT_ASSERT( bool(aVec.lower_bound(p4.get()) == aVec.end()) );
    }

    void testBasics_FindPtr()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent>,
            o3tl::find_partialorder_ptrequals> aVec;
        std::unique_ptr<SwContent> p1( new SwContent(1) );
        std::unique_ptr<SwContent> p2( new SwContent(2) );
        SwContent *p2_2 = new SwContent(2);
        std::unique_ptr<SwContent> p2_3( new SwContent(2) );
        SwContent *p2_4 = new SwContent(2);
        SwContent *p3 = new SwContent(3);
        std::unique_ptr<SwContent> p4( new SwContent(4) );

        CPPUNIT_ASSERT( aVec.insert(p3).second );
        CPPUNIT_ASSERT( aVec.insert(p1.get()).second );
        CPPUNIT_ASSERT( !aVec.insert(p3).second );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(2), aVec.size() );

        CPPUNIT_ASSERT_EQUAL( p1.get(), aVec[0] );
        CPPUNIT_ASSERT_EQUAL( p3, aVec[1] );

        CPPUNIT_ASSERT( aVec.insert(p2_2).second );
        CPPUNIT_ASSERT( aVec.insert(p2_3.get()).second );
        CPPUNIT_ASSERT( !aVec.insert(p2_2).second );
        CPPUNIT_ASSERT( aVec.insert(p2_4).second );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(5), aVec.size() );

        CPPUNIT_ASSERT_EQUAL( p1.get(), *aVec.begin() );
        CPPUNIT_ASSERT_EQUAL( p3, *(aVec.end()-1) );

        CPPUNIT_ASSERT_EQUAL( p1.get(), aVec.front() );
        CPPUNIT_ASSERT_EQUAL( p3, aVec.back() );

        CPPUNIT_ASSERT( aVec.find(p1.get()) != aVec.end() );
        CPPUNIT_ASSERT_EQUAL( static_cast<std::ptrdiff_t>(0), aVec.find(p1.get()) - aVec.begin() );
        CPPUNIT_ASSERT( aVec.find(p3) != aVec.end() );
        CPPUNIT_ASSERT_EQUAL( static_cast<std::ptrdiff_t>(4), aVec.find(p3) - aVec.begin() );
        CPPUNIT_ASSERT( bool(aVec.find(p2.get()) == aVec.end()) );
        CPPUNIT_ASSERT( bool(aVec.find(p4.get()) == aVec.end()) );
        CPPUNIT_ASSERT( aVec.find(p2_2) != aVec.end() );
        CPPUNIT_ASSERT( aVec.find(p2_2) - aVec.begin() >= 1 );
        CPPUNIT_ASSERT( aVec.find(p2_2) - aVec.begin() <  4 );
        CPPUNIT_ASSERT( aVec.find(p2_3.get()) != aVec.end() );
        CPPUNIT_ASSERT( aVec.find(p2_3.get()) - aVec.begin() >= 1 );
        CPPUNIT_ASSERT( aVec.find(p2_3.get()) - aVec.begin() <  4 );
        CPPUNIT_ASSERT( aVec.find(p2_4) != aVec.end() );
        CPPUNIT_ASSERT( aVec.find(p2_4) - aVec.begin() >= 1 );
        CPPUNIT_ASSERT( aVec.find(p2_4) - aVec.begin() <  4 );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1.get()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(4), aVec.size() );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.erase(p2.get()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p2_3.get()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(3), aVec.size() );

        aVec.DeleteAndDestroyAll();
    }

    void testErase_FindPtr()
    {
        o3tl::sorted_vector<SwContent*, o3tl::less_ptr_to<SwContent>,
            o3tl::find_partialorder_ptrequals> aVec;
        std::unique_ptr<SwContent> p1( new SwContent(1) );
        SwContent *p1_2 = new SwContent(1);
        std::unique_ptr<SwContent> p1_3( new SwContent(1) );
        SwContent *p2 = new SwContent(2);
        SwContent *p3 = new SwContent(3);
        std::unique_ptr<SwContent> p4( new SwContent(4) );

        aVec.insert(p1.get());
        aVec.insert(p2);
        aVec.insert(p3);

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1.get()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(2), aVec.size() );

        aVec.erase(1);
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.size() );

        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.erase(p4.get()) );

        aVec.clear();
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.size() );

        aVec.insert(p1.get());
        aVec.insert(p2);
        aVec.insert(p3);
        aVec.insert(p1_2);
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(4), aVec.size() );
        aVec.insert(p1_3.get());
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(5), aVec.size() );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1.get()) );
        CPPUNIT_ASSERT( bool(aVec.find(p1.get()) == aVec.end()) );
        CPPUNIT_ASSERT( aVec.find(p1_2) != aVec.end() );
        CPPUNIT_ASSERT( aVec.find(p1_3.get()) != aVec.end() );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(1), aVec.erase(p1_3.get()) );
        CPPUNIT_ASSERT( bool(aVec.find(p1.get()) == aVec.end()) );
        CPPUNIT_ASSERT( aVec.find(p1_2) != aVec.end() );
        CPPUNIT_ASSERT( bool(aVec.find(p1_3.get()) == aVec.end()) );
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.erase(p1_3.get()) );
        CPPUNIT_ASSERT( bool(aVec.find(p1.get()) == aVec.end()) );
        CPPUNIT_ASSERT( aVec.find(p1_2) != aVec.end() );
        CPPUNIT_ASSERT( bool(aVec.find(p1_3.get()) == aVec.end()) );

        aVec.DeleteAndDestroyAll();
        CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.size() );
    }


    // Change the following lines only, if you add, remove or rename
    // member functions of the current class,
    // because these macros are need by auto register mechanism.

    CPPUNIT_TEST_SUITE(sorted_vector_test);
    CPPUNIT_TEST(testBasics);
    CPPUNIT_TEST(testErase);
    CPPUNIT_TEST(testInsertRange);
    CPPUNIT_TEST(testLowerBound);
    CPPUNIT_TEST(testBasics_FindPtr);
    CPPUNIT_TEST(testErase_FindPtr);
    CPPUNIT_TEST_SUITE_END();
};


CPPUNIT_TEST_SUITE_REGISTRATION(sorted_vector_test);

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */