-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree_test.go
More file actions
159 lines (137 loc) · 4.17 KB
/
binaryTree_test.go
File metadata and controls
159 lines (137 loc) · 4.17 KB
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
package bosc_test
import (
"bosc"
"fmt"
"math/rand"
"testing"
)
type myIndex struct {
key int
value string
}
func (a myIndex) Compare(b bosc.Comparable) int {
return a.Key().(int) - b.(bosc.Comparable).Key().(int)
}
func (a myIndex) Key() interface{} {
return a.key
}
func newIndex(n int) *myIndex {
return &myIndex{key: n, value: fmt.Sprintf("Number %d", n)}
}
func Test_Add_Find(t *testing.T) {
for _, treeType := range bosc.TreeTypes() {
tree, err := bosc.NewTree(treeType)
if err != nil {
t.Errorf("Tree type <%s>. Error creating tree <%v>", treeType, err)
}
// Add values
for _, i := range rand.Perm(5000) {
tree.Add(newIndex(i))
}
// Search values
for _, i := range rand.Perm(5000) {
if index, err := tree.Find(&myIndex{key: i}); err != nil {
t.Errorf("Tree type <%s>. <%s>", treeType, err)
} else {
expected := fmt.Sprintf("Number %d", i)
if index.(*myIndex).value != expected {
t.Errorf("Tree type <%s>, Error retriving key. Expecting a value of <%s>, got <%s>", treeType, expected, index.(*myIndex).value)
}
}
}
}
}
func Test_Min_Max(t *testing.T) {
for _, treeType := range bosc.TreeTypes() {
tree, err := bosc.NewTree(treeType)
if err != nil {
t.Errorf("Tree type <%s>. Error creating tree <%v>", treeType, err)
}
// Add values
for _, i := range rand.Perm(5000) {
tree.Add(newIndex(i))
}
min := tree.Min().Key().(int)
max := tree.Max().Key().(int)
if min != 0 {
t.Errorf("Tree type <%s>. Wrong min value. Expecting <0>, got <%d>", treeType, min)
}
if max != 4999 {
t.Errorf("Tree type <%s>. Wrong max value. Expecting <5000>, got <%d>", treeType, max)
}
}
}
func Test_Add_Remove(t *testing.T) {
for _, treeType := range bosc.TreeTypes() {
tree, err := bosc.NewTree(treeType)
if err != nil {
t.Errorf("Tree type <%s>. Error creating tree <%v>", treeType, err)
}
// Add values
for _, i := range rand.Perm(5000) {
tree.Add(newIndex(i))
}
// Removing values
for _, i := range rand.Perm(5000) {
if !tree.Remove(newIndex(i)) {
t.Errorf("Tree type <%s>. Error removing item <%d>. Item not found", treeType, i)
}
if _, err := tree.Find(newIndex(i)); err == nil {
t.Errorf("Tree type <%s>. Error removing item <%d>: <%s>", treeType, newIndex(i), err)
}
}
}
}
func Test_Ranges(t *testing.T) {
for _, treeType := range bosc.TreeTypes() {
tree, err := bosc.NewTree(treeType)
if err != nil {
t.Errorf("Tree type <%s>. Error creating tree <%v>", treeType, err)
}
// Add values
for _, i := range rand.Perm(5000) {
tree.Add(newIndex(i))
}
// Removing values
last := -1
tree.RangeAll(func(c bosc.Comparable) {
if c.Key().(int) < 0 || c.Key().(int) > 5000 {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll <%d>", treeType, c.Key().(int))
}
if c.Key().(int) <= last {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll. Expecting <%d>, got <%d>", treeType, last, c.Key().(int))
}
last = c.Key().(int)
})
last = 2499
tree.RangeFrom(newIndex(2500), func(c bosc.Comparable) {
if c.Key().(int) < 2500 || c.Key().(int) > 5000 {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll <%d>", treeType, c.Key().(int))
}
if c.Key().(int) <= last {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll. Expecting <%d>, got <%d>", treeType, last, c.Key().(int))
}
last = c.Key().(int)
})
last = -1
tree.RangeTo(newIndex(2500), func(c bosc.Comparable) {
if c.Key().(int) < 0 || c.Key().(int) > 2500 {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll <%d>", treeType, c.Key().(int))
}
if c.Key().(int) <= last {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll. Expecting <%d>, got <%d>", treeType, last, c.Key().(int))
}
last = c.Key().(int)
})
last = 999
tree.Range(newIndex(1000), newIndex(4000), func(c bosc.Comparable) {
if c.Key().(int) < 1000 || c.Key().(int) > 4000 {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll <%d>", treeType, c.Key().(int))
}
if c.Key().(int) <= last {
t.Errorf("Tree type <%s>. Found a wrong value in RangeAll. Expecting <%d>, got <%d>", treeType, last, c.Key().(int))
}
last = c.Key().(int)
})
}
}