Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basiccheck: add SimilarSlice function #26

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* add `SimilarSlice` function in `basiccheck` package

## v0.10.0

* add `StringHasOneOfSuffixes` function in `basiccheck` package
Expand Down
11 changes: 11 additions & 0 deletions basiccheck/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func ExampleEqualSlice() {
}
}

func ExampleSimilarSlice() {
a := []string{"foo", "bar", "baz"}
b := []string{"baz", "foo", "bar"}
if basiccheck.SimilarSlice(a, b) {
fmt.Print("a == b")
// Output: a == b
} else {
fmt.Print("a != b")
}
}

func ExampleOneInSliceWith() {
input := []string{"foo", "bar", "baz"}
if basiccheck.OneInSliceWith(input, func(s string) bool {
Expand Down
15 changes: 15 additions & 0 deletions basiccheck/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ func EqualSlice[T comparable](a, b []T) bool {
return true
}

// SimilarSlice check if two slice is Similar:
// same length, same element (not necessarily in same order).
func SimilarSlice[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for _, v := range a {
if !InSlice(v, b) {
return false
}
}

return true
}

// OneInSliceWith check if at least one element in a slice
// returns true with the function 'find' passed in arguments.
//
Expand Down
32 changes: 32 additions & 0 deletions basiccheck/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ func TestEqualSlice(t *testing.T) {
}
}

func TestSimilarSlice(t *testing.T) {
sliceA := []string{"foo", "bar", "baz"}
sliceB := []string{"foo", "baz", "bar"}
sliceC := []string{"foo", "bar"}

if !basiccheck.SimilarSlice(sliceA, sliceB) {
t.Errorf("SimilarSlice didn't find similar slice %v, %v", sliceA, sliceB)
}
if basiccheck.SimilarSlice(sliceA, sliceC) {
t.Errorf("SimilarSlice found similar slice %v, %v", sliceA, sliceC)
}

sliceC = append(sliceC, "baz")
if !basiccheck.SimilarSlice(sliceA, sliceC) {
t.Errorf("SimilarSlice didn't find similar slice %v, %v", sliceA, sliceC)
}

sliceC[0] = "fo"
if basiccheck.SimilarSlice(sliceA, sliceC) {
t.Errorf("SimilarSlice found similar slice %v, %v", sliceA, sliceC)
}

sliceC = nil
if basiccheck.SimilarSlice(sliceA, sliceC) {
t.Errorf("SimilarSlice found similar slice %v, %v", sliceA, sliceC)
}
sliceA = nil
if !basiccheck.SimilarSlice(sliceA, sliceC) {
t.Errorf("SimilarSlice didn't find similar slice %v, %v", sliceA, sliceC)
}
}

func TestOneInSliceWith(t *testing.T) {
sliceOfString := []string{}

Expand Down
Loading