-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmap_test.go
788 lines (721 loc) · 20.6 KB
/
map_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// Copyright (c) 2017 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package path
import (
"errors"
"fmt"
"testing"
"github.com/aristanetworks/goarista/key"
"github.com/aristanetworks/goarista/test"
)
func accumulator(counter map[int]int) VisitorFunc {
return func(val interface{}) error {
counter[val.(int)]++
return nil
}
}
func TestIsEmpty(t *testing.T) {
m := Map{}
if !m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return true; Got false")
}
nonWildcardPath := key.Path{key.New("foo")}
wildcardPath := key.Path{Wildcard, key.New("bar"), key.New("baz")}
m.Set(nonWildcardPath, 0)
if m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return false; Got true")
}
m.Set(wildcardPath, 2)
if m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return false; Got true")
}
m.Delete(nonWildcardPath)
if m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return false; Got true")
}
m.Delete(wildcardPath)
if !m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return true; Got false")
}
m.Set(nil, nil)
if m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return false; Got true")
}
m.Delete(nil)
if !m.IsEmpty() {
t.Errorf("Expected IsEmpty() to return true; Got false")
}
}
func TestMapSet(t *testing.T) {
m := Map{}
a := m.Set(key.Path{key.New("foo")}, 0)
b := m.Set(key.Path{key.New("foo")}, 1)
if !a || b {
t.Fatal("Map.Set not working properly")
}
}
func TestMapVisit(t *testing.T) {
m := Map{}
m.Set(key.Path{key.New("foo"), key.New("bar"), key.New("baz")}, 1)
m.Set(key.Path{Wildcard, key.New("bar"), key.New("baz")}, 2)
m.Set(key.Path{Wildcard, Wildcard, key.New("baz")}, 3)
m.Set(key.Path{Wildcard, Wildcard, Wildcard}, 4)
m.Set(key.Path{key.New("foo"), Wildcard, Wildcard}, 5)
m.Set(key.Path{key.New("foo"), key.New("bar"), Wildcard}, 6)
m.Set(key.Path{key.New("foo"), Wildcard, key.New("baz")}, 7)
m.Set(key.Path{Wildcard, key.New("bar"), Wildcard}, 8)
m.Set(key.Path{}, 10)
m.Set(key.Path{Wildcard}, 20)
m.Set(key.Path{key.New("foo")}, 21)
m.Set(key.Path{key.New("zap"), key.New("zip")}, 30)
m.Set(key.Path{key.New("zap"), key.New("zip")}, 31)
m.Set(key.Path{key.New("zip"), Wildcard}, 40)
m.Set(key.Path{key.New("zip"), Wildcard}, 41)
testCases := []struct {
path key.Path
expected map[int]int
}{{
path: key.Path{key.New("foo"), key.New("bar"), key.New("baz")},
expected: map[int]int{1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
}, {
path: key.Path{key.New("qux"), key.New("bar"), key.New("baz")},
expected: map[int]int{2: 1, 3: 1, 4: 1, 8: 1},
}, {
path: key.Path{key.New("foo"), key.New("qux"), key.New("baz")},
expected: map[int]int{3: 1, 4: 1, 5: 1, 7: 1},
}, {
path: key.Path{key.New("foo"), key.New("bar"), key.New("qux")},
expected: map[int]int{4: 1, 5: 1, 6: 1, 8: 1},
}, {
path: key.Path{},
expected: map[int]int{10: 1},
}, {
path: key.Path{key.New("foo")},
expected: map[int]int{20: 1, 21: 1},
}, {
path: key.Path{key.New("foo"), key.New("bar")},
expected: map[int]int{},
}, {
path: key.Path{key.New("zap"), key.New("zip")},
expected: map[int]int{31: 1},
}, {
path: key.Path{key.New("zip"), key.New("zap")},
expected: map[int]int{41: 1},
}}
for _, tc := range testCases {
result := make(map[int]int, len(tc.expected))
m.Visit(tc.path, accumulator(result))
if diff := test.Diff(tc.expected, result); diff != "" {
t.Errorf("Test case %v: %s", tc.path, diff)
}
}
}
func TestMapVisitError(t *testing.T) {
m := Map{}
m.Set(key.Path{key.New("foo"), key.New("bar")}, 1)
m.Set(key.Path{Wildcard, key.New("bar")}, 2)
errTest := errors.New("Test")
err := m.Visit(key.Path{key.New("foo"), key.New("bar")},
func(v interface{}) error { return errTest })
if err != errTest {
t.Errorf("Unexpected error. Expected: %v, Got: %v", errTest, err)
}
err = m.VisitPrefixes(key.Path{key.New("foo"), key.New("bar"), key.New("baz")},
func(v interface{}) error { return errTest })
if err != errTest {
t.Errorf("Unexpected error. Expected: %v, Got: %v", errTest, err)
}
}
func TestMapGet(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 1)
m.Set(key.Path{key.New("foo"), Wildcard}, 2)
m.Set(key.Path{Wildcard, key.New("bar")}, 3)
m.Set(key.Path{key.New("zap"), key.New("zip")}, 4)
m.Set(key.Path{key.New("baz"), key.New("qux")}, nil)
testCases := []struct {
path key.Path
v interface{}
ok bool
}{{
path: nil,
v: 0,
ok: true,
}, {
path: key.Path{key.New("foo"), key.New("bar")},
v: 1,
ok: true,
}, {
path: key.Path{key.New("foo"), Wildcard},
v: 2,
ok: true,
}, {
path: key.Path{Wildcard, key.New("bar")},
v: 3,
ok: true,
}, {
path: key.Path{key.New("baz"), key.New("qux")},
v: nil,
ok: true,
}, {
path: key.Path{key.New("bar"), key.New("foo")},
v: nil,
}, {
path: key.Path{key.New("zap"), Wildcard},
v: nil,
}}
for _, tc := range testCases {
v, ok := m.Get(tc.path)
if v != tc.v || ok != tc.ok {
t.Errorf("Test case %v: Expected (v: %v, ok: %t), Got (v: %v, ok: %t)",
tc.path, tc.v, tc.ok, v, ok)
}
}
}
func TestMapGetLongestPrefix(t *testing.T) {
type testMap struct {
pathMap Map
expectedValues map[string]interface{}
}
makeMap := func(paths []string) (result testMap) {
result.expectedValues = make(map[string]interface{})
nextValue := uint32(1)
for _, path := range paths {
result.pathMap.Set(FromString(path), nextValue)
result.expectedValues[path] = nextValue
nextValue++
}
return
}
regularMap := makeMap([]string{
"/",
"/a",
"/a/b",
"/a/b/c/d",
"/a/b/c/d/e",
"/r/s",
"/r/s/t",
"/u/v",
})
noEntryAtRootMap := makeMap([]string{
"/r/s",
"/r/s/t",
"/u/v",
})
rootOnlyMap := makeMap([]string{"/"})
emptyMap := makeMap(nil)
testCases := []struct {
name string
mp testMap
path string
ok bool
longestPath string
}{
// The root path
{
name: "exact match, descendents, root path",
mp: regularMap,
path: "/",
ok: true,
longestPath: "/",
},
{
name: "no exact match, descendents, root path",
mp: noEntryAtRootMap,
path: "/",
ok: false,
longestPath: "",
},
{
name: "exact match, no descendents, root path",
mp: rootOnlyMap,
path: "/",
ok: true,
longestPath: "/",
},
{
name: "no exact match, no descendents, root path",
mp: emptyMap,
path: "/",
ok: false,
longestPath: "",
},
// Non-root paths when the path map has entries associated with shorter
// prefixes
{
name: "exact match, descendents, ancestor",
mp: regularMap,
path: "/a/b/c/d",
ok: true,
longestPath: "/a/b/c/d",
},
{
name: "no exact match, descendents, ancestor",
mp: regularMap,
path: "/a/b/c",
ok: true,
longestPath: "/a/b",
},
{
name: "exact match, no descendents, ancestor",
mp: regularMap,
path: "/a/b/c/d/e",
ok: true,
longestPath: "/a/b/c/d/e",
},
// When considering divergent paths (i.e. paths p where the path map has
// neither an entry associated with p nor any entry associated with a
// descendent path of p), they may diverge from the map nodes at a node
// representing an entry or they may diverge from a node representing a
// non-entry.
{
name: "no exact match, no descendents, ancestor, stray from " +
"internal entry",
mp: regularMap,
path: "/a/b/f",
ok: true,
longestPath: "/a/b",
},
{
name: "no exact match, no descendents, ancestor, stray two entries " +
"from internal entry",
mp: regularMap,
path: "/a/b/f/g",
ok: true,
longestPath: "/a/b",
},
{
name: "no exact match, no descendents, ancestor, stray from leaf " +
"entry",
mp: regularMap,
path: "/a/b/c/d/e/f",
ok: true,
longestPath: "/a/b/c/d/e",
},
{
name: "no exact match, no descendents, ancestor, stray two entries " +
"from leaf entry",
mp: regularMap,
path: "/a/b/c/d/e/f/g",
ok: true,
longestPath: "/a/b/c/d/e",
},
{
name: "no exact match, no descendents, ancestor, stray from " +
"internal non-entry",
mp: regularMap,
path: "/a/b/c/f",
ok: true,
longestPath: "/a/b",
},
{
name: "no exact match, no descendents, ancestor, stray two entries " +
"from internal non-entry",
mp: regularMap,
path: "/a/b/c/f",
ok: true,
longestPath: "/a/b",
},
// Non-root paths when the path map has no entries associated with shorter
// prefixes except for an entry associated with the root path
{
name: "exact match, descendents, ancestor is root",
mp: regularMap,
path: "/r/s",
ok: true,
longestPath: "/r/s",
},
{
name: "no exact match, descendents, ancestor is root",
mp: regularMap,
path: "/r",
ok: true,
longestPath: "/",
},
{
name: "exact match, no descendents, ancestor is root",
mp: regularMap,
path: "/u/v",
ok: true,
longestPath: "/u/v",
},
{
name: "no exact match, no descendents, ancestor is root",
mp: regularMap,
path: "/x/y/z",
ok: true,
longestPath: "/",
},
// Non-root paths when the path map has no entries associated with shorter
// prefixes
{
name: "exact match, descendents, no ancestor",
mp: noEntryAtRootMap,
path: "/r/s",
ok: true,
longestPath: "/r/s",
},
{
name: "no exact match, descendents, no ancestor",
mp: noEntryAtRootMap,
path: "/r",
ok: false,
longestPath: "",
},
{
name: "exact match, no descendents, no ancestor",
mp: noEntryAtRootMap,
path: "/u/v",
ok: true,
longestPath: "/u/v",
},
{
name: "no exact match, no descendents, no ancestor",
mp: noEntryAtRootMap,
path: "/x/y/z",
ok: false,
longestPath: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Ensure single canonical representation of test case struct.
if !tc.ok && tc.longestPath != "" {
t.Fatalf(
"Test case %q expects ok == false but has a configured "+
"longestPath value of %q. Please clear this.",
tc.name,
tc.longestPath,
)
}
// Dump details of test case for easy access on failure.
defer func() {
if t.Failed() {
t.Logf("Failed with test case: %+v", tc)
}
}()
inputPath := FromString(tc.path)
t.Logf("Running GetLongestPrefix with %v", inputPath)
longestPrefix, v, ok := tc.mp.pathMap.GetLongestPrefix(inputPath)
if ok != tc.ok {
t.Fatalf("Unexpected ok value; expected:%v actual:%v", tc.ok, ok)
}
if !ok {
if !Equal(longestPrefix, nil) {
// Note: path.Equal([]key.Key{}, nil) == true.
t.Errorf("Unexpected non-empty longestPrefix: %v", longestPrefix)
}
if v != nil {
t.Errorf(
"Expected zero-value (nil); received unexpected value: %v",
v,
)
}
} else {
expectedlongestPrefix := FromString(tc.longestPath)
if !Equal(longestPrefix, expectedlongestPrefix) {
t.Errorf(
"Unexpected longestPrefix; expected:%v actual:%v",
expectedlongestPrefix,
longestPrefix,
)
}
expectedValue := tc.mp.expectedValues[tc.longestPath]
if v != expectedValue {
t.Errorf(
"Unexpected entry value; expected:%v actual:%v",
expectedValue,
v,
)
}
}
})
}
}
func countNodes(m *Map) int {
if m == nil {
return 0
}
count := 1
count += countNodes(m.wildcard)
for it := m.children.Iter(); it.Next(); {
count += countNodes(it.Elem())
}
return count
}
func TestMapDelete(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{Wildcard}, 1)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 2)
m.Set(key.Path{key.New("foo"), Wildcard}, 3)
m.Set(key.Path{key.New("foo")}, 4)
n := countNodes(&m)
if n != 5 {
t.Errorf("Initial count wrong. Expected: 5, Got: %d", n)
}
testCases := []struct {
del key.Path // key.Path to delete
expected bool // expected return value of Delete
visit key.Path // key.Path to Visit
before map[int]int // Expected to find items before deletion
after map[int]int // Expected to find items after deletion
count int // Count of nodes
}{{
del: key.Path{key.New("zap")}, // A no-op Delete
expected: false,
visit: key.Path{key.New("foo"), key.New("bar")},
before: map[int]int{2: 1, 3: 1},
after: map[int]int{2: 1, 3: 1},
count: 5,
}, {
del: key.Path{key.New("foo"), key.New("bar")},
expected: true,
visit: key.Path{key.New("foo"), key.New("bar")},
before: map[int]int{2: 1, 3: 1},
after: map[int]int{3: 1},
count: 4,
}, {
del: key.Path{key.New("foo")},
expected: true,
visit: key.Path{key.New("foo")},
before: map[int]int{1: 1, 4: 1},
after: map[int]int{1: 1},
count: 4,
}, {
del: key.Path{key.New("foo")},
expected: false,
visit: key.Path{key.New("foo")},
before: map[int]int{1: 1},
after: map[int]int{1: 1},
count: 4,
}, {
del: key.Path{Wildcard},
expected: true,
visit: key.Path{key.New("foo")},
before: map[int]int{1: 1},
after: map[int]int{},
count: 3,
}, {
del: key.Path{Wildcard},
expected: false,
visit: key.Path{key.New("foo")},
before: map[int]int{},
after: map[int]int{},
count: 3,
}, {
del: key.Path{key.New("foo"), Wildcard},
expected: true,
visit: key.Path{key.New("foo"), key.New("bar")},
before: map[int]int{3: 1},
after: map[int]int{},
count: 1, // Should have deleted "foo" and "bar" nodes
}, {
del: key.Path{},
expected: true,
visit: key.Path{},
before: map[int]int{0: 1},
after: map[int]int{},
count: 1, // Root node can't be deleted
}}
for i, tc := range testCases {
beforeResult := make(map[int]int, len(tc.before))
m.Visit(tc.visit, accumulator(beforeResult))
if diff := test.Diff(tc.before, beforeResult); diff != "" {
t.Errorf("Test case %d (%v): %s", i, tc.del, diff)
}
if got := m.Delete(tc.del); got != tc.expected {
t.Errorf("Test case %d (%v): Unexpected return. Expected %t, Got: %t",
i, tc.del, tc.expected, got)
}
afterResult := make(map[int]int, len(tc.after))
m.Visit(tc.visit, accumulator(afterResult))
if diff := test.Diff(tc.after, afterResult); diff != "" {
t.Errorf("Test case %d (%v): %s", i, tc.del, diff)
}
}
}
func TestMapVisitPrefixes(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{key.New("foo")}, 1)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 2)
m.Set(key.Path{key.New("foo"), key.New("bar"), key.New("baz")}, 3)
m.Set(key.Path{key.New("foo"), key.New("bar"), key.New("baz"), key.New("quux")}, 4)
m.Set(key.Path{key.New("quux"), key.New("bar")}, 5)
m.Set(key.Path{key.New("foo"), key.New("quux")}, 6)
m.Set(key.Path{Wildcard}, 7)
m.Set(key.Path{key.New("foo"), Wildcard}, 8)
m.Set(key.Path{Wildcard, key.New("bar")}, 9)
m.Set(key.Path{Wildcard, key.New("quux")}, 10)
m.Set(key.Path{key.New("quux"), key.New("quux"), key.New("quux"), key.New("quux")}, 11)
testCases := []struct {
path key.Path
expected map[int]int
}{{
path: key.Path{key.New("foo"), key.New("bar"), key.New("baz")},
expected: map[int]int{0: 1, 1: 1, 2: 1, 3: 1, 7: 1, 8: 1, 9: 1},
}, {
path: key.Path{key.New("zip"), key.New("zap")},
expected: map[int]int{0: 1, 7: 1},
}, {
path: key.Path{key.New("foo"), key.New("zap")},
expected: map[int]int{0: 1, 1: 1, 8: 1, 7: 1},
}, {
path: key.Path{key.New("quux"), key.New("quux"), key.New("quux")},
expected: map[int]int{0: 1, 7: 1, 10: 1},
}}
for _, tc := range testCases {
result := make(map[int]int, len(tc.expected))
m.VisitPrefixes(tc.path, accumulator(result))
if diff := test.Diff(tc.expected, result); diff != "" {
t.Errorf("Test case %v: %s", tc.path, diff)
}
}
}
func TestMapVisitPrefixed(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{key.New("qux")}, 1)
m.Set(key.Path{key.New("foo")}, 2)
m.Set(key.Path{key.New("foo"), key.New("qux")}, 3)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 4)
m.Set(key.Path{Wildcard, key.New("bar")}, 5)
m.Set(key.Path{key.New("foo"), Wildcard}, 6)
m.Set(key.Path{key.New("qux"), key.New("foo"), key.New("bar")}, 7)
testCases := []struct {
in key.Path
out map[int]int
}{{
in: key.Path{},
out: map[int]int{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1},
}, {
in: key.Path{key.New("qux")},
out: map[int]int{1: 1, 5: 1, 7: 1},
}, {
in: key.Path{key.New("foo")},
out: map[int]int{2: 1, 3: 1, 4: 1, 5: 1, 6: 1},
}, {
in: key.Path{key.New("foo"), key.New("qux")},
out: map[int]int{3: 1, 6: 1},
}, {
in: key.Path{key.New("foo"), key.New("bar")},
out: map[int]int{4: 1, 5: 1, 6: 1},
}, {
in: key.Path{key.New(int64(0))},
out: map[int]int{5: 1},
}, {
in: key.Path{Wildcard},
out: map[int]int{5: 1},
}, {
in: key.Path{Wildcard, Wildcard},
out: map[int]int{},
}}
for _, tc := range testCases {
out := make(map[int]int, len(tc.out))
m.VisitPrefixed(tc.in, accumulator(out))
if diff := test.Diff(tc.out, out); diff != "" {
t.Errorf("Test case %v: %s", tc.out, diff)
}
}
}
func TestMapVisitChildren(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{key.New("foo")}, 1)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 2)
m.Set(key.Path{key.New("foo"), key.New("bar"), key.New("baz")}, 3)
m.Set(key.Path{key.New("foo"), key.New("bar"), key.New("baz"), key.New("quux")}, 4)
m.Set(key.Path{key.New("quux"), key.New("bar")}, 5)
m.Set(key.Path{key.New("foo"), key.New("quux")}, 6)
m.Set(key.Path{Wildcard}, 7)
m.Set(key.Path{key.New("foo"), Wildcard}, 8)
m.Set(key.Path{Wildcard, key.New("bar")}, 9)
m.Set(key.Path{Wildcard, key.New("bar"), key.New("quux")}, 10)
m.Set(key.Path{key.New("quux"), key.New("quux"), key.New("quux"), key.New("quux")}, 11)
m.Set(key.Path{key.New("a"), key.New("b"), key.New("c"), key.New("d")}, 12)
m.Set(key.Path{key.New("a"), key.New("b")}, 13)
testCases := []struct {
path key.Path
expected map[int]int
}{{
path: key.Path{key.New("foo"), key.New("bar"), key.New("baz")},
expected: map[int]int{4: 1},
}, {
path: key.Path{key.New("zip"), key.New("zap")},
expected: map[int]int{},
}, {
path: key.Path{key.New("foo"), key.New("bar")},
expected: map[int]int{3: 1, 10: 1},
}, {
path: key.Path{key.New("quux"), key.New("quux"), key.New("quux")},
expected: map[int]int{11: 1},
}, {
path: key.Path{key.New("a"), key.New("b")},
expected: map[int]int{},
}}
for _, tc := range testCases {
result := make(map[int]int, len(tc.expected))
m.VisitChildren(tc.path, accumulator(result))
if diff := test.Diff(tc.expected, result); diff != "" {
t.Errorf("Test case %v: %s", tc.path, diff)
t.Errorf("tc.expected: %#v, got %#v", tc.expected, result)
}
}
}
func TestMapString(t *testing.T) {
m := Map{}
m.Set(key.Path{}, 0)
m.Set(key.Path{key.New("foo"), key.New("bar")}, 1)
m.Set(key.Path{key.New("foo"), key.New("quux")}, 2)
m.Set(key.Path{key.New("foo"), Wildcard}, 3)
expected := `Val: 0
Child "foo":
Child "*":
Val: 3
Child "bar":
Val: 1
Child "quux":
Val: 2
`
got := fmt.Sprint(&m)
if expected != got {
t.Errorf("Unexpected string. Expected:\n\n%s\n\nGot:\n\n%s", expected, got)
}
}
func genWords(count, wordLength int) key.Path {
chars := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
if count+wordLength > len(chars) {
panic("need more chars")
}
result := make(key.Path, count)
for i := 0; i < count; i++ {
result[i] = key.New(string(chars[i : i+wordLength]))
}
return result
}
func benchmarkPathMap(pathLength, pathDepth int, b *testing.B) {
// Push pathDepth paths, each of length pathLength
path := genWords(pathLength, 10)
words := genWords(pathDepth, 10)
root := &Map{}
m := root
for _, element := range path {
m.children = newKeyMap[any]()
for _, word := range words {
m.children.Set(word, &Map{})
}
next, _ := m.children.Get(element)
m = next
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
root.Visit(path, func(v interface{}) error { return nil })
}
}
func BenchmarkPathMap1x25(b *testing.B) { benchmarkPathMap(1, 25, b) }
func BenchmarkPathMap10x50(b *testing.B) { benchmarkPathMap(10, 25, b) }
func BenchmarkPathMap20x50(b *testing.B) { benchmarkPathMap(20, 25, b) }