-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl_test.go
More file actions
233 lines (218 loc) · 4.51 KB
/
Copy pathavl_test.go
File metadata and controls
233 lines (218 loc) · 4.51 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
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
package avl
import (
"testing"
)
var cmpInt = func(a, b int) int { return a - b }
func isAVL[T any](node *Node[T]) bool {
if node == nil {
return true
}
l := 0
if node.left != nil {
l = int(node.left.heigth)
}
r := 0
if node.right != nil {
r = int(node.right.heigth)
}
return l-r < 2 || r-l < 2
}
func TestAVL_Insert(t *testing.T) {
a := NewSimple[int]()
a.Insert(10)
if a.root.heigth != 1 {
t.Errorf("invalid heigth: got %d, wanted %d", a.root.heigth, 1)
t.Logf("avl: %s", a)
}
a.Insert(5, 100)
if a.root.heigth != 2 {
t.Errorf("invalid heigth: got %d, wanted %d", a.root.heigth, 2)
t.Logf("avl: %s", a)
}
a.Insert(1)
if a.root.heigth != 3 {
t.Errorf("invalid heigth: got %d, wanted %d", a.root.heigth, 3)
t.Logf("avl: %s", a)
}
if !isAVL(a.root) {
t.Errorf("invalid tree: not an avl")
t.Logf("avl: %s", a)
}
a = NewSimple[int]()
for i := range 10 {
a.Insert(i)
if !isAVL(a.root) {
t.Errorf("invalid tree: not an avl")
t.Logf("avl: %s", a)
}
}
a = NewSimple[int]()
for i := range 10 {
a.Insert(9 - i)
if !isAVL(a.root) {
t.Errorf("invalid tree: not an avl")
t.Logf("avl: %s", a)
}
}
}
func TestAVL_Get(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
for i := range 10 {
g := a.Get(func(v int) int { return v - i })
if g == nil {
t.Errorf("get not found, wanted %d", i)
t.Logf("avl: %s", a)
} else if *g != i {
t.Errorf("invalid get: got %d, wanted %d", *g, i)
t.Logf("avl: %s", a)
}
}
}
func TestAVL_Delete(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
for i := range 10 {
a.Delete(i)
if !isAVL(a.root) {
t.Errorf("invalid tree: not an avl")
t.Logf("avl: %s", a)
}
g := a.Get(func(v int) int { return v - i })
if g != nil {
t.Errorf("Get found %d, wanted not", *g)
t.Logf("avl: %s", a)
}
}
a.Insert(1, 2, 3)
a.Delete(2)
st := a.Sort()
ln := len(st)
if ln != 2 || st[0] != 1 || st[1] != 3 {
t.Errorf("invalid value: got %v, wanted %v", st, []int{1, 3})
t.Logf("avl: %s", a)
}
}
func TestAVL_Min(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
for i := range 10 {
got := a.Min()
if got == nil {
t.Errorf("invalid min: got nil, wanted %v", i)
t.Logf("avl: %s", a)
} else if i != *got {
t.Errorf("invalid min: got %v, wanted %v", *got, i)
t.Logf("avl: %s", a)
}
a.Delete(i)
}
}
func TestAVL_Max(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
for i := range 10 {
i = 9 - i
got := a.Max()
if got == nil {
t.Errorf("invalid max: got nil, wanted %v", i)
t.Logf("avl: %s", a)
} else if i != *got {
t.Errorf("invalid min: got %v, wanted %v", *got, i)
t.Logf("avl: %s", a)
}
a.Delete(i)
}
}
func TestAVL_Sort(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
st := a.Sort()
for i := range 10 {
if st[i] != i {
t.Errorf("invalid value: got %d, wanted %d", st[i], i)
}
}
}
func TestAVL_Clone(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
cl := a.Clone()
a.Insert(11)
got := cl.Get(func(v int) int { return v - 11 })
if got != nil {
t.Errorf("invalid value: got %d, wanted nothing", *got)
t.Logf("avl: %s", a)
t.Logf("clone: %s", cl)
}
}
func TestAVL_Size(t *testing.T) {
a := NewSimple[int]()
for i := range 10 {
a.Insert(i)
}
if a.Size() != 10 {
t.Errorf("invalid size: got %d, wanted %d", a.Size(), 10)
t.Logf("avl: %s", a)
}
for i := range 10 {
a.Insert(i)
}
if a.Size() != 10 {
t.Errorf("invalid size: got %d, wanted %d", a.Size(), 10)
t.Logf("avl: %s", a)
}
for i := range 10 {
a.Delete(i)
}
if a.Size() != 0 {
t.Errorf("invalid size: got %d, wanted %d", a.Size(), 0)
t.Logf("avl: %s", a)
}
for i := range 10 {
a.Delete(i)
}
if a.Size() != 0 {
t.Errorf("invalid size: got %d, wanted %d", a.Size(), 0)
t.Logf("avl: %s", a)
}
}
type cloneStruct []int
func (c cloneStruct) Clone() cloneStruct {
dst := make([]int, len(c))
copy(dst, c)
return dst
}
func TestClonable_AutoClone(t *testing.T) {
a := New(func(a, b cloneStruct) int { return len(a) - len(b) })
cl := cloneStruct{1, 2, 3}
a.Insert(cl)
got := a.Get(func(v cloneStruct) int { return 3 - len(v) })
if got == nil {
t.Errorf("invalid value: got nil")
t.Logf("avl: %s", a)
return
}
for i := range *got {
(*got)[i] = (i + 1) << 3
}
ng := a.Get(func(v cloneStruct) int { return 3 - len(v) })
for i, r := range *ng {
if i+1 != r {
t.Errorf("invalid value: got %d, wanted %d", r, i+1)
t.Logf("avl: %s", a)
}
}
}