Skip to content

Commit

Permalink
Add more to Set API
Browse files Browse the repository at this point in the history
  • Loading branch information
moorara committed Jul 7, 2023
1 parent e17bd61 commit 6e65d0a
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
25 changes: 25 additions & 0 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/moorara/algo/generic"
)

// Predicate is a function for testing a member of a set.
type Predicate[T any] func(T) bool

// Set represents a set abstract data type.
type Set[T any] interface {
Add(...T)
Expand All @@ -17,6 +20,8 @@ type Set[T any] interface {
IsEmpty() bool
Contains(...T) bool
Equals(Set[T]) bool
Any(Predicate[T]) bool
All(Predicate[T]) bool
Members() []T
Clone() Set[T]
CloneEmpty() Set[T]
Expand Down Expand Up @@ -98,6 +103,26 @@ func (s *set[T]) Equals(t Set[T]) bool {
return true
}

func (s *set[T]) Any(p Predicate[T]) bool {
for _, m := range s.members {
if p(m) {
return true
}
}

return false
}

func (s *set[T]) All(p Predicate[T]) bool {
for _, m := range s.members {
if !p(m) {
return false
}
}

return len(s.members) > 0
}

func (s *set[T]) Members() []T {
members := make([]T, len(s.members))
copy(members, s.members)
Expand Down
99 changes: 99 additions & 0 deletions set/set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package set

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -268,6 +269,104 @@ func TestSet_Equals(t *testing.T) {
}
}

func TestSet_Any(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()
predicate := func(s string) bool {
return strings.ToUpper(s) == s
}

tests := []struct {
name string
s *set[string]
p Predicate[string]
expected bool
}{
{
name: "Empty",
s: &set[string]{
equal: eqFunc,
members: []string{},
},
p: predicate,
expected: false,
},
{
name: "NonEmpty_No",
s: &set[string]{
equal: eqFunc,
members: []string{"a", "b", "c", "d"},
},
p: predicate,
expected: false,
},
{
name: "NonEmpty_Yes",
s: &set[string]{
equal: eqFunc,
members: []string{"a", "B", "c", "d"},
},
p: predicate,
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
b := tc.s.Any(tc.p)
assert.Equal(t, tc.expected, b)
})
}
}

func TestSet_All(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()
predicate := func(s string) bool {
return strings.ToUpper(s) == s
}

tests := []struct {
name string
s *set[string]
p Predicate[string]
expected bool
}{
{
name: "Empty",
s: &set[string]{
equal: eqFunc,
members: []string{},
},
p: predicate,
expected: false,
},
{
name: "NonEmpty_No",
s: &set[string]{
equal: eqFunc,
members: []string{"A", "B", "c", "D"},
},
p: predicate,
expected: false,
},
{
name: "NonEmpty_Yes",
s: &set[string]{
equal: eqFunc,
members: []string{"A", "B", "C", "D"},
},
p: predicate,
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
b := tc.s.All(tc.p)
assert.Equal(t, tc.expected, b)
})
}
}

func TestSet_Members(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()

Expand Down

0 comments on commit 6e65d0a

Please sign in to comment.