From 8c89a8623c5d994e52309beff59a176aef347b9e Mon Sep 17 00:00:00 2001 From: Han Kang Date: Mon, 26 Feb 2024 07:45:37 -0800 Subject: [PATCH] add test coverage to set.go --- set/set_test.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/set/set_test.go b/set/set_test.go index a4bef5c0..b44ed15b 100644 --- a/set/set_test.go +++ b/set/set_test.go @@ -151,13 +151,17 @@ func TestStringSetEquals(t *testing.T) { if !a.Equal(b) { t.Errorf("Expected to be equal: %v vs %v", a, b) } - + a = New[string]("1", "2", "3") + b = New[string]("2", "1") + if a.Equal(b) { + t.Errorf("Expected to be not-equal: %v vs %v", a, b) + } // It is a set; duplicates are ignored + a = New[string]("1", "2") b = New[string]("2", "2", "1") if !a.Equal(b) { t.Errorf("Expected to be equal: %v vs %v", a, b) } - // Edge cases around empty sets / empty strings a = New[string]() b = New[string]() @@ -369,3 +373,32 @@ func clearSetAndAdd[T ordered](s Set[T], a T) { s.Clear() s.Insert(a) } + +func TestPopAny(t *testing.T) { + a := New[string]("1", "2") + _, popped := a.PopAny() + if !popped { + t.Errorf("got len(%d): wanted 1", a.Len()) + } + _, popped = a.PopAny() + if !popped { + t.Errorf("got len(%d): wanted 0", a.Len()) + } + zeroVal, popped := a.PopAny() + if popped { + t.Errorf("got len(%d): wanted 0", a.Len()) + } + if zeroVal != "" { + t.Errorf("should have gotten zero value when popping an empty set") + } +} + +func TestClone(t *testing.T) { + a := New[string]("1", "2") + a.Insert("3") + + got := a.Clone() + if !reflect.DeepEqual(got, a) { + t.Errorf("Expected to be equal: %v vs %v", got, a) + } +}