-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
189 lines (143 loc) · 3.86 KB
/
test.c
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
#include <assert.h>
#include <stdio.h>
#include "bst.h"
void test_bst_creation_successful();
void test_bst_get_node_count();
void test_bst_insert_new_node();
void test_bst_search_for_a_node();
void test_bst_get_min_value();
void test_bst_get_max_value();
void test_get_height_function();
void test_validate_tree_is_bst_function();
int main() {
printf("==============================================================\n");
printf("BINARY SEARCH TREE TESTS\n");
printf("==============================================================\n");
test_bst_creation_successful();
test_bst_get_node_count();
test_bst_insert_new_node();
test_bst_search_for_a_node();
test_bst_get_min_value();
test_bst_get_max_value();
test_get_height_function();
test_validate_tree_is_bst_function();
return 0;
}
void test_bst_creation_successful() {
printf("test bst creation successful\n");
BSTree *bst = construct_bst();
assert(bst->root == NULL);
destroy_bst(bst);
}
void test_bst_get_node_count() {
printf("test bst get_node_count function\n");
BSTree *bst = construct_bst();
assert(get_node_count(bst) == 0);
destroy_bst(bst);
}
void test_bst_insert_new_node() {
printf("test that inserting a new node works as expected\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(bst->root->data == 5);
assert(bst->root->left->data == 3);
assert(bst->root->right->data == 7);
print_values(bst->root); // 1, 2, 3, 4, 5, 6, 7, 8, 9
printf("\n");
destroy_bst(bst);
}
void test_bst_search_for_a_node() {
printf("test that searching for a node works as expected\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(in_tree(bst, 9) == 1);
assert(in_tree(bst, 15) == 0);
assert(in_tree(bst, 1) == 1);
destroy_bst(bst);
}
void test_bst_get_min_value() {
printf("test that searching for minimum value works as expected\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(get_min(bst) == 1);
destroy_bst(bst);
}
void test_bst_get_max_value() {
printf("test that searching for maximum value works as expected\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(get_max(bst) == 9);
destroy_bst(bst);
}
void test_get_height_function() {
printf("test that get_height works as expected\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(get_height(bst) == 4);
destroy_bst(bst);
}
void test_validate_tree_is_bst_function() {
printf("test that the tree is a bst\n");
BSTree *bst = construct_bst();
insert(bst, 5);
insert(bst, 3);
insert(bst, 7);
insert(bst, 9);
insert(bst, 6);
insert(bst, 2);
insert(bst, 1);
insert(bst, 3);
insert(bst, 8);
insert(bst, 4);
assert(is_bst(bst) == 1);
BSTree *non_bst = construct_bst();
non_bst->root = create_node(1);
non_bst->root->left = create_node(9);
non_bst->root->right = create_node(0);
destroy_bst(bst);
}