-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_test.go
More file actions
57 lines (45 loc) · 1.21 KB
/
slice_test.go
File metadata and controls
57 lines (45 loc) · 1.21 KB
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
package benchmarkfun_test
import (
"reflect"
"testing"
"github.com/willemschots/benchmarkfun"
)
func Test_SliceSet_Intersection(t *testing.T) {
for name, tc := range tests() {
t.Run(name, func(t *testing.T) {
s1 := benchmarkfun.NewSliceSet(tc.s1...)
s2 := benchmarkfun.NewSliceSet(tc.s2...)
want := benchmarkfun.NewSliceSet(tc.want...)
got := s1.Intersection(s2)
if !reflect.DeepEqual(got, want) {
t.Fatalf("want\n%+v\ngot\n%+v\n", want, got)
}
})
}
}
var sliceSetGlobal int
func Benchmark_SliceSet(b *testing.B) {
for _, tc := range benchmarks() {
b.Run(tc.name, func(b *testing.B) {
// set up the sets
s1 := benchmarkfun.NewSliceSet(tc.s1...)
s2 := benchmarkfun.NewSliceSet(tc.s2...)
// set up is not part of the benchmark.
b.ResetTimer()
// we now need to be careful the compiler
// doesn't optimize away our benchmark.
//
// To do this, we need to assign our results
// to a global variable. This is slow however,
// so we first assign to a local variable.
// local variable
var result benchmarkfun.SliceSet
// the benchmark
for range b.N {
result = s1.Intersection(s2)
}
// global variable
sliceSetGlobal = len(result)
})
}
}