-
Notifications
You must be signed in to change notification settings - Fork 1
/
rbt_test.go
82 lines (69 loc) · 1.38 KB
/
rbt_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
package rbt
import (
"strconv"
"testing"
)
func assertEqual(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Fatalf("not equal, a=%s,b=%s", a, b)
}
}
func TestTraverse(t *testing.T) {
tree := NewTree()
tree.Insert(1, "1")
tree.Insert(3, "3")
tree.Insert(4, "4")
tree.Insert(6, "6")
tree.Insert(5, "5")
tree.Insert(2, "2")
if tree.Size() != 6 {
t.Error("Error whilst insertion")
}
}
func TestSearch(t *testing.T) {
tree := NewTree()
tree.Insert(1, "1")
tree.Insert(2, "2")
tree.Insert(3, "3")
n := tree.Search(1)
if n.value != "1" {
t.Error("Wrong value")
}
}
func TestDelete(t *testing.T) {
tree := NewTree()
for i := 1; i < 1000; i++ {
tree.Insert(int64(i), strconv.Itoa(i))
}
for i := 1; i <= 1000; i++ {
tree.Delete(int64(i))
}
if tree.Size() != 0 {
t.Error("Error whilst deletion")
}
}
func Test_Nearest(t *testing.T) {
tree := NewTree()
tree.Insert(1, "")
tree.Insert(2, "")
tree.Insert(3, "")
tree.Insert(5, "")
tree.Insert(8, "")
tree.Insert(9, "")
tree.Insert(12, "")
y := tree.Nearest(int64(10))
assertEqual(t, y.key, int64(9))
}
func Test_Circular_Queue(t *testing.T) {
tree := NewTree()
tree.Insert(1, "")
tree.Insert(2, "")
tree.Insert(3, "")
tree.Insert(5, "")
tree.Insert(8, "")
tree.Insert(9, "")
tree.Insert(12, "")
h := tree.Minimum()
assertEqual(t, h.key, int64(1))
assertEqual(t, h.successor().key, int64(2))
}