Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
add IPSet.Equal
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Bleecher Snyder <[email protected]>
  • Loading branch information
josharian committed Jan 29, 2021
1 parent 06debf9 commit 5315a37
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,18 @@ func (s *IPSet) ContainsFunc() (contains func(IP) bool) {
return rv[i].contains(ip)
}
}

// Equal reports whether s contains exactly the same IPs as t.
func (s *IPSet) Equal(t *IPSet) bool {
sr := s.Ranges()
tr := t.Ranges()
if len(sr) != len(tr) {
return false
}
for i := range sr {
if sr[i] != tr[i] {
return false
}
}
return true
}
32 changes: 32 additions & 0 deletions ipset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,35 @@ func TestIPSetRangesStress(t *testing.T) {
}
}
}

func TestIPSetEqual(t *testing.T) {
a := new(IPSet)
b := new(IPSet)

assertEqual := func(want bool) {
t.Helper()
if got := a.Equal(b); got != want {
t.Errorf("%v.Equal(%v) = %v want %v", a, b, got, want)
}
}

a.Add(MustParseIP("1.1.1.0"))
a.Add(MustParseIP("1.1.1.1"))
a.Add(MustParseIP("1.1.1.2"))
b.AddPrefix(MustParseIPPrefix("1.1.1.0/31"))
b.Add(MustParseIP("1.1.1.2"))
assertEqual(true)

a.RemoveSet(a)
assertEqual(false)
b.RemoveSet(b)
assertEqual(true)

a.Add(MustParseIP("1.1.1.0"))
a.Add(MustParseIP("1.1.1.1"))
a.Add(MustParseIP("1.1.1.2"))

b.AddPrefix(MustParseIPPrefix("1.1.1.0/30"))
b.Remove(MustParseIP("1.1.1.3"))
assertEqual(true)
}

0 comments on commit 5315a37

Please sign in to comment.