This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
forked from ancientlore/go-avltree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_test.go
263 lines (195 loc) · 5.89 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
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
package avltree
import (
"fmt"
"math/rand"
"testing"
)
func compareInt(a interface{}, b interface{}) int {
if a.(int) < b.(int) {
return -1
} else if a.(int) > b.(int) {
return 1
}
return 0
}
func TestTree(t *testing.T) {
// no duplicates tests
tree := New(compareInt, 0)
if tree.root != nil {
t.Errorf("Initialized tree root should be nil: %v\n", tree.root)
}
if tree.treeFlags != 0 {
t.Errorf("Initialized tree flags should be zero: %v\n", tree.treeFlags)
}
//if tree.compare != compareInt {
// t.Errorf("Initialized tree compare function not correct: %v\n", tree.compare)
//}
if tree.Len() != 0 || tree.Cap() != 0 || tree.Height() != 0 {
t.Errorf("Initialized tree sizes should all be zero: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
v, dupe := tree.Add(14)
if v.(int) != 14 || dupe != false {
t.Errorf("Result of add should be 14/false: %v/%v\n", v, dupe)
}
if tree.Len() != 1 || tree.Cap() != 1 || tree.Height() != 1 {
t.Errorf("Tree sizes with one element should all be one: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
if tree.At(0).(int) != 14 {
t.Errorf("Single value should be 14: %v\n", tree.At(0))
}
v, dupe = tree.Add(14)
if v.(int) != 14 || dupe != true {
t.Errorf("Result of add should be 14/true: %v/%v\n", v, dupe)
}
v, dupe = tree.Add(15)
if v.(int) != 15 || dupe != false {
t.Errorf("Result of add should be 15/false: %v/%v\n", v, dupe)
}
if tree.At(1).(int) != 15 {
t.Errorf("Second value should be 15: %v\n", tree.At(0))
}
if tree.Len() != 2 || tree.Cap() != 3 || tree.Height() != 2 {
t.Errorf("Tree sizes with two elements should be 2/3/2: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
// reinitialize and allow duplicates
tree.Init(compareInt, AllowDuplicates)
if tree.root != nil {
t.Errorf("Reinitialized tree root should be nil: %v\n", tree.root)
}
if tree.treeFlags != AllowDuplicates {
t.Errorf("Reinitialized tree flags should be AllowDuplicates: %v\n", tree.treeFlags)
}
//if tree.compare != compareInt {
// t.Errorf("Reinitialized tree compare function not correct: %v\n", tree.compare)
//}
if tree.Len() != 0 || tree.Cap() != 0 || tree.Height() != 0 {
t.Errorf("Initialized tree sizes should all be zero: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
v, dupe = tree.Add(14)
if v.(int) != 14 || dupe != false {
t.Errorf("Result of add should be 14/false: %v/%v\n", v, dupe)
}
if tree.Len() != 1 || tree.Cap() != 1 || tree.Height() != 1 {
t.Errorf("Tree sizes with one element should all be one: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
if tree.At(0).(int) != 14 {
t.Errorf("Single value should be 14: %v\n", tree.At(0))
}
v, dupe = tree.Add(14)
if v.(int) != 14 || dupe != false {
t.Errorf("Result of add should be 14/false: %v/%v\n", v, dupe)
}
v, dupe = tree.Add(15)
if v.(int) != 15 || dupe != false {
t.Errorf("Result of add should be 15/false: %v/%v\n", v, dupe)
}
if tree.At(2).(int) != 15 {
t.Errorf("Third value should be 15: %v\n", tree.At(0))
}
if tree.Len() != 3 || tree.Cap() != 3 || tree.Height() != 2 {
t.Errorf("Tree sizes with three elements should be 3/3/2: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
// test Find
v = tree.Find(15)
if v.(int) != 15 {
t.Errorf("Find should locate 15: %v\n", v)
}
v = tree.Find(90)
if v != nil {
t.Errorf("Found an item that isn't there: %v\n", v)
}
// test Data (slice)
slice := tree.Data()
if len(slice) != 3 || slice[2].(int) != 15 {
t.Errorf("Slice is incorrect, expected 3/15: %v/%v\n", len(slice), slice[2].(int))
}
// test Do
x := 0
tree.Do(func(z interface{}) { x += z.(int) })
if x != 43 {
t.Errorf("Do function did not add up values correctly, expected 43: %d\n", x)
}
// test Remove
v, dupe = tree.Add(16)
if v.(int) != 16 || dupe != false {
t.Errorf("Result of add should be 16/false: %v/%v\n", v, dupe)
}
v = tree.Remove(14)
if v.(int) != 14 || tree.Len() != 3 {
t.Errorf("Result of Remove should be 14/3: %v/%v\n", v, tree.Len())
}
// test Iter
tree.Add(20)
tree.Add(19)
tree.Add(18)
tree.Add(17)
// test FindPath first.
vs := tree.FindPath(15)
if fmt.Sprintf("%v", vs) != "[16 15]" {
t.Errorf("FindPath expected [16 15] got %v\n", fmt.Sprintf("%v", vs))
}
vs = tree.FindPath(20)
if fmt.Sprintf("%v", vs) != "[16 19 20]" {
t.Errorf("FindPath expected [16 19 20] got %v\n", fmt.Sprintf("%v", vs))
}
x = 14
for v := range tree.Iter() {
if v.(int) != x {
t.Error("Iter expected", x, "got", v.(int))
}
x++
}
if x != 21 {
t.Errorf("Iter ran wrong number of elements, expected 7, got %d\n", x-14)
}
x = 15
// test IterRange
for v := range tree.IterRange(15, 19) {
if v.(int) != x {
t.Error("IterRange expected", x, "got", v.(int))
}
x++
}
if x != 20 {
t.Errorf("IterRange ran wrong number of elements, expect 3, got %d\n", x-15)
}
// test clear
tree.Clear()
if tree.root != nil {
t.Errorf("Cleared tree root should be nil: %v\n", tree.root)
}
if tree.treeFlags != AllowDuplicates {
t.Errorf("Cleared tree flags should still be AllowDuplicates: %v\n", tree.treeFlags)
}
//if tree.compare != compareInt {
// t.Errorf("Cleared tree compare function not correct: %v\n", tree.compare)
//}
if tree.Len() != 0 || tree.Cap() != 0 || tree.Height() != 0 {
t.Errorf("Cleared tree sizes should all be zero: %d Len, %d Cap, %d Height\n",
tree.Len(), tree.Cap(), tree.Height())
}
// one more test, larger data
for j := 0; j < 100000; j++ {
tree.Add(rand.Int())
}
if tree.Len() != 100000 {
t.Errorf("Expected 100000 elements, got %d\n", tree.Len())
}
var prev int
prev = -1
// make sure elements sorted
tree.Do(func(elem interface{}) {
var cur int
cur = elem.(int)
if prev > cur {
t.Errorf("Elements not in order, previous = %d, current = %d\n", prev, cur)
}
prev = cur
})
}