-
Notifications
You must be signed in to change notification settings - Fork 0
/
findlastindex_test.go
51 lines (46 loc) · 1.16 KB
/
findlastindex_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package slices_test
import (
"testing"
"github.com/dosadczuk/go-slices"
"github.com/google/go-cmp/cmp"
)
func TestFindLastIndex(t *testing.T) {
tt := map[string]struct {
// input
values []int
predicate func(int) bool
// assert
wantIndex int
wantFound bool
}{
"empty slice": {
values: nil, // zero value
predicate: func(_ int) bool { return true },
wantIndex: 0, // zero value
wantFound: false, // zero value
},
"slice with value matching predicate": {
values: []int{1, 2, 3, 4, 5},
predicate: func(val int) bool { return val%2 == 0 },
wantIndex: 3,
wantFound: true,
},
"slice with value not matching predicate": {
values: []int{1, 2, 3, 4, 5},
predicate: func(val int) bool { return val < 0 },
wantIndex: 0, // zero value
wantFound: false, // zero value
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
haveIndex, haveFound := slices.FindLastIndex(tc.values, tc.predicate)
if !cmp.Equal(tc.wantIndex, haveIndex) {
t.Error(cmp.Diff(tc.wantIndex, haveIndex))
}
if !cmp.Equal(tc.wantFound, haveFound) {
t.Error(cmp.Diff(tc.wantFound, haveFound))
}
})
}
}