-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfastrand_test.go
More file actions
90 lines (82 loc) · 1.55 KB
/
fastrand_test.go
File metadata and controls
90 lines (82 loc) · 1.55 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package fastrand
import (
"testing"
)
func TestRNGSeed(t *testing.T) {
var r RNG
for _, seed := range []uint32{1, 2, 1234, 432432, 34324432} {
r.Seed(seed)
m := make(map[uint32]struct{})
for i := 0; i < 1e6; i++ {
n := r.Uint32()
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
}
m[n] = struct{}{}
}
}
}
func TestUint32(t *testing.T) {
m := make(map[uint32]struct{})
for i := 0; i < 1e6; i++ {
n := Uint32()
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
}
m[n] = struct{}{}
}
}
func TestRNGUint32(t *testing.T) {
var r RNG
m := make(map[uint32]struct{})
for i := 0; i < 1e6; i++ {
n := r.Uint32()
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
}
m[n] = struct{}{}
}
}
func TestUint32n(t *testing.T) {
m := make(map[uint32]int)
for i := 0; i < 1e6; i++ {
n := Uint32n(1e2)
if n >= 1e2 {
t.Fatalf("n > 1000: %v", n)
}
m[n]++
}
// check distribution
avg := 1e6 / 1e2
for k, v := range m {
p := (float64(v) - float64(avg)) / float64(avg)
if p < 0 {
p = -p
}
if p > 0.05 {
t.Fatalf("skew more than 5%% for k=%v: %v", k, p*100)
}
}
}
func TestRNGUint32n(t *testing.T) {
var r RNG
m := make(map[uint32]int)
for i := 0; i < 1e6; i++ {
n := r.Uint32n(1e2)
if n >= 1e2 {
t.Fatalf("n > 1000: %v", n)
}
m[n]++
}
// check distribution
avg := 1e6 / 1e2
for k, v := range m {
p := (float64(v) - float64(avg)) / float64(avg)
if p < 0 {
p = -p
}
if p > 0.05 {
t.Fatalf("skew more than 5%% for k=%v: %v", k, p*100)
}
}
}