aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/deque_ut.cpp
blob: 38c421c7c7910e34c9ce187d7430efbee43a77bb (plain) (blame)
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
#include "deque.h"

#include <library/cpp/testing/unittest/registar.h>

#include <utility>
#include "yexception.h"

class TDequeTest: public TTestBase {
    UNIT_TEST_SUITE(TDequeTest);
    UNIT_TEST(TestConstructorsAndAssignments);
    UNIT_TEST(TestDeque1);
    UNIT_TEST(TestAt);
    UNIT_TEST(TestInsert);
    UNIT_TEST(TestErase);
    UNIT_TEST(TestAutoRef);
    UNIT_TEST_SUITE_END();

protected:
    void TestConstructorsAndAssignments();
    void TestDeque1();
    void TestInsert();
    void TestErase();
    void TestAt();
    void TestAutoRef();
};

UNIT_TEST_SUITE_REGISTRATION(TDequeTest);

void TDequeTest::TestConstructorsAndAssignments() {
    using container = TDeque<int>;

    container c1;
    c1.push_back(100);
    c1.push_back(200);

    container c2(c1);

    UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
    UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
    UNIT_ASSERT_VALUES_EQUAL(100, c1.at(0));
    UNIT_ASSERT_VALUES_EQUAL(200, c2.at(1));

    container c3(std::move(c1));

    UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
    UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
    UNIT_ASSERT_VALUES_EQUAL(100, c3.at(0));

    c2.push_back(300);
    c3 = c2;

    UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
    UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
    UNIT_ASSERT_VALUES_EQUAL(300, c3.at(2));

    c2.push_back(400);
    c3 = std::move(c2);

    UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
    UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
    UNIT_ASSERT_VALUES_EQUAL(400, c3.at(3));

    int array[] = {2, 3, 4};
    container c4 = {2, 3, 4};
    UNIT_ASSERT_VALUES_EQUAL(c4, container(std::begin(array), std::end(array)));
}

void TDequeTest::TestDeque1() {
    TDeque<int> d;
    UNIT_ASSERT(!d);

    d.push_back(4);
    d.push_back(9);
    d.push_back(16);
    d.push_front(1);

    UNIT_ASSERT(d);

    UNIT_ASSERT(d[0] == 1);
    UNIT_ASSERT(d[1] == 4);
    UNIT_ASSERT(d[2] == 9);
    UNIT_ASSERT(d[3] == 16);

    d.pop_front();
    d[2] = 25;

    UNIT_ASSERT(d[0] == 4);
    UNIT_ASSERT(d[1] == 9);
    UNIT_ASSERT(d[2] == 25);

    //Some compile time tests:
    TDeque<int>::iterator dit = d.begin();
    TDeque<int>::const_iterator cdit(d.begin());

    UNIT_ASSERT((dit - cdit) == 0);
    UNIT_ASSERT((cdit - dit) == 0);
    UNIT_ASSERT((dit - dit) == 0);
    UNIT_ASSERT((cdit - cdit) == 0);
    UNIT_ASSERT(!((dit < cdit) || (dit > cdit) || (dit != cdit) || !(dit <= cdit) || !(dit >= cdit)));
}

