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 authored and GitHub committed 2022-01-02 22:05:27 +08:00
1 parent b4a49fccfd
commit f15131f032
4 files changed
+27 -6

No files matched your search

+16 -1
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"}