-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_search_test.go
105 lines (98 loc) · 2.96 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package binarysearch
import (
"testing"
)
func TestBinarySearch(t *testing.T) {
tests := []struct {
name string
intArray []int
valueToFind int
expectedIndex int
}{
{
name: "Value to find exists at start of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 2,
expectedIndex: 0,
},
{
name: "Value to find exists near start of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 3,
expectedIndex: 1,
},
{
name: "Value to find exists at end of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 67,
expectedIndex: 18,
},
{
name: "Value to find exists mear end of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 59,
expectedIndex: 16,
},
{
name: "Value to find does not exist in array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 100,
expectedIndex: -1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
received := binarySearch(test.intArray, test.valueToFind)
if received != test.expectedIndex {
t.Errorf("Expected index %d, received %d instead.", test.expectedIndex, received)
}
})
}
}
func TestBinarySearchRecursive(t *testing.T) {
tests := []struct {
name string
intArray []int
valueToFind int
expectedIndex int
}{
{
name: "Value to find exists at start of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 2,
expectedIndex: 0,
},
{
name: "Value to find exists near start of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 3,
expectedIndex: 1,
},
{
name: "Value to find exists at end of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 67,
expectedIndex: 18,
},
{
name: "Value to find exists mear end of array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 59,
expectedIndex: 16,
},
{
name: "Value to find does not exist in array",
intArray: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67},
valueToFind: 100,
expectedIndex: -1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
received := binarySearchRecursive(test.intArray, test.valueToFind, 0, len(test.intArray)-1)
if received != test.expectedIndex {
t.Errorf("Expected index %d, received %d instead.", test.expectedIndex, received)
}
})
}
}