-
Notifications
You must be signed in to change notification settings - Fork 17
/
btree_test.cc
216 lines (179 loc) · 6.07 KB
/
btree_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "btree.h"
#include <map>
#include "assert.h"
#include "stupidunit.h"
using std::pair;
using std::map;
using std::vector;
class BTreeTest : public Test {
protected:
static const int MAX_KEYS = 3;
static const int DEPTH_3 = MAX_KEYS+1 + (MAX_KEYS/2+1)*MAX_KEYS;
void createDepth3() {
for (int i = 0; i < DEPTH_3; ++i) {
tree_.insert(i*2, i*2);
}
}
typedef BPlusTree<int, int, MAX_KEYS, MAX_KEYS> TreeType;
TreeType tree_;
int output_;
};
TEST_F(BTreeTest, SimpleInsertAndFind) {
EXPECT_FALSE(tree_.find(52));
tree_.insert(52, 77);
EXPECT_TRUE(tree_.find(52, &output_));
EXPECT_EQ(77, output_);
EXPECT_FALSE(tree_.find(54));
EXPECT_FALSE(tree_.find(50));
// Insert again = overwrite
tree_.insert(52, 78);
EXPECT_TRUE(tree_.find(52, &output_));
EXPECT_EQ(78, output_);
tree_.insert(54, 79);
tree_.insert(50, 80);
EXPECT_TRUE(tree_.find(52, &output_));
EXPECT_EQ(78, output_);
EXPECT_TRUE(tree_.find(54, &output_));
EXPECT_EQ(79, output_);
EXPECT_TRUE(tree_.find(50, &output_));
EXPECT_EQ(80, output_);
EXPECT_FALSE(tree_.find(55));
EXPECT_FALSE(tree_.find(51));
EXPECT_FALSE(tree_.find(0));
}
TEST_F(BTreeTest, FindLastLessThan) {
// Empty tree: nothing to find
EXPECT_FALSE(tree_.findLastLessThan(52));
// Everything is >= key, nothing to find
tree_.insert(52, 1);
EXPECT_FALSE(tree_.findLastLessThan(52));
// This works
int out_key = 0;
EXPECT_TRUE(tree_.findLastLessThan(100, &output_, &out_key));
EXPECT_EQ(1, output_);
EXPECT_EQ(52, out_key);
tree_.insert(50, 2);
EXPECT_TRUE(tree_.findLastLessThan(53, &output_, &out_key));
EXPECT_EQ(1, output_);
EXPECT_EQ(52, out_key);
EXPECT_TRUE(tree_.findLastLessThan(52, &output_, &out_key));
EXPECT_EQ(2, output_);
EXPECT_EQ(50, out_key);
EXPECT_TRUE(tree_.findLastLessThan(51, &output_, &out_key));
EXPECT_EQ(2, output_);
EXPECT_EQ(50, out_key);
output_ = -1;
out_key = -1;
EXPECT_FALSE(tree_.findLastLessThan(50, &output_, &out_key));
EXPECT_EQ(-1, output_);
EXPECT_EQ(-1, out_key);
tree_.insert(49, 3);
EXPECT_TRUE(tree_.findLastLessThan(52, &output_, &out_key));
EXPECT_EQ(2, output_);
EXPECT_EQ(50, out_key);
}
// This test currently fails: findLastLessThan does not work when things are deleted.
TEST_F(BTreeTest, FindLastLessThanDeleted) {
tree_.insert(52, 1);
tree_.insert(50, 2);
tree_.del(52);
EXPECT_TRUE(tree_.findLastLessThan(53, &output_));
EXPECT_EQ(2, output_);
}
TEST_F(BTreeTest, FindLastLessThan2) {
tree_.insert(42, -1);
tree_.insert(1804289383, 1);
tree_.insert(1804289383, 2);
tree_.insert( 719885386, 3);
tree_.insert(1804289383, 4);
tree_.insert( 783368690, 5);
tree_.insert( 719885386, 6);
EXPECT_TRUE(tree_.findLastLessThan(2044897763, &output_));
EXPECT_EQ(4, output_);
tree_.insert( 304089172, 7);
EXPECT_TRUE(tree_.findLastLessThan(1804289383, &output_));
EXPECT_EQ(5, output_);
}
// Runs find, lower_bound, and upper_bound in both the STL set and the B-tree_.
bool doFind(const map<int, int>& std_map, const BPlusTree<int, int, 3, 3>& tree, const int key) {
// Test the weird "find first less than". Not compatible with deletes
map<int, int>::const_iterator i = std_map.lower_bound(key);
int map_less_than = -2;
if (i != std_map.begin()) {
// Go back one
--i;
ASSERT(i->first < key);
map_less_than = i->second;
}
int tree_less_than = -2;
tree.findLastLessThan(key, &tree_less_than);
ASSERT(map_less_than == tree_less_than);
i = std_map.find(key);
int output = 0;
bool tree_found = tree.find(key, &output);
if (i != std_map.end()) {
ASSERT(tree_found && output == i->second);
return true;
} else {
ASSERT(!tree_found);
return false;
}
}
template <typename T> size_t chooseIndex(const vector<T>& v) {
return static_cast<size_t>(random()) % v.size();
}
template <typename T> T choose(const vector<T>& v) {
return v[chooseIndex(v)];
}
TEST_F(BTreeTest, RandomInsertUnique) {
srandom(static_cast<unsigned int>(time(NULL)));
map<int, int> std_map;
vector<int> values;
tree_.insert(42, -1);
std_map.insert(std::make_pair(42, -1));
values.push_back(42);
//~ printf("insert %d = %d\n", 42, -1);
for (int loops = 0; loops < 1000; ++loops) {
// Do an insert (alternating between new and old inserts)
int value;
if (loops % 2 == 0) {
value = static_cast<int>(random());
} else {
value = choose(values);
}
pair<map<int, int>::iterator, bool> map_result = std_map.insert(std::make_pair(value, loops+1));
tree_.insert(value, loops+1);
//~ printf("insert %d = %d\n", value, loops+1);
if (map_result.second) {
values.push_back(value);
} else {
map_result.first->second = loops+1;
}
// Find a value in the set
value = choose(values);
//~ printf("find %d\n", value);
ASSERT_TRUE(doFind(std_map, tree_, value));
// Find a value that is likely not in the set
value = static_cast<int>(random());
vector<int>::iterator it = std::find(values.begin(), values.end(), value);
//~ printf("find %d\n", value);
ASSERT_EQ(it != values.end(), doFind(std_map, tree_, value));
// Randomly delete a value from the set
if (loops % 3 == 2) {
size_t index = chooseIndex(values);
value = values[index];
values.erase(values.begin() + static_cast<ssize_t>(index));
//~ printf("del %d\n", value);
std_map.erase(std_map.find(value));
tree_.del(value);
}
}
for (size_t i = 0; i < values.size(); ++i) {
ASSERT_TRUE(tree_.find(values[i], &output_));
map<int, int>::const_iterator it = std_map.find(values[i]);
ASSERT_EQ(it->second, output_);
}
}
int main() {
return TestSuite::globalInstance()->runAll();
}