-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.cpp
278 lines (220 loc) · 8.09 KB
/
test.cpp
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
272
273
274
275
276
277
278
#include <iostream>
#include <cstdlib>
#include <thread>
#include <ctime>
#include <ucset/consistent_set.hpp>
#include <ucset/consistent_avl.hpp>
#include <gtest/gtest.h>
using namespace unum::ucset;
constexpr std::size_t size = 128;
struct pair_t {
std::size_t key;
std::size_t value;
pair_t(std::size_t key = 0, std::size_t value = 0) noexcept : key(key), value(value) {}
explicit operator std::size_t() const noexcept { return key; }
operator bool() const noexcept { return key != -1; }
};
struct pair_compare_t {
using value_type = std::size_t;
bool operator()(pair_t a, pair_t b) const noexcept { return a.key < b.key; }
bool operator()(std::size_t a, pair_t b) const noexcept { return a < b.key; }
bool operator()(pair_t a, std::size_t b) const noexcept { return a.key < b; }
};
using stl_t = consistent_set_gt<pair_t, pair_compare_t>;
using avl_t = consistent_avl_gt<pair_t, pair_compare_t>;
using id_stl_t = typename stl_t::identifier_t;
using id_avl_t = typename avl_t::identifier_t;
template <typename cont_t>
void test_with_threads(std::size_t threads_count) {
auto cont = *cont_t::make();
std::vector<std::thread> threads;
threads.reserve(threads_count);
auto upsert = [&](std::size_t offset, std::size_t length) {
for (std::size_t idx = offset; idx < length; ++idx)
EXPECT_TRUE(cont.upsert(pair_t {idx, idx}));
};
std::size_t shift = (size / threads_count);
for (std::size_t idx = 0; idx < threads_count; ++idx)
threads.push_back(std::thread(upsert, idx * shift, idx * shift + shift));
for (std::size_t idx = 0; idx < threads_count; ++idx)
threads[idx].join();
EXPECT_EQ(cont.size(), size);
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(cont.find(idx, [](auto const&) noexcept {}));
}
TEST(upsert_and_find_set, with_threads) {
test_with_threads<stl_t>(2);
test_with_threads<stl_t>(4);
test_with_threads<stl_t>(8);
test_with_threads<stl_t>(16);
test_with_threads<avl_t>(2);
test_with_threads<avl_t>(4);
test_with_threads<avl_t>(8);
test_with_threads<avl_t>(16);
}
TEST(upsert_and_find_set, ascending) {
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx) {
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
EXPECT_TRUE(set.find(idx, [](auto const&) noexcept {}));
}
EXPECT_EQ(set.size(), size);
}
TEST(upsert_and_find_set, descending) {
auto set = *stl_t::make();
for (std::size_t idx = size; idx > 0; --idx) {
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
EXPECT_TRUE(set.find(idx, [](auto const&) noexcept {}));
}
EXPECT_EQ(set.size(), size);
}
TEST(upsert_and_find_set, random) {
std::srand(std::time(nullptr));
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx) {
std::size_t val = std::rand();
EXPECT_TRUE(set.upsert(pair_t {val, val}));
EXPECT_TRUE(set.find(val, [](auto const&) noexcept {}));
}
}
TEST(upsert_and_find_avl, ascending) {
auto avl = *avl_t::make();
for (std::size_t idx = 0; idx < size; ++idx) {
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
EXPECT_TRUE(avl.find(idx, [](auto const&) noexcept {}));
}
EXPECT_EQ(avl.size(), size);
}
TEST(upsert_and_find_avl, descending) {
auto avl = *avl_t::make();
for (std::size_t idx = size; idx > 0; --idx) {
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
EXPECT_TRUE(avl.find(idx, [](auto const&) noexcept {}));
}
EXPECT_EQ(avl.size(), size);
}
TEST(upsert_and_find_avl, random) {
std::srand(std::time(nullptr));
auto avl = *avl_t::make();
for (std::size_t idx = 0; idx < size; ++idx) {
std::size_t val = std::rand();
EXPECT_TRUE(avl.upsert(pair_t {val, val}));
EXPECT_TRUE(avl.find(val, [](auto const&) noexcept {}));
}
}
TEST(upsert_and_find_avl, iterators) {
std::vector<pair_t> vec(size);
auto avl = *avl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
vec[idx] = pair_t {idx, idx};
EXPECT_TRUE(avl.upsert(vec.begin(), vec.end()));
EXPECT_EQ(avl.size(), size);
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(avl.find(idx, [](auto const&) noexcept {}));
}
TEST(upsert_and_find_set, iterators) {
std::vector<pair_t> vec(size);
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
vec[idx] = pair_t {idx, idx};
EXPECT_TRUE(set.upsert(std::make_move_iterator(vec.begin()), std::make_move_iterator(vec.end())));
EXPECT_EQ(set.size(), size);
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(set.find(idx, [](auto const&) noexcept {}));
}
TEST(test_set, range) {
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
for (std::size_t idx = 0; idx < size; idx += 8) {
std::size_t val = idx;
EXPECT_TRUE(set.range(idx, idx + 7, [&](auto const& rhs) noexcept {
EXPECT_EQ(val, rhs.key);
++val;
}));
}
}
TEST(test_avl, range) {
auto avl = *avl_t::make();
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
bool state = false;
for (std::size_t idx = 0; idx < size; idx += 8) {
EXPECT_TRUE(avl.range(idx, idx + 7, [&](auto const& rhs) noexcept {
EXPECT_TRUE(set.upsert(pair_t {rhs.key, rhs.value}));
}));
for (std::size_t i = idx; i < idx + 8; ++i) {
EXPECT_TRUE(set.find(i, [&](auto const&) noexcept { state = true; }));
EXPECT_TRUE(state);
state = false;
}
EXPECT_TRUE(set.clear());
}
}
TEST(test_set, erase) {
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
bool state = true;
for (std::size_t idx = 0; idx < size; idx += 10) {
EXPECT_TRUE(set.erase_range(idx, idx + 10, [](auto const&) noexcept {}));
for (std::size_t i = idx; i < idx + 10; ++i) {
EXPECT_TRUE(set.find(i, [&](auto const&) noexcept { state = false; }));
EXPECT_TRUE(state);
}
}
}
TEST(test_avl, erase) {
auto avl = *avl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
bool state = true;
for (std::size_t idx = 0; idx < size; idx += 10) {
EXPECT_TRUE(avl.erase_range(idx, idx + 10, [](auto const&) noexcept {}));
for (std::size_t i = idx; i < idx + 10; ++i) {
EXPECT_TRUE(avl.find(i, [&](auto const&) noexcept {
state = false; }));
EXPECT_TRUE(state);
}
}
}
TEST(test_avl, upper_bound) {
auto avl = *avl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
for (std::size_t idx = 0; idx < size - 1; ++idx) {
EXPECT_TRUE(avl.upper_bound(idx, [&](auto const& rhs) noexcept { EXPECT_TRUE(pair_compare_t {}(idx, rhs)); }));
}
}
TEST(test_set, upper_bound) {
auto set = *stl_t::make();
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
for (std::size_t idx = 0; idx < size - 1; ++idx) {
EXPECT_TRUE(set.upper_bound(idx, [&](auto const& rhs) noexcept { EXPECT_TRUE(pair_compare_t {}(idx, rhs)); }));
}
}
TEST(test_set, reserve_clear) {
auto set = *stl_t::make();
EXPECT_TRUE(set.reserve(size));
EXPECT_EQ(set.size(), 0);
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(set.upsert(pair_t {idx, idx}));
EXPECT_EQ(set.size(), size);
EXPECT_TRUE(set.clear());
EXPECT_EQ(set.size(), 0);
}
TEST(test_avl, clear) {
auto avl = *avl_t::make();
EXPECT_EQ(avl.size(), 0);
for (std::size_t idx = 0; idx < size; ++idx)
EXPECT_TRUE(avl.upsert(pair_t {idx, idx}));
EXPECT_EQ(avl.size(), size);
EXPECT_TRUE(avl.clear());
EXPECT_EQ(avl.size(), 0);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}