-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
54 lines (50 loc) · 1.01 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
package sort
import (
"fmt"
"math/rand"
"sort"
"testing"
)
func testSlice(size uint) sort.IntSlice {
rng := rand.New(rand.NewSource(0))
s := make(sort.IntSlice, size)
for idx := range s {
s[idx] = rng.Intn(int(size))
}
sort.Sort(s)
return s
}
func TestSearchOrdered(t *testing.T) {
s := testSlice(1000)
v := 100
idx0 := SearchOrdered(s, v)
idx1 := sort.Search(len(s), func(i int) bool {
return s[i] >= v
})
if idx0 != idx1 {
t.Fatalf("%d != %d", idx0, idx1)
}
}
func Benchmark(b *testing.B) {
for size := uint(1); size < 1024*1024; size *= 2 {
b.Run(fmt.Sprintf("size-%d", size), func(b *testing.B) {
s := testSlice(size)
b.Run("Search", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sort.Search(len(s), func(i int) bool {
return s[i] >= i%int(size)
})
}
})
b.Run("SearchOrdered", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
SearchOrdered(s, i%int(size))
}
})
})
}
}