-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrand_test.go
87 lines (65 loc) · 1.33 KB
/
rand_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
package rand
import (
crand "crypto/rand"
mrand "math/rand"
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type RandSuite struct{}
var _ = Suite(&RandSuite{})
// Random numbers are generated as 64bit values. Check corner cases
func (r *RandSuite) TestRdRand(c *C) {
var rnd uint64
if IsRand64Supported() {
RdRand64(&rnd)
b := make([]byte, 5)
Read(b)
b = make([]byte, 8)
Read(b)
b = make([]byte, 1000)
Read(b)
}
if IsSeed64Supported() {
// RDSEED requires Broadwell or newer
RdSeed64(&rnd)
}
}
// math random routines are not cryptographic strength but fast
func (r *RandSuite) BenchmarkMathRand8(c *C) {
rnd := mrand.New(mrand.NewSource(0))
for i := 0; i < c.N; i++ {
rnd.Uint64()
}
}
func (r *RandSuite) BenchmarkRdRand8(c *C) {
if !IsRand64Supported() {
return
}
b := make([]byte, 8)
for i := 0; i < c.N; i++ {
Read(b)
}
}
func (r *RandSuite) BenchmarkRdRand1000(c *C) {
if !IsRand64Supported() {
return
}
b := make([]byte, 1000)
for i := 0; i < c.N; i++ {
Read(b)
}
}
func (r *RandSuite) BenchmarkCryptoRand8(c *C) {
b := make([]byte, 8)
for i := 0; i < c.N; i++ {
crand.Read(b)
}
}
func (r *RandSuite) BenchmarkCryptoRand1000(c *C) {
b := make([]byte, 1000)
for i := 0; i < c.N; i++ {
crand.Read(b)
}
}