1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 00:32:27 +08:00

Slice: find nothing case (#11)

The current behavior can result into wired edge cases.

**Current behavior**

if find was unsuccessfully then it will return the first element of the
slice

**Desired behavior**

if find was unsuccessfully then it should return negative ok and a none
value
This commit is contained in:
donutloop
2022-01-02 15:05:27 +01:00
committed by GitHub
parent b4a49fccfd
commit f15131f032
4 changed files with 27 additions and 6 deletions

View File

@@ -190,13 +190,28 @@ func TestFind(t *testing.T) {
even := func(i, num int) bool {
return num%2 == 0
}
res := Find(nums, even)
res, ok := Find(nums, even)
if !ok {
t.Fatal("found nothing")
}
if res != 2 {
internal.LogFailedTestInfo(t, "Find", nums, 2, res)
t.FailNow()
}
}
func TestFindFoundNothing(t *testing.T) {
nums := []int{1, 1, 1, 1, 1, 1}
findFunc := func(i, num int) bool {
return num > 1
}
_, ok := Find(nums, findFunc)
if ok {
t.Fatal("found something")
}
}
func TestFlattenDeep(t *testing.T) {
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
expected := []string{"a", "b", "c", "d"}