void TDequeTest::TestInsert() {
    TDeque<int> d;
    d.push_back(0);
    d.push_back(1);
    d.push_back(2);

    UNIT_ASSERT(d.size() == 3);

    TDeque<int>::iterator dit;

    //Insertion before begin:
    dit = d.insert(d.begin(), 3);
    UNIT_ASSERT(dit != d.end());
    UNIT_ASSERT(*dit == 3);
    UNIT_ASSERT(d.size() == 4);
    UNIT_ASSERT(d[0] == 3);

    //Insertion after begin:
    dit = d.insert(d.begin() + 1, 4);
    UNIT_ASSERT(dit != d.end());
    UNIT_ASSERT(*dit == 4);
    UNIT_ASSERT(d.size() == 5);
    UNIT_ASSERT(d[1] == 4);

    //Insertion at end:
    dit = d.insert(d.end(), 5);
    UNIT_ASSERT(dit != d.end());
    UNIT_ASSERT(*dit == 5);
    UNIT_ASSERT(d.size() == 6);
    UNIT_ASSERT(d[5] == 5);

    //Insertion before last element:
    dit = d.insert(d.end() - 1, 6);
    UNIT_ASSERT(dit != d.end());
    UNIT_ASSERT(*dit == 6);
    UNIT_ASSERT(d.size() == 7);
    UNIT_ASSERT(d[5] == 6);

    //Insertion of several elements before begin
    d.insert(d.begin(), 2, 7);
    UNIT_ASSERT(d.size() == 9);
    UNIT_ASSERT(d[0] == 7);
    UNIT_ASSERT(d[1] == 7);

    //Insertion of several elements after begin
    //There is more elements to insert than elements before insertion position
    d.insert(d.begin() + 1, 2, 8);
    UNIT_ASSERT(d.size() == 11);
    UNIT_ASSERT(d[1] == 8);
    UNIT_ASSERT(d[2] == 8);

    //There is less elements to insert than elements before insertion position
    d.insert(d.begin() + 3, 2, 9);
    UNIT_ASSERT(d.size() == 13);
    UNIT_ASSERT(d[3] == 9);
    UNIT_ASSERT(d[4] == 9);

    //Insertion of several elements at end:
    d.insert(d.end(), 2, 10);
    UNIT_ASSERT(d.size() == 15);
    UNIT_ASSERT(d[14] == 10);
    UNIT_ASSERT(d[13] == 10);

    //Insertion of several elements before last:
    //There is more elements to insert than elements after insertion position
    d.insert(d.end() - 1, 2, 11);
    UNIT_ASSERT(d.size() == 17);
    UNIT_ASSERT(d[15] == 11);
    UNIT_ASSERT(d[14] == 11);

    //There is less elements to insert than elements after insertion position
    d.insert(d.end() - 3, 2, 12);
    UNIT_ASSERT(d.size() == 19);
    UNIT_ASSERT(d[15] == 12);
    UNIT_ASSERT(d[14] == 12);
}

void TDequeTest::TestAt() {
    TDeque<int> d;
    TDeque<int> const& cd = d;

    d.push_back(10);
    UNIT_ASSERT(d.at(0) == 10);
    d.at(0) = 20;
    UNIT_ASSERT(cd.at(0) == 20);
    UNIT_ASSERT_EXCEPTION(d.at(1) = 20, std::out_of_range);
}

void TDequeTest::TestAutoRef() {
    int i;
    TDeque<int> ref;
    for (i = 0; i < 5; ++i) {
        ref.push_back(i);
    }

    TDeque<TDeque<int>> d_d_int(1, ref);
    d_d_int.push_back(d_d_int[0]);
    d_d_int.push_back(ref);
    d_d_int.push_back(d_d_int[0]);
    d_d_int.push_back(d_d_int[0]);
    d_d_int.push_back(ref);

    for (i = 0; i < 5; ++i) {
        UNIT_ASSERT(d_d_int[i] == ref);
    }
}

void TDequeTest::TestErase() {
    TDeque<int> dint;
    dint.push_back(3);
    dint.push_front(2);
    dint.push_back(4);
    dint.push_front(1);
    dint.push_back(5);
    dint.push_front(0);
    dint.push_back(6);

    TDeque<int>::iterator it(dint.begin() + 1);
    UNIT_ASSERT(*it == 1);

    dint.erase(dint.begin());
    UNIT_ASSERT(*it == 1);

    it = dint.end() - 2;
    UNIT_ASSERT(*it == 5);

    dint.erase(dint.end() - 1);
    UNIT_ASSERT(*it == 5);

    dint.push_back(6);
    dint.push_front(0);

    it = dint.begin() + 2;
    UNIT_ASSERT(*it == 2);

    dint.erase(dint.begin(), dint.begin() + 2);
    UNIT_ASSERT(*it == 2);

    it = dint.end() - 3;
    UNIT_ASSERT(*it == 4);

    dint.erase(dint.end() - 2, dint.end());
    UNIT_ASSERT(*it == 4);
}