-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_test.go
97 lines (88 loc) · 3.53 KB
/
remove_test.go
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
package avl_test
import "testing"
func Test_Tree_Remove(t *testing.T) {
testTree := &Tree{root: nil}
input := []int{5, 5, 4, 5, 6, 5}
purpose := []String{
"Remove last node",
"Remove w/ left subtree",
"Remove at left subtree",
"Remove w/ right subtree",
"Remove at right subtree",
"Remove & replace w/ IOS",
}
testTree.root = &node{data: Int(5), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[0])
if testTree.root == nil {
t.Logf("'%v': Node deleted\t\t(PASSED)", purpose[0])
} else {
t.Errorf("'%v': Node deleted\t\t(FAILED)", purpose[0])
}
testTree.root = &node{data: Int(5), height: 1, balance: 0, left: nil, right: nil}
testTree.root.left = &node{data: Int(4), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[1])
if testTree.root != nil && testTree.root.left == nil {
t.Logf("'%v': Node deleted\t(PASSED)", purpose[1])
if testTree.root.data == Int(4) {
t.Logf("'%v': Node replaced\t(PASSED)", purpose[1])
} else {
t.Errorf("'%v': Node replaced\t(FAILED)", purpose[1])
}
} else {
t.Errorf("'%v': Node deleted\t(FAILED)", purpose[1])
}
testTree.root = &node{data: Int(5), height: 2, balance: 1, left: nil, right: nil}
testTree.root.left = &node{data: Int(4), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[2])
if testTree.root != nil && testTree.root.left == nil {
t.Logf("'%v': Node deleted\t(PASSED)", purpose[2])
if testTree.root.data == Int(5) {
t.Logf("'%v': Node replaced\t(PASSED)", purpose[2])
} else {
t.Errorf("'%v': Node replaced\t(FAILED)", purpose[2])
}
} else {
t.Errorf("'%v': Node deleted\t(FAILED)", purpose[2])
}
testTree.root = &node{data: Int(5), height: 2, balance: -1, left: nil, right: nil}
testTree.root.right = &node{data: Int(6), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[3])
if testTree.root != nil && testTree.root.right == nil {
t.Logf("'%v': Node deleted\t(PASSED)", purpose[3])
if testTree.root.data == Int(6) {
t.Logf("'%v': Node replaced\t(PASSED)", purpose[3])
} else {
t.Errorf("'%v': Node replaced\t(FAILED)", purpose[3])
}
} else {
t.Errorf("'%v': Node deleted\t(FAILED)", purpose[3])
}
testTree.root = &node{data: Int(5), height: 2, balance: -1, left: nil, right: nil}
testTree.root.right = &node{data: Int(6), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[4])
if testTree.root != nil && testTree.root.right == nil {
t.Logf("'%v': Node deleted\t(PASSED)", purpose[4])
if testTree.root.data == Int(5) {
t.Logf("'%v': Node replaced\t(PASSED)", purpose[4])
} else {
t.Errorf("'%v': Node replaced\t(FAILED)", purpose[4])
}
} else {
t.Errorf("'%v': Node deleted\t(FAILED)", purpose[4])
}
testTree.root = &node{data: Int(5), height: 3, balance: -1, left: nil, right: nil}
testTree.root.left = &node{data: Int(4), height: 1, balance: 0, left: nil, right: nil}
testTree.root.right = &node{data: Int(6), height: 2, balance: 1, left: nil, right: nil}
testTree.root.right.left = &node{data: Int(5), height: 1, balance: 0, left: nil, right: nil}
testTree.Remove(input[5])
if testTree.root != nil && testTree.root.right != nil && testTree.root.left != nil && testTree.root.right.left == nil {
t.Logf("'%v': Node deleted\t(PASSED)", purpose[5])
if testTree.root.data == Int(5) && testTree.root.left.data == Int(4) && testTree.root.right.data == Int(6) {
t.Logf("'%v': Node replaced\t(PASSED)", purpose[5])
} else {
t.Errorf("'%v': Node replaced\t(FAILED)", purpose[5])
}
} else {
t.Errorf("'%v': Node deleted\t(FAILED)", purpose[5])
}
}