Source file
src/runtime/rand_test.go
1
2
3
4
5 package runtime_test
6
7 import (
8 . "runtime"
9 "strconv"
10 "testing"
11 _ "unsafe"
12 )
13
14 func TestReadRandom(t *testing.T) {
15 if *ReadRandomFailed {
16 switch GOOS {
17 default:
18 t.Fatalf("readRandom failed at startup")
19 case "plan9":
20
21 }
22 }
23 }
24
25 func BenchmarkCheaprand(b *testing.B) {
26 for b.Loop() {
27 Cheaprand()
28 }
29 }
30
31 func BenchmarkCheaprand64(b *testing.B) {
32 for b.Loop() {
33 Cheaprand64()
34 }
35 }
36
37 func BenchmarkFastrand(b *testing.B) {
38 b.RunParallel(func(pb *testing.PB) {
39 for pb.Next() {
40 Fastrand()
41 }
42 })
43 }
44
45 func BenchmarkFastrand64(b *testing.B) {
46 b.RunParallel(func(pb *testing.PB) {
47 for pb.Next() {
48 Fastrand64()
49 }
50 })
51 }
52
53 func BenchmarkFastrandHashiter(b *testing.B) {
54 var m = make(map[int]int, 10)
55 for i := 0; i < 10; i++ {
56 m[i] = i
57 }
58 b.RunParallel(func(pb *testing.PB) {
59 for pb.Next() {
60 for range m {
61 break
62 }
63 }
64 })
65 }
66
67 var sink32 uint32
68
69 func BenchmarkFastrandn(b *testing.B) {
70 for n := uint32(2); n <= 5; n++ {
71 b.Run(strconv.Itoa(int(n)), func(b *testing.B) {
72 for i := 0; i < b.N; i++ {
73 sink32 = Fastrandn(n)
74 }
75 })
76 }
77 }
78
79
80 func fastrand() uint32
81
82
83 func fastrandn(uint32) uint32
84
85
86 func fastrand64() uint64
87
88 func TestLegacyFastrand(t *testing.T) {
89
90
91 {
92 x, y, z := fastrand(), fastrand(), fastrand()
93 if x == y && y == z {
94 t.Fatalf("fastrand three times = %#x, %#x, %#x, want different numbers", x, y, z)
95 }
96 }
97 {
98 x, y, z := fastrandn(1e9), fastrandn(1e9), fastrandn(1e9)
99 if x == y && y == z {
100 t.Fatalf("fastrandn three times = %#x, %#x, %#x, want different numbers", x, y, z)
101 }
102 }
103 {
104 x, y, z := fastrand64(), fastrand64(), fastrand64()
105 if x == y && y == z {
106 t.Fatalf("fastrand64 three times = %#x, %#x, %#x, want different numbers", x, y, z)
107 }
108 }
109 }
110
View as plain text