-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestbst.cpp
More file actions
113 lines (100 loc) · 2.36 KB
/
testbst.cpp
File metadata and controls
113 lines (100 loc) · 2.36 KB
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
// /**
// * Testing BST - Binary Search Tree functions
// *
// * This file has series of tests for BST
// * Each test is independent and uses assert statements
// * Test functions are of the form
// *
// * test_netidXX()
// *
// * where netid is UW netid and XX is the test number starting from 01
// *
// * Test functions can only use the public functions from BST
// * testBSTAll() is called from main in main.cpp
// * testBSTAll calls all other functions
// * @author Multiple
// * @date ongoing
// */
#include "bstmap.h"
#include <cassert>
#include <sstream>
using namespace std;
// global value for testing
// NOLINTNEXTLINE
stringstream globalSS;
// need to reset SS before calling this
void printer(const BSTMap::value_type &p) {
globalSS << "[" << p.first << "=" << p.second << "]";
}
// // Testing == and []
void test01() {
cout << "Starting test01" << endl;
cout << "* Testing ==, !=, [] and copy constructor" << endl;
BSTMap b1;
auto val = b1["hello"];
assert(val == 0);
b1["hello"] = 5;
val = b1["hello"];
assert(val == 5);
b1["world"] = 42;
BSTMap b2;
assert(b1 != b2);
b2["hello"] = 5;
b2["world"] = 42;
assert(b1 == b2);
BSTMap b3(b2);
assert(b1 == b3);
cout << "Ending tes01" << endl;
}
// Testing traversal
void test02() {
cout << "Starting test02" << endl;
cout << "* Testing traversal" << endl;
BSTMap b;
b["x"] = 10;
b["f"] = 5;
b["b"] = 3;
b["e"] = 4;
b["z"] = 50;
// cout << b;
globalSS.str("");
b.inorder(printer);
string order = globalSS.str();
assert(order == "[b=3][e=4][f=5][x=10][z=50]");
globalSS.str("");
b.preorder(printer);
order = globalSS.str();
assert(order == "[x=10][f=5][b=3][e=4][z=50]");
globalSS.str("");
b.postorder(printer);
order = globalSS.str();
assert(order == "[e=4][b=3][f=5][z=50][x=10]");
cout << "Ending test02" << endl;
}
// Testing rebalance
void test03() {
cout << "Starting test03" << endl;
cout << "* Testing rebalance" << endl;
BSTMap b;
b["1"] = 1;
b["2"] = 2;
b["3"] = 3;
b["4"] = 4;
b["5"] = 5;
b["6"] = 6;
assert(b.height() == 6);
// cout << b << endl;
b.rebalance();
assert(b.height() == 3);
// cout << b << endl;
b.clear();
assert(b.height() == 0);
cout << "Ending test03" << endl;
}
// // Calling all test functions
void testBSTAll() {
test01();
test02();
test03();
// TODO(student) Add more tests
}