summaryrefslogtreecommitdiffstats
path: root/src/runtime/rand_test.go
blob: baecb6984d02500a78b07b7b9258c005f6977ea3 (plain)
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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime_test

import (
	. "runtime"
	"strconv"
	"testing"
	_ "unsafe" // for go:linkname
)

func TestReadRandom(t *testing.T) {
	if *ReadRandomFailed {
		switch GOOS {
		default:
			t.Fatalf("readRandom failed at startup")
		case "plan9":
			// ok
		}
	}
}

func BenchmarkFastrand(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Fastrand()
		}
	})
}

func BenchmarkFastrand64(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Fastrand64()
		}
	})
}

func BenchmarkFastrandHashiter(b *testing.B) {
	var m = make(map[int]int, 10)
	for i := 0; i < 10; i++ {
		m[i] = i
	}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for range m {
				break
			}
		}
	})
}

var sink32 uint32

func BenchmarkFastrandn(b *testing.B) {
	for n := uint32(2); n <= 5; n++ {
		b.Run(strconv.Itoa(int(n)), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				sink32 = Fastrandn(n)
			}
		})
	}
}

//go:linkname fastrand runtime.fastrand
func fastrand() uint32

//go:linkname fastrandn runtime.fastrandn
func fastrandn(uint32) uint32

//go:linkname fastrand64 runtime.fastrand64
func fastrand64() uint64

func TestLegacyFastrand(t *testing.T) {
	// Testing mainly that the calls work at all,
	// but check that all three don't return the same number (1 in 2^64 chance)
	{
		x, y, z := fastrand(), fastrand(), fastrand()
		if x == y && y == z {
			t.Fatalf("fastrand three times = %#x, %#x, %#x, want different numbers", x, y, z)
		}
	}
	{
		x, y, z := fastrandn(1e9), fastrandn(1e9), fastrandn(1e9)
		if x == y && y == z {
			t.Fatalf("fastrandn three times = %#x, %#x, %#x, want different numbers", x, y, z)
		}
	}
	{
		x, y, z := fastrand64(), fastrand64(), fastrand64()
		if x == y && y == z {
			t.Fatalf("fastrand64 three times = %#x, %#x, %#x, want different numbers", x, y, z)
		}
	}
}