-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_driver.cpp
240 lines (188 loc) · 5.1 KB
/
bt_driver.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
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include "bt_builder.h"
#include "bt_node.h"
#include "bt_errors.h"
#include "bt_leaf.h"
#include "bt_index.h"
#include "bt_driver.h"
#include "bt_scan.h"
Status BtreeTest::runTests(){
test1();
test2();
test3();
test4();
cout<<"B+tree test complete"<<endl;
return OK;
}
void BtreeTest::test1() {
cout << "\n---------test1() key type is Integer-- initial test------\n";
Status status;
KeyId num = 25;
BtreeBuilder *btb = new BtreeBuilder();
status = btb->deleteBuilderKey(num);
if (status == OK)
{
cout<<"Problem with delete function!!!!!"<<endl;
}else{
cout<<"Deletion of key ["<<num<<"] failed as expected"<<endl;
}
status = btb->destroyBtree();
if(status != OK)
{
cout<<"Problem with BTree destroy!!!!"<<endl;
}
delete btb;
cout << "\n---------------End of Test 1----------------------\n\n";
}
void BtreeTest::test2() {
cout << "\n---------test2() key type is Integer--sequential insertion and deletion test------\n";
Status status;
KeyId num = 10000;
BtreeBuilder *btb = new BtreeBuilder();
//test inserting 10000 keys
for(int i = 0; i < num;i++)
{
status = btb->insertBuilderKey(i);
if (status == OK){
cout<<"Inserted key ["<<i<<"] successfully!"<<endl;
}else{
cout<<"Problem encountered while inserting key ["<<i<<"]"<<endl;
}
}
// test delete()
cout << "\nstart Btree deletion"<< endl;
for (int j = 0; j < num; j++)
{
status = btb->deleteBuilderKey(j);
if (status == OK){
cout<<"Deleted key ["<<j<<"] successfully!"<<endl;
}else if(status == KEY_NOT_FOUND){
cout<<"Key "<<j<<" does not exist in the B+tree"<<endl;
}else{
cout<<"Problem encountered while deleting key ["<<j<<"]"<<endl;
}
}
status = btb->destroyBtree();
if(status != OK)
{
cout<<"Problem with BTree destroy!!!!"<<endl;
}
delete btb;
cout << "\n---------------End of Test 2----------------------\n\n";
}
void BtreeTest::test3() {
cout << "\n-------------test3() key type is Integer-random insertion and deletion-----------\n";
Status status, status1;
int num_mult, num_add;
int num = 5000;
int num1, num2;
BtreeBuilder *btb = new BtreeBuilder();
num1 = 5;
num2 = 1;
//test inserting keys
for(int i = 0; i < num ;i++)
{
num_mult = (num1 * num2);
status = btb->insertBuilderKey(num_mult);
if (status == OK){
cout<<"Inserted key ["<<num_mult<<"] successfully!"<<endl;
}else{
cout<<"Problem encountered while inserting key ["<<num_mult<<"]"<<endl;
}
num_add = (num1 + num2);
status1 = btb->insertBuilderKey(num_add);
if (status1 == OK){
cout<<"Inserted key ["<<num_add<<"] successfully!"<<endl;
}else{
cout<<"Problem encountered while inserting key ["<<num_add<<"]"<<endl;
}
num2++;
}
//deletion test
num1 = 5;
num2 = 1;
for(int i = 0; i < num ;i++)
{
num_mult = (num1 * num2);
status = btb->deleteBuilderKey(num_mult);
if (status == OK){
cout<<"Deleted key ["<<num_mult<<"] successfully!"<<endl;
}else if(status == KEY_NOT_FOUND){
cout<<"Key "<<i<<" does not exist in the B+tree"<<endl;
}else{
cout<<"Problem encountered while deleting key ["<<num_mult<<"]"<<endl;
}
num_add = (num1 + num2);
status1 = btb->deleteBuilderKey(num_add);
if (status1 == OK){
cout<<"Deleted key ["<<num_add<<"] successfully!"<<endl;
}else{
cout<<"Problem encountered while deleting key ["<<num_add<<"]"<<endl;
}
num2++;
}
status = btb->destroyBtree();
if(status != OK)
{
cout<<"Problem with BTree destroy!!!!"<<endl;
}
delete btb;
cout << "\n ----End of Test 3----------------------\n\n";
}
void BtreeTest::test4() {
cout << "\n---------test4() key type is Integer--scan test------\n";
Status status;
KeyId num = 2000;
BtreeBuilder *btb = new BtreeBuilder();
BtreeScan *new_scan;
KeyId k, *key;
key = &k;
int count = 0;
//test inserting 2000 keys
for(int i = 0; i < num;i++)
{
status = btb->insertBuilderKey(i);
if (status == OK){
cout<<"Inserted key ["<<i<<"] successfully!"<<endl;
}else{
cout<<"Problem encountered while inserting key ["<<i<<"]"<<endl;
}
}
//scan test
cout<<"whole tree scan"<<endl;
new_scan = btb->openScan(NULL,NULL);
while((status = new_scan->getNext(key)) ==OK)
{
count++;
cout<<"Scanned key ["<<*key<<"]"<<endl;
}
cout<<"Scanned "<<count<<" Keys"<<endl;
cout<<"range tree scan first to 500"<<endl;
new_scan = btb->openScan(NULL,500);
while((status = new_scan->getNext(key)) ==OK)
{
count++;
cout<<"Scanned key ["<<*key<<"]"<<endl;
}
cout<<"Scanned "<<count<<" Keys"<<endl;
cout<<"range tree scan 500 to 1000"<<endl;
new_scan = btb->openScan(500,1000);
while((status = new_scan->getNext(key)) ==OK)
{
count++;
cout<<"Scanned key ["<<*key<<"]"<<endl;
}
cout<<"Scanned "<<count<<" Keys"<<endl;
cout<<"range tree scan 1500 to last"<<endl;
new_scan = btb->openScan(1500, NULL);
while((status = new_scan->getNext(key)) ==OK)
{
count++;
cout<<"Scanned key ["<<*key<<"]"<<endl;
}
cout<<"Scanned "<<count<<" Keys"<<endl;
cout << "\n\n---------End of Test 4 ---------------------\n\n";
}