Unverified Commit 7768f965 authored by Marten Seemann's avatar Marten Seemann Committed by GitHub

add a Contains function (#172)

parent f9011923
......@@ -200,3 +200,13 @@ addrloop:
}
return b
}
// Contains reports whether addr is contained in addrs.
func Contains(addrs []Multiaddr, addr Multiaddr) bool {
for _, a := range addrs {
if addr.Equal(a) {
return true
}
}
return false
}
......@@ -762,3 +762,16 @@ func TestFilterAddrs(t *testing.T) {
require.ElementsMatch(t, FilterAddrs(good, filter), good)
require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good)
}
func TestContains(t *testing.T) {
a1 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")
a2 := newMultiaddr(t, "/ip4/1.1.1.1/tcp/999")
a3 := newMultiaddr(t, "/ip4/1.2.3.4/udp/443/quic")
addrs := []Multiaddr{a1, a2, a3}
require.True(t, Contains(addrs, a1))
require.True(t, Contains(addrs, a2))
require.True(t, Contains(addrs, a3))
require.False(t, Contains(addrs, newMultiaddr(t, "/ip4/4.3.2.1/udp/1234/utp")))
require.False(t, Contains(nil, a1))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment