From 7e26999cb807087ab240ec5a45470816f7373d77 Mon Sep 17 00:00:00 2001 From: lixiaohui Date: Fri, 13 May 2022 17:58:35 +0800 Subject: [PATCH] [sets] remove equals & refactor tests --- sets/set.go | 19 ++------ sets/set_test.go | 122 +++++++++++++++++++++-------------------------- 2 files changed, 58 insertions(+), 83 deletions(-) diff --git a/sets/set.go b/sets/set.go index b0982e4..be3b5e6 100644 --- a/sets/set.go +++ b/sets/set.go @@ -104,22 +104,6 @@ func (self HashSet[TKey]) ToSlice() []TKey { return keys } -// Equals returns true if the given set is equal to this set. -func (self HashSet[TKey]) Equals(other HashSet[TKey]) bool { - if other == nil { - return false - } - if other.Size() != self.Size() { - return false - } - for key := range self { - if !other.Contains(key) { - return false - } - } - return true -} - // Clone returns a copy of the set. func (self HashSet[TKey]) Clone() HashSet[TKey] { other := NewHashSet[TKey]() @@ -210,6 +194,9 @@ func (self HashSet[TKey]) SymmetricDifference(other HashSet[TKey]) HashSet[TKey] // Equal returns true if the given set is equal to this set. func (self HashSet[TKey]) Equal(other HashSet[TKey]) bool { + if other == nil || self == nil { + return false + } if self.Size() != other.Size() { return false } diff --git a/sets/set_test.go b/sets/set_test.go index a203c52..af2aeb5 100644 --- a/sets/set_test.go +++ b/sets/set_test.go @@ -15,19 +15,19 @@ func TestHashSet_Add(t *testing.T) { s := NewHashSet[int]() s.Add(1) s.Add(2) - a.Assert(true, quicktest.Equals, s.Contains(1)) - a.Assert(true, quicktest.Equals, s.Contains(2)) - a.Assert(false, quicktest.Equals, s.Contains(3)) + a.Assert(s.Contains(1), quicktest.IsTrue) + a.Assert(s.Contains(2), quicktest.IsTrue) + a.Assert(s.Contains(3), quicktest.IsFalse) } func TestHashSet_AddAll(t *testing.T) { a := quicktest.New(t) s := NewHashSet[int]() s.AddAll([]int{1, 2}) - a.Assert(true, quicktest.Equals, s.Contains(1)) - a.Assert(true, quicktest.Equals, s.Contains(2)) - a.Assert(false, quicktest.Equals, s.Contains(3)) - a.Assert(2, quicktest.Equals, s.Size()) + a.Assert(s.Contains(1), quicktest.IsTrue) + a.Assert(s.Contains(2), quicktest.IsTrue) + a.Assert(s.Contains(3), quicktest.IsFalse) + a.Assert(s.Size(), quicktest.Equals, 2) } func TestHashSet_Remove(t *testing.T) { @@ -36,9 +36,9 @@ func TestHashSet_Remove(t *testing.T) { s.Add(1) s.Add(2) s.Remove(1) - a.Assert(false, quicktest.Equals, s.Contains(1)) - a.Assert(true, quicktest.Equals, s.Contains(2)) - a.Assert(1, quicktest.Equals, s.Size()) + a.Assert(s.Contains(1), quicktest.IsFalse) + a.Assert(s.Contains(2), quicktest.IsTrue) + a.Assert(s.Size(), quicktest.Equals, 1) } func TestHashSet_RemoveBy(t *testing.T) { @@ -48,10 +48,10 @@ func TestHashSet_RemoveBy(t *testing.T) { s.Add(2) s.Add(3) s.RemoveBy(func(i int) bool { return i == 1 }) - a.Assert(false, quicktest.Equals, s.Contains(1)) - a.Assert(true, quicktest.Equals, s.Contains(2)) - a.Assert(true, quicktest.Equals, s.Contains(3)) - a.Assert(2, quicktest.Equals, s.Size()) + a.Assert(s.Contains(1), quicktest.IsFalse) + a.Assert(s.Contains(2), quicktest.IsTrue) + a.Assert(s.Contains(3), quicktest.IsTrue) + a.Assert(s.Size(), quicktest.Equals, 2) } func TestHashSet_Clear(t *testing.T) { @@ -60,9 +60,9 @@ func TestHashSet_Clear(t *testing.T) { s.Add(1) s.Add(2) s.Clear() - a.Assert(false, quicktest.Equals, s.Contains(1)) - a.Assert(false, quicktest.Equals, s.Contains(2)) - a.Assert(0, quicktest.Equals, s.Size()) + a.Assert(s.Contains(1), quicktest.IsFalse) + a.Assert(s.Contains(2), quicktest.IsFalse) + a.Assert(s.Size(), quicktest.Equals, 0) } func TestHashSet_ForEach(t *testing.T) { @@ -72,7 +72,7 @@ func TestHashSet_ForEach(t *testing.T) { s.Add(item{key: 2}) s.Add(item{key: 3}) s.ForEach(func(i item) { - a.Assert(i.key, quicktest.Equals, i.key) + a.Assert([]int{1, 2, 3}, quicktest.Contains, i.key) }) } @@ -81,9 +81,9 @@ func TestHashSet_Contains(t *testing.T) { s := NewHashSet[int]() s.Add(1) s.Add(2) - a.Assert(true, quicktest.Equals, s.Contains(1)) - a.Assert(true, quicktest.Equals, s.Contains(2)) - a.Assert(false, quicktest.Equals, s.Contains(3)) + a.Assert(s.Contains(1), quicktest.IsTrue) + a.Assert(s.Contains(2), quicktest.IsTrue) + a.Assert(s.Contains(3), quicktest.IsFalse) } func TestHashSet_ContainsAll(t *testing.T) { @@ -92,8 +92,8 @@ func TestHashSet_ContainsAll(t *testing.T) { s.Add(1) s.Add(2) s.Add(3) - a.Assert(true, quicktest.Equals, s.ContainsAll([]int{1, 2, 3})) - a.Assert(false, quicktest.Equals, s.ContainsAll([]int{1, 2, 4})) + a.Assert(s.ContainsAll([]int{1, 2, 3}), quicktest.IsTrue) + a.Assert(s.ContainsAll([]int{1, 2, 4}), quicktest.IsFalse) } func TestHashSet_ContainsAny(t *testing.T) { @@ -113,19 +113,19 @@ func TestHashSet_Size(t *testing.T) { s.Add(1) s.Add(2) s.Add(3) - a.Assert(3, quicktest.Equals, s.Size()) + a.Assert(s.Size(), quicktest.Equals, 3) s.Add(5) - a.Assert(4, quicktest.Equals, s.Size()) + a.Assert(s.Size(), quicktest.Equals, 4) s.Remove(3) - a.Assert(3, quicktest.Equals, s.Size()) + a.Assert(s.Size(), quicktest.Equals, 3) } func TestHashSet_IsEmpty(t *testing.T) { a := quicktest.New(t) s := NewHashSet[int]() - a.Assert(true, quicktest.Equals, s.IsEmpty()) + a.Assert(s.IsEmpty(), quicktest.IsTrue) s.Add(1) - a.Assert(false, quicktest.Equals, s.IsEmpty()) + a.Assert(s.IsEmpty(), quicktest.IsFalse) } func TestHashSet_ToSlice(t *testing.T) { @@ -134,23 +134,10 @@ func TestHashSet_ToSlice(t *testing.T) { s.Add(1) s.Add(2) s.Add(3) - a.Assert([]int{1, 2, 3}, quicktest.DeepEquals, s.ToSlice()) -} - -func TestHashSet_Equals(t *testing.T) { - a := quicktest.New(t) - s1 := NewHashSet[int]() - s2 := NewHashSet[int]() - a.Assert(true, quicktest.Equals, s1.Equals(s2)) - s1.Add(1) - s1.Add(2) - s1.Add(3) - s2.Add(1) - s2.Add(2) - s2.Add(3) - a.Assert(true, quicktest.Equals, s1.Equals(s2)) - s2.Add(4) - a.Assert(false, quicktest.Equals, s1.Equals(s2)) + a.Assert(s.ToSlice(), quicktest.HasLen, 3) + a.Assert(s.ToSlice(), quicktest.Contains, 1) + a.Assert(s.ToSlice(), quicktest.Contains, 2) + a.Assert(s.ToSlice(), quicktest.Contains, 3) } func TestHashSet_Clone(t *testing.T) { @@ -160,9 +147,9 @@ func TestHashSet_Clone(t *testing.T) { s1.Add(2) s1.Add(3) s2 := s1.Clone() - a.Assert(true, quicktest.Equals, s1.Equals(s2)) + a.Assert(s1.Equal(s2), quicktest.IsTrue) s2.Add(4) - a.Assert(false, quicktest.Equals, s1.Equals(s2)) + a.Assert(s1.Equal(s2), quicktest.IsFalse) } func TestHashSet_DeepCloneBy(t *testing.T) { @@ -172,9 +159,9 @@ func TestHashSet_DeepCloneBy(t *testing.T) { s1.Add(2) s1.Add(3) s2 := s1.DeepCloneBy(func(i int) int { return i }) - a.Assert(true, quicktest.Equals, s1.Equals(s2)) + a.Assert(s1.Equal(s2), quicktest.IsTrue) s2.Add(4) - a.Assert(false, quicktest.Equals, s1.Equals(s2)) + a.Assert(s1.Equal(s2), quicktest.IsFalse) } func TestHashSet_SupersetOf(t *testing.T) { @@ -184,13 +171,13 @@ func TestHashSet_SupersetOf(t *testing.T) { s1.Add(2) s1.Add(3) s2 := NewHashSet[int]() - a.Assert(true, quicktest.Equals, s1.SupersetOf(s2)) + a.Assert(s1.SupersetOf(s2), quicktest.IsTrue) s2.Add(1) s2.Add(2) s2.Add(3) - a.Assert(true, quicktest.Equals, s1.SupersetOf(s2)) + a.Assert(s1.SupersetOf(s2), quicktest.IsTrue) s2.Add(4) - a.Assert(false, quicktest.Equals, s1.SupersetOf(s2)) + a.Assert(s1.SupersetOf(s2), quicktest.IsFalse) } func TestHashSet_SubsetOf(t *testing.T) { @@ -200,13 +187,13 @@ func TestHashSet_SubsetOf(t *testing.T) { s1.Add(2) s1.Add(3) s2 := NewHashSet[int]() - a.Assert(true, quicktest.Equals, s2.SubsetOf(s1)) + a.Assert(s2.SubsetOf(s1), quicktest.IsTrue) s2.Add(1) s2.Add(2) s2.Add(3) - a.Assert(true, quicktest.Equals, s2.SubsetOf(s1)) + a.Assert(s2.SubsetOf(s1), quicktest.IsTrue) s2.Add(4) - a.Assert(false, quicktest.Equals, s2.SubsetOf(s1)) + a.Assert(s2.SubsetOf(s1), quicktest.IsFalse) } func TestHashSet_Union(t *testing.T) { @@ -220,7 +207,8 @@ func TestHashSet_Union(t *testing.T) { s2.Add(4) s2.Add(5) s3 := s1.Union(s2) - a.Assert(true, quicktest.Equals, s3.ContainsAll([]int{1, 2, 3, 4, 5})) + a.Assert(s3.ContainsAll([]int{1, 2, 3, 4, 5}), quicktest.IsTrue) + a.Assert(s3.Size(), quicktest.Equals, 5) } func TestHashSet_Intersection(t *testing.T) { @@ -234,8 +222,8 @@ func TestHashSet_Intersection(t *testing.T) { s2.Add(4) s2.Add(5) s3 := s1.Intersection(s2) - a.Assert(true, quicktest.Equals, s3.ContainsAll([]int{1})) - a.Assert(1, quicktest.Equals, s3.Size()) + a.Assert(s3.ContainsAll([]int{1}), quicktest.IsTrue) + a.Assert(s3.Size(), quicktest.Equals, 1) } func TestHashSet_Difference(t *testing.T) { @@ -249,8 +237,8 @@ func TestHashSet_Difference(t *testing.T) { s2.Add(4) s2.Add(5) s3 := s1.Difference(s2) - a.Assert(true, quicktest.Equals, s3.ContainsAll([]int{2, 3})) - a.Assert(2, quicktest.Equals, s3.Size()) + a.Assert(s3.ContainsAll([]int{2, 3}), quicktest.IsTrue) + a.Assert(s3.Size(), quicktest.Equals, 2) } func TestHashSet_SymmetricDifference(t *testing.T) { @@ -264,8 +252,8 @@ func TestHashSet_SymmetricDifference(t *testing.T) { s2.Add(4) s2.Add(5) s3 := s1.SymmetricDifference(s2) - a.Assert(true, quicktest.Equals, s3.ContainsAll([]int{2, 3, 4, 5})) - a.Assert(4, quicktest.Equals, s3.Size()) + a.Assert(s3.ContainsAll([]int{2, 3, 4, 5}), quicktest.IsTrue) + a.Assert(s3.Size(), quicktest.Equals, 4) } func TestHashSet_Equal(t *testing.T) { @@ -278,9 +266,9 @@ func TestHashSet_Equal(t *testing.T) { s2.Add(1) s2.Add(2) s2.Add(3) - a.Assert(true, quicktest.Equals, s1.Equal(s2)) + a.Assert(s1.Equal(s2), quicktest.IsTrue) s2.Add(4) - a.Assert(false, quicktest.Equals, s1.Equal(s2)) + a.Assert(s1.Equal(s2), quicktest.IsFalse) } func TestDeepClone(t *testing.T) { @@ -290,9 +278,9 @@ func TestDeepClone(t *testing.T) { s1.Add(item{key: 2}) s1.Add(item{key: 3}) s2 := DeepClone(s1) - a.Assert(true, quicktest.Equals, s1.Equal(s2)) + a.Assert(s1.Equal(s2), quicktest.IsTrue) s2.Add(item{key: 4}) - a.Assert(false, quicktest.Equals, s1.Equal(s2)) + a.Assert(s1.Equal(s2), quicktest.IsFalse) } func TestMap(t *testing.T) { @@ -302,5 +290,5 @@ func TestMap(t *testing.T) { s1.Add(2) s1.Add(3) s2 := Map(s1, func(i int) int { return i * 2 }) - a.Assert(true, quicktest.Equals, s2.ContainsAll([]int{2, 4, 6})) + a.Assert(s2.ContainsAll([]int{2, 4, 6}), quicktest.IsTrue) }