-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinarySearch_test.go
47 lines (38 loc) · 1.11 KB
/
binarySearch_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
package binarySearch
import "testing"
// The recursive version of binary search.
func TestSearchRecursive(t *testing.T) {
cases := []struct {
in []int
search, want int
}{
{[]int{1, 2, 46, 74, 89, 105}, 89, 4},
{[]int{5, 6, 78, 343, 568, 999}, 1000, -1},
{[]int{0, 0, 23, 34, 450, 550, 555, 679, 843}, 843, 8},
{[]int{10, 16, 80, 143, 268, 599, 768}, 10, 0},
}
for _, c := range cases {
got := SearchRecursive(c.in, c.search, 0, len(c.in)-1)
if got != c.want {
t.Errorf("Search for (%v) in %v\n Result = %v,\n Wants = %v", c.search, c.in, got, c.want)
}
}
}
// The iterative version of binary search.
func TestSearchIterative(t *testing.T) {
cases := []struct {
in []int
search, want int
}{
{[]int{1, 2, 46, 74, 89, 105}, 89, 4},
{[]int{5, 6, 78, 343, 568, 999}, 1000, -1},
{[]int{0, 0, 23, 34, 450, 550, 555, 679, 843}, 843, 8},
{[]int{10, 16, 80, 143, 268, 599, 768}, 10, 0},
}
for _, c := range cases {
got := SearchIterative(c.in, c.search)
if got != c.want {
t.Errorf("Search for (%v) in %v\n Result = %v,\n Wants %v", c.search, c.in, got, c.want)
}
}
}