update:intersect containsby utest

This commit is contained in:
Xan
2022-03-08 15:52:40 +08:00
parent 0cfa22a9fe
commit 2491b05d09
2 changed files with 23 additions and 1 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ func Contains[T comparable](collection []T, element T) bool {
}
// ContainsBy returns true if predicate function return true.
func ContainsBy[T any](collection []T, predicate func(T)bool) bool {
func ContainsBy[T any](collection []T, predicate func(T) bool) bool {
for _, item := range collection {
if predicate(item) {
return true
+22
View File
@@ -16,6 +16,28 @@ func TestContains(t *testing.T) {
is.Equal(result2, false)
}
func TestContainsBy(t *testing.T) {
is := assert.New(t)
type a struct {
A int
B string
}
a1 := []a{a{A: 1, B: "1"}, a{A: 2, B: "2"}, a{A: 3, B: "3"}}
result1 := ContainsBy[a](a1, func(t a) bool { return t.A == 1 && t.B == "2" })
result2 := ContainsBy[a](a1, func(t a) bool { return t.A == 2 && t.B == "2" })
a2 := []string{"aaa", "bbb", "ccc"}
result3 := ContainsBy[string](a2, func(t string) bool { return t == "ccc" })
result4 := ContainsBy[string](a2, func(t string) bool { return t == "ddd" })
is.Equal(result1, false)
is.Equal(result2, true)
is.Equal(result3, true)
is.Equal(result4, false)
}
func TestSome(t *testing.T) {
is := assert.New(t)