-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search_test.go
62 lines (60 loc) · 1.31 KB
/
binary-search_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
package algorithms
import "testing"
func TestBinarySearch(t *testing.T) {
type args struct {
arr []int
val int
}
tests := []struct {
name string
args args
want int
}{
{
name: "last element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 1000},
want: 7,
},
{
name: "non-existing element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 2},
want: -1,
},
{
name: "non-existing negative element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: -344},
want: -1,
},
{
name: "non-existing large value",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 30000},
want: -1,
},
{
name: "first element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 43},
want: 0,
},
{
name: "middle element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 100},
want: 3,
},
{
name: "pre-last element",
args: args{arr: []int{43, 56, 93, 100, 245, 673, 933, 1000}, val: 933},
want: 6,
},
{
name: "empty array",
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := BinarySearch(tt.args.arr, tt.args.val); got != tt.want {
t.Errorf("BinarySearch() = %v, want %v", got, tt.want)
}
})
}
}