diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f0c39..8e9fce3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v0.12.2] - UNRELEASED + +### Added + +- pkg/sets: add Intersect() and Add() methods. + ## [v0.12.1] - 2024-04-03 ### Minor breaking change diff --git a/go.mod b/go.mod index 59371ca..b61e843 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 require ( dario.cat/mergo v1.0.0 github.com/alexflint/go-arg v1.4.3 - github.com/go-quicktest/qt v1.101.0 + github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6 github.com/google/go-cmp v0.6.0 github.com/sasbury/mini v0.0.0-20181226232755-dc74af49394b gotest.tools/v3 v3.5.1 diff --git a/go.sum b/go.sum index 6926d49..4516abd 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI= -github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow= +github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6 h1:teYtXy9B7y5lHTp8V9KPxpYRAVA7dozigQcMiBust1s= +github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6/go.mod h1:p4lGIVX+8Wa6ZPNDvqcxq36XpUDLh42FLetFU7odllI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= diff --git a/sets/sets.go b/sets/sets.go index 82863c1..3c892f8 100644 --- a/sets/sets.go +++ b/sets/sets.go @@ -60,6 +60,15 @@ func (s *Set[T]) Contains(item T) bool { return found } +// Add inserts item into s. Returns true if the item was present. +func (s *Set[T]) Add(item T) bool { + if s.Contains(item) { + return true + } + s.items[item] = struct{}{} + return false +} + // Remove deletes item from s. Returns true if the item was present. func (s *Set[T]) Remove(item T) bool { if !s.Contains(item) { diff --git a/sets/sets_test.go b/sets/sets_test.go index 3a68e63..32c3fa5 100644 --- a/sets/sets_test.go +++ b/sets/sets_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - "gotest.tools/v3/assert" + "github.com/go-quicktest/qt" "github.com/Pix4D/cogito/sets" ) @@ -22,9 +22,9 @@ func TestFromInt(t *testing.T) { s := sets.From(tc.items...) sorted := s.OrderedList() - assert.Equal(t, s.Size(), tc.wantSize) - assert.DeepEqual(t, sorted, tc.wantList) - assert.Equal(t, fmt.Sprint(s), tc.wantString) + qt.Assert(t, qt.Equals(s.Size(), tc.wantSize)) + qt.Assert(t, qt.DeepEquals(sorted, tc.wantList)) + qt.Assert(t, qt.Equals(fmt.Sprint(s), tc.wantString)) } testCases := []testCase{ @@ -69,9 +69,9 @@ func TestFromString(t *testing.T) { s := sets.From(tc.items...) sorted := s.OrderedList() - assert.Equal(t, s.Size(), tc.wantSize) - assert.DeepEqual(t, sorted, tc.wantList) - assert.Equal(t, fmt.Sprint(s), tc.wantString) + qt.Assert(t, qt.Equals(s.Size(), tc.wantSize)) + qt.Assert(t, qt.DeepEquals(sorted, tc.wantList)) + qt.Assert(t, qt.Equals(fmt.Sprint(s), tc.wantString)) } testCases := []testCase{ @@ -101,7 +101,7 @@ func TestDifference(t *testing.T) { result := tc.s.Difference(tc.x) sorted := result.OrderedList() - assert.DeepEqual(t, sorted, tc.wantList) + qt.Assert(t, qt.DeepEquals(sorted, tc.wantList)) } testCases := []testCase{ @@ -154,7 +154,7 @@ func TestIntersection(t *testing.T) { result := tc.s.Intersection(tc.x) sorted := result.OrderedList() - assert.DeepEqual(t, sorted, tc.wantList) + qt.Assert(t, qt.DeepEquals(sorted, tc.wantList)) } testCases := []testCase{ @@ -214,8 +214,8 @@ func TestRemoveFound(t *testing.T) { found := s.Remove(tc.remove) - assert.DeepEqual(t, s.OrderedList(), tc.wantList) - assert.Assert(t, found) + qt.Assert(t, qt.DeepEquals(s.OrderedList(), tc.wantList)) + qt.Assert(t, qt.IsTrue(found)) } testCases := []testCase{ @@ -250,8 +250,8 @@ func TestRemoveNotFound(t *testing.T) { found := s.Remove(tc.remove) - assert.DeepEqual(t, s.OrderedList(), tc.items) - assert.Assert(t, !found) + qt.Assert(t, qt.DeepEquals(s.OrderedList(), tc.items)) + qt.Assert(t, qt.IsFalse(found)) } testCases := []testCase{ @@ -271,3 +271,41 @@ func TestRemoveNotFound(t *testing.T) { t.Run(tc.name, func(t *testing.T) { test(t, tc) }) } } + +func TestAdd(t *testing.T) { + type testCase struct { + name string + items []int + wantList []int + } + + test := func(t *testing.T, tc testCase) { + s := sets.New[int](5) + for _, item := range tc.items { + s.Add(item) + } + qt.Assert(t, qt.DeepEquals(s.OrderedList(), tc.wantList)) + } + + testCases := []testCase{ + { + name: "one item", + items: []int{3}, + wantList: []int{3}, + }, + { + name: "multiple items", + items: []int{3, 0, 42}, + wantList: []int{0, 3, 42}, + }, + { + name: "duplicates", + items: []int{10, 5, 5, 10, 1}, + wantList: []int{1, 5, 10}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { test(t, tc) }) + } +}