File: tree_v4_test.go

package info (click to toggle)
golang-github-kentik-patricia 0.0~git20200128.c35d94c-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,368 kB
  • sloc: makefile: 38
file content (932 lines) | stat: -rw-r--r-- 28,531 bytes parent folder | download
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
package template

import (
	"encoding/binary"
	"fmt"
	"math/rand"
	"testing"

	"github.com/kentik/patricia"
	"github.com/stretchr/testify/assert"
)

func ipv4FromBytes(bytes []byte, length int) patricia.IPv4Address {
	return patricia.IPv4Address{
		Address: binary.BigEndian.Uint32(bytes),
		Length:  uint(length),
	}
}

func BenchmarkFindTags(b *testing.B) {
	tagA := "tagA"
	tagB := "tagB"
	tagC := "tagC"
	tagZ := "tagD"

	tree := NewTreeV4()

	tree.Add(patricia.IPv4Address{}, tagZ, nil) // default
	tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), tagA, nil)
	tree.Add(ipv4FromBytes([]byte{160, 0, 0, 0}, 2), tagB, nil) // 160 -> 128
	tree.Add(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), tagC, nil)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		address := patricia.NewIPv4Address(uint32(2156823809), 32)
		tree.FindTags(address)
	}
}

func BenchmarkFindDeepestTag(b *testing.B) {
	tree := NewTreeV4()
	for i := 32; i > 0; i-- {
		tree.Add(ipv4FromBytes([]byte{127, 0, 0, 1}, i), fmt.Sprintf("Tag-%d", i), nil)
	}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		address := patricia.NewIPv4Address(uint32(2130706433), 32)
		tree.FindDeepestTag(address)
	}
}

func BenchmarkBuildTreeAndFindDeepestTag(b *testing.B) {
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		tree := NewTreeV4()

		// populate

		address := patricia.NewIPv4Address(uint32(1653323544), 32)
		tree.Add(address, "tagA", nil)

		address = patricia.NewIPv4Address(uint32(3334127283), 32)
		tree.Add(address, "tagB", nil)

		address = patricia.NewIPv4Address(uint32(2540010580), 32)
		tree.Add(address, "tagC", nil)

		// search

		address = patricia.NewIPv4Address(uint32(1653323544), 32)
		tree.FindDeepestTag(address)

		address = patricia.NewIPv4Address(uint32(3334127283), 32)
		tree.FindDeepestTag(address)

		address = patricia.NewIPv4Address(uint32(2540010580), 32)
		tree.FindDeepestTag(address)
	}
}

