-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmq_test.hpp
152 lines (127 loc) · 3.83 KB
/
rmq_test.hpp
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
/**
* A test for the RMQ problem.
*/
#pragma once
#include <assert.h>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <vector>
namespace rmq_test {
template<typename impl>
void test() {
#define EXPECT_RMQ(u, v, expect) do { \
size_t _ret = im.query(&input[(u)], &input[(v)]); \
if (input[_ret] != input[(expect)]) { \
std::cout << "rmq(" << u << ", " << v << ") = " << _ret << std::endl; \
std::cout << "(expected " << expect << ")" << std::endl; \
} \
assert(input[_ret] == input[(expect)]); \
} while (0)
if (0) {
int input[] = {
1,
};
impl im(&input[0], input + ((sizeof input) / (sizeof *input)));
EXPECT_RMQ(0, 1, 0);
}
{
int input[] = {
1,
1,
1,
1,
1,
1,
};
impl im(&input[0], input + ((sizeof input) / (sizeof *input)));
EXPECT_RMQ(0, 3, 2);
EXPECT_RMQ(0, 2, 1);
EXPECT_RMQ(2, 6, 5);
EXPECT_RMQ(3, 6, 5);
}
{
int input[] = {
3,
1,
2,
1,
4,
5,
};
impl im(&input[0], input + ((sizeof input) / (sizeof *input)));
EXPECT_RMQ(0, 3, 1);
EXPECT_RMQ(0, 2, 1);
EXPECT_RMQ(2, 6, 3);
EXPECT_RMQ(3, 6, 3);
}
{
int input[] = {
3,
1,
1,
1,
4,
5,
};
impl im(&input[0], input + ((sizeof input) / (sizeof *input)));
EXPECT_RMQ(0, 3, 2);
}
{
int input[] = {
10,
8,
9,
2,
4,
5,
1,
16,
4,
7,
};
impl im(&input[0], input + ((sizeof input) / (sizeof *input)));
EXPECT_RMQ(0, 3, 1);
EXPECT_RMQ(0, 6, 3);
EXPECT_RMQ(3, 8, 6);
EXPECT_RMQ(0, 10, 6);
}
#undef EXPECT_RMQ
}
template<typename impl>
void vector_test() {
// Lower this number if you want to run naive_rmq.
size_t N = 1000000;
std::vector<int> input(N);
for (std::vector<int>::iterator it = input.begin(); it != input.end(); ++it) {
*it = std::rand() % 1000;
}
auto t0 = std::chrono::high_resolution_clock::now();
impl im(input.begin(), input.end());
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::microseconds elapsed = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0);
std::cout << "built in " << elapsed.count() << "us" << std::endl;
size_t K = 100;
for (size_t i = 0; i < N - K; ++i) {
size_t len = std::max(size_t(1), size_t(std::abs(std::rand() % K)));
std::vector<int>::const_iterator expected = std::min_element(input.begin() + i, input.begin() + i + len);
std::vector<int>::difference_type found = im.query(input.begin() + i, input.begin() + i + len);
if (*expected != input[found]) {
std::cout << "error: " << *expected << " at " << expected - input.begin() << " != " << input[found] << " at " << found << std::endl;
std::cout << "array from " << i << " to " << i + len << ":" << std::endl;
std::copy(input.begin() + i, input.begin() + i + len,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
assert(*expected == input[found]);
}
}
}
#define TEST_IMPL(impl) \
int main(int argc, const char *argv[]) { \
rmq_test::test<impl<int *>>(); \
rmq_test::vector_test<impl<std::vector<int>::const_iterator>>(); \
return 0; \
}