-
Notifications
You must be signed in to change notification settings - Fork 10
/
tree_test.go
225 lines (163 loc) · 4.95 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
package avltree
import (
"math/rand"
"testing"
)
func TestTree(t *testing.T) {
// no duplicates tests
tree := NewOrdered[int](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.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 == nil || *v != 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())
}
v = tree.At(0)
if v == nil || *v != 14 {
t.Errorf("Single value should be 14: %v\n", tree.At(0))
}
v, dupe = tree.Add(14)
if v == nil || *v != 14 || dupe != true {
t.Errorf("Result of add should be 14/true: %v/%v\n", v, dupe)
}
v, dupe = tree.Add(15)
if v == nil || *v != 15 || dupe != false {
t.Errorf("Result of add should be 15/false: %v/%v\n", v, dupe)
}
v = tree.At(1)
if v == nil || *v != 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 = NewOrdered[int](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.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 == nil || *v != 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())
}
v = tree.At(0)
if v == nil || *v != 14 {
t.Errorf("Single value should be 14: %v\n", tree.At(0))
}
v, dupe = tree.Add(14)
if v == nil || *v != 14 || dupe != false {
t.Errorf("Result of add should be 14/false: %v/%v\n", v, dupe)
}
v, dupe = tree.Add(15)
if v == nil || *v != 15 || dupe != false {
t.Errorf("Result of add should be 15/false: %v/%v\n", v, dupe)
}
v = tree.At(2)
if v == nil || *v != 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 == nil || *v != 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] != 15 {
t.Errorf("Slice is incorrect, expected 3/15: %v/%v\n", len(slice), slice[2])
}
// test Do
x := 0
tree.Do(func(z int) bool { x += z; return true })
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 == nil || *v != 16 || dupe != false {
t.Errorf("Result of add should be 16/false: %v/%v\n", v, dupe)
}
v = tree.Remove(14)
if v == nil || *v != 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)
x = 14
for v := range tree.Iter() {
if v != x {
t.Error("Iter expected", x, "got", v)
}
x++
}
if x != 21 {
t.Errorf("Iter ran wrong number of elements, expected 7, got %d\n", x-14)
}
// 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.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 int) bool {
var cur int
cur = elem
if prev > cur {
t.Errorf("Elements not in order, previous = %d, current = %d\n", prev, cur)
return false
}
prev = cur
return true
})
}