func TestTree2(t *testing.T) {
	tree := NewTreeV4()

	// insert a bunch of tags
	v4, _, err := patricia.ParseIPFromString("1.2.3.0/24")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "foo", nil)
	tree.Add(*v4, "bar", nil)

	v4, _, err = patricia.ParseIPFromString("188.212.216.242")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "a", nil)

	v4, _, err = patricia.ParseIPFromString("171.233.143.228")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "b", nil)

	v4, _, err = patricia.ParseIPFromString("186.244.183.12")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "c", nil)

	v4, _, err = patricia.ParseIPFromString("171.233.143.222")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "d", nil)

	v4, _, err = patricia.ParseIPFromString("190.207.189.24")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "e", nil)

	v4, _, err = patricia.ParseIPFromString("188.212.216.240")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "f", nil)

	v4, _, err = patricia.ParseIPFromString("185.76.10.148")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "g", nil)

	v4, _, err = patricia.ParseIPFromString("14.208.248.50")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "h", nil)

	v4, _, err = patricia.ParseIPFromString("59.60.75.52")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "i", nil)

	v4, _, err = patricia.ParseIPFromString("185.76.10.146")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "j", nil)
	tree.Add(*v4, "k", nil)

	// --------
	// now assert they're all found
	v4, _, _ = patricia.ParseIPFromString("188.212.216.242")
	found, tag, err := tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "a", tag)

	v4, _, _ = patricia.ParseIPFromString("171.233.143.228")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "b", tag)

	v4, _, _ = patricia.ParseIPFromString("186.244.183.12")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "c", tag)

	v4, _, _ = patricia.ParseIPFromString("171.233.143.222")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "d", tag)

	v4, _, _ = patricia.ParseIPFromString("190.207.189.24")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "e", tag)

	v4, _, _ = patricia.ParseIPFromString("188.212.216.240")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "f", tag)

	v4, _, _ = patricia.ParseIPFromString("185.76.10.148")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "g", tag)

	v4, _, _ = patricia.ParseIPFromString("14.208.248.50")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "h", tag)

	v4, _, _ = patricia.ParseIPFromString("59.60.75.52")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "i", tag)

	v4, _, _ = patricia.ParseIPFromString("185.76.10.146")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "j", tag)

	v4, _, _ = patricia.ParseIPFromString("185.76.10.146")
	found, tags, err := tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "j", tags[0])
	assert.Equal(t, "k", tags[1])

	// test searching for addresses with no leaf nodes
	v4, _, _ = patricia.ParseIPFromString("1.2.3.4")
	found, tags, err = tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "foo", tags[0])
	assert.Equal(t, "bar", tags[1])

	v4, _, _ = patricia.ParseIPFromString("1.2.3.5")
	found, tags, err = tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "foo", tags[0])
	assert.Equal(t, "bar", tags[1])

	v4, _, _ = patricia.ParseIPFromString("1.2.3.4")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "foo", tag)

	// test searching for an address that has nothing
	v4, _, _ = patricia.ParseIPFromString("9.9.9.9")
	found, tags, err = tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.False(t, found)
	assert.NotNil(t, tags)
	assert.Equal(t, 0, len(tags))

	// test searching for an empty address
	v4, _, _ = patricia.ParseIPFromString("9.9.9.9/0")
	found, tags, err = tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.False(t, found)
	assert.NotNil(t, tags)
	assert.Equal(t, 0, len(tags))

	// add a root node tag and try again
	v4, _, err = patricia.ParseIPFromString("1.1.1.1/0")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	tree.Add(*v4, "root_node", nil)

	v4, _, _ = patricia.ParseIPFromString("9.9.9.9/0")
	found, tags, err = tree.FindDeepestTags(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.NotNil(t, tags)
	assert.Equal(t, 1, len(tags))
	assert.Equal(t, "root_node", tags[0])

	v4, _, _ = patricia.ParseIPFromString("9.9.9.9/0")
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "root_node", tag)
}

// test that the find functions don't destroy an address - too brittle and confusing for caller for what gains?
func TestAddressReusable(t *testing.T) {
	tree := NewTreeV4()

	pv4, pv6, err := patricia.ParseIPFromString("59.60.75.53") // needs to share same second-level node with the address we're going to work with
	tree.Add(*pv4, "Don't panic!", nil)
	assert.NoError(t, err)
	assert.NotNil(t, pv4)
	assert.Nil(t, pv6)

	v4, v6, err := patricia.ParseIPFromString("59.60.75.52")
	assert.NoError(t, err)
	assert.NotNil(t, v4)
	assert.Nil(t, v6)

	tree.Add(*v4, "Hello", nil)
	found, tag, err := tree.FindDeepestTag(*v4)
	assert.True(t, found)
	assert.Equal(t, "Hello", tag)
	assert.NoError(t, err)

	// search again with same address
	found, tag, err = tree.FindDeepestTag(*v4)
	assert.True(t, found)
	assert.Equal(t, "Hello", tag)
	assert.NoError(t, err)

	// search again with same address
	tags, err := tree.FindTags(*v4)
	if assert.Equal(t, 1, len(tags)) {
		assert.Equal(t, "Hello", tags[0])
	}
	assert.NoError(t, err)

	// search again with same address
	tags, err = tree.FindTags(*v4)
	if assert.Equal(t, 1, len(tags)) {
		assert.Equal(t, "Hello", tags[0])
	}
	assert.NoError(t, err)
}

func TestSimpleTree1(t *testing.T) {
	tree := NewTreeV4()

	ipv4a := ipv4FromBytes([]byte{98, 139, 183, 24}, 32)
	ipv4b := ipv4FromBytes([]byte{198, 186, 190, 179}, 32)
	ipv4c := ipv4FromBytes([]byte{151, 101, 124, 84}, 32)

	tree.Add(ipv4a, "tagA", nil)
	tree.Add(ipv4b, "tagB", nil)
	tree.Add(ipv4c, "tagC", nil)

	found, tag, err := tree.FindDeepestTag(ipv4FromBytes([]byte{98, 139, 183, 24}, 32))
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "tagA", tag)

	found, tag, err = tree.FindDeepestTag(ipv4FromBytes([]byte{198, 186, 190, 179}, 32))
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "tagB", tag)

	found, tag, err = tree.FindDeepestTag(ipv4FromBytes([]byte{151, 101, 124, 84}, 32))
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, "tagC", tag)
}

