-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathany_test.go
More file actions
73 lines (66 loc) · 1.4 KB
/
Copy pathany_test.go
File metadata and controls
73 lines (66 loc) · 1.4 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package and
import (
"math/rand/v2"
"reflect"
"runtime"
"testing"
)
func anyNaive(a []byte) bool {
for _, v := range a {
if v != 0 {
return true
}
}
return false
}
func testAny(t *testing.T, fancy, generic func(a []byte) bool, size int) {
a := make([]byte, size)
idx := rand.IntN(size)
a[idx] = uint8(rand.Int() & 2)
r1 := fancy(a)
r2 := generic(a)
if r1 != r2 {
t.Fatalf("%s produced a different result from %s at length %d:\n%t\n%t", runtime.FuncForPC(reflect.ValueOf(fancy).Pointer()).Name(), runtime.FuncForPC(reflect.ValueOf(generic).Pointer()).Name(), size, r1, r2)
}
}
func TestAny(t *testing.T) {
for i := 0; i < 20; i++ {
size := 1 << i
testAny(t, Any, anyNaive, size)
testAny(t, anyGeneric, anyNaive, size)
for j := 0; j < 10; j++ {
testAny(t, Any, anyNaive, size+rand.IntN(100))
testAny(t, anyGeneric, anyNaive, size+rand.IntN(100))
}
}
}
func BenchmarkAny(b *testing.B) {
b.StopTimer()
size := 32000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
Any(a)
}
}
func BenchmarkAnyGeneric(b *testing.B) {
b.StopTimer()
size := 32000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
anyGeneric(a)
}
}
func BenchmarkAnyNaive(b *testing.B) {
b.StopTimer()
size := 32000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
anyNaive(a)
}
}