Skip to content

Commit

Permalink
Merge pull request #156 from Pix4D/set-add
Browse files Browse the repository at this point in the history
sets: add the Add() method
  • Loading branch information
marco-m-pix4d authored May 15, 2024
2 parents fb7c0cc + 78d52de commit 9bc5442
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions sets/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
64 changes: 51 additions & 13 deletions sets/sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"gotest.tools/v3/assert"
"github.com/go-quicktest/qt"

"github.com/Pix4D/cogito/sets"
)
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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) })
}
}

0 comments on commit 9bc5442

Please sign in to comment.