-
Notifications
You must be signed in to change notification settings - Fork 3
/
k2_treap_test.cpp
274 lines (255 loc) · 8.26 KB
/
k2_treap_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
#include "sdsl/k2_treap.hpp"
#include "sdsl/bit_vectors.hpp"
#include "gtest/gtest.h"
#include <vector>
#include <tuple>
#include <string>
#include <algorithm> // for std::min. std::sort
#include <random>
namespace
{
using namespace sdsl;
using namespace std;
typedef int_vector<>::size_type size_type;
string test_file;
string temp_file;
bool in_memory;
template<class T>
class k2_treap_test : public ::testing::Test { };
using testing::Types;
typedef Types<
k2_treap<2, bit_vector>,
k2_treap<2, rrr_vector<63>>,
k2_treap<3, bit_vector>,
k2_treap<4, rrr_vector<63>>,
k2_treap<5, rrr_vector<63>>,
k2_treap<6, rrr_vector<63>>,
k2_treap<16, rrr_vector<63>>
> Implementations;
TYPED_TEST_CASE(k2_treap_test, Implementations);
TYPED_TEST(k2_treap_test, CreateAndStoreTest)
{
TypeParam k2treap;
construct(k2treap, test_file);
ASSERT_TRUE(store_to_file(k2treap, temp_file));
}
template<class t_k2treap>
void topk_test(
const t_k2treap& k2treap,
complex<uint64_t> min_xy,
complex<uint64_t> max_xy,
const int_vector<>& x,
const int_vector<>& y,
const int_vector<>& w)
{
auto res_it = top_k(k2treap, {real(min_xy),imag(min_xy)}, {real(max_xy),imag(max_xy)});
typedef tuple<uint64_t, uint64_t, uint64_t> t_xyw;
vector<t_xyw> vec;
for (uint64_t i = 0; i < x.size(); ++i) {
if (x[i] >= real(min_xy) and x[i] <= real(max_xy)
and y[i] >= imag(min_xy) and y[i] <= imag(max_xy)) {
vec.emplace_back(x[i], y[i], w[i]);
}
}
sort(vec.begin(), vec.end(), [](const t_xyw& a, const t_xyw& b) {
if (get<2>(a) != get<2>(b))
return get<2>(a) > get<2>(b);
else if (get<0>(a) != get<0>(b))
return get<0>(a) < get<0>(b);
return get<1>(a) < get<1>(b);
});
uint64_t cnt = 0;
while (res_it) {
ASSERT_TRUE(cnt < vec.size());
auto p = *res_it;
ASSERT_EQ(get<2>(vec[cnt]), p.second);
ASSERT_EQ(get<0>(vec[cnt]), real(p.first));
ASSERT_EQ(get<1>(vec[cnt]), imag(p.first));
++res_it;
++cnt;
}
ASSERT_FALSE(res_it);
}
TYPED_TEST(k2_treap_test, size_and_top_k)
{
TypeParam k2treap;
ASSERT_TRUE(load_from_file(k2treap, temp_file));
int_vector<> x,y,w;
ASSERT_TRUE(load_from_file(x, test_file+".x"));
ASSERT_TRUE(load_from_file(y, test_file+".y"));
ASSERT_EQ(x.size(), y.size());
ASSERT_TRUE(load_from_file(w, test_file+".w"));
ASSERT_EQ(x.size(), w.size());
ASSERT_EQ(x.size(), k2treap.size());
uint64_t maxx=0, maxy=0;
if (x.size() > 0) {
maxx = *max_element(x.begin(), x.end());
maxy = *max_element(y.begin(), y.end());
}
uint64_t minx=0, miny=0;
topk_test(k2treap, {minx,maxx}, {miny,maxy}, x, y, w);
if (x.size() > 0) {
std::mt19937_64 rng;
std::uniform_int_distribution<uint64_t> distribution(0, x.size()-1);
auto dice = bind(distribution, rng);
for (size_t i=0; i<20; ++i) {
auto idx = dice();
uint64_t xx = x[idx];
uint64_t yy = y[idx];
uint64_t dd = 20;
uint64_t minx=0, miny=0, maxx=xx+dd, maxy=yy+dd;
if (xx >= dd)
minx = xx - dd;
if (yy >= dd)
miny = yy - dd;
topk_test(k2treap, {minx, miny}, {maxx,maxy}, x, y, w);
}
}
}
template<class t_k2treap>
void range3d_test(
const t_k2treap& k2treap,
complex<uint64_t> min_xy,
complex<uint64_t> max_xy,
complex<uint64_t> z,
const int_vector<>& x,
const int_vector<>& y,
const int_vector<>& w)
{
auto res_it = range_3d(k2treap, {real(min_xy),imag(min_xy)},
{real(max_xy),imag(max_xy)},
{real(z), imag(z)});
typedef tuple<uint64_t, uint64_t, uint64_t> t_xyw;
vector<t_xyw> vec;
for (uint64_t i = 0; i < x.size(); ++i) {
if (x[i] >= real(min_xy) and x[i] <= real(max_xy)
and y[i] >= imag(min_xy) and y[i] <= imag(max_xy)) {
vec.emplace_back(x[i], y[i], w[i]);
}
}
sort(vec.begin(), vec.end(), [](const t_xyw& a, const t_xyw& b) {
if (get<2>(a) != get<2>(b))
return get<2>(a) > get<2>(b);
else if (get<0>(a) != get<0>(b))
return get<0>(a) < get<0>(b);
return get<1>(a) < get<1>(b);
});
uint64_t cnt = 0;
while (res_it) {
ASSERT_TRUE(cnt < vec.size());
auto p = *res_it;
ASSERT_EQ(get<2>(vec[cnt]), p.second);
ASSERT_EQ(get<0>(vec[cnt]), real(p.first));
ASSERT_EQ(get<1>(vec[cnt]), imag(p.first));
++res_it;
++cnt;
}
ASSERT_FALSE(res_it);
}
TYPED_TEST(k2_treap_test, range_3d)
{
TypeParam k2treap;
ASSERT_TRUE(load_from_file(k2treap, temp_file));
int_vector<> x,y,w;
ASSERT_TRUE(load_from_file(x, test_file+".x"));
ASSERT_TRUE(load_from_file(y, test_file+".y"));
ASSERT_EQ(x.size(), y.size());
ASSERT_TRUE(load_from_file(w, test_file+".w"));
ASSERT_EQ(x.size(), w.size());
ASSERT_EQ(x.size(), k2treap.size());
if (x.size() > 0) {
std::mt19937_64 rng;
std::uniform_int_distribution<uint64_t> distribution(0, x.size()-1);
auto dice = bind(distribution, rng);
for (size_t i=0; i<20; ++i) {
auto idx = dice();
uint64_t xx = x[idx];
uint64_t yy = y[idx];
uint64_t ww = w[idx];
uint64_t dd = 20;
uint64_t dw = 100;
uint64_t minx=0, miny=0, maxx=xx+dd, maxy=yy+dd, minw=0, maxw=ww+dw;
if (xx >= dd)
minx = xx - dd;
if (yy >= dd)
miny = yy - dd;
if (ww >= dw)
minw = ww - dw;
range3d_test(k2treap, {minx, miny}, {maxx,maxy}, {minw,maxw}, x, y, w);
}
}
}
template<class t_k2treap>
void count_test(
const t_k2treap& k2treap,
complex<uint64_t> min_xy,
complex<uint64_t> max_xy,
const int_vector<>& x,
const int_vector<>& y)
{
uint64_t cnt = 0;
for (uint64_t i = 0; i < x.size(); ++i) {
if (x[i] >= real(min_xy) and x[i] <= real(max_xy)
and y[i] >= imag(min_xy) and y[i] <= imag(max_xy)) {
++cnt;
}
}
ASSERT_EQ(cnt, count(k2treap, {real(min_xy),imag(min_xy)}, {real(max_xy),imag(max_xy)}));
}
TYPED_TEST(k2_treap_test, count)
{
TypeParam k2treap;
ASSERT_TRUE(load_from_file(k2treap, temp_file));
int_vector<> x,y;
ASSERT_TRUE(load_from_file(x, test_file+".x"));
ASSERT_TRUE(load_from_file(y, test_file+".y"));
ASSERT_EQ(x.size(), y.size());
ASSERT_EQ(x.size(), k2treap.size());
if (x.size() > 0) {
std::mt19937_64 rng;
std::uniform_int_distribution<uint64_t> distribution(0, x.size()-1);
auto dice = bind(distribution, rng);
for (size_t i=0; i<3; ++i) {
auto idx1 = dice();
auto idx2 = dice();
uint64_t x1 = x[idx1];
uint64_t y1 = y[idx1];
uint64_t x2 = x[idx2];
uint64_t y2 = y[idx2];
count_test(k2treap, {std::min(x1,x2), std::min(y1,y2)}, {std::max(x1,x2),std::max(y1,y2)}, x, y);
}
}
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
if (argc < 3) {
// LCOV_EXCL_START
cout << "Usage: " << argv[0] << " file temp_file [in-memory]" << endl;
cout << " (1) Generates a k2-treap out of file.x, file.y, and file.w." << endl;
cout << " Result is stored in temp_file." << endl;
cout << " If `in-memory` is specified, the in-memory construction is tested." << endl;
cout << " (2) Performs tests." << endl;
cout << " (3) Deletes temp_file." << endl;
return 1;
// LCOV_EXCL_STOP
}
test_file = argv[1];
temp_file = argv[2];
in_memory = argc > 3;
if (in_memory) {
auto load_and_store_in_mem = [&](string suf) {
int_vector<> data;
string file = temp_file + suf;
load_vector_from_file(data,file);
string ram_file = ram_file_name(file);
store_to_file(data, ram_file);
};
load_and_store_in_mem("x");
load_and_store_in_mem("y");
load_and_store_in_mem("w");
temp_file = ram_file_name(temp_file);
}
return RUN_ALL_TESTS();
}