Skip to content

Commit ecd9d48

Browse files
committed
Ensure set.Equal() compares the two set lengths
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 <[email protected]>
1 parent 9f67429 commit ecd9d48

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

set/set.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
155155
// Equal returns true if and only if s1 is equal (as a set) to s2.
156156
// Two sets are equal if their membership is identical.
157157
func (s Set[E]) Equal(s2 Set[E]) bool {
158-
return s.Len() == s.Len() && s.IsSuperset(s2)
158+
return s.Len() == s2.Len() && s.IsSuperset(s2)
159159
}
160160

161161
type sortableSlice[E ordered] []E

set/set_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,17 @@ func TestStringSetEquals(t *testing.T) {
169169
if a.Equal(b) {
170170
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
171171
}
172+
if b.Equal(a) {
173+
t.Errorf("Expected to be not-equal: %v vs %v", b, a)
174+
}
172175

173176
b = New[string]("1", "2", "")
174177
if a.Equal(b) {
175178
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
176179
}
180+
if b.Equal(a) {
181+
t.Errorf("Expected to be not-equal: %v vs %v", b, a)
182+
}
177183

178184
// Check for equality after mutation
179185
a = New[string]()

0 commit comments

Comments
 (0)