From af0138249fb91a6a16d80c777e6f792d94451112 Mon Sep 17 00:00:00 2001 From: janbar Date: Mon, 8 Apr 2024 14:46:13 +0800 Subject: [PATCH 1/2] fix(vfox env): Modifying the slice while traversing the slice reference causes an error Closes https://github.com/version-fox/vfox/issues/186 --- internal/util/set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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] { From 364de326f8f9f13c43af35d51043bec034d63460 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 8 Apr 2024 15:23:30 +0800 Subject: [PATCH 2/2] Add test --- internal/util/set_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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 + } + } }