diff --git a/internal/util/set.go b/internal/util/set.go index dc834525..958de146 100644 --- a/internal/util/set.go +++ b/internal/util/set.go @@ -107,7 +107,7 @@ func (s *SortedSet[T]) Len() int { } func (s *SortedSet[T]) Slice() []T { - return s.elements + return append([]T{}, s.elements...) } func NewSortedSet[T comparable]() Set[T] { diff --git a/internal/util/set_test.go b/internal/util/set_test.go index 94318275..5c2c3491 100644 --- a/internal/util/set_test.go +++ b/internal/util/set_test.go @@ -80,5 +80,29 @@ func TestSortedSetSort(t *testing.T) { if !reflect.DeepEqual(s.Slice(), elements) { t.Errorf("Expected set to contain %v, got %v", elements, s.Slice()) } +} +func TestLoopRemoveSortedSet(t *testing.T) { + s1 := NewSortedSet[int]() + s2 := NewSortedSet[int]() + s3 := NewSortedSet[int]() + for i := 0; i < 10; i++ { + s1.Add(i) + if i < 5 { + s2.Add(i) + } else { + s3.Add(i) + } + } + for _, s := range s1.Slice() { + if s2.Contains(s) { + s1.Remove(s) + } + } + for _, s := range s1.Slice() { + if !s3.Contains(s) { + t.Errorf("Expected set to equal %v, got %v", s3.Slice(), s1.Slice()) + break + } + } }