-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_test.go
91 lines (85 loc) · 1.72 KB
/
tree_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
package splaytree
import "testing"
func TestClear(t *testing.T) {
tree := NewSplayTree()
tree.InsertAll([]Item{Int(6), Int(4), Int(3), Int(1)})
tree.Check()
tree.Clear()
if tree.root != nil {
t.Errorf("root != nil")
}
}
func TestCount(t *testing.T) {
tree := NewSplayTree()
if tree.Count() != 0 {
t.Errorf("count !0")
}
tree.InsertAll([]Item{Int(6), Int(4), Int(3), Int(1)})
tree.Check()
if tree.Count() != 4 {
t.Errorf("count !4")
}
tree.DeleteAll([]Item{Int(3), Int(2)})
if tree.Count() != 3 {
t.Errorf("count !3")
}
tree.Clear()
if tree.Count() != 0 {
t.Errorf("count !0")
}
}
func TestHeight(t *testing.T) {
tree := NewSplayTree()
if tree.Height() != -1 {
t.Errorf("height !-1")
}
tree.Insert(Int(3))
if tree.Height() != 0 {
t.Errorf("height !0")
}
tree.Insert(Int(3))
if tree.Height() != 0 {
t.Errorf("height !0")
}
tree.Insert(Int(1))
if h := tree.Height(); h != 1 {
t.Errorf("height !1 %v %v", h, tree.Count())
}
tree.Insert(Int(2))
if h := tree.Height(); h != 1 {
t.Errorf("height !1 %v %v", h, tree.Count())
}
tree.Insert(Int(9))
if h := tree.Height(); h != 2 && h != 3 {
t.Errorf("height !<2 %v %v", h, tree.Count())
}
tree.Check()
}
func TestNonEmpty(t *testing.T) {
tree := NewSplayTree()
if tree.NonEmpty() != false {
t.Errorf("NonEmpty !false")
}
tree.Insert(Int(2))
if tree.NonEmpty() != true {
t.Errorf("NonEmpty !true")
}
tree.Clear()
if tree.NonEmpty() {
t.Errorf("NonEmpty clear")
}
}
func TestRoot(t *testing.T) {
tree := NewSplayTree()
if r := tree.Root(); r != nil {
t.Errorf("Root !false")
}
tree.Insert(Int(2))
if r := tree.Root(); r == nil {
t.Errorf("Root !true")
}
tree.Clear()
if r := tree.Root(); r != nil {
t.Errorf("Root !false")
}
}