// Test having a couple of inner nodes
func TestSimpleTree2(t *testing.T) {
	ipA, _, _ := patricia.ParseIPFromString("203.143.220.0/23")
	ipB, _, _ := patricia.ParseIPFromString("203.143.220.198/32")
	ipC, _, _ := patricia.ParseIPFromString("203.143.0.0/16")
	ipD, _, _ := patricia.ParseIPFromString("203.143.221.75/32")

	// add the 4 addresses
	tree := NewTreeV4()
	tree.Add(*ipA, "A", nil)
	tree.Add(*ipB, "B", nil)
	tree.Add(*ipC, "C", nil)
	tree.Add(*ipD, "D", nil)

	// find the 4 addresses
	found, _, _ := tree.FindDeepestTag(*ipA)
	assert.True(t, found)
	found, _, _ = tree.FindDeepestTag(*ipB)
	assert.True(t, found)
	found, _, _ = tree.FindDeepestTag(*ipC)
	assert.True(t, found)
	found, _, _ = tree.FindDeepestTag(*ipD)
	assert.True(t, found)

	// delete each one
	matchFunc := func(a GeneratedType, b GeneratedType) bool {
		return a == b
	}
	deleteCount, err := tree.Delete(*ipA, matchFunc, "A")
	assert.NoError(t, err)
	assert.Equal(t, 1, deleteCount)
	deleteCount, err = tree.Delete(*ipB, matchFunc, "B")
	assert.NoError(t, err)
	assert.Equal(t, 1, deleteCount)
	deleteCount, err = tree.Delete(*ipC, matchFunc, "C")
	assert.NoError(t, err)
	assert.Equal(t, 1, deleteCount)
	deleteCount, err = tree.Delete(*ipD, matchFunc, "D")
	assert.NoError(t, err)
	assert.Equal(t, 1, deleteCount)

	// should have zero logical nodes except for root
	assert.Equal(t, 1, tree.countNodes(1))
}

func TestSimpleTree(t *testing.T) {
	tree := NewTreeV4()

	for i := 32; i > 0; i-- {
		countIncreased, count, err := tree.Add(ipv4FromBytes([]byte{127, 0, 0, 1}, i), fmt.Sprintf("Tag-%d", i), nil)
		assert.NoError(t, err)
		assert.True(t, countIncreased)
		assert.Equal(t, 1, count)
	}

	tags, err := tree.FindTags(ipv4FromBytes([]byte{127, 0, 0, 1}, 32))
	assert.NoError(t, err)
	if assert.Equal(t, 32, len(tags)) {
		assert.Equal(t, "Tag-32", tags[31].(string))
		assert.Equal(t, "Tag-31", tags[30].(string))
	}

	tags, err = tree.FindTags(ipv4FromBytes([]byte{63, 3, 0, 1}, 32))
	assert.NoError(t, err)
	if assert.Equal(t, 1, len(tags)) {
		assert.Equal(t, "Tag-1", tags[0].(string))
	}

	// find deepest tag: match at lowest level
	found, tag, err := tree.FindDeepestTag(ipv4FromBytes([]byte{127, 0, 0, 1}, 32))
	assert.True(t, found)
	if assert.NotNil(t, tag) {
		assert.Equal(t, "Tag-32", tag.(string))
	}

	// find deepest tag: match at top level
	found, tag, err = tree.FindDeepestTag(ipv4FromBytes([]byte{63, 5, 4, 3}, 32))
	assert.True(t, found)
	if assert.NotNil(t, tag) {
		assert.Equal(t, "Tag-1", tag.(string))
	}

	// find deepest tag: match at mid level
	found, tag, err = tree.FindDeepestTag(ipv4FromBytes([]byte{119, 5, 4, 3}, 32))
	assert.True(t, found)
	if assert.NotNil(t, tag) {
		assert.Equal(t, "Tag-4", tag.(string))
	}

	// find deepest tag: no match
	found, tag, err = tree.FindDeepestTag(ipv4FromBytes([]byte{128, 4, 3, 2}, 32))
	assert.False(t, found)
	assert.Nil(t, tag)

	// Add a couple root tags
	countIncreased, count, err := tree.Add(ipv4FromBytes([]byte{127, 0, 0, 1}, 0), "root1", nil)
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 1, count)
	countIncreased, count, err = tree.Add(patricia.IPv4Address{}, "root2", nil)
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 2, count)

	tags, err = tree.FindTags(patricia.IPv4Address{})
	assert.NoError(t, err)
	if assert.Equal(t, 2, len(tags)) {
		assert.Equal(t, "root1", tags[0].(string))
		assert.Equal(t, "root2", tags[1].(string))
	}

}

