Debian Codegraph @

radix_test.go

raw symbols SCIP
1 package radixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/
2
3 import (
4 crandscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/ "crypto/randscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/"
5 "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/"
6 "reflectscip-go gomod github.com/golang/go/src go1.16 reflect/"
7 "sortscip-go gomod github.com/golang/go/src go1.16 sort/"
8 "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/"
9 "testingscip-go gomod github.com/golang/go/src go1.16 testing/"
10 )
11
12 func TestRadixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestRadix().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
13 var min, max string
14 inp := make(map[string]interface{})
15 for i := 0; i < 1000; i++ {
16 gen := generateUUIDscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/generateUUID().()
17 inp[gen] = i
18 if gen < min || i == 0 {
19 min = gen
20 }
21 if gen > max || i == 0 {
22 max = gen
23 }
24 }
25
26 r := NewFromMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/NewFromMap().(inp)
27 if r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() != len(inp) {
28 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad length: %v %v", r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(), len(inp))
29 }
30
31 r.Walkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Walk().(func(k string, v interface{}) bool {
32 println(k)
33 return false
34 })
35
36 for k, v := range inp {
37 out, ok := r.Getscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Get().(k)
38 if !ok {
39 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("missing key: %v", k)
40 }
41 if out != v {
42 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("value mis-match: %v %v", out, v)
43 }
44 }
45
46 // Check min and max
47 outMin, _, _ := r.Minimumscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Minimum().()
48 if outMin != min {
49 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad minimum: %v %v", outMin, min)
50 }
51 outMax, _, _ := r.Maximumscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Maximum().()
52 if outMax != max {
53 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad maximum: %v %v", outMax, max)
54 }
55
56 for k, v := range inp {
57 out, ok := r.Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().(k)
58 if !ok {
59 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("missing key: %v", k)
60 }
61 if out != v {
62 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("value mis-match: %v %v", out, v)
63 }
64 }
65 if r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() != 0 {
66 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad length: %v", r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().())
67 }
68 }
69
70 func TestRootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestRoot().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
71 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
72 _, ok := r.Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().("")
73 if ok {
74 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad")
75 }
76 _, ok = r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("", true)
77 if ok {
78 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad")
79 }
80 val, ok := r.Getscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Get().("")
81 if !ok || val != true {
82 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad: %v", val)
83 }
84 val, ok = r.Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().("")
85 if !ok || val != true {
86 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad: %v", val)
87 }
88 }
89
90 func TestDeletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestDelete().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
91
92 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
93
94 s := []string{"", "A", "AB"}
95
96 for _, ss := range s {
97 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(ss, true)
98 }
99
100 for _, ss := range s {
101 _, ok := r.Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().(ss)
102 if !ok {
103 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad %q", ss)
104 }
105 }
106 }
107
108 func TestDeletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestDeletePrefix().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
109 type exp struct {
110 inp []string
111 prefix string
112 out []string
113 numDeleted int
114 }
115
116 cases := []exp{
117 {[]string{"", "A", "AB", "ABC", "R", "S"}, "A", []string{"", "R", "S"}, 3},
118 {[]string{"", "A", "AB", "ABC", "R", "S"}, "ABC", []string{"", "A", "AB", "R", "S"}, 1},
119 {[]string{"", "A", "AB", "ABC", "R", "S"}, "", []string{}, 6},
120 {[]string{"", "A", "AB", "ABC", "R", "S"}, "S", []string{"", "A", "AB", "ABC", "R"}, 1},
121 {[]string{"", "A", "AB", "ABC", "R", "S"}, "SS", []string{"", "A", "AB", "ABC", "R", "S"}, 0},
122 }
123
124 for _, test := range cases {
125 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
126 for _, ss := range test.inp {
127 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(ss, true)
128 }
129
130 deleted := r.DeletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#DeletePrefix().(test.prefix)
131 if deleted != test.numDeleted {
132 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("Bad delete, expected %v to be deleted but got %v", test.numDeleted, deleted)
133 }
134
135 out := []string{}
136 fn := func(s string, v interface{}) bool {
137 out = append(out, s)
138 return false
139 }
140 r.Walkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Walk().(fn)
141
142 if !reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.16 reflect/DeepEqual().(out, test.out) {
143 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("mis-match: %v %v", out, test.out)
144 }
145 }
146 }
147
148 func TestLongestPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestLongestPrefix().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
149 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
150
151 keys := []string{
152 "",
153 "foo",
154 "foobar",
155 "foobarbaz",
156 "foobarbazzip",
157 "foozip",
158 }
159 for _, k := range keys {
160 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(k, nil)
161 }
162 if r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() != len(keys) {
163 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad len: %v %v", r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(), len(keys))
164 }
165
166 type exp struct {
167 inp string
168 out string
169 }
170 cases := []exp{
171 {"a", ""},
172 {"abc", ""},
173 {"fo", ""},
174 {"foo", "foo"},
175 {"foob", "foo"},
176 {"foobar", "foobar"},
177 {"foobarba", "foobar"},
178 {"foobarbaz", "foobarbaz"},
179 {"foobarbazzi", "foobarbaz"},
180 {"foobarbazzip", "foobarbazzip"},
181 {"foozi", "foo"},
182 {"foozip", "foozip"},
183 {"foozipzap", "foozip"},
184 }
185 for _, test := range cases {
186 m, _, ok := r.LongestPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#LongestPrefix().(test.inp)
187 if !ok {
188 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("no match: %v", test)
189 }
190 if m != test.out {
191 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("mis-match: %v %v", m, test)
192 }
193 }
194 }
195
196 func TestWalkPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestWalkPrefix().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
197 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
198
199 keys := []string{
200 "foobar",
201 "foo/bar/baz",
202 "foo/baz/bar",
203 "foo/zip/zap",
204 "zipzap",
205 }
206 for _, k := range keys {
207 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(k, nil)
208 }
209 if r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() != len(keys) {
210 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad len: %v %v", r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(), len(keys))
211 }
212
213 type exp struct {
214 inp string
215 out []string
216 }
217 cases := []exp{
218 {
219 "f",
220 []string{"foobar", "foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
221 },
222 {
223 "foo",
224 []string{"foobar", "foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
225 },
226 {
227 "foob",
228 []string{"foobar"},
229 },
230 {
231 "foo/",
232 []string{"foo/bar/baz", "foo/baz/bar", "foo/zip/zap"},
233 },
234 {
235 "foo/b",
236 []string{"foo/bar/baz", "foo/baz/bar"},
237 },
238 {
239 "foo/ba",
240 []string{"foo/bar/baz", "foo/baz/bar"},
241 },
242 {
243 "foo/bar",
244 []string{"foo/bar/baz"},
245 },
246 {
247 "foo/bar/baz",
248 []string{"foo/bar/baz"},
249 },
250 {
251 "foo/bar/bazoo",
252 []string{},
253 },
254 {
255 "z",
256 []string{"zipzap"},
257 },
258 }
259
260 for _, test := range cases {
261 out := []string{}
262 fn := func(s string, v interface{}) bool {
263 out = append(out, s)
264 return false
265 }
266 r.WalkPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#WalkPrefix().(test.inp, fn)
267 sortscip-go gomod github.com/golang/go/src go1.16 sort/.Stringsscip-go gomod github.com/golang/go/src go1.16 sort/Strings().(out)
268 sortscip-go gomod github.com/golang/go/src go1.16 sort/.Stringsscip-go gomod github.com/golang/go/src go1.16 sort/Strings().(test.out)
269 if !reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.16 reflect/DeepEqual().(out, test.out) {
270 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("mis-match: %v %v", out, test.out)
271 }
272 }
273 }
274
275 func TestWalkPathscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestWalkPath().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
276 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
277
278 keys := []string{
279 "foo",
280 "foo/bar",
281 "foo/bar/baz",
282 "foo/baz/bar",
283 "foo/zip/zap",
284 "zipzap",
285 }
286 for _, k := range keys {
287 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(k, nil)
288 }
289 if r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() != len(keys) {
290 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("bad len: %v %v", r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(), len(keys))
291 }
292
293 type exp struct {
294 inp string
295 out []string
296 }
297 cases := []exp{
298 {
299 "f",
300 []string{},
301 },
302 {
303 "foo",
304 []string{"foo"},
305 },
306 {
307 "foo/",
308 []string{"foo"},
309 },
310 {
311 "foo/ba",
312 []string{"foo"},
313 },
314 {
315 "foo/bar",
316 []string{"foo", "foo/bar"},
317 },
318 {
319 "foo/bar/baz",
320 []string{"foo", "foo/bar", "foo/bar/baz"},
321 },
322 {
323 "foo/bar/bazoo",
324 []string{"foo", "foo/bar", "foo/bar/baz"},
325 },
326 {
327 "z",
328 []string{},
329 },
330 }
331
332 for _, test := range cases {
333 out := []string{}
334 fn := func(s string, v interface{}) bool {
335 out = append(out, s)
336 return false
337 }
338 r.WalkPathscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#WalkPath().(test.inp, fn)
339 sortscip-go gomod github.com/golang/go/src go1.16 sort/.Stringsscip-go gomod github.com/golang/go/src go1.16 sort/Strings().(out)
340 sortscip-go gomod github.com/golang/go/src go1.16 sort/.Stringsscip-go gomod github.com/golang/go/src go1.16 sort/Strings().(test.out)
341 if !reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.16 reflect/DeepEqual().(out, test.out) {
342 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("mis-match: %v %v", out, test.out)
343 }
344 }
345 }
346
347 func TestWalkDeletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/TestWalkDelete().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) {
348 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
349 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init0/0", nil)
350 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init0/1", nil)
351 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init0/2", nil)
352 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init0/3", nil)
353 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init1/0", nil)
354 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init1/1", nil)
355 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init1/2", nil)
356 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init1/3", nil)
357 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().("init2", nil)
358
359 deleteFn := func(s string, v interface{}) bool {
360 r.Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().(s)
361 return false
362 }
363
364 r.WalkPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#WalkPrefix().("init1", deleteFn)
365
366 for _, s := range []string{"init0/0", "init0/1", "init0/2", "init0/3", "init2"} {
367 if _, ok := r.Getscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Get().(s); !ok {
368 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expecting to still find %q", s)
369 }
370 }
371 if n := r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(); n != 5 {
372 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected to find exactly 5 nodes, instead found %d: %v", n, r.ToMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#ToMap().())
373 }
374
375 r.Walkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Walk().(deleteFn)
376 if n := r.Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().(); n != 0 {
377 t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected to find exactly 0 nodes, instead found %d: %v", n, r.ToMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#ToMap().())
378 }
379 }
380
381 // generateUUID is used to generate a random UUID
382 func generateUUIDscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/generateUUID().() string {
383 buf := make([]byte, 16)
384 if _, err := crandscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/Read().(buf); err != nil {
385 panic(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to read random bytes: %v", err))
386 }
387
388 return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%08x-%04x-%04x-%04x-%12x",
389 buf[0:4],
390 buf[4:6],
391 buf[6:8],
392 buf[8:10],
393 buf[10:16])
394 }
395
396 func BenchmarkInsertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/BenchmarkInsert().(b *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Bscip-go gomod github.com/golang/go/src go1.16 testing/B#) {
397 r := Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().()
398 for i := 0; i < 10000; i++ {
399 r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("init%d", i), true)
400 }
401 b.ResetTimerscip-go gomod github.com/golang/go/src go1.16 testing/B#ResetTimer().()
402 for n := 0; n < b.Nscip-go gomod github.com/golang/go/src go1.16 testing/B#N.; n++ {
403 _, updated := r.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.Itoascip-go gomod github.com/golang/go/src go1.16 strconv/Itoa().(n), true)
404 if updated {
405 b.Fatalscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatal().("bad")
406 }
407 }
408 }