-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree_test.go
237 lines (191 loc) · 4.68 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
package gtl
import (
"strings"
"testing"
)
func TestTreeKeyInt(t *testing.T) {
var tree Tree[int, string]
tree.Set("hello", 1, 2, 3)
tree.Set("world", 1, 2)
tree.Set("idk", 1)
data := tree.Fetch(1, 2, 3)
if data.Get() != "hello" {
t.Fatalf("expected hello, got `%s`", data.Get())
}
data = tree.Fetch(1, 2)
if data.Get() != "world" {
t.Fatalf("expected world, got %s", data.Get())
}
}
func TestTree(t *testing.T) {
var tree Tree[string, int]
tree.Set(1, strings.Split("d/c/b/a", "/")...)
tree.Set(2, strings.Split("d/c/b/a", "/")...)
tree.Set(8, strings.Split("d/c/b", "/")...)
tree.Set(3, strings.Split("d/c", "/")...)
tree.Set(4, "d")
depth, data := tree.Get(strings.Split("d/c/b/a", "/")...)
if !data.HasValue() || depth != 3 {
t.Fatalf("failed, expecting depth 3, got %d", depth)
}
if data.Get() != 2 {
t.Fatal("n is not 2")
}
depth, data = tree.Get("d", "c")
if !data.HasValue() || depth != 1 {
t.Fatal("failed 2")
}
if data.Get() != 3 {
t.Fatal("n is not 3")
}
depth, data = tree.Get("d")
if !data.HasValue() || depth != 0 {
t.Fatal("failed 3")
}
if data.Get() != 4 {
t.Fatal("n is not 4")
}
depth, data = tree.Get("d", "c", "b", "a", "z", "s")
if !data.HasValue() || depth != 3 {
t.Fatal("failed 4")
}
if data.Get() != 2 {
t.Fatal("n is not 2")
}
tree.Del("d", "c", "b", "a")
data = tree.Fetch("d", "c", "b", "a")
if data.HasValue() {
t.Fatal("Data has not been removed from `a`")
}
data = tree.Fetch("d", "c", "b")
if !data.HasValue() {
t.Fatal("Data has been removed from `b`")
}
tree.Del("d", "c")
data = tree.Fetch("d", "c", "b")
if data.HasValue() {
t.Fatal("Data has not been removed from `c`")
}
data = tree.Fetch("d", "c")
if data.HasValue() {
t.Fatal("Data has not been removed from `c`")
}
data = tree.Fetch("d")
if !data.HasValue() {
t.Fatal("Data has been removed from `d`")
}
}
func TestTreeTravel(t *testing.T) {
var tree Tree[string, int]
tree.Set(1, strings.Split("d/c/b/a", "/")...)
tree.Set(2, strings.Split("d/c/b", "/")...)
tree.Set(3, strings.Split("d/c", "/")...)
tree.Set(4, "d")
tree.Set(80, "d", "c", "x")
rangeCount := 0
tree.RangeAll(func(node *Tree[string, int]) bool {
rangeCount++
return true
})
if rangeCount != 5 {
t.Fatalf("Unexpected range: %d<>5", rangeCount)
}
tree.RangeLimit(func(node *Tree[string, int]) bool {
if node.Name() != "d" {
t.Fatal("expected d only")
}
return true
}, 1)
iter := 0
tree.RangeLevel(func(node *Tree[string, int]) bool {
iter++
if node.Name() != "d" {
t.Fatalf("Expected d, got %s", node.Name())
}
return true
}, 0)
if iter != 1 {
t.Fatalf("unexpected iterations: %d", iter)
}
countLevel := 0
tree.RangeLevel(func(node *Tree[string, int]) bool {
countLevel++
if node.Name() != "c" {
t.Fatalf("Expected b, got %s", node.Name())
}
return true
}, 1) // should iterate over c's child
if countLevel != 1 {
t.Fatalf("Unexpected count level: %d", countLevel)
}
countLevel = 0
tree.RangeLevel(func(node *Tree[string, int]) bool {
countLevel++
if countLevel == 1 && node.Name() != "b" {
t.Fatalf("Expected b, got %s", node.Name())
}
if countLevel == 2 && node.Name() != "x" {
t.Fatalf("Expected x, got %s", node.Name())
}
if countLevel > 2 {
t.Fatalf("Iterated over childs more than two times")
}
return true
}, 2) // should iterate over c's child
if countLevel != 2 {
t.Fatalf("Unexpected count level: %d", countLevel)
}
tree.Del("d", "c", "x")
count := 0
tree.RangeLimit(func(node *Tree[string, int]) bool {
switch count {
case 0:
if node.Name() != "c" {
t.Fatal("expected c only")
}
case 1:
if node.Name() != "d" {
t.Fatal("expected d only")
}
}
count++
return true
}, 2)
order := []string{"a", "b", "c", "d"}
datas := []int{1, 2, 3, 4}
idx := 0
tree.Range(func(n *Tree[string, int]) bool {
if n.Name() != order[idx] {
t.Fatalf("unexpected element. Expected %s got %s", order[idx], n.Name())
}
if n.Data().Get() != datas[idx] {
t.Fatalf("unexpected data. Expected %d got %d", datas[idx], n.Data().Get())
}
path := n.Path()
for i, j := idx, len(path)-1; i < len(path); i++ {
if path[i] != order[j] {
t.Fatalf("unexpected path: %d: %s <> %s", idx, path[i], order[j])
}
j--
}
idx++
return true
})
if idx != len(order) {
t.Fatal("Not travelled enough")
}
idx = 0
tree.Range(func(n *Tree[string, int]) bool {
if n.Name() != order[idx] {
t.Fatalf("unexpected element. Expected %s got %s", order[idx], n.Name())
}
if n.Data().Get() != datas[idx] {
t.Fatalf("unexpected data. Expected %d got %d", datas[idx], n.Data().Get())
}
idx++
return true
})
if idx == 3 {
t.Fatal("expecting 3 iterations")
}
}