-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
90 lines (77 loc) · 1.81 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package algo
import (
"fmt"
"testing"
)
func TestLinearSearchInt(t *testing.T) {
testCases := []struct {
input []int
search int
exp int
}{
{[]int{3, 17, 75, 80, 202}, 22, -1},
{[]int{3, 24, 33, 64, 79, 300, 455}, 64, 3},
}
for _, tc := range testCases {
input := fmt.Sprintf("%v", tc.input)
t.Run(input, func(t *testing.T) {
if got := linearSearch(tc.input, tc.search); got != tc.exp {
t.Errorf("Exp %d, got %d", tc.exp, got)
}
})
}
}
func TestLinearSearchFloat(t *testing.T) {
testCases := []struct {
input []float64
search float64
exp int
}{
{[]float64{3, 5.6, 78, 99.6, 300.789, 563.4}, 563.4, 5},
{[]float64{3, 5.6, 78, 99.6, 300.789, 563.4}, 99, -1},
}
for _, tc := range testCases {
input := fmt.Sprintf("%v", tc.input)
t.Run(input, func(t *testing.T) {
if got := linearSearch(tc.input, tc.search); got != tc.exp {
t.Errorf("Exp %d, got %d", tc.exp, got)
}
})
}
}
func TestBinarySearchInt(t *testing.T) {
testCases := []struct {
input []int
search int
exp int
}{
{[]int{3, 17, 75, 80, 202}, 22, -1},
{[]int{3, 24, 33, 64, 79, 300, 455}, 64, 3},
}
for _, tc := range testCases {
input := fmt.Sprintf("%v", tc.input)
t.Run(input, func(t *testing.T) {
if got := binarySearch(tc.input, tc.search); got != tc.exp {
t.Errorf("Exp %d, got %d", tc.exp, got)
}
})
}
}
func TestBinarySearchFloat(t *testing.T) {
testCases := []struct {
input []float64
search float64
exp int
}{
{[]float64{3, 5.6, 78, 99.6, 300.789, 563.4}, 563.4, 5},
{[]float64{3, 5.6, 78, 99.6, 300.789, 563.4}, 99, -1},
}
for _, tc := range testCases {
input := fmt.Sprintf("%v", tc.input)
t.Run(input, func(t *testing.T) {
if got := binarySearch(tc.input, tc.search); got != tc.exp {
t.Errorf("Exp %d, got %d", tc.exp, got)
}
})
}
}