-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bst_example.cpp
287 lines (262 loc) · 9.09 KB
/
test_bst_example.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
//
// test_bst_example.cpp
// CS 271 BST Project: Example Test File
//
// Created by Dr. Stacey Truex
//
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include "usecase.cpp"
using namespace std;
void test_empty() {
try
{
BST<string, int> bst;
if(!bst.empty()) {
cout << "Incorrect empty result." << endl;
}
bst.insert("one",1);
if(bst.empty()) {
cout << "Incorrect empty result." << endl;
}
}
catch(exception& e)
{
cerr << "Error in determining if BST is empty : " << e.what() << endl;
}
}
void test_insert() {
try {
BST<string, int> bst;
bst.insert("one", 1);
string bst_str = bst.to_string();
if(bst_str != "1") {
cout << "Incorrect result of inserting (\"one\", 1). Expected 1 but got : " << bst_str << endl;
}
for(int i = 2; i <= 10; i++) {
bst.insert("some data", i);
}
bst_str = bst.to_string();
if(bst_str != "1 2 3 4 5 6 7 8 9 10") {
cout << "Incorrect result of inserting keys 1-10 in order. Expected 1 2 3 4 5 6 7 8 9 10 but got : " << bst_str << endl;
}
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert("some data", vals[i]);
}
bst_str = balanced_bst.to_string();
if(bst_str != "5 2 7 1 3 6 9 4 8 10") {
cout << "Incorrect result of inserting keys {5, 2, 7, 1, 3, 4, 6, 9, 8, 10}. Expected 5 2 7 1 3 6 9 4 8 10 but got : " << bst_str << endl;
}
} catch(exception& e) {
cerr << "Error inserting into bst : " << e.what() << endl;
}
}
void test_get() {
try {
BST<string, int> bst;
string val = bst.get(0);
if(val!="") {
cout << "Incorrect get result from empty bst. Expected 0 but got " << val << endl;
}
bst.insert("one",1);
val = bst.get(1);
if(val != "one") {
cout << "Incorrect get result. Expected \"one\" but got : " << val << endl;
}
} catch(exception& e) {
cerr << "Error in getting data from bst : " << e.what() << endl;
}
}
void test_remove() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert("some data", vals[i]);
}
balanced_bst.remove(7);
string bst_str = balanced_bst.to_string();
if(bst_str != "5 2 8 1 3 6 9 4 10") {
cout << "Incorrect result of removing 7. Expected 5 2 8 1 3 6 9 4 10 but got : " << bst_str << endl;
}
} catch(exception& e) {
cerr << "Error in removing node from bst : " << e.what() << endl;
}
}
void test_max_data() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert(to_string(vals[i]) + " data", vals[i]);
}
string max_str = balanced_bst.max_data();
if(max_str != "10 data") {
cout << "Incorrect result of max_data. Expected \"10 data\" but got : " << max_str << endl;
}
} catch(exception& e) {
cerr << "Error in determining data of max node in bst : " << e.what() << endl;
}
}
void test_max_key() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert(to_string(vals[i]) + " data", vals[i]);
}
int max_k = balanced_bst.max_key();
if(max_k != 10) {
cout << "Incorrect result of max_key. Expected 10 but got : " << max_k << endl;
}
} catch(exception& e) {
cerr << "Error in determining key of max node in bst : " << e.what() << endl;
}
}
void test_min_data() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert(to_string(vals[i]) + " data", vals[i]);
}
string min_str = balanced_bst.min_data();
if(min_str != "1 data") {
cout << "Incorrect result of min_data. Expected \"1 data\" but got : " << min_str << endl;
}
} catch(exception& e) {
cerr << "Error in determining data of min node in bst : " << e.what() << endl;
}
}
void test_min_key() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert(to_string(vals[i]) + " data", vals[i]);
}
int min_k = balanced_bst.min_key();
if(min_k != 1) {
cout << "Incorrect result of min_key. Expected 10 but got : " << min_k << endl;
}
} catch(exception& e) {
cerr << "Error in determining key of min node in bst : " << e.what() << endl;
}
}
void test_successor() {
try {
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert(to_string(vals[i]) + " data", vals[i]);
}
int succ = balanced_bst.successor(4);
if(succ != 5) {
cout << "Incorrect result of successor of 4. Expected 5 but got : " << succ << endl;
}
succ = balanced_bst.successor(7);
if(succ != 8) {
cout << "Incorrect result of successor of 7. Expected 8 but got : " << succ << endl;
}
succ = balanced_bst.successor(10);
if(succ != 0) {
cout << "Incorrect result of successor of 10. Expected 0 but got : " << succ << endl;
}
} catch(exception& e) {
cerr << "Error in determining successor in bst : " << e.what() << endl;
}
}
void test_in_order() {
try {
BST<string, int> bst;
for(int i = 1; i <= 10; i++) {
bst.insert("some data", i);
}
string bst_str = bst.in_order();
if(bst_str != "1 2 3 4 5 6 7 8 9 10") {
cout << "Incorrect in_order result after inserting keys 1-10 in order. Expected 1 2 3 4 5 6 7 8 9 10 but got : " << bst_str << endl;
}
int vals[10] = {5, 2, 7, 1, 3, 4, 6, 9, 8, 10};
BST<string, int> balanced_bst;
for(int i = 0; i < 10; i++) {
balanced_bst.insert("some data", vals[i]);
}
bst_str = balanced_bst.in_order();
if(bst_str != "1 2 3 4 5 6 7 8 9 10") {
cout << "Incorrect in_order result after inserting keys {5, 2, 7, 1, 3, 4, 6, 9, 8, 10}. Expected 1 2 3 4 5 6 7 8 9 10 but got : " << bst_str << endl;
}
} catch(exception& e) {
cerr << "Error getting keys in_order from bst : " << e.what() << endl;
}
}
void test_trim() {
try {
BST<string,int> bst;
int vals[3] = {1, 0, 2};
for(int i = 0; i < 3; i++) {
bst.insert(to_string(vals[i])+" data", vals[i]);
}
bst.trim(1,2);
string bst_str = bst.to_string();
if(bst_str != "1 2") {
cout << "Incorrect tree after trimming 1 0 2 with low=1, high=2. Expected 1 2 but got : " << bst_str << endl;
}
BST<string, int> bst2;
int vals2[5] = {3, 0, 4, 2, 1};
for(int i = 0; i < 5; i++) {
bst2.insert(to_string(vals2[i])+" data", vals2[i]);
}
bst2.trim(1,3);
bst_str = bst2.to_string();
if(bst_str != "3 2 1") {
cout << "Incorrect tree after trimming 3 0 4 2 1 with low=1, high=3. Expected 3 2 1 but got : " << bst_str << endl;
}
} catch(exception& e) {
cerr << "Error in trimming the bst : " << e.what() << endl;
}
}
void test_binhex(){
try {
BST<string,string>* bst1 = create_bst<string,string>("binhex.txt");
string bin1 = "111010100101";
string expected_hex1 = "EA5";
string hex1 = convert<string,string>(bst1, bin1);
delete bst1;
if(hex1!=expected_hex1) {
cout << "Incorrect result converting " << bin1 << " to hex. Expected : " << expected_hex1 << ", but got : " << hex1 << endl;
}
} catch(exception& e) {
cerr << "Error converting binary to hex : " << e.what() << endl;
}
try {
BST<string,string>* bst2 = create_bst<string,string>("binhex.txt");
string bin2 = "110101";
string expected_hex2 = "35";
string hex2 = convert<string,string>(bst2, bin2);
delete bst2;
if(hex2!=expected_hex2) {
cout << "Incorrect result converting " << bin2 << " to hex. Expected : " << expected_hex2 << ", but got : " << hex2 << endl;
}
} catch(exception& e) {
cerr << "Error converting binary to hex : " << e.what() << endl;
}
}
int main() {
test_empty();
test_insert();
test_get();
test_remove();
test_max_data();
test_max_key();
test_min_data();
test_min_key();
test_successor();
test_in_order();
test_trim();
test_binhex();
cout << "Testing completed" << endl;
return 0;
}