forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerger_test.cc
169 lines (146 loc) · 4.24 KB
/
merger_test.cc
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
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include <vector>
#include <string>
#include "table/merger.h"
#include "util/testharness.h"
#include "util/testutil.h"
namespace rocksdb {
class MergerTest : public testing::Test {
public:
MergerTest()
: rnd_(3), merging_iterator_(nullptr), single_iterator_(nullptr) {}
~MergerTest() = default;
std::vector<std::string> GenerateStrings(size_t len, int string_len) {
std::vector<std::string> ret;
for (size_t i = 0; i < len; ++i) {
ret.push_back(test::RandomHumanReadableString(&rnd_, string_len));
}
return ret;
}
void AssertEquivalence() {
auto a = merging_iterator_.get();
auto b = single_iterator_.get();
if (!a->Valid()) {
ASSERT_TRUE(!b->Valid());
} else {
ASSERT_TRUE(b->Valid());
ASSERT_EQ(b->key().ToString(), a->key().ToString());
ASSERT_EQ(b->value().ToString(), a->value().ToString());
}
}
void SeekToRandom() { Seek(test::RandomHumanReadableString(&rnd_, 5)); }
void Seek(std::string target) {
merging_iterator_->Seek(target);
single_iterator_->Seek(target);
}
void SeekToFirst() {
merging_iterator_->SeekToFirst();
single_iterator_->SeekToFirst();
}
void SeekToLast() {
merging_iterator_->SeekToLast();
single_iterator_->SeekToLast();
}
void Next(int times) {
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
AssertEquivalence();
merging_iterator_->Next();
single_iterator_->Next();
}
AssertEquivalence();
}
void Prev(int times) {
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
AssertEquivalence();
merging_iterator_->Prev();
single_iterator_->Prev();
}
AssertEquivalence();
}
void NextAndPrev(int times) {
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
AssertEquivalence();
if (rnd_.OneIn(2)) {
merging_iterator_->Prev();
single_iterator_->Prev();
} else {
merging_iterator_->Next();
single_iterator_->Next();
}
}
AssertEquivalence();
}
void Generate(size_t num_iterators, size_t strings_per_iterator,
int letters_per_string) {
std::vector<Iterator*> small_iterators;
for (size_t i = 0; i < num_iterators; ++i) {
auto strings = GenerateStrings(strings_per_iterator, letters_per_string);
small_iterators.push_back(new test::VectorIterator(strings));
all_keys_.insert(all_keys_.end(), strings.begin(), strings.end());
}
merging_iterator_.reset(
NewMergingIterator(BytewiseComparator(), &small_iterators[0],
static_cast<int>(small_iterators.size())));
single_iterator_.reset(new test::VectorIterator(all_keys_));
}
Random rnd_;
std::unique_ptr<Iterator> merging_iterator_;
std::unique_ptr<Iterator> single_iterator_;
std::vector<std::string> all_keys_;
};
TEST_F(MergerTest, SeekToRandomNextTest) {
Generate(1000, 50, 50);
for (int i = 0; i < 10; ++i) {
SeekToRandom();
AssertEquivalence();
Next(50000);
}
}
TEST_F(MergerTest, SeekToRandomNextSmallStringsTest) {
Generate(1000, 50, 2);
for (int i = 0; i < 10; ++i) {
SeekToRandom();
AssertEquivalence();
Next(50000);
}
}
TEST_F(MergerTest, SeekToRandomPrevTest) {
Generate(1000, 50, 50);
for (int i = 0; i < 10; ++i) {
SeekToRandom();
AssertEquivalence();
Prev(50000);
}
}
TEST_F(MergerTest, SeekToRandomRandomTest) {
Generate(200, 50, 50);
for (int i = 0; i < 3; ++i) {
SeekToRandom();
AssertEquivalence();
NextAndPrev(5000);
}
}
TEST_F(MergerTest, SeekToFirstTest) {
Generate(1000, 50, 50);
for (int i = 0; i < 10; ++i) {
SeekToFirst();
AssertEquivalence();
Next(50000);
}
}
TEST_F(MergerTest, SeekToLastTest) {
Generate(1000, 50, 50);
for (int i = 0; i < 10; ++i) {
SeekToLast();
AssertEquivalence();
Prev(50000);
}
}
} // namespace rocksdb
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}