// assert two collections of arrays have the same tags - don't worry about performance
func tagArraysEqual(a []GeneratedType, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := 0; i < len(a); i++ {
		found := false
		for j := 0; j < len(b); j++ {
			if a[i].(string) == b[j] {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}
	return true
}

func TestTree1FindTags(t *testing.T) {
	tagA := "tagA"
	tagB := "tagB"
	tagC := "tagC"
	tagZ := "tagD"

	tree := NewTreeV4()
	tree.Add(ipv4FromBytes([]byte{1, 2, 3, 4}, 0), tagZ, nil) // default
	tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), tagA, nil)
	tree.Add(ipv4FromBytes([]byte{160, 0, 0, 0}, 2), tagB, nil) // 160 -> 128
	tree.Add(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), tagC, nil)

	// three tags in a hierarchy - ask for all but the most specific
	tags, err := tree.FindTags(ipv4FromBytes([]byte{128, 142, 133, 1}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagZ}))

	// three tags in a hierarchy - ask for an exact match, receive all 3
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagZ}))

	// three tags in a hierarchy - get just the first
	tags, err = tree.FindTags(ipv4FromBytes([]byte{162, 1, 0, 5}, 30))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagB, tagZ}))

	// three tags in hierarchy - get none
	tags, err = tree.FindTags(ipv4FromBytes([]byte{1, 0, 0, 0}, 1))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagZ}))
}

func TestTree1FindTagsWithFilter(t *testing.T) {
	tagA := "tagA"
	tagB := "tagB"
	tagC := "tagC"
	tagZ := "tagD"

	filterFunc := func(val GeneratedType) bool {
		return val == tagA || val == tagB
	}

	tree := NewTreeV4()
	tree.Add(ipv4FromBytes([]byte{1, 2, 3, 4}, 0), tagZ, nil) // default
	tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), tagA, nil)
	tree.Add(ipv4FromBytes([]byte{160, 0, 0, 0}, 2), tagB, nil) // 160 -> 128
	tree.Add(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), tagC, nil)

	// three tags in a hierarchy - ask for all but the most specific
	tags, err := tree.FindTagsWithFilter(ipv4FromBytes([]byte{128, 142, 133, 1}, 32), filterFunc)
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB}))

	// three tags in a hierarchy - ask for an exact match, receive all 3
	tags, err = tree.FindTagsWithFilter(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), filterFunc)
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB}))

	// three tags in a hierarchy - get just the first
	tags, err = tree.FindTagsWithFilter(ipv4FromBytes([]byte{162, 1, 0, 5}, 30), filterFunc)
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagB}))

	// three tags in hierarchy - get none
	tags, err = tree.FindTagsWithFilter(ipv4FromBytes([]byte{1, 0, 0, 0}, 1), filterFunc)
	assert.NoError(t, err)
	assert.Zero(t, len(tags))
}

