/
search_test.go
65 lines (53 loc) · 1.38 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
62
63
64
65
package binarysearch_test
import (
"github.com/droxer/binarysearch"
"github.com/droxer/quicksort"
"math/rand"
"testing"
"time"
)
var (
sortedArray10000 []int
sortedArray100000 []int
sortedArray1000000 []int
)
func init() {
seed := time.Now().Unix()
rand.Seed(seed)
}
func sortedRandomArray(n int) []int {
sliced := rand.Perm(n * 20)[n : n*2-1]
quicksort.Sort(sliced)
return sliced
}
func init() {
sortedArray10000 = sortedRandomArray(10000)
sortedArray100000 = sortedRandomArray(100000)
sortedArray1000000 = sortedRandomArray(1000000)
}
func TestBSearch10000(t *testing.T) {
testBSearch(sortedArray10000, t)
}
func testBSearch(sorted []int, t *testing.T) {
expected := rand.Intn(len(sorted) - 1)
target := sorted[expected]
found := binarysearch.Search(sorted, target)
if found != expected {
t.Fatalf("expected %d, actual is %d", expected, found)
}
}
func BenchmarkBSearch10000(b *testing.B) {
benchmarkBSearch(sortedArray10000, b)
}
func BenchmarkBSearch100000(b *testing.B) {
benchmarkBSearch(sortedArray100000, b)
}
func BenchmarkBSearch1000000(b *testing.B) {
benchmarkBSearch(sortedArray1000000, b)
}
func benchmarkBSearch(sorted []int, b *testing.B) {
for j := 0; j < b.N; j++ {
target := sorted[rand.Intn(len(sorted)-1)]
binarysearch.Search(sorted, target)
}
}