forked from juliangruber/go-intersect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersect_test.go
92 lines (75 loc) · 2.01 KB
/
intersect_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
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
package intersect
import (
"fmt"
"math"
"math/rand"
"testing"
"github.com/bmizerany/assert"
)
func TestSimpleGeneric(t *testing.T) {
s := SimpleGeneric([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []int{})
s = SimpleGeneric([]int{1, 2}, []int{2})
assert.Equal(t, s, []int{2})
}
func TestSortedGeneric(t *testing.T) {
s := SortedGeneric([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []int{})
s = SortedGeneric([]int{1, 2}, []int{2})
assert.Equal(t, s, []int{2})
}
func TestHashGeneric(t *testing.T) {
s := HashGeneric([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []int{})
s = HashGeneric([]int{1, 2}, []int{2})
assert.Equal(t, s, []int{2})
}
func TestSimple(t *testing.T) {
s := Simple([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []interface{}{})
s = Simple([]int{1, 2}, []int{2})
assert.Equal(t, s, []interface{}{2})
}
func TestSorted(t *testing.T) {
s := Sorted([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []interface{}{})
s = Sorted([]int{1, 2}, []int{2})
assert.Equal(t, s, []interface{}{2})
}
func TestHash(t *testing.T) {
s := Hash([]int{1}, []int{2})
assert.Equal(t, len(s), 0)
assert.Equal(t, s, []interface{}{})
s = Hash([]int{1, 2}, []int{2})
assert.Equal(t, s, []interface{}{2})
}
var blackholeHashGeneric []int
var blackholeHash []interface{}
func BenchmarkHash(b *testing.B) {
for _, v := range []int{1, 10, 100, 1_000, 10_000} {
aSlice := createRandomSlice(v)
bSlice := createRandomSlice(v)
b.Run(fmt.Sprintf("Size %d- interface", v), func(b *testing.B) {
for i := 0; i < b.N; i++ {
blackholeHash = Hash(aSlice, bSlice)
}
})
b.Run(fmt.Sprintf("Size %d- generics", v), func(b *testing.B) {
for i := 0; i < b.N; i++ {
blackholeHashGeneric = HashGeneric(aSlice, bSlice)
}
})
}
}
func createRandomSlice(size int) []int {
slice := make([]int, size)
for i := 0; i < size; i++ {
slice[i] = rand.Intn(int(math.Pow(float64(size), 2)))
}
return slice
}