Skip to content

Commit

Permalink
Ensure set.Equal() compares the two set lengths
Browse files Browse the repository at this point in the history
set.Equal() currently checks the length of the owning set against
itself, so a.Equal(b) returns true if a is a superset of b, with no
further restriction.

This fixes the length check and adds unit tests to catch this case.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Jul 7, 2023
1 parent 9f67429 commit ecd9d48
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
// Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical.
func (s Set[E]) Equal(s2 Set[E]) bool {
return s.Len() == s.Len() && s.IsSuperset(s2)
return s.Len() == s2.Len() && s.IsSuperset(s2)
}

type sortableSlice[E ordered] []E
Expand Down
6 changes: 6 additions & 0 deletions set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,17 @@ func TestStringSetEquals(t *testing.T) {
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
if b.Equal(a) {
t.Errorf("Expected to be not-equal: %v vs %v", b, a)
}

b = New[string]("1", "2", "")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
if b.Equal(a) {
t.Errorf("Expected to be not-equal: %v vs %v", b, a)
}

// Check for equality after mutation
a = New[string]()
Expand Down

0 comments on commit ecd9d48

Please sign in to comment.