5
5
"testing"
6
6
)
7
7
8
+ // Ensure items are inserted into the tree
8
9
func TestInsertSingle (t * testing.T ) {
9
10
tests := []struct {
10
11
name string
@@ -66,6 +67,7 @@ func TestInsertBulkCheckCorrectStructure(t *testing.T) {
66
67
}
67
68
}
68
69
70
+ // Make sure that we form a tree of the expected height
69
71
func TestHeightCount (t * testing.T ) {
70
72
tests := []struct {
71
73
name string
@@ -97,6 +99,7 @@ func TestHeightCount(t *testing.T) {
97
99
}
98
100
}
99
101
102
+ // Test find Min/Max functionality of the BST
100
103
func TestMinMax (t * testing.T ) {
101
104
tests := []struct {
102
105
name string
@@ -146,15 +149,11 @@ func TestMinMax(t *testing.T) {
146
149
// Fulfill interface for Sort, see: https://gobyexample.com/sorting-by-functions
147
150
type myIntSlice []MyInt
148
151
149
- func (s myIntSlice ) Len () int {
150
- return len (s )
151
- }
152
- func (s myIntSlice ) Swap (i , j int ) {
153
- s [i ], s [j ] = s [j ], s [i ]
154
- }
155
- func (s myIntSlice ) Less (i , j int ) bool {
156
- return s [i ] < s [j ]
157
- }
152
+ func (s myIntSlice ) Len () int { return len (s ) }
153
+ func (s myIntSlice ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
154
+ func (s myIntSlice ) Less (i , j int ) bool { return s [i ] < s [j ] }
155
+
156
+ // Ensure that in-order traversal works as expected
158
157
func TestInOrder (t * testing.T ) {
159
158
tests := []struct {
160
159
name string
@@ -175,16 +174,9 @@ func TestInOrder(t *testing.T) {
175
174
t .Run (test .name , func (t * testing.T ) {
176
175
test .tree = & Node {}
177
176
_ , _ = insertBulk (test .tree , test .input )
178
- // Test Min
179
177
got := test .tree .InOrder ()
180
- //var want []int
181
- sort .Sort (myIntSlice (test .input ))
178
+ sort .Sort (myIntSlice (test .input )) // Use Sort to verify traversal
182
179
want := test .input
183
- //if !reflect.DeepEqual(want, got) {
184
- // if !(len(want) == 0 && len(got) == 0) { // TODO DeepEqual on empty slices
185
- // t.Errorf("wrong InOrder(). got %+v, want %+v", got, want)
186
- // }
187
- //}
188
180
for i := 0 ; i < len (want )- 1 ; i ++ {
189
181
if ! (got [i ].Equals (want [i ])) {
190
182
t .Errorf ("wrong InOrder(). got %+v, want %+v" , got , want )
@@ -194,7 +186,8 @@ func TestInOrder(t *testing.T) {
194
186
}
195
187
}
196
188
197
- func TestInSearch (t * testing.T ) {
189
+ // Test BST's search function
190
+ func TestSearch (t * testing.T ) {
198
191
tests := []struct {
199
192
name string
200
193
tree * Node
@@ -253,11 +246,7 @@ func TestRemove(t *testing.T) {
253
246
t .Run (test .name , func (t * testing.T ) {
254
247
test .tree = & Node {}
255
248
_ , _ = insertBulk (test .tree , test .input )
256
- // Test Remove
257
- //fmt.Println("Test tree bf remove", test.tree)
258
249
test .tree = test .tree .Remove (test .removeVal )
259
- //fmt.Println("Test tree af remove", test.tree)
260
-
261
250
// test that only the specified node was removed
262
251
for _ , inputVal := range test .input {
263
252
stillPresent , _ := test .tree .Search (inputVal )
@@ -279,7 +268,6 @@ func TestRemove(t *testing.T) {
279
268
// make sure the ordering is still correct
280
269
gotOrdered := test .tree .InOrder ()
281
270
for i := 1 ; i < len (gotOrdered ); i ++ {
282
- //fmt.Println("hi")
283
271
if ! (gotOrdered [i - i ].Less (gotOrdered [i ])) {
284
272
t .Errorf ("Remove(%v) not in ascending order %v %v %v" , test .removeVal , gotOrdered , test .input , i )
285
273
}
0 commit comments