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

add IPSet.Equal #127

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effectively zeros a. But the underlying representation contains non-empty in and out. Fixing this is in part the topic of #109 and #110 and a decision about when to minimize the representation.

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)
}