Deps update

This commit is contained in:
Frank Denis 2018-04-25 20:23:31 +02:00
parent dd878d4c60
commit e8b183e576
12 changed files with 188 additions and 145 deletions

View file

@ -1,3 +1,7 @@
## 1.2.0 (2018/04/25)
- Add ContainedIP() as fast way to check an IP [#7](https://github.com/k-sone/critbitgo/pull/7)
## 1.1.0 (2016/12/29)
- Add `LongestPrefix ` and `Walk` functions

View file

@ -236,9 +236,11 @@ func (t *Trie) Allprefixed(prefix []byte, handle func(key []byte, value interfac
p := &t.root
top := p
if len(prefix) > 0 {
for p.internal != nil {
top = p
p = &p.internal.child[p.internal.direction(prefix)]
for q := p.internal; q != nil; q = p.internal {
p = &q.child[q.direction(prefix)]
if q.offset < len(prefix) {
top = p
}
}
// check prefix

View file

@ -137,7 +137,7 @@ func TestSize(t *testing.T) {
}
func TestAllprefixed(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab", "bc"}
trie := buildTrie(t, keys)
elems := make([]string, 0, len(keys))
@ -150,10 +150,10 @@ func TestAllprefixed(t *testing.T) {
if !trie.Allprefixed([]byte{}, handle) {
t.Error("Allprefixed() - invalid result")
}
if len(elems) != 9 {
if len(elems) != 10 {
t.Errorf("Allprefixed() - invalid elems length [%v]", elems)
}
for i, key := range []string{"", "a", "aa", "ab", "aba", "b", "ba", "bab", "bb"} {
for i, key := range []string{"", "a", "aa", "ab", "aba", "b", "ba", "bab", "bb", "bc"} {
if key != elems[i] {
t.Errorf("Allprefixed() - not found [%s]", key)
}
@ -172,6 +172,19 @@ func TestAllprefixed(t *testing.T) {
}
}
elems = make([]string, 0, len(keys))
if !trie.Allprefixed([]byte("bb"), handle) {
t.Error("Allprefixed() - invalid result")
}
if len(elems) != 1 {
t.Errorf("Allprefixed() - invalid elems length [%v]", elems)
}
for i, key := range []string{"bb"} {
if key != elems[i] {
t.Errorf("Allprefixed() - not found [%s]", key)
}
}
elems = make([]string, 0, len(keys))
handle = func(key []byte, value interface{}) bool {
if k := string(key); k == value {

View file

@ -97,9 +97,25 @@ func (n *Net) MatchCIDR(s string) (route *net.IPNet, value interface{}, err erro
return
}
// Return a bool indicating whether a route would be found
func (n *Net) ContainedIP(ip net.IP) (contained bool, err error) {
k, _, err := n.matchIP(ip)
contained = k != nil
return
}
// Return a specific route by using the longest prefix matching.
// If `ip` is invalid IP, or a route is not found, `route` is nil.
func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error) {
k, v, err := n.matchIP(ip)
if k != nil {
route = netKeyToIPNet(k)
value = v
}
return
}
func (n *Net) matchIP(ip net.IP) (k []byte, v interface{}, err error) {
var isV4 bool
ip, isV4, err = netValidateIP(ip)
if err != nil {
@ -111,10 +127,7 @@ func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error
} else {
mask = mask128
}
if k, v := n.match(netIPNetToKey(ip, mask)); k != nil {
route = netKeyToIPNet(k)
value = v
}
k, v = n.match(netIPNetToKey(ip, mask))
return
}

View file

@ -31,6 +31,12 @@ func TestNet(t *testing.T) {
if r, v, err := trie.MatchIP(hostIP); r != nil || v != nil || err != nil {
t.Errorf("MatchIP() - phantom: %v, %v, %v", r, v, err)
}
if _, err := trie.ContainedIP(net.IP([]byte{})); err == nil {
t.Error("ContainedIP() - not error")
}
if b, err := trie.ContainedIP(hostIP); b || err != nil {
t.Errorf("ContainedIP() - phantom: %v, %v", b, err)
}
if _, _, err := trie.DeleteCIDR(""); err == nil {
t.Error("DeleteCIDR() - not error")
}
@ -50,6 +56,9 @@ func TestNet(t *testing.T) {
if r, v, err := trie.MatchIP(hostIP); r == nil || r.String() != cidr || v != &cidr || err != nil {
t.Errorf("MatchIP() - failed: %v, %v, %v", r, v, err)
}
if b, err := trie.ContainedIP(hostIP); !b || err != nil {
t.Errorf("ContainedIP() - failed: %v, %v", b, err)
}
if v, ok, err := trie.DeleteCIDR(cidr); v != &cidr || !ok || err != nil {
t.Errorf("DeleteCIDR() - failed: %v, %v, %v", v, ok, err)
}