forked from simongog/sdsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdac_vector_test.cpp
194 lines (166 loc) · 5.67 KB
/
dac_vector_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
#include <algorithm>
#include <chrono>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "sdsl/dac_vector.hpp"
#include "sdsl/util.hpp"
namespace
{
typedef sdsl::int_vector<>::size_type size_type;
typedef sdsl::int_vector<>::value_type value_type;
void run_test(const std::vector<value_type>& vec) {
{
sdsl::dac_vector<> v(vec);
std::stringstream ss;
v.serialize(ss);
std::cout << " old = " << ss.str().size() << std::endl;
sdsl::dac_vector<> w;
w.load(ss);
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], w[i]);
}
size_t last_levels = -1;
for (int max_levels = 1; max_levels <= 32; max_levels *= 2) {
sdsl::dac_vector_dp<> v(vec, max_levels);
if (v.levels() == last_levels)
break;
last_levels = v.levels();
std::stringstream ss;
v.serialize(ss);
std::cout << " new (plain) with " << v.levels() << " levels = "
<< ss.str().size() << std::endl;
sdsl::dac_vector_dp<> w;
w.load(ss);
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], w[i]);
// test move
sdsl::dac_vector_dp<> z = std::move(w);
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], z[i]);
// test copy
w = z;
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], w[i]);
}
for (int max_levels = 1; max_levels <= 32; max_levels *= 2) {
sdsl::dac_vector_dp<sdsl::rrr_vector<>> v(vec, max_levels);
if (v.levels() == last_levels)
break;
last_levels = v.levels();
std::stringstream ss;
v.serialize(ss);
std::cout << " new (rrr) with " << v.levels() << " levels = "
<< ss.str().size() << std::endl;
sdsl::dac_vector_dp<sdsl::rrr_vector<>> w;
w.load(ss);
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], w[i]);
// test move
sdsl::dac_vector_dp<> z = std::move(w);
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], z[i]);
// test copy
w = z;
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], w[i]);
}
}
TEST(DacVectorTest, SmokeTest) {
std::vector<value_type> vec { 1, 3, 5, 7, 100, 2000000000 };
sdsl::dac_vector_dp<> v(vec, 3);
ASSERT_EQ(3ul, v.levels());
for (size_t i = 0; i < vec.size(); ++i)
ASSERT_EQ(vec[i], v[i]);
}
TEST(DacVectorTest, SmokeTestLarge) {
std::vector<value_type> vec, add { 1, 3, 5, 7, 100, 2000000000 };
for (int i = 0; i < 50; ++i)
std::copy(add.begin(), add.end(), std::back_inserter(vec));
run_test(vec);
}
TEST(DacVectorTest, LargeRandom) {
for (int i = 0; i < 10; ++i) {
std::cout << "random uniform test " << i << std::endl;
std::vector<value_type> vec;
std::mt19937_64 rng;
{
std::uniform_int_distribution<uint64_t> distribution(0, 100000000);
auto dice = bind(distribution, rng);
for (size_t i=0; i < 100000; ++i)
vec.push_back(dice());
}
run_test(vec);
}
for (int i = 0; i < 10; ++i) {
std::cout << "random exponential test " << i << std::endl;
std::vector<value_type> vec;
std::mt19937_64 rng;
{
std::exponential_distribution<double> dist(3.5);
auto dice = bind(dist, rng);
for (size_t i = 0; i < 100000; ++i)
vec.push_back(uint64_t(dice() * 1000000));
}
run_test(vec);
}
}
template <typename F>
uint64_t measure(F f) {
using timer = std::chrono::high_resolution_clock;
auto start = timer::now();
f();
return std::chrono::duration_cast<std::chrono::microseconds>(
timer::now() - start).count();
}
TEST(DacVectorTest, LargeRandomBench) {
for (size_t sz : {10000, 100000, 1000000, 10000000}) {
size_t queries = 100000;
std::vector<value_type> vec;
std::vector<size_t> query_indexes;
std::mt19937_64 rng;
{
std::exponential_distribution<double> dist(3.5);
auto dice = bind(dist, rng);
for (size_t i=0; i < sz; ++i)
vec.push_back(uint64_t(dice() * 1000000));
}
{
std::uniform_int_distribution<uint64_t> distribution(0, sz - 1);
auto dice = bind(distribution, rng);
for (size_t i=0; i < queries; ++i)
query_indexes.push_back(dice());
}
sdsl::dac_vector<> v1(vec);
sdsl::dac_vector_dp<> v2(vec);
sdsl::dac_vector_dp<sdsl::rrr_vector<>> v3(vec);
std::cout << "benchmark size " << sz << std::endl;
std::cout << " dac_vector levels " << v1.levels() << std::endl;
std::cout << " dac_vector_dp levels " << v2.levels() << std::endl;
size_t s1=0;
std::cout << " time dac_vector " << measure([&]() {
for (size_t query : query_indexes)
s1+=v1[query];
}) << std::endl;
size_t s2=0;
std::cout << " time dac_vector_dp " << measure([&]() {
for (size_t query : query_indexes)
s2+=v2[query];
}) << std::endl;
size_t s3=0;
std::cout << " time dac_vector_dp<rrr_vector<>> " << measure([&]() {
for (size_t query : query_indexes)
s3+=v3[query];
}) << std::endl;
ASSERT_EQ(s1, s2);
ASSERT_EQ(s1, s3);
}
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}