-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
82 lines (75 loc) · 1.4 KB
/
slice_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package search
import "testing"
type testStruct struct {
data int
}
func (t testStruct) CompareTo(other testStruct) int {
return other.data - t.data
}
func TestComparableSlice(t *testing.T) {
tests := []struct {
input []testStruct
element testStruct
expected int
}{
{
[]testStruct{
{1},
{2},
{3},
{4},
{5},
{6},
},
testStruct{3},
2,
},
{
[]testStruct{
{10},
{20},
{30},
{40},
{50},
{60},
},
testStruct{30},
2,
},
{
[]testStruct{
{10},
{20},
{30},
{40},
{50},
{60},
},
testStruct{70},
-1,
},
}
for _, test := range tests {
if actual := ComparableSlice(test.input, test.element); actual != test.expected {
t.Errorf("expected index %d, but got %d, for slice %v, with element %d", test.expected, actual, test.input, test.element)
}
}
}
func TestSlice(t *testing.T) {
tests := []struct {
input []int
element int
expected int
}{
{[]int{1, 2, 3, 4, 5, 6, 7, 8}, 4, 3},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, 9, 8},
{[]int{1, 2, 3, 4, 5, 6, 7, 8}, 9, -1},
{[]int{-10, -5, 0, 10, 30, 55}, -5, 1},
{[]int{-10, -5, 0, 10, 30, 55}, 0, 2},
}
for _, test := range tests {
if actual := Slice(test.input, test.element); actual != test.expected {
t.Errorf("expected index %d, but got %d, for slice %v, with element %d", test.expected, actual, test.input, test.element)
}
}
}