forked from gaissmai/bart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlaps_test.go
188 lines (163 loc) · 4.4 KB
/
overlaps_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
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//
// some tests modified from github.com/tailscale/art
// for this implementation by:
//
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart
import (
"net/netip"
"testing"
)
func TestRegressionOverlaps(t *testing.T) {
t.Run("overlaps_divergent_children_with_parent_route_entry", func(t *testing.T) {
t.Parallel()
t1, t2 := new(Table[int]), new(Table[int])
t1.Insert(mpp("128.0.0.0/2"), 1)
t1.Insert(mpp("99.173.128.0/17"), 1)
t1.Insert(mpp("219.150.142.0/23"), 1)
t1.Insert(mpp("164.148.190.250/31"), 1)
t1.Insert(mpp("48.136.229.233/32"), 1)
t2.Insert(mpp("217.32.0.0/11"), 1)
t2.Insert(mpp("38.176.0.0/12"), 1)
t2.Insert(mpp("106.16.0.0/13"), 1)
t2.Insert(mpp("164.85.192.0/23"), 1)
t2.Insert(mpp("225.71.164.112/31"), 1)
if !t1.Overlaps(t2) {
t.Fatal("tables unexpectedly do not overlap")
}
})
t.Run("overlaps_parent_child_comparison_with_route_in_parent", func(t *testing.T) {
t.Parallel()
t1, t2 := new(Table[int]), new(Table[int])
t1.Insert(mpp("226.0.0.0/8"), 1)
t1.Insert(mpp("81.128.0.0/9"), 1)
t1.Insert(mpp("152.0.0.0/9"), 1)
t1.Insert(mpp("151.220.0.0/16"), 1)
t1.Insert(mpp("89.162.61.0/24"), 1)
t2.Insert(mpp("54.0.0.0/9"), 1)
t2.Insert(mpp("35.89.128.0/19"), 1)
t2.Insert(mpp("72.33.53.0/24"), 1)
t2.Insert(mpp("2.233.60.32/27"), 1)
t2.Insert(mpp("152.42.142.160/28"), 1)
if !t1.Overlaps(t2) {
t.Fatal("tables unexpectedly do not overlap")
}
})
}
func TestOverlapsCompare(t *testing.T) {
t.Parallel()
// Empirically, between 5 and 6 routes per table results in ~50%
// of random pairs overlapping. Cool example of the birthday paradox!
const numEntries = 6
seen := map[bool]int{}
for j := 0; j < 10_000; j++ {
pfxs := randomPrefixes(numEntries)
fast := new(Table[int])
gold := new(goldTable[int]).insertMany(pfxs)
for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}
inter := randomPrefixes(numEntries)
goldInter := new(goldTable[int]).insertMany(inter)
fastInter := new(Table[int])
for _, pfx := range inter {
fastInter.Insert(pfx.pfx, pfx.val)
}
gotGold := gold.overlaps(goldInter)
gotFast := fast.Overlaps(fastInter)
if gotGold != gotFast {
t.Fatalf("Overlaps(...) = %v, want %v\nTable1:\n%s\nTable:\n%v",
gotFast, gotGold, fast.String(), fastInter.String())
}
seen[gotFast]++
}
}
func TestOverlapsPrefixCompare(t *testing.T) {
t.Parallel()
pfxs := randomPrefixes(100_000)
fast := new(Table[int])
gold := new(goldTable[int]).insertMany(pfxs)
for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}
tests := randomPrefixes(10_000)
for _, tt := range tests {
gotGold := gold.overlapsPrefix(tt.pfx)
gotFast := fast.OverlapsPrefix(tt.pfx)
if gotGold != gotFast {
t.Fatalf("overlapsPrefix(%q) = %v, want %v", tt.pfx, gotFast, gotGold)
}
}
}
func TestOverlapsChildren(t *testing.T) {
t.Parallel()
pfxs1 := []netip.Prefix{
// pfxs
mpp("10.0.0.0/8"),
mpp("11.0.0.0/8"),
mpp("12.0.0.0/8"),
mpp("13.0.0.0/8"),
mpp("14.0.0.0/8"),
// chi5dren
mpp("10.100.0.0/17"),
mpp("11.100.0.0/17"),
mpp("12.100.0.0/17"),
mpp("13.100.0.0/17"),
mpp("14.100.0.0/17"),
mpp("15.100.0.0/17"),
mpp("16.100.0.0/17"),
mpp("17.100.0.0/17"),
mpp("18.100.0.0/17"),
mpp("19.100.0.0/17"),
mpp("20.100.0.0/17"),
mpp("21.100.0.0/17"),
mpp("22.100.0.0/17"),
mpp("23.100.0.0/17"),
mpp("24.100.0.0/17"),
mpp("25.100.0.0/17"),
mpp("26.100.0.0/17"),
mpp("27.100.0.0/17"),
mpp("28.100.0.0/17"),
}
pfxs2 := []netip.Prefix{
mpp("200.0.0.0/8"),
mpp("201.0.0.0/8"),
mpp("202.0.0.0/8"),
mpp("203.0.0.0/8"),
mpp("204.0.0.0/8"),
// children
mpp("201.200.0.0/18"),
mpp("202.200.0.0/18"),
mpp("203.200.0.0/18"),
mpp("204.200.0.0/18"),
mpp("205.200.0.0/18"),
mpp("206.200.0.0/18"),
mpp("207.200.0.0/18"),
mpp("208.200.0.0/18"),
mpp("209.200.0.0/18"),
mpp("210.200.0.0/18"),
mpp("211.200.0.0/18"),
mpp("212.200.0.0/18"),
mpp("213.200.0.0/18"),
mpp("214.200.0.0/18"),
mpp("215.200.0.0/18"),
mpp("216.200.0.0/18"),
mpp("217.200.0.0/18"),
mpp("218.200.0.0/18"),
mpp("219.200.0.0/18"),
}
tbl1 := new(Table[string])
for _, pfx := range pfxs1 {
tbl1.Insert(pfx, pfx.String())
}
tbl2 := new(Table[string])
for _, pfx := range pfxs2 {
tbl2.Insert(pfx, pfx.String())
}
if tbl1.Overlaps(tbl2) {
t.Fatal("tables unexpectedly do overlap")
}
}