-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleBinaryTree_test.go
270 lines (223 loc) · 6.65 KB
/
simpleBinaryTree_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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package bosc
import (
"testing"
"math/rand"
"fmt"
)
type myNumber int
func (a myNumber) Compare(b Comparable) int {
return int(a.Key().(int) - b.(myNumber).Key().(int))
}
func (a myNumber) Key() interface{} {
return int(a)
}
func TestSimpleBinaryTree(t *testing.T) {
tree := newSimpleBinaryTree()
perms := rand.Perm(1000)
for _, i := range perms {
tree.Add(myNumber(i))
}
for i := 0; i < 1000; i++ {
if _, err := tree.Find(myNumber(i)); err != nil {
t.Error("La cagastes Burt Lancaster")
}
}
tree.RangeAll(func(val Comparable) { fmt.Printf("%v ", val) })
fmt.Println("")
tree.Range(myNumber(100), myNumber(200), func(val Comparable) { fmt.Printf("%v ", val) })
}
func TestSimpleBinaryTree_Remove_NoChilds(t *testing.T) {
// Let's create a tree unbalanced to the right
tree := newSimpleBinaryTree()
for i := 0; i < 10; i++ {
tree.Add(myNumber(i))
}
// Removing the last one
tree.Remove(myNumber(9))
values := []myNumber{}
tree.RangeAll(func(val Comparable) { values = append(values, val.(myNumber)) })
if !testSliceEqual([]myNumber{0, 1, 2, 3, 4, 5, 6, 7, 8}, values) {
t.Error("Error removing the a node with no childs")
}
}
func TestSimpleBinaryTree_Remove_OnlyOneNodeInTree(t *testing.T) {
tree := newSimpleBinaryTree()
tree.Add(myNumber(5))
tree.Remove(myNumber(5))
}
func TestSimpleBinaryTree_Remove_OneChild(t *testing.T) {
// Let's create an unbalanced tree to the right
treeRight := newSimpleBinaryTree()
for i := 0; i < 10; i++ {
treeRight.Add(myNumber(i))
}
// Let's create an unbalanced tree to the left
treeLeft := newSimpleBinaryTree()
for i := 9; i >= 0; i-- {
treeLeft.Add(myNumber(i))
}
// Removing a node in the middle
treeRight.Remove(myNumber(5))
treeLeft.Remove(myNumber(5))
// Let's check right tree type
values := []myNumber{}
treeRight.RangeAll(func(val Comparable) { values = append(values, val.(myNumber)) })
if !testSliceEqual([]myNumber{0, 1, 2, 3, 4, 6, 7, 8, 9}, values) {
t.Error("Error removing a node with one right child")
}
// Let's check left tree type
values = []myNumber{}
treeLeft.RangeAll(func(val Comparable) { values = append(values, val.(myNumber)) })
if !testSliceEqual([]myNumber{0, 1, 2, 3, 4, 6, 7, 8, 9}, values) {
t.Error("Error removing a node with one left child")
}
// Let's remove the root
treeLeft.Remove(myNumber(9))
}
func TestSimpleBinaryTree_Remove2Childs(t *testing.T) {
tree := newSimpleBinaryTree()
tree.Add(myNumber(5))
tree.Add(myNumber(2))
tree.Add(myNumber(3))
tree.Add(myNumber(4))
tree.Add(myNumber(1))
tree.Add(myNumber(8))
tree.Add(myNumber(7))
tree.Add(myNumber(9))
tree.Add(myNumber(6))
tree.Add(myNumber(0))
tree.Remove(myNumber(2))
values := []myNumber{}
tree.RangeAll(func(val Comparable) { values = append(values, val.(myNumber)) })
if !testSliceEqual([]myNumber{0, 1, 3, 4, 5, 6, 7, 8, 9}, values) {
t.Error("Error removing a node with one left child")
}
// Let's remove the root
tree.Remove(myNumber(5))
values = []myNumber{}
tree.RangeAll(func(val Comparable) { values = append(values, val.(myNumber)) })
if !testSliceEqual([]myNumber{0, 1, 3, 4, 6, 7, 8, 9}, values) {
t.Error("Error removing a node with one left child")
}
}
func TestSimpleBinaryTree_Range(t *testing.T) {
ITEMS := 1000000
tree := newSimpleBinaryTree()
for _, j := range rand.Perm(ITEMS) {
if err := tree.Add(myNumber(j)); err != nil {
t.Error(err)
}
}
from := myNumber(45000)
to := myNumber(46000)
nodes := 0
tree.Range(from, to, func(val Comparable) {
nodes++
if val.Compare(from) < 0 {
t.Errorf("Error in range. Found a value <%d> that shouldn't be in range", val.Key().(int))
}
if val.Compare(to) > 0 {
t.Errorf("Error in range. Found a value <%d> that shouldn't be in range", val.Key().(int))
}
})
nodes_expected := to.Key().(int) - from.Key().(int) + 1
if nodes != nodes_expected {
t.Errorf("Range function executed over %d nodes. Expecting %d", nodes, nodes_expected)
}
}
func TestSimpleBinaryTree_RangeFrom(t *testing.T) {
ITEMS := 1000000
tree := newSimpleBinaryTree()
for _, j := range rand.Perm(ITEMS) {
if err := tree.Add(myNumber(j)); err != nil {
t.Error(err)
}
}
from := myNumber(1000)
nodes := 0
tree.RangeFrom(from, func(val Comparable) {
nodes++
if val.Compare(from) < 0 {
t.Errorf("Error in range. Found a value <%d> that shouldn't be in range", val.Key().(int))
}
})
nodes_expected := tree.Max().Key().(int) - from.Key().(int) + 1
if nodes != nodes_expected {
t.Errorf("Range function executed over %d nodes. Expecting %d", nodes, nodes_expected)
}
}
func TestSimpleBinaryTree_RangeTo(t *testing.T) {
ITEMS := 1000000
tree := newSimpleBinaryTree()
for _, j := range rand.Perm(ITEMS) {
if err := tree.Add(myNumber(j)); err != nil {
t.Error(err)
}
}
to := myNumber(5000)
nodes := 0
tree.RangeTo(to, func(val Comparable) {
nodes++
if val.Compare(to) > 0 {
t.Errorf("Error in range. Found a value <%d> that shouldn't be in range", val.Key().(int))
}
})
nodes_expected := to.Key().(int) - tree.Min().Key().(int) + 1
if nodes != nodes_expected {
t.Errorf("Range function executed over %d nodes. Expecting %d", nodes, nodes_expected)
}
}
func TestSimpleBinaryTree_RemoveBrutalRandom(t *testing.T) {
ITEMS := 100000
TIMES := 5
for i := 0; i < TIMES; i++ {
control := make(map[int]bool, ITEMS)
// Let's create a tree and fill it with random nodes
tree := newSimpleBinaryTree()
for _, j := range rand.Perm(ITEMS) {
if err := tree.Add(myNumber(j)); err != nil {
t.Error(err)
}
control[j] = true
}
// Let's check if all nodes are in the tree
for _, j := range rand.Perm(ITEMS) {
if _, err := tree.Find(myNumber(j)); err != nil {
t.Error(err)
}
}
// Let's remove all nodes randomly
for _, j := range rand.Perm(ITEMS) {
if ok := tree.Remove(myNumber(j)); !ok {
t.Errorf("Unkown error removing item <%v>, loop:<%v>", j, i)
}
delete(control, j)
// Let's do some checks from time to time
if j%10000 == 0 || j < 100 {
// Tree has correct len?
if tree.Count() != uint64(len(control)) {
t.Errorf("Wrong tree lenght. Error removing. Expecting %d items, got %d", len(control), tree.Count())
}
// All items in tree are inside control?
tree.RangeAll(func(val Comparable) {
if _, ok := control[val.Key().(int)]; !ok {
t.Errorf("Error removing items. %d removed unnintentionally", val.Key().(int))
}
})
}
}
}
}
// Returns true if 2 slices are equal, in length, values and order.
// false otherwise
func testSliceEqual(slice1 []myNumber, slice2 []myNumber) bool {
if len(slice1) != len(slice2) {
return false
}
for i := range (slice1) {
if slice1[i] != slice2[i] {
return false
}
}
return true
}