Skip to content

Commit

Permalink
reorg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Aug 29, 2024
1 parent 48ae62c commit 8ffb23c
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 119 deletions.
File renamed without changes.
78 changes: 78 additions & 0 deletions deprecated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package bart

import (
"net/netip"
"reflect"
"testing"
)

func TestEachSubnetCompare(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := &Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

for range 10_000 {
pfx := randomPrefix()
goldPfxs := gold.subnets(pfx)

var fastPfxs []netip.Prefix
values := map[netip.Prefix]int{}

fast.EachSubnet(pfx, func(p netip.Prefix, val int) bool {
fastPfxs = append(fastPfxs, p)
values[p] = val
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSubnets(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}

// also check the values handled by yield function
for pfx, val := range values {
got, ok := fast.Get(pfx)

if !ok || got != val {
t.Fatalf("EachSubnets: Get(%q), got: %d,%v, want: %d,%v", pfx, got, ok, val, true)
}
}
}
}

func TestEachLookupPrefix(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

var fastPfxs []netip.Prefix
for range 10_000 {
pfx := randomPrefix()

goldPfxs := gold.lookupPrefixReverse(pfx)

fastPfxs = nil
fast.EachLookupPrefix(pfx, func(p netip.Prefix, _ int) bool {
fastPfxs = append(fastPfxs, p)
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSupernet(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}

}
}
79 changes: 5 additions & 74 deletions table_cb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,89 +10,20 @@ import (
"testing"
)

func TestEachSubnetCompare(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := &Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

for range 10_000 {
pfx := randomPrefix()
goldPfxs := gold.subnets(pfx)

var fastPfxs []netip.Prefix
values := map[netip.Prefix]int{}

fast.EachSubnet(pfx, func(p netip.Prefix, val int) bool {
fastPfxs = append(fastPfxs, p)
values[p] = val
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSubnets(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}

// also check the values handled by yield function
for pfx, val := range values {
got, ok := fast.Get(pfx)

if !ok || got != val {
t.Fatalf("EachSubnets: Get(%q), got: %d,%v, want: %d,%v", pfx, got, ok, val, true)
}
}
}
}

func TestEachLookupPrefix(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

var fastPfxs []netip.Prefix
for range 10_000 {
pfx := randomPrefix()

goldPfxs := gold.lookupPrefixReverse(pfx)

fastPfxs = nil
fast.EachLookupPrefix(pfx, func(p netip.Prefix, _ int) bool {
fastPfxs = append(fastPfxs, p)
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSupernet(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}

}
}

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
Expand All @@ -117,19 +48,19 @@ func TestSubnetsCB(t *testing.T) {

rtbl := new(Table[any])
pfx := mpp("::1/128")
rtbl.Supernets(pfx)(func(_ netip.Prefix, _ any) bool {
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.Supernets(netip.Prefix{})(func(_ netip.Prefix, _ any) bool {
rtbl.Subnets(netip.Prefix{})(func(_ netip.Prefix, _ any) bool {
t.Errorf("invalid prefix, must not range over")
return false
})

rtbl.Supernets(pfx)(func(p netip.Prefix, v any) bool {
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
Expand Down
45 changes: 0 additions & 45 deletions table_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,6 @@ import (
"testing"
)

func TestTableIsEmptyIterators(t *testing.T) {
rt := new(Table[byte])

_ = rt.All()
if rt.isInit() {
t.Error("All() changed the init state of Table")
}

_ = rt.All4()
if rt.isInit() {
t.Error("All4() changed the init state of Table")
}

_ = rt.All6()
if rt.isInit() {
t.Error("All6() changed the init state of Table")
}

_ = rt.AllSorted()
if rt.isInit() {
t.Error("AllSorted() changed the init state of Table")
}

_ = rt.AllSorted4()
if rt.isInit() {
t.Error("AllSorted4() changed the init state of Table")
}

_ = rt.AllSorted6()
if rt.isInit() {
t.Error("AllSorted6() changed the init state of Table")
}

pfx := mpp("1.2.3.4/32")
_ = rt.Subnets(pfx)
if rt.isInit() {
t.Error("Subnets() changed the init state of Table")
}

_ = rt.Supernets(pfx)
if rt.isInit() {
t.Error("Supernets() changed the init state of Table")
}
}

func TestAll4RangeOverFunc(t *testing.T) {
pfxs := randomPrefixes4(10_000)
seen := make(map[netip.Prefix]int, 10_000)
Expand Down
132 changes: 132 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,138 @@ func TestSize(t *testing.T) {
}
}

func TestInitTableIsEmpty(t *testing.T) {
rt := new(Table[byte])
ot := new(Table[byte])

pfx := mpp("1.2.3.4/32")

_ = rt.String()
if rt.isInit() {
t.Error("String() changed the init state of Table")
}

_ = rt.Fprint(nil)
if rt.isInit() {
t.Error("Fprint() changed the init state of Table")
}

_, _ = rt.MarshalText()
if rt.isInit() {
t.Error("MarshalText() changed the init state of Table")
}

_, _ = rt.MarshalJSON()
if rt.isInit() {
t.Error("MarshalJSON() changed the init state of Table")
}

_ = rt.DumpList4()
if rt.isInit() {
t.Error("DumpList4() changed the init state of Table")
}

_ = rt.DumpList6()
if rt.isInit() {
t.Error("DumpList6() changed the init state of Table")
}

_, _ = rt.Get(pfx)
if rt.isInit() {
t.Error("Get() changed the init state of Table")
}

_, _ = rt.Lookup(pfx.Addr())
if rt.isInit() {
t.Error("Lookup() changed the init state of Table")
}

_, _ = rt.LookupPrefix(pfx)
if rt.isInit() {
t.Error("LookupPrefix() changed the init state of Table")
}

_, _, _ = rt.LookupPrefixLPM(pfx)
if rt.isInit() {
t.Error("LookupPrefixLPM() changed the init state of Table")
}

_ = rt.OverlapsPrefix(pfx)
if rt.isInit() {
t.Error("OverlapsPrefix() changed the init state of Table")
}

_ = rt.Overlaps(ot)
if rt.isInit() {
t.Error("Overlaps() changed the init state of Table")
}

rt.Union(ot)
if rt.isInit() || ot.isInit() {
t.Error("Union() changed the init state of Table")
}

_ = rt.Clone()
if rt.isInit() {
t.Error("Clone() changed the init state of Table")
}

_ = rt.Size()
if rt.isInit() {
t.Error("Size() changed the init state of Table")
}

_ = rt.Size4()
if rt.isInit() {
t.Error("Size4() changed the init state of Table")
}

_ = rt.Size6()
if rt.isInit() {
t.Error("Size6() changed the init state of Table")
}

_ = rt.All()
if rt.isInit() {
t.Error("All() changed the init state of Table")
}

_ = rt.All4()
if rt.isInit() {
t.Error("All4() changed the init state of Table")
}

_ = rt.All6()
if rt.isInit() {
t.Error("All6() changed the init state of Table")
}

_ = rt.AllSorted()
if rt.isInit() {
t.Error("AllSorted() changed the init state of Table")
}

_ = rt.AllSorted4()
if rt.isInit() {
t.Error("AllSorted4() changed the init state of Table")
}

_ = rt.AllSorted6()
if rt.isInit() {
t.Error("AllSorted6() changed the init state of Table")
}

_ = rt.Subnets(pfx)
if rt.isInit() {
t.Error("Subnets() changed the init state of Table")
}

_ = rt.Supernets(pfx)
if rt.isInit() {
t.Error("Supernets() changed the init state of Table")
}
}

// ############ benchmarks ################################

var benchRouteCount = []int{1, 2, 5, 10, 100, 1000, 10_000, 100_000}
Expand Down

0 comments on commit 8ffb23c

Please sign in to comment.