-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
57 lines (54 loc) · 897 Bytes
/
main_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
// Slices: How to check if a slice contains a specific element
package main
import "testing"
func SliceContains(s []int, x int) bool {
if len(s) == 0 {
return false
}
for k := range s {
if s[k] == x {
return true
}
}
return false
}
func TestSliceContains(t *testing.T) {
tests := []struct {
name string
s []int
x int
want bool
}{
{
name: "nil",
s: nil,
x: 1,
want: false,
},
{
name: "empty",
s: []int{},
x: 1,
want: false,
},
{
name: "value exists",
s: []int{2, 1},
x: 1,
want: true,
},
{
name: "value does not exist",
s: []int{2, 1},
x: 3,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SliceContains(tt.s, tt.x); got != tt.want {
t.Errorf("SliceContains(%v, %v) = %v, want %v", tt.s, tt.x, got, tt.want)
}
})
}
}