From ecd9d48119372e074ca1defad8e8383897219f33 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 7 Jul 2023 09:49:19 +0200 Subject: [PATCH] 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 --- set/set.go | 2 +- set/set_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/set/set.go b/set/set.go index 172482cd..563bb43c 100644 --- a/set/set.go +++ b/set/set.go @@ -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 diff --git a/set/set_test.go b/set/set_test.go index ea2d9d50..a4bef5c0 100644 --- a/set/set_test.go +++ b/set/set_test.go @@ -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]()