// Test that all queries get the root nodes
func TestRootNode(t *testing.T) {
	tagA := "tagA"
	tagB := "tagB"
	tagC := "tagC"
	tagD := "tagD"
	tagZ := "tagE"

	tree := NewTreeV4()

	// root node gets tags A & B
	tree.Add(patricia.IPv4Address{}, tagA, nil)
	tree.Add(patricia.IPv4Address{}, tagB, nil)

	// query the root node with no address
	tags, err := tree.FindTags(patricia.IPv4Address{})
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB}))

	// query a node that doesn't exist
	tags, err = tree.FindTags(ipv4FromBytes([]byte{1, 2, 3, 4}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB}))

	// create a new /16 node with C & D
	tree.Add(ipv4FromBytes([]byte{1, 2, 3, 4}, 16), tagC, nil)
	tree.Add(ipv4FromBytes([]byte{1, 2, 3, 4}, 16), tagD, nil)
	tags, err = tree.FindTags(ipv4FromBytes([]byte{1, 2, 3, 4}, 16))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagD}))

	// create a node under the /16 node
	tree.Add(ipv4FromBytes([]byte{1, 2, 3, 4}, 32), tagZ, nil)
	tags, err = tree.FindTags(ipv4FromBytes([]byte{1, 2, 3, 4}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagD, tagZ}))

	// check the /24 and make sure we still get the /16 and root
	tags, err = tree.FindTags(ipv4FromBytes([]byte{1, 2, 3, 4}, 24))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagD}))
}

// TestAdd returns the right counts
func TestAdd(t *testing.T) {
	address := ipv4FromBytes([]byte{1, 2, 3, 4}, 32)

	tree := NewTreeV4()
	countIncreased, count, err := tree.Add(address, "hi", nil)
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 1, count)

	countIncreased, count, err = tree.Add(address, "hi", nil)
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 2, count)

	countIncreased, count, err = tree.Add(address, "hi", nil)
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 3, count)
}

// Test setting a value to a node, rather than adding to a list
func TestSet(t *testing.T) {
	address := ipv4FromBytes([]byte{1, 2, 3, 4}, 32)

	tree := NewTreeV4()

	// add a parent node, just to mix things up
	countIncreased, count, err := tree.Set(ipv4FromBytes([]byte{1, 2, 3, 0}, 24), "parent")
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 1, count)

	countIncreased, count, err = tree.Set(address, "tagA")
	assert.NoError(t, err)
	assert.True(t, countIncreased)
	assert.Equal(t, 1, count)
	found, tag, err := tree.FindDeepestTag(address)
	assert.True(t, found)
	assert.NoError(t, err)
	assert.Equal(t, "tagA", tag)

	countIncreased, count, err = tree.Set(address, "tagB")
	assert.Equal(t, 1, count)
	assert.False(t, countIncreased)
	assert.NoError(t, err)
	found, tag, err = tree.FindDeepestTag(address)
	assert.True(t, found)
	assert.NoError(t, err)
	assert.Equal(t, "tagB", tag)

	countIncreased, count, err = tree.Set(address, "tagC")
	assert.Equal(t, 1, count)
	assert.False(t, countIncreased)
	assert.NoError(t, err)
	found, tag, err = tree.FindDeepestTag(address)
	assert.True(t, found)
	assert.NoError(t, err)
	assert.Equal(t, "tagC", tag)

	countIncreased, count, err = tree.Set(address, "tagD")
	assert.Equal(t, 1, count)
	assert.False(t, countIncreased)
	assert.NoError(t, err)
	found, tag, err = tree.FindDeepestTag(address)
	assert.True(t, found)
	assert.NoError(t, err)
	assert.Equal(t, "tagD", tag)

	// now delete the tag
	delCount, err := tree.Delete(address, func(a GeneratedType, b GeneratedType) bool { return true }, "")
	assert.Equal(t, 1, delCount)
	assert.NoError(t, err)

	// verify it's gone - should get the parent
	found, tag, err = tree.FindDeepestTag(address)
	assert.True(t, found)
	assert.NoError(t, err)
	assert.Equal(t, "parent", tag)
}

