Skip to content

Commit

Permalink
Fix bug in AddSet and RemoveSet methods of ds.Set (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Jul 10, 2023
1 parent 3eaa195 commit ff5dc0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions ds/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func (s *Set[T]) Add(item T) bool {
func (s *Set[T]) AddSet(other *Set[T]) bool {
var changed bool
for item := range other.items {
changed = changed || s.Add(item)
// NOTE: OR order is important.
changed = s.Add(item) || changed
}
return changed
}
Expand All @@ -135,7 +136,8 @@ func (s *Set[T]) Remove(item T) bool {
func (s *Set[T]) RemoveSet(other *Set[T]) bool {
var changed bool
for item := range other.items {
changed = changed || s.Remove(item)
// NOTE: OR order is important.
changed = s.Remove(item) || changed
}
return changed
}
Expand Down
8 changes: 5 additions & 3 deletions ds/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,20 @@ var _ = Describe("Set", func() {
other.Add("second")
other.Add("third")
other.Add("fourth")
other.Add("fifth")
Expect(set.AddSet(other)).To(BeTrue())
})

It("changes its size accordingly", func() {
Expect(set.Size()).To(Equal(4))
Expect(set.Size()).To(Equal(5))
})

It("contains the union of the sets", func() {
Expect(set.Contains("first")).To(BeTrue())
Expect(set.Contains("second")).To(BeTrue())
Expect(set.Contains("third")).To(BeTrue())
Expect(set.Contains("fourth")).To(BeTrue())
Expect(set.Contains("fifth")).To(BeTrue())
})

It("returns false if the set is already contained", func() {
Expand All @@ -147,18 +149,18 @@ var _ = Describe("Set", func() {
When("another set is removed", func() {
BeforeEach(func() {
other := ds.NewSet[string](2)
other.Add("second")
other.Add("third")
other.Add("fourth")
Expect(set.RemoveSet(other)).To(BeTrue())
})

It("changes its size accordingly", func() {
Expect(set.Size()).To(Equal(2))
Expect(set.Size()).To(Equal(1))
})

It("contains the difference of the sets", func() {
Expect(set.Contains("first")).To(BeTrue())
Expect(set.Contains("second")).To(BeTrue())
})

It("returns false if the set is not contained", func() {
Expand Down

0 comments on commit ff5dc0c

Please sign in to comment.