forked from simongog/sdsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn_dict_dynamic_test.cpp
164 lines (144 loc) · 4.35 KB
/
nn_dict_dynamic_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
#include "sdsl/nn_dict_dynamic.hpp"
#include "sdsl/nearest_neighbour_dictionary.hpp"
#include "sdsl/util.hpp"
#include "gtest/gtest.h"
#include <string>
#include <random>
namespace
{
std::string temp_dir;
// The fixture for testing class nn_dict_dynamic.
class nn_dict_dynamic_test : public ::testing::Test
{
protected:
nn_dict_dynamic_test() {}
virtual ~nn_dict_dynamic_test() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
void compare_bv_and_nndd(const sdsl::bit_vector& bv, const sdsl::nn_dict_dynamic& nndd)
{
sdsl::nearest_neighbour_dictionary<32> exp(bv);
uint64_t first_one = exp.select(1);
uint64_t last_one = exp.select(exp.rank(exp.size()));
for (uint64_t i=0; i<first_one; ++i) {
ASSERT_EQ(exp.size(), nndd.prev(i));
ASSERT_EQ(exp.next(i), nndd.next(i));
}
for (uint64_t i=first_one; i<=last_one; ++i) {
ASSERT_EQ(exp.prev(i), nndd.prev(i));
ASSERT_EQ(exp.next(i), nndd.next(i));
}
for (uint64_t i=last_one+1; i<exp.size(); ++i) {
ASSERT_EQ(exp.prev(i), nndd.prev(i));
ASSERT_EQ(exp.size(), nndd.next(i));
}
}
//! Test Constructors
TEST_F(nn_dict_dynamic_test, constructors)
{
static_assert(sdsl::util::is_regular<sdsl::nn_dict_dynamic>::value, "Type is not regular");
uint64_t testsize = 100000;
sdsl::bit_vector bv(testsize, 0);
sdsl::nn_dict_dynamic nndd(testsize);
// Fill nndd
std::mt19937_64 rng;
std::uniform_int_distribution<uint64_t> distribution(0, testsize-1);
auto dice = bind(distribution, rng);
for (uint64_t i=0; i<testsize/4; ++i) {
uint64_t value = dice();
if (bv[value]) {
bv[value] = 0;
nndd[value] = 0;
} else {
bv[value] = 1;
nndd[value] = 1;
}
}
// Copy-constructor
sdsl::nn_dict_dynamic nndd2(nndd);
compare_bv_and_nndd(bv, nndd2);
// Move-constructor
sdsl::nn_dict_dynamic nndd3(std::move(nndd2));
compare_bv_and_nndd(bv, nndd3);
ASSERT_EQ((uint64_t)0, nndd2.size());
// Copy-Assign
sdsl::nn_dict_dynamic nndd4;
nndd4 = nndd3;
compare_bv_and_nndd(bv, nndd4);
// Move-Assign
sdsl::nn_dict_dynamic nndd5;
nndd5 = std::move(nndd4);
compare_bv_and_nndd(bv, nndd5);
ASSERT_EQ((uint64_t)0, nndd4.size());
}
//! Test Operations next and prev
TEST_F(nn_dict_dynamic_test, next_and_prev)
{
uint64_t testsize = 100000;
sdsl::bit_vector bv(testsize, 0);
sdsl::nn_dict_dynamic nndd(testsize);
for (uint64_t ones=1; ones<testsize; ones *= 2) {
std::mt19937_64 rng(ones);
std::uniform_int_distribution<uint64_t> distribution(0, testsize-1);
auto dice = bind(distribution, rng);
for (uint64_t i=0; i<ones; ++i) {
uint64_t value = dice();
if (bv[value]) {
bv[value] = 0;
nndd[value] = 0;
} else {
bv[value] = 1;
nndd[value] = 1;
}
}
bv[testsize/4] = 1;
nndd[testsize/4] = 1;
bv[3*testsize/4] = 1;
nndd[3*testsize/4] = 1;
compare_bv_and_nndd(bv, nndd);
}
}
//! Test Serialize and Load
TEST_F(nn_dict_dynamic_test, serialize_and_load)
{
std::string file_name = temp_dir+"/nn_dict_dynamic";
uint64_t testsize = 100000;
sdsl::bit_vector bv(testsize, 0);
{
std::mt19937_64 rng;
std::uniform_int_distribution<uint64_t> distribution(0, testsize-1);
auto dice = bind(distribution, rng);
sdsl::nn_dict_dynamic nndd(testsize);
for (uint64_t i=0; i<testsize/4; ++i) {
uint64_t value = dice();
if (bv[value]) {
bv[value] = 0;
nndd[value] = 0;
} else {
bv[value] = 1;
nndd[value] = 1;
}
}
sdsl::store_to_file(nndd, file_name);
}
{
sdsl::nn_dict_dynamic nndd(0);
sdsl::load_from_file(nndd, file_name);
compare_bv_and_nndd(bv, nndd);
}
sdsl::remove(file_name);
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
if (argc < 2) {
// LCOV_EXCL_START
std::cout << "Usage: " << argv[0] << " tmp_dir" << std::endl;
return 1;
// LCOV_EXCL_STOP
}
temp_dir = argv[1];
return RUN_ALL_TESTS();
}