From 3748c683f78cba03009c8cb5cdac1f80a500ffa1 Mon Sep 17 00:00:00 2001 From: Marcin Praski Date: Fri, 16 Sep 2022 12:30:56 +0200 Subject: [PATCH] Improve arrays API --- arrays/arrays.go | 19 +++++++++++++++---- arrays/arrays_test.go | 9 ++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/arrays/arrays.go b/arrays/arrays.go index cba036d..f4e3373 100644 --- a/arrays/arrays.go +++ b/arrays/arrays.go @@ -1,9 +1,11 @@ package arrays -func Contains[T comparable](s []T, e T) bool { +func Contains[T comparable](s []T, e ...T) bool { for _, a := range s { - if a == e { - return true + for _, b := range e { + if a == b { + return true + } } } @@ -12,7 +14,16 @@ func Contains[T comparable](s []T, e T) bool { func Subset[T comparable](s []T, e ...T) bool { for _, a := range e { - if !Contains(s, a) { + var c bool + + for _, b := range s { + if a == b { + c = true + break + } + } + + if !c { return false } } diff --git a/arrays/arrays_test.go b/arrays/arrays_test.go index 5511564..8a6ced8 100644 --- a/arrays/arrays_test.go +++ b/arrays/arrays_test.go @@ -1,6 +1,7 @@ package arrays_test import ( + "strings" "testing" "github.com/blue-health/blue-go-toolbox/arrays" @@ -95,9 +96,11 @@ func TestSubset(t *testing.T) { } for _, c := range cases { - if arrays.Subset(c.super, c.sub...) != c.result { - t.Fatalf("Subset returned false bool. Superset: %v, Subset: %s, Expected: %t", c.super, c.sub, c.result) - } + t.Run(strings.Join(c.super, ",")+":"+strings.Join(c.sub, ","), func(t *testing.T) { + if arrays.Subset(c.super, c.sub...) != c.result { + t.Fatalf("Subset returned false bool. Superset: %v, Subset: %s, Expected: %t", c.super, c.sub, c.result) + } + }) } }