-
Notifications
You must be signed in to change notification settings - Fork 3
/
table_cb_test.go
249 lines (205 loc) · 5.21 KB
/
table_cb_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
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart
import (
"net/netip"
"reflect"
"slices"
"testing"
)
func TestSupernetsEdgeCaseCB(t *testing.T) {
t.Parallel()
rtbl := new(Table[any])
pfx := mpp("::1/128")
rtbl.Supernets(pfx)(func(_ netip.Prefix, _ any) bool {
t.Errorf("empty table, must not range over")
return false
})
val := "foo"
rtbl.Insert(pfx, val)
rtbl.Supernets(netip.Prefix{})(func(_ netip.Prefix, _ any) bool {
t.Errorf("invalid prefix, must not range over")
return false
})
rtbl.Supernets(netip.Prefix{})(func(p netip.Prefix, v any) bool {
if p != pfx {
t.Errorf("Supernets(%v), got: %v, want: %v", pfx, p, pfx)
return false
}
if v.(string) != val {
t.Errorf("Supernets(%v), got: %v, want: %v", pfx, v.(string), val)
return false
}
return true
})
}
func TestSubnetsCB(t *testing.T) {
t.Parallel()
rtbl := new(Table[any])
pfx := mpp("::1/128")
rtbl.Subnets(pfx)(func(_ netip.Prefix, _ any) bool {
t.Errorf("empty table, must not range over")
return false
})
val := "foo"
rtbl.Insert(pfx, val)
rtbl.Subnets(netip.Prefix{})(func(_ netip.Prefix, _ any) bool {
t.Errorf("invalid prefix, must not range over")
return false
})
rtbl.Subnets(pfx)(func(p netip.Prefix, v any) bool {
if p != pfx {
t.Errorf("Subnet(%v), got: %v, want: %v", pfx, p, pfx)
return false
}
if v.(string) != val {
t.Errorf("Subnet(%v), got: %v, want: %v", pfx, v.(string), val)
return false
}
return true
})
}
func TestAll(t *testing.T) {
t.Parallel()
pfxs := randomPrefixes(10_000)
t.Run("All", func(t *testing.T) {
rtbl := new(Table[int])
seen := make(map[netip.Prefix]int, 10_000)
for _, item := range pfxs {
rtbl.Insert(item.pfx, item.val)
seen[item.pfx] = item.val
}
// check if pfx/val is as expected
rtbl.All()(func(pfx netip.Prefix, val int) bool {
if seen[pfx] != val {
t.Errorf("%v got value: %v, expected: %v", pfx, val, seen[pfx])
}
delete(seen, pfx)
return true
})
// check if all entries visited
if len(seen) != 0 {
t.Fatalf("traverse error, not all entries visited")
}
})
t.Run("All_4&6", func(t *testing.T) {
rtbl := new(Table[int])
seen := make(map[netip.Prefix]int, 10_000)
for _, item := range pfxs {
rtbl.Insert(item.pfx, item.val)
seen[item.pfx] = item.val
}
// check if pfx/val is as expected
rtbl.All4()(func(pfx netip.Prefix, val int) bool {
if seen[pfx] != val {
t.Errorf("%v got value: %v, expected: %v", pfx, val, seen[pfx])
}
delete(seen, pfx)
return true
})
rtbl.All6()(func(pfx netip.Prefix, val int) bool {
if seen[pfx] != val {
t.Errorf("%v got value: %v, expected: %v", pfx, val, seen[pfx])
}
delete(seen, pfx)
return true
})
// check if all entries visited
if len(seen) != 0 {
t.Fatalf("traverse error, not all entries visited")
}
})
// make an iteration and update the values in the callback
t.Run("All and Update", func(t *testing.T) {
rtbl := new(Table[int])
seen := make(map[netip.Prefix]int, 10_000)
for _, item := range pfxs {
rtbl.Insert(item.pfx, item.val)
seen[item.pfx] = item.val + 1
}
// update callback, add 1 to val
updateValue := func(val int, _ bool) int {
return val + 1
}
yield := func(pfx netip.Prefix, _ int) bool {
rtbl.Update(pfx, updateValue)
return true
}
// iterate and update the values
rtbl.All()(yield)
// test if all values got updated, yield now as closure
rtbl.All()(func(pfx netip.Prefix, val int) bool {
if seen[pfx] != val {
t.Errorf("%v got value: %v, expected: %v", pfx, val, seen[pfx])
}
return true
})
})
t.Run("All with premature exit", func(t *testing.T) {
rtbl := new(Table[int])
for _, item := range pfxs {
rtbl.Insert(item.pfx, item.val)
}
// check if callback stops prematurely
countV6 := 0
rtbl.All()(func(pfx netip.Prefix, _ int) bool {
// max 1000 IPv6 prefixes
if !pfx.Addr().Is4() {
countV6++
}
// premature STOP condition
return countV6 < 1000
})
// check if iteration stopped with error
if countV6 > 1000 {
t.Fatalf("expected premature stop with error")
}
})
}
// After go version 1.22 we can use range iterators
func TestAllSorted(t *testing.T) {
t.Parallel()
n := 10_000
pfxs := randomPrefixes(n)
t.Run("All versus slices.SortFunc", func(t *testing.T) {
t.Parallel()
expect := make([]netip.Prefix, 0, n)
got := make([]netip.Prefix, 0, n)
rtbl := new(Table[int])
for _, item := range pfxs {
rtbl.Insert(item.pfx, item.val)
expect = append(expect, item.pfx)
}
slices.SortFunc(expect, cmpPrefix)
rtbl.AllSorted()(func(pfx netip.Prefix, _ int) bool {
got = append(got, pfx)
return true
})
if !reflect.DeepEqual(got, expect) {
t.Fatalf("All differs with slices.SortFunc")
}
})
}
func BenchmarkAll(b *testing.B) {
n := 100_000
rtbl := new(Table[int])
for _, item := range randomPrefixes(n) {
rtbl.Insert(item.pfx, item.val)
}
b.Run("All", func(b *testing.B) {
b.ResetTimer()
for range b.N {
rtbl.All()(func(_ netip.Prefix, _ int) bool {
return true
})
}
})
b.Run("AllSorted", func(b *testing.B) {
b.ResetTimer()
for range b.N {
rtbl.AllSorted()(func(_ netip.Prefix, _ int) bool {
return true
})
}
})
}