-
Notifications
You must be signed in to change notification settings - Fork 349
/
rmq_test.cpp
156 lines (136 loc) · 4.73 KB
/
rmq_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
#include "sdsl/rmq_support.hpp"
#include "gtest/gtest.h"
#include <vector>
#include <string>
#include <stack>
using namespace std;
using namespace sdsl;
namespace
{
typedef vector<uint64_t> tVUI;
string test_file;
string temp_file;
template<class T>
class rmq_test : public ::testing::Test { };
using testing::Types;
typedef Types<sdsl::rmq_succinct_sct<>,
sdsl::rmq_succinct_sada<>
> Implementations;
TYPED_TEST_CASE(rmq_test, Implementations);
TYPED_TEST(rmq_test, construct_and_store)
{
static_assert(sdsl::util::is_regular<TypeParam>::value, "Type is not regular");
int_vector<> v;
load_from_file(v, test_file);
TypeParam rmq(&v);
ASSERT_TRUE(sdsl::store_to_file(rmq, temp_file));
}
// helper class for next test
class state
{
public:
uint64_t l, r; // left and right border of interval
uint64_t idx; // index of the min value
uint64_t min; // min value in the interval
state(uint64_t fl=0, uint64_t fr=0, uint64_t fidx = 0, uint64_t fmin=0) :
l(fl), r(fr), idx(fidx), min(fmin) {}
};
//! Test range minimum queries
TYPED_TEST(rmq_test, rmq_load_and_query)
{
int_vector<> v;
ASSERT_TRUE(load_from_file(v, test_file));
TypeParam rmq;
ASSERT_TRUE(load_from_file(rmq, temp_file));
ASSERT_EQ(v.size(), rmq.size());
if (rmq.size() > 0) {
stack<state> s;
uint64_t idx = rmq(0, rmq.size()-1);
ASSERT_TRUE(idx < rmq.size());
s.push(state(0, rmq.size()-1, idx, v[idx]));
while (!s.empty()) {
state st = s.top(); s.pop();
if (st.l < st.idx) {
idx = rmq(st.l, st.idx-1);
ASSERT_TRUE(idx >= st.l); ASSERT_TRUE(idx <= st.idx-1);
ASSERT_TRUE(v[idx] >= v[st.idx])
<< "v["<<idx<<"]="<< v[idx]
<< " < " << "v["<<st.idx<<"]="
<< v[st.idx] << endl
<< "[" << st.l << "," << st.r << "]" << endl;
s.push(state(st.l, st.idx-1, idx, v[idx]));
}
if (st.idx < st.r) {
idx = rmq(st.idx+1, st.r);
ASSERT_TRUE(idx >= st.idx+1); ASSERT_TRUE(idx <= st.r);
ASSERT_TRUE(v[idx] >= v[st.idx])
<< "v["<<idx<<"]="<< v[idx]
<< " < " << "v["<<st.idx<<"]="
<< v[st.idx] << endl
<< "[" << st.l << "," << st.r << "]" << endl;
s.push(state(st.idx+1, st.r, idx, v[idx]));
}
}
}
}
//! Test range minimum queries
TYPED_TEST(rmq_test, rmq_load_and_move_and_query)
{
int_vector<> v;
ASSERT_TRUE(load_from_file(v, test_file));
TypeParam rmq_load;
ASSERT_TRUE(load_from_file(rmq_load, temp_file));
TypeParam rmq = std::move(rmq_load);
ASSERT_EQ(v.size(), rmq.size());
if (rmq.size() > 0) {
stack<state> s;
uint64_t idx = rmq(0, rmq.size()-1);
ASSERT_TRUE(idx < rmq.size());
s.push(state(0, rmq.size()-1, idx, v[idx]));
while (!s.empty()) {
state st = s.top(); s.pop();
if (st.l < st.idx) {
idx = rmq(st.l, st.idx-1);
ASSERT_TRUE(idx >= st.l); ASSERT_TRUE(idx <= st.idx-1);
ASSERT_TRUE(v[idx] >= v[st.idx])
<< "v["<<idx<<"]="<< v[idx]
<< " < " << "v["<<st.idx<<"]="
<< v[st.idx] << endl
<< "[" << st.l << "," << st.r << "]" << endl;
s.push(state(st.l, st.idx-1, idx, v[idx]));
}
if (st.idx < st.r) {
idx = rmq(st.idx+1, st.r);
ASSERT_TRUE(idx >= st.idx+1); ASSERT_TRUE(idx <= st.r);
ASSERT_TRUE(v[idx] >= v[st.idx])
<< "v["<<idx<<"]="<< v[idx]
<< " < " << "v["<<st.idx<<"]="
<< v[st.idx] << endl
<< "[" << st.l << "," << st.r << "]" << endl;
s.push(state(st.idx+1, st.r, idx, v[idx]));
}
}
}
}
TYPED_TEST(rmq_test, delete_)
{
sdsl::remove(temp_file);
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
if (argc < 3) {
// LCOV_EXCL_START
cout << "Usage: " << argv[0] << " test_file temp_file" << endl;
cout << " (1) Generates a RMQ out of the serialized int_vector in test_file." << endl;
cout << " in test_file. Stores RMQ in temp_file." << 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];
return RUN_ALL_TESTS();
}