forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_test.go
61 lines (48 loc) · 1.36 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
package stl4go
import "testing"
func TestLowerBound(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
expectEq(t, LowerBound(a, 1), 0)
expectEq(t, LowerBound(a, 5), 3)
expectEq(t, LowerBound(a, 7), len(a))
}
func TestLowerBoundFunc(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
expectEq(t, LowerBoundFunc(a, 1, Less[int]), 0)
expectEq(t, LowerBoundFunc(a, 5, Less[int]), 3)
expectEq(t, LowerBoundFunc(a, 7, Less[int]), len(a))
}
func TestUpperBound(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
expectEq(t, UpperBound(a, 1), 1)
expectEq(t, UpperBound(a, 5), 5)
expectEq(t, UpperBound(a, 7), len(a))
}
func TestUpperBoundFunc(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
expectEq(t, UpperBoundFunc(a, 1, Less[int]), 1)
expectEq(t, UpperBoundFunc(a, 5, Less[int]), 5)
expectEq(t, UpperBoundFunc(a, 7, Less[int]), len(a))
}
func TestBinarySearch(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
i, ok := BinarySearch(a, 4)
expectTrue(t, ok)
expectEq(t, i, 2)
i, ok = BinarySearch(a, 5)
expectTrue(t, ok)
expectEq(t, i, 3)
i, ok = BinarySearch(a, 3)
expectFalse(t, ok)
}
func TestBinarySearchFunc(t *testing.T) {
a := []int{1, 2, 4, 5, 5, 6}
i, ok := BinarySearchFunc(a, 4, Less[int])
expectTrue(t, ok)
expectEq(t, i, 2)
i, ok = BinarySearchFunc(a, 5, Less[int])
expectTrue(t, ok)
expectEq(t, i, 3)
i, ok = BinarySearchFunc(a, 3, Less[int])
expectFalse(t, ok)
}