func TestDelete1(t *testing.T) {
	matchFunc := func(tagData GeneratedType, val GeneratedType) bool {
		return tagData.(string) == val.(string)
	}

	tagA := "tagA"
	tagB := "tagB"
	tagC := "tagC"
	tagZ := "tagZ"

	tree := NewTreeV4()
	assert.Equal(t, 1, tree.countNodes(1))
	tree.Add(ipv4FromBytes([]byte{8, 7, 6, 5}, 0), tagZ, nil) // default
	assert.Equal(t, 1, tree.countNodes(1))
	assert.Zero(t, len(tree.availableIndexes))
	assert.Equal(t, 2, len(tree.nodes)) // empty first node plus root

	tree.Add(ipv4FromBytes([]byte{128, 3, 0, 5}, 7), tagA, nil) // 1000000
	assert.Equal(t, 2, tree.countNodes(1))
	assert.Equal(t, 3, len(tree.nodes))

	tree.Add(ipv4FromBytes([]byte{128, 5, 1, 1}, 2), tagB, nil) // 10
	assert.Equal(t, 3, tree.countNodes(1))
	assert.Equal(t, 4, len(tree.nodes))

	tree.Add(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), tagC, nil)
	assert.Equal(t, 4, tree.countNodes(1))
	assert.Equal(t, 5, len(tree.nodes))

	// verify status of internal nodes collections
	assert.Zero(t, len(tree.availableIndexes))
	assert.Equal(t, "tagZ", tree.tagsForNode(1)[0])
	assert.Equal(t, "tagA", tree.tagsForNode(2)[0])
	assert.Equal(t, "tagB", tree.tagsForNode(3)[0])
	assert.Equal(t, "tagC", tree.tagsForNode(4)[0])

	// three tags in a hierarchy - ask for an exact match, receive all 3 and the root
	tags, err := tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagZ}))

	// 1. delete a tag that doesn't exist
	count := 0
	count, err = tree.Delete(ipv4FromBytes([]byte{9, 9, 9, 9}, 32), matchFunc, "bad tag")
	assert.NoError(t, err)
	assert.Equal(t, 0, count)
	assert.Equal(t, 4, tree.countNodes(1))
	assert.Equal(t, 4, tree.countTags(1))

	// 2. delete a tag on an address that exists, but doesn't have the tag
	count, err = tree.Delete(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), matchFunc, "bad tag")
	assert.Equal(t, 0, count)
	assert.NoError(t, err)

	// verify
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC, tagZ}))
	assert.Equal(t, 4, tree.countNodes(1))
	assert.Equal(t, 4, tree.countTags(1))

	// 3. delete the default/root tag
	count, err = tree.Delete(ipv4FromBytes([]byte{0, 0, 0, 0}, 0), matchFunc, "tagZ")
	assert.NoError(t, err)
	assert.Equal(t, 1, count)
	assert.Equal(t, 4, tree.countNodes(1)) // doesn't delete anything
	assert.Equal(t, 3, tree.countTags(1))
	assert.Equal(t, 0, len(tree.availableIndexes))

	// three tags in a hierarchy - ask for an exact match, receive all 3, not the root, which we deleted
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagA, tagB, tagC}))

	// 4. delete tagA
	count, err = tree.Delete(ipv4FromBytes([]byte{128, 0, 0, 0}, 7), matchFunc, "tagA")
	assert.NoError(t, err)
	assert.Equal(t, 1, count)

	// verify
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagB, tagC}))
	assert.Equal(t, 3, tree.countNodes(1))
	assert.Equal(t, 2, tree.countTags(1))
	assert.Equal(t, 1, len(tree.availableIndexes))
	assert.Equal(t, uint(2), tree.availableIndexes[0])

	// 5. delete tag B
	count, err = tree.Delete(ipv4FromBytes([]byte{128, 0, 0, 0}, 2), matchFunc, "tagB")
	assert.NoError(t, err)
	assert.Equal(t, 1, count)

	// verify
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{tagC}))
	assert.Equal(t, 2, tree.countNodes(1))
	assert.Equal(t, 1, tree.countTags(1))
	assert.Equal(t, 2, len(tree.availableIndexes))
	assert.Equal(t, uint(3), tree.availableIndexes[1])

	// add tagE & tagF to the same node
	tree.Add(ipv4FromBytes([]byte{1, 3, 6, 240}, 32), "tagE", nil)
	tree.Add(ipv4FromBytes([]byte{1, 3, 6, 240}, 32), "tagF", nil)
	assert.Equal(t, 3, tree.countNodes(1))
	assert.Equal(t, 3, tree.countTags(1))

	// this should be recycling tagB
	assert.Equal(t, 1, len(tree.availableIndexes))
	assert.Equal(t, uint(2), tree.availableIndexes[0])
	assert.Equal(t, "tagE", tree.tagsForNode(3)[0])

	// 6. delete tag C
	count, err = tree.Delete(ipv4FromBytes([]byte{128, 3, 6, 240}, 32), matchFunc, "tagC")
	assert.NoError(t, err)
	assert.Equal(t, 1, count)

	// verify
	tags, err = tree.FindTags(ipv4FromBytes([]byte{128, 3, 6, 240}, 32))
	assert.NoError(t, err)
	assert.True(t, tagArraysEqual(tags, []string{}))
	assert.Equal(t, 2, tree.countNodes(1))
	assert.Equal(t, 2, tree.countTags(1))
}

