-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
103 lines (88 loc) · 1.83 KB
/
main_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
package nachtsImStall
import (
"log"
"testing"
)
// Most games should be settled after this number of rolls
const mostRolls = 64
func TestOneGameStdlibs(t *testing.T) {
won, rolls := Play(stdlibs)
log.Printf("stdlibs: Won: %v after %d rolls\n", won, rolls)
}
func TestOneGameDevUrandom(t *testing.T) {
f := devUrandom(mostRolls)
won, rolls := Play(f)
log.Printf("/dev/urandom: Won: %v after %d rolls\n", won, rolls)
}
// ration in %
func ratio(won, n int) int {
return 100 * won / n
}
func TestManyGamesStdlib(t *testing.T) {
n := 10000
total := 0
for i := 0; i < n; i++ {
won, _ := Play(stdlibs)
if won {
total++
}
}
log.Printf("Win rate(stdlibs): %d%%\n", ratio(total, n))
}
func TestManyGamesDevUrandom(t *testing.T) {
n := 1000
total := 0
for i := 0; i < n; i++ {
won, _ := Play(devUrandom(mostRolls))
if won {
total++
}
}
log.Printf("Win rate(/dev/urandom): %d%%\n", ratio(total, n))
}
func BenchmarkGamesStdlib(b *testing.B) {
for n := 0; n < b.N; n++ {
Play(stdlibs)
}
}
func BenchmarkGamesDevUrandom(b *testing.B) {
f := devUrandom(b.N * mostRolls)
for n := 0; n < b.N; n++ {
Play(f)
}
}
func TestDevUrandom(t *testing.T) {
readDevUrandom(1)
}
func BenchmarkReadManySmallReads(b *testing.B) {
for n := 0; n < b.N; n++ {
for i := 0; i < 1e6; i++ {
readDevUrandom(6)
}
}
}
func BenchmarkReadOneLarge(b *testing.B) {
for n := 0; n < b.N; n++ {
readDevUrandom(6e6)
}
}
func TestRndStdlibs(t *testing.T) {
rndTest(t, 10000, stdlibs)
}
func TestRndDevUrandom(t *testing.T) {
f := devUrandom(10000 * mostRolls)
rndTest(t, 10000, f)
}
func rndTest(t *testing.T, n int, f func() int) {
is := make([]int, 6)
for i := 0; i < n; i++ {
r := f()
if r < 0 || r > 5 {
log.Fatalf("Expected [0..6) but got %d\n", r)
}
is[r]++
}
for i, v := range is {
log.Printf("%d: %d\n", i, v)
}
}