-
Notifications
You must be signed in to change notification settings - Fork 21
/
search_test.go
61 lines (56 loc) · 1.12 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
// Copyright 2020 The golang.design Initiative authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
package lockfree_test
import (
"testing"
"golang.design/x/lockfree"
)
func TestBinarySearch(t *testing.T) {
tests := []struct {
input []interface{}
x interface{}
less lockfree.Less
want int
}{
{
input: []interface{}{1, 2, 3, 4, 5, 6, 7},
x: 6,
less: func(a, b interface{}) bool {
if a.(int) < b.(int) {
return true
}
return false
},
want: 5,
},
{
input: []interface{}{1, 2, 3, 4, 5, 6, 7},
x: 2,
less: func(a, b interface{}) bool {
if a.(int) < b.(int) {
return true
}
return false
},
want: 1,
},
{
input: []interface{}{},
x: 2,
less: func(a, b interface{}) bool {
if a.(int) < b.(int) {
return true
}
return false
},
want: -1,
},
}
for _, tt := range tests {
r := lockfree.BinarySearch(tt.input, tt.x, tt.less)
if r != tt.want {
t.Fatalf("BinarySearch %v of %v: want %v, got %v", tt.x, tt.input, tt.want, r)
}
}
}