func TestTryToBreak(t *testing.T) {
	tree := NewTreeV4()
	for a := byte(1); a < 10; a++ {
		for b := byte(1); b < 10; b++ {
			for c := byte(1); c < 10; c++ {
				for d := byte(1); d < 10; d++ {
					tree.Add(ipv4FromBytes([]byte{a, b, c, d}, rand.Intn(32)), "tag", nil)
				}
			}
		}
	}
}

func TestTagsMap(t *testing.T) {
	tree := NewTreeV4()

	// insert tags
	tree.addTag("tagA", 1, nil, false)
	tree.addTag("tagB", 1, nil, false)
	tree.addTag("tagC", 1, nil, false)
	tree.addTag("tagD", 0, nil, false) // there's no node0, but it exists, so use it for this test

	// verify
	assert.Equal(t, 3, tree.nodes[1].TagCount)
	assert.Equal(t, "tagA", tree.firstTagForNode(1))
	assert.Equal(t, 3, len(tree.tagsForNode(1)))
	assert.Equal(t, "tagA", tree.tagsForNode(1)[0])
	assert.Equal(t, "tagB", tree.tagsForNode(1)[1])
	assert.Equal(t, "tagC", tree.tagsForNode(1)[2])

	// delete tagB
	matchesFunc := func(payload GeneratedType, val GeneratedType) bool {
		return payload == val
	}
	deleted, kept := tree.deleteTag(1, "tagB", matchesFunc)

	// verify
	assert.Equal(t, 1, deleted)
	assert.Equal(t, 2, kept)
	assert.Equal(t, 2, tree.nodes[1].TagCount)
	assert.Equal(t, "tagA", tree.tagsForNode(1)[0])
	assert.Equal(t, "tagC", tree.tagsForNode(1)[1])
}

// test duplicate tags with no match func
func TestDuplicateTagsWithNoMatchFunc(t *testing.T) {
	matchFunc := MatchesFunc(nil)

	tree := NewTreeV4()

	wasAdded, count, err := tree.Add(patricia.IPv4Address{}, "FOO", matchFunc) // default
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	// add another at previous node
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "FOOBAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 2, count)
	assert.NoError(t, err)

	// add a dupe to the previous node - will be fine since match is nil
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 3, count)
	assert.NoError(t, err)
}

// test duplicate tags with match func that always returns false
func TestDuplicateTagsWithFalseMatchFunc(t *testing.T) {
	matchFunc := func(val1 GeneratedType, val2 GeneratedType) bool {
		return false
	}

	tree := NewTreeV4()

	wasAdded, count, err := tree.Add(patricia.IPv4Address{}, "FOO", matchFunc) // default
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	// add another at previous node
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "FOOBAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 2, count)
	assert.NoError(t, err)

	// add a dupe to the previous node - will be fine since match is nil
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 3, count)
	assert.NoError(t, err)
}

// test duplicate tags with match func that does something
func TestDuplicateTagsWithMatchFunc(t *testing.T) {
	matchFunc := func(val1 GeneratedType, val2 GeneratedType) bool {
		return val1 == val2
	}

	tree := NewTreeV4()

	wasAdded, count, err := tree.Add(patricia.IPv4Address{}, "FOO", matchFunc) // default
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 1, count)
	assert.NoError(t, err)

	// add another at previous node
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "FOOBAR", matchFunc)
	assert.True(t, wasAdded)
	assert.Equal(t, 2, count)
	assert.NoError(t, err)

	// add a dupe to the previous node - will be fine since match is nil
	wasAdded, count, err = tree.Add(ipv4FromBytes([]byte{129, 0, 0, 1}, 7), "BAR", matchFunc)
	assert.False(t, wasAdded)
	assert.Equal(t, 2, count)
	assert.NoError(t, err)
}

func payloadToByteArrays(tags []GeneratedType) [][]byte {
	ret := make([][]byte, 0, len(tags))
	for _, tag := range tags {
		ret = append(ret, tag.([]byte))
	}
	return ret
}