Skip to content

Commit

Permalink
[collection] add helpers easier to use with slices (#547)
Browse files Browse the repository at this point in the history
<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description

add utilities to the collection module


### Test Coverage

<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->

- [x]  This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [ ] Additional tests are not required for this change (e.g.
documentation update).
  • Loading branch information
acabarbaye authored Jan 10, 2025
1 parent 70aef6a commit 545d07c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/20250108150125.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[collection]` add helpers easier to use with slices
36 changes: 36 additions & 0 deletions utils/collection/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func UniqueEntries[T comparable](slice []T) []T {

// Any returns true if there is at least one element of the slice which is true.
func Any(slice []bool) bool {
if len(slice) == 0 {
return false
}
for i := range slice {
if slice[i] {
return true
Expand All @@ -67,6 +70,21 @@ func Any(slice []bool) bool {
return false
}

// AnyTrue returns whether there is a value set to true
func AnyTrue(values ...bool) bool {
return Any(values)
}

// AnyFunc returns whether there is at least one element of slice s for which f() returns true.
func AnyFunc[S ~[]E, E any](s S, f func(E) bool) bool {
all := make([]bool, 0, len(s))
for i := range s {
all = append(all, f(s[i]))

}
return Any(all)
}

// AnyEmpty returns whether there is one entry in the slice which is empty.
// If strict, then whitespaces are considered as empty strings
func AnyEmpty(strict bool, slice []string) bool {
Expand All @@ -76,6 +94,9 @@ func AnyEmpty(strict bool, slice []string) bool {

// All returns true if all items of the slice are true.
func All(slice []bool) bool {
if len(slice) == 0 {
return false
}
for i := range slice {
if !slice[i] {
return false
Expand All @@ -84,6 +105,21 @@ func All(slice []bool) bool {
return true
}

// AllTrue returns whether all values are true.
func AllTrue(values ...bool) bool {
return All(values)
}

// AllFunc returns whether f returns true for all the elements of slice s.
func AllFunc[S ~[]E, E any](s S, f func(E) bool) bool {
all := make([]bool, 0, len(s))
for i := range s {
all = append(all, f(s[i]))

}
return All(all)
}

// AllNotEmpty returns whether all elements of the slice are not empty.
// If strict, then whitespaces are considered as empty strings
func AllNotEmpty(strict bool, slice []string) bool {
Expand Down
39 changes: 39 additions & 0 deletions utils/collection/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,58 @@ func TestFindInSlice(t *testing.T) {
}

func TestAny(t *testing.T) {
assert.False(t, Any([]bool{}))
assert.True(t, Any([]bool{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false}))
assert.False(t, Any([]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}))
assert.True(t, Any([]bool{true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}))
assert.True(t, Any([]bool{true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true}))
}

func TestAnyTrue(t *testing.T) {
assert.False(t, AnyTrue())
assert.True(t, AnyTrue(false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false))
assert.False(t, AnyTrue(false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false))
assert.True(t, AnyTrue(true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true))
assert.True(t, AnyTrue(true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true))
}

func TestAnyFunc(t *testing.T) {
f := func(v bool) bool {
return v
}
assert.False(t, AnyFunc([]bool{}, f))
assert.True(t, AnyFunc([]bool{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false}, f))
assert.False(t, AnyFunc([]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}, f))
assert.True(t, AnyFunc([]bool{true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}, f))
assert.True(t, AnyFunc([]bool{true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true}, f))
}

func TestAll(t *testing.T) {
assert.False(t, All([]bool{}))
assert.False(t, All([]bool{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false}))
assert.False(t, All([]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}))
assert.True(t, All([]bool{true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}))
assert.False(t, All([]bool{true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true}))
}

func TestAllTrue(t *testing.T) {
assert.False(t, AllTrue(false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false))
assert.False(t, AllTrue(false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false))
assert.True(t, AllTrue(true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true))
assert.False(t, AllTrue(true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true))
}

func TestAllFunc(t *testing.T) {
f := func(v bool) bool {
return v
}
assert.False(t, AllFunc([]bool{}, f))
assert.False(t, AllFunc([]bool{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false}, f))
assert.False(t, AllFunc([]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}, f))
assert.True(t, AllFunc([]bool{true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}, f))
assert.False(t, AllFunc([]bool{true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true}, f))
}

func TestAnyEmpty(t *testing.T) {
assert.True(t, AnyEmpty(false, []string{faker.Username(), faker.Name(), faker.Sentence(), ""}))
assert.False(t, AnyEmpty(false, []string{faker.Username(), " ", faker.Name(), faker.Sentence()}))
Expand Down

0 comments on commit 545d07c

Please sign in to comment.