-
Notifications
You must be signed in to change notification settings - Fork 8
/
global_test.go
128 lines (113 loc) · 2.58 KB
/
global_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2022 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rand_test
import (
"math"
"math/bits"
"pgregory.net/rand"
"pgregory.net/rapid"
"sync/atomic"
"testing"
)
const (
wyrandAdd = 0xa0761d6478bd642f
wyrandXor = 0xe7037ed1a0b428db
)
func wyrand64(state *uint64) uint64 {
s := *state + wyrandAdd
*state = s
hi, lo := bits.Mul64(s, s^wyrandXor)
return hi ^ lo
}
func wyrand64Atomic(state *uint64) uint64 {
s := atomic.AddUint64(state, wyrandAdd)
hi, lo := bits.Mul64(s, s^wyrandXor)
return hi ^ lo
}
func BenchmarkRand64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s = rand.Uint64()
}
sinkUint64 = s
}
func BenchmarkWyRand64(b *testing.B) {
var s uint64
var state uint64
for i := 0; i < b.N; i++ {
s = wyrand64(&state)
}
sinkUint64 = s
}
func BenchmarkWyRand64Atomic(b *testing.B) {
var s uint64
var state uint64
for i := 0; i < b.N; i++ {
s = wyrand64Atomic(&state)
}
sinkUint64 = s
}
func TestFloat32(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
f := rand.Float32()
if f < 0 || f >= 1 {
t.Fatalf("got %v outside of [0, 1)", f)
}
})
}
func TestFloat64(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
f := rand.Float64()
if f < 0 || f >= 1 {
t.Fatalf("got %v outside of [0, 1)", f)
}
})
}
func TestInt31n(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
n := rapid.Int32Range(1, math.MaxInt32).Draw(t, "n").(int32)
v := rand.Int31n(n)
if v < 0 || v >= n {
t.Fatalf("got %v outside of [0, %v)", v, n)
}
})
}
func TestInt63n(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
n := rapid.Int64Range(1, math.MaxInt64).Draw(t, "n").(int64)
v := rand.Int63n(n)
if v < 0 || v >= n {
t.Fatalf("got %v outside of [0, %v)", v, n)
}
})
}
func TestIntn(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
n := rapid.IntRange(1, math.MaxInt).Draw(t, "n").(int)
v := rand.Intn(n)
if v < 0 || v >= n {
t.Fatalf("got %v outside of [0, %v)", v, n)
}
})
}
func TestUint32n(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
n := rapid.Uint32Range(1, math.MaxUint32).Draw(t, "n").(uint32)
v := rand.Uint32n(n)
if v >= n {
t.Fatalf("got %v outside of [0, %v)", v, n)
}
})
}
func TestUint64n(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
n := rapid.Uint64Range(1, math.MaxUint64).Draw(t, "n").(uint64)
v := rand.Uint64n(n)
if v >= n {
t.Fatalf("got %v outside of [0, %v)", v, n)
}
})
}