-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveb_test.cpp
More file actions
217 lines (188 loc) · 3.92 KB
/
veb_test.cpp
File metadata and controls
217 lines (188 loc) · 3.92 KB
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
#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include "catch.hpp"
#include "veb.h"
#include <random>
#include <algorithm>
#define LOG2_U 20
namespace
{
std::vector<doo::u64> gen_permut(doo::u64 start, doo::u64 end)
{
std::vector<doo::u64> out;
out.reserve(end - start);
for (doo::u64 i = start; i <= end; i++)
{
out.push_back(i);
}
auto dre = std::default_random_engine{};
std::shuffle(out.begin(), out.end(), dre);
return out;
}
void insert(doo::veb<LOG2_U>& v, const std::vector<doo::u64>& to_insert)
{
for (doo::u64 key : to_insert)
{
v.insert(key);
}
}
void del(doo::veb<LOG2_U>& v, const std::vector<doo::u64>& to_delete)
{
for (doo::u64 key : to_delete)
{
v.del(key);
}
}
std::vector<doo::u64> sort(doo::veb<LOG2_U>& v, const std::vector<doo::u64>& to_sort)
{
std::vector<doo::u64> sorted;
insert(v, to_sort);
sorted.reserve(to_sort.size());
auto temp = v.get_min();
while (temp.has_value())
{
sorted.push_back(temp.value());
temp = v.succ(temp.value());
}
return sorted;
}
}
TEST_CASE("Insert & Delete")
{
SECTION("Insert and Delete Min")
{
doo::veb<LOG2_U> v;
int end = 500000;
int start = 0;
auto to_insert = gen_permut(start, end);
insert(v, to_insert);
for (int i = start; i <= end; i++)
{
auto min = v.remove_min();
REQUIRE(min.has_value());
REQUIRE(min.value() == i);
}
}
SECTION("Duplicate Insert")
{
doo::veb<LOG2_U> v;
v.insert_if_not_exists(0);
v.insert_if_not_exists(0);
v.insert_if_not_exists(2);
v.insert_if_not_exists(2);
v.insert_if_not_exists(1);
v.insert_if_not_exists(1);
REQUIRE(v.member(0));
REQUIRE(v.member(1));
REQUIRE(v.member(2));
v.del(0);
v.del(2);
v.del(1);
REQUIRE(!v.member(0));
REQUIRE(!v.member(1));
REQUIRE(!v.member(2));
}
}
TEST_CASE("Delete")
{
doo::veb<LOG2_U> v;
int start = 0; int end = 500000;
auto to_insert = gen_permut(start, end);
insert(v, to_insert);
del(v, to_insert);
REQUIRE(v.empty());
}
TEST_CASE("Successor")
{
doo::veb<LOG2_U> v;
v.insert(0);
v.insert(15);
v.insert(16);
auto s = v.succ(4);
REQUIRE(s.has_value());
REQUIRE(s.value() == 15);
s = v.succ(0);
REQUIRE(s.has_value());
REQUIRE(s.value() == 15);
s = v.succ(16);
REQUIRE(!s.has_value());
s = v.succ(17);
REQUIRE(!s.has_value());
}
TEST_CASE("Predecessor")
{
doo::veb<LOG2_U> v;
v.insert(1);
v.insert(15);
v.insert(16);
auto s = v.pred(13);
REQUIRE(s.has_value());
REQUIRE(s.value() == 1);
s = v.pred(16);
REQUIRE(s.has_value());
REQUIRE(s.value() == 15);
s = v.pred(200);
REQUIRE(s.has_value());
REQUIRE(s.value() == 16);
s = v.pred(346);
REQUIRE(s.has_value());
REQUIRE(s.value() == 16);
s = v.pred(1);
REQUIRE(!s.has_value());
}
TEST_CASE("Member")
{
doo::veb<LOG2_U> v;
REQUIRE(!v.member(0));
v.insert(10);
v.insert(3);
v.insert(2);
v.insert(1);
v.insert(0);
REQUIRE(v.member(3));
REQUIRE(v.member(2));
REQUIRE(v.member(1));
REQUIRE(v.member(0));
REQUIRE(!v.member(5));
REQUIRE(!v.member(7));
}
TEST_CASE("Sorting")
{
auto ti = gen_permut(0, 500000);
// sort the permutation for asserting that veb sort worked
std::vector<doo::u64> sorted = ti;
std::sort(sorted.begin(), sorted.end());
doo::veb<LOG2_U> v;
auto vebSorted = sort(v, ti);
REQUIRE(sorted.size() == vebSorted.size());
REQUIRE(sorted == vebSorted);
}
TEST_CASE("Benchmark")
{
// generate a permultation of random ids for testing
auto ti = gen_permut(0, 500000);
// sort the permutation for asserting that veb sort worked
std::vector<doo::u64> sorted = ti;
std::sort(sorted.begin(), sorted.end());
BENCHMARK("Insert Performance")
{
doo::veb<LOG2_U> v;
REQUIRE(v.empty());
insert(v, ti);
REQUIRE(!v.empty());
};
BENCHMARK("Insert & Delete Performance")
{
doo::veb<LOG2_U> v;
REQUIRE(v.empty());
insert(v, ti);
REQUIRE(!v.empty());
del(v, ti);
REQUIRE(v.empty());
};
BENCHMARK("Sort Performance")
{
doo::veb<LOG2_U> v;
auto vebSorted = sort(v, ti);
};
}