-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_test.go
More file actions
169 lines (123 loc) · 4.13 KB
/
binary_search_test.go
File metadata and controls
169 lines (123 loc) · 4.13 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package binarySearch
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestBiSearch(t *testing.T) {
t.Run("it finds the indicies of the target", func(t *testing.T) {
nums := []int {1, 2, 3, 4, 5, 7}
target := 7
result := BiSearch(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 5, result)
})
t.Run("it finds the indicies of a negative value target", func(t *testing.T) {
nums := []int {-1, 0, 1, 2, 3, 4, 5, 7, 33}
target := -1
result := BiSearch(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 0, result)
})
t.Run("it returns -1 if the target isn't in the sorted slice", func(t *testing.T) {
nums := []int {1, 2, 3, 4, 5, 7}
target := 6
result := BiSearch(nums, target)
assert.NotNil(t, result)
assert.Equal(t, -1 , result)
})
}
func TestFindPosition(t *testing.T) {
t.Run("it returns the position where the value belongs in the sorted slice", func(t *testing.T) {
nums := []int {-10, -4, 0, 3, 7, 9, 12, 42}
target := 5
result := FindPosition(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 4 , result)
})
t.Run("it returns the position where the value belongs in the sorted slice when it belongs at the end",
func(t *testing.T) {
nums := []int {-10, -4, 0, 3, 7, 9, 12, 42}
target := 55
result := FindPosition(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 8 , result)
})
t.Run("it returns the position where the value belongs in the sorted slice when slice is only 1 int long",
func(t *testing.T) {
nums := []int { 3}
target := 5
result := FindPosition(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 1 , result)
})
t.Run("it returns the position where the value belongs in the sorted slice when target value is negative",
func(t *testing.T) {
nums := []int { -10, -4, 0, 3, 7, 9, 12, 42}
target := -15
result := FindPosition(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 0 , result)
})
t.Run("it returns the position where the value belongs in the sorted slice when the slice is empty",
func(t *testing.T) {
var nums []int
target := -15
result := FindPosition(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 0 , result)
})
}
func TestVerifyBinary(t *testing.T) {
t.Run("it returns true when slice is binary sorted", func(t *testing.T) {
nums := []int {0, 1,2, 3}
result := VerifyBinary(nums)
assert.NotNil(t, result)
assert.True(t, result)
})
t.Run("it returns false when slice is not binary sorted", func(t *testing.T) {
nums := []int {0, 12, 3, 7}
result := VerifyBinary(nums)
assert.NotNil(t, result)
assert.False(t, result)
})
t.Run("it returns true when slice is empty (effectively sorted)", func(t *testing.T) {
var nums []int
result := VerifyBinary(nums)
assert.NotNil(t, result)
assert.True(t, result)
})
}
func TestReturnNewBinarySort(t *testing.T) {
t.Run("correctly inserts target value into slice that is returned sorted", func(t *testing.T) {
nums := []int {0,1,2,4,5}
target := 3
result := ReturnNewBinarySort(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 6 ,len(result))
assert.ElementsMatch(t, []int{0,1,2,3,4,5}, result)
})
t.Run("correctly inserts negative target value into slice that is returned sorted", func(t *testing.T) {
nums := []int {-1, 0,1,2,4,5}
target := -3
result := ReturnNewBinarySort(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 7 ,len(result))
assert.ElementsMatch(t, []int{-3, -1,0,1,2,4,5}, result)
})
t.Run("it does not error when inserting a value into an empty slice", func(t *testing.T) {
var nums []int
target := 42
result := ReturnNewBinarySort(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 1 ,len(result))
assert.ElementsMatch(t, []int{42}, result)
})
t.Run("it does correctly inserts a value when the value already exists", func(t *testing.T) {
nums := []int {0,1,2,3,4,5}
target := 3
result := ReturnNewBinarySort(nums, target)
assert.NotNil(t, result)
assert.Equal(t, 7 ,len(result))
assert.ElementsMatch(t, []int{0,1,2,3,3,4,5}, result)
})
}