Skip to content

Commit

Permalink
Fix pfx dedup and add tests (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
takt authored and czerwonk committed Oct 29, 2019
1 parent 5496009 commit 9a2f84b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
25 changes: 25 additions & 0 deletions net/ip_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net

import (
"testing"

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

func TestIPCache(t *testing.T) {
a := &IP{
higher: 100,
lower: 200,
isLegacy: false,
}
b := &IP{
higher: 100,
lower: 200,
isLegacy: false,
}

x := a.Dedup()
y := b.Dedup()

assert.Equal(t, true, x == y)
}
2 changes: 1 addition & 1 deletion net/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *Prefix) Dedup() *Prefix {

// Less compares prefixes for use in btree.Btree
func (p *Prefix) Less(other btree.Item) bool {
if uintptr(unsafe.Pointer(p)) < uintptr(unsafe.Pointer(other.(*Prefix))) {
if uintptr(unsafe.Pointer(p.addr)) < uintptr(unsafe.Pointer(other.(*Prefix).addr)) {
return true
}

Expand Down
34 changes: 34 additions & 0 deletions net/prefix_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net

import (
"testing"

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

func TestPrefixCache(t *testing.T) {
a := &Prefix{
addr: &IP{
higher: 100,
lower: 200,
isLegacy: false,
},
pfxlen: 64,
}
b := &Prefix{
addr: &IP{
higher: 100,
lower: 200,
isLegacy: false,
},
pfxlen: 64,
}

a.addr = a.addr.Dedup()
b.addr = b.addr.Dedup()

x := a.Dedup()
y := b.Dedup()

assert.Equal(t, true, x == y)
}

0 comments on commit 9a2f84b

Please sign in to comment.