-
Notifications
You must be signed in to change notification settings - Fork 1
/
btree_test.cpp
311 lines (248 loc) · 9 KB
/
btree_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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// btree_test.cpp
//
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "btree.h"
#include "btree_unittest_help.h"
#include <iostream>
#include <vector>
using namespace std;
// Test cases - study these, and the helper functions in btree_unittest_help.cpp.
TEST_CASE("B-Tree: Sanity Check", "[sanity]") {
// run this one with the following command line:
// ./btree_test "[sanity]"
btree* small = build_small();
cout << "print_tree writes something like: \"graph btree{ ... }\" to stdout." << endl;
cout << "use webgraphviz.com to turn that into diagrams of your tree." << endl << endl;
print_tree(small);
cout << endl;
REQUIRE(check_tree(small));
REQUIRE(private_contains(small, 13));
REQUIRE_FALSE(private_contains(small, 14));
btree* broken = build_broken(); // invariant should fail
REQUIRE_FALSE(check_tree(broken)); // be sure we catch that
}
TEST_CASE("B-Tree: Report number of nodes", "[count nodes]") {
btree* empty = build_empty();
REQUIRE(count_nodes(empty) == 1); // not zero, since there's a root node
btree* small = build_small();
REQUIRE(count_nodes(small) == 4);
btree* two_thin = build_two_tier();
REQUIRE(count_nodes(two_thin) == 5);
btree* two_full = build_full_two_tier();
REQUIRE(count_nodes(two_full) == 6);
btree* thrice = build_thin_three_tier();
REQUIRE(count_nodes(thrice) == 9);
}
TEST_CASE("B-Tree: Report number of keys", "[count keys]") {
btree* empty = build_empty();
REQUIRE(count_keys(empty) == 0);
btree* small = build_small();
REQUIRE(count_keys(small) == 8);
btree* two_thin = build_two_tier();
REQUIRE(count_keys(two_thin) == 14);
btree* two_full = build_full_two_tier();
REQUIRE(count_keys(two_full) == 19);
btree* thrice = build_thin_three_tier();
REQUIRE(count_keys(thrice) == 17);
}
TEST_CASE("B-Tree: Find present key in leaf", "[find present leaf]") {
btree* small = build_small();
btree* node;
node = find(small, 17);
REQUIRE(node == small->children[1]);
node = find(small, 2);
REQUIRE(node == small->children[0]);
node = find(small, 28);
REQUIRE(node == small->children[2]);
}
TEST_CASE("B-Tree: Find present key in internal node", "[find present intnl]") {
btree* thrice = build_thin_three_tier();
btree* node;
node = find(thrice, 7);
REQUIRE(node == thrice->children[0]);
node = find(thrice, 17);
REQUIRE(node == thrice->children[1]);
}
TEST_CASE("B-Tree: Find present key in root", "[find present root]") {
btree* small = build_small();
btree* node;
node = find(small, 10);
REQUIRE(node == small);
node = find(small, 20);
REQUIRE(node == small);
}
TEST_CASE("B-Tree: Find not present key", "[find not present]") {
btree* small = build_small();
btree* node;
node = find(small, 6);
REQUIRE(node == small->children[0]);
node = find(small, 15);
REQUIRE(node == small->children[1]);
node = find(small, 21);
REQUIRE(node == small->children[2]);
REQUIRE(check_tree(small));
}
TEST_CASE("B-Tree: Insert key into empty root", "[ins root empty]") {
btree* empty = build_empty();
insert(empty, 42);
REQUIRE(check_tree(empty));
REQUIRE(private_contains(empty, 42));
}
TEST_CASE("B-Tree: Insert key into semifull root", "[ins root semifull]") {
btree* semi = build_semifull();
insert(semi, 42);
REQUIRE(check_tree(semi));
REQUIRE(private_contains(semi, 10));
REQUIRE(private_contains(semi, 30));
REQUIRE(private_contains(semi, 42));
insert(semi, 7);
REQUIRE(check_tree(semi));
REQUIRE(private_contains(semi, 7));
REQUIRE(private_contains(semi, 10));
REQUIRE(private_contains(semi, 30));
REQUIRE(private_contains(semi, 42));
}
TEST_CASE("B-Tree: Insert key into full root", "[ins root full]") {
btree* full = build_full_leaf_root();
insert(full, 15);
REQUIRE(check_tree(full));
// root should have split and given another level of height
int height = 0;
bool leaves_ok = check_height(full, height);
REQUIRE(leaves_ok);
REQUIRE(height == 1);
}
TEST_CASE("B-Tree: Insert key into semifull leaf node", "[ins leaf semifull]") {
// get a tree with semifull leaf
btree* semi = build_two_tier();
int height = 0;
bool leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
// insert a key that would put it in the right spot
insert(semi, 4); // should add to child[0]
REQUIRE(check_tree(semi));
// check that height did not change
leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
// check that key is now present
REQUIRE(private_contains(semi->children[0], 4));
insert(semi, 24); // should add to child[2]
REQUIRE(check_tree(semi));
leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
REQUIRE(private_contains(semi->children[2], 24));
insert(semi, 40); // should add to child[3]
REQUIRE(check_tree(semi));
leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
REQUIRE(private_contains(semi->children[3], 40));
}
TEST_CASE("B-Tree: Insert key into full leaf node", "[ins leaf full]") {
btree* semi = build_two_tier();
int height = 0;
bool leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
// insert a key that should split child[1]
insert(semi, 18); // should add to child[0]
REQUIRE(check_tree(semi));
// shouldn't have raised height; parent of insert site had room
leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
// key 17 should have moved to the root
REQUIRE(private_contains(semi, 17));
// key 15 should be in child[1]
REQUIRE(private_contains(semi->children[1], 15));
// and key 19 shoudl be in child[2]
REQUIRE(private_contains(semi->children[2], 19));
// remember if you're having trouble with this, you can always hack
// this test file and put some print_tree calls at the trouble
// spots, then recompile and run to troubleshoot.
}
TEST_CASE("B-Tree: Remove not present key from empty tree", "[rm not present empty]") {
btree* semi = build_empty();
remove(semi, 28); // should have no effect
REQUIRE(check_tree(semi));
REQUIRE_FALSE(private_contains(semi, 28)); // no idea why this would be the case
}
TEST_CASE("B-Tree: Remove key from non-empty root", "[rm root not empty]") {
btree* full = build_full_leaf_root();
REQUIRE(private_contains(full, 30));
remove(full, 30);
REQUIRE(check_tree(full));
REQUIRE_FALSE(private_contains(full, 30));
}
TEST_CASE("B-Tree: Remove not present key from leaf", "[rm not present leaf]") {
btree* semi = build_two_tier();
remove(semi, 28); // should have no effect
REQUIRE(check_tree(semi));
REQUIRE_FALSE(private_contains(semi, 28)); // no idea why this would be the case
}
TEST_CASE("B-Tree: Remove key from leaf with full siblings", "[rm leaf sibs full]") {
btree* semi = build_two_tier();
remove(semi, 27); // should cause a rotate right involving parent and sibling to left
REQUIRE(check_tree(semi));
// height should remain at 1
int height = 0;
bool leaves_ok = check_height(semi, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
// ensure no node has 27
REQUIRE_FALSE(private_search_all(semi, 27));
}
TEST_CASE("B-Tree: Remove key from leaf with at-min-capactiy siblings", "[rm leaf sibs mincap]") {
btree* thrice = build_thin_three_tier();
int height = 0;
bool leaves_ok = check_height(thrice, height);
REQUIRE(height == 2); // just a sanity check
REQUIRE(leaves_ok);
remove(thrice, 1); // rm 1, smallest value. should result in shorter tree
leaves_ok = check_height(thrice, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
REQUIRE(check_tree(thrice));
REQUIRE_FALSE(private_search_all(thrice, 1));
// reset, do it again but remove 16. this one is on the inside.
thrice = build_thin_three_tier();
remove(thrice, 16);
leaves_ok = check_height(thrice, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
REQUIRE(check_tree(thrice));
REQUIRE_FALSE(private_search_all(thrice, 16));
// again, remove 26, this is the largest key in the tree
thrice = build_thin_three_tier();
remove(thrice, 26);
leaves_ok = check_height(thrice, height);
REQUIRE(height == 1);
REQUIRE(leaves_ok);
REQUIRE(check_tree(thrice));
REQUIRE_FALSE(private_search_all(thrice, 26));
}
TEST_CASE("B-Tree: Remove key from internal node with at-min-capacity siblings", "[rm intnl sibs mincap]") {
btree* thrice = build_thin_three_tier();
int height = 0;
bool leaves_ok = check_height(thrice, height);
REQUIRE(height == 2); // just a sanity check
REQUIRE(leaves_ok);
remove(thrice, 4); // rm 4, a key in an internal node.
leaves_ok = check_height(thrice, height);
REQUIRE(height == 1); // should have shrunk tree by one
REQUIRE(leaves_ok);
REQUIRE(check_tree(thrice));
REQUIRE_FALSE(private_search_all(thrice, 4));
thrice = build_thin_three_tier();
remove(thrice, 24); // rm 24, a key in an internal node.
leaves_ok = check_height(thrice, height);
REQUIRE(height == 1); // should have shrunk tree by one
REQUIRE(leaves_ok);
REQUIRE(check_tree(thrice));
REQUIRE_FALSE(private_search_all(thrice, 24));
}