-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll_test.go
94 lines (82 loc) · 2.13 KB
/
roll_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
package main
import "testing"
// This should be set to any number that is at least two times
// the total number of values that can be generated by the roll.
const executions int = 1000
func testResults(spec RollSpec, t *testing.T) {
totalDiceRolled := spec.DieCount
if spec.BestOf != 0 {
totalDiceRolled = spec.BestOf
}
minimumResult := int(totalDiceRolled + spec.Modifier)
maximumResult := int(totalDiceRolled * (spec.Sides + spec.Modifier))
for i := 0; i < executions; i++ {
result := DoRolls(spec)
var e int64
for e = 0; e < spec.Times; e++ {
total := result.Rolls[e].Total
if total < minimumResult {
t.Fatalf("Roll %d was below minimum of %d", total, minimumResult)
}
if total > maximumResult {
t.Fatalf("Roll %d was greater than maximum result of %d", total, maximumResult)
}
}
}
}
func TestD10(t *testing.T) {
rollSpec, err := Parse("d10")
if err != nil {
t.Fatalf("Error parsing 'd10': %v", err)
}
testResults(*rollSpec, t)
}
func TestD10Plus1(t *testing.T) {
rollSpec, err := Parse("d10+1")
if err != nil {
t.Fatalf("Error parsing 'd10+1': %v", err)
}
testResults(*rollSpec, t)
}
func Test1D10Plus2(t *testing.T) {
rollSpec, err := Parse("1d10+2")
if err != nil {
t.Fatalf("Error parsing '1d10+2': %v", err)
}
testResults(*rollSpec, t)
}
func TestD6Times6(t *testing.T) {
rollSpec, err := Parse("3d6x6")
if err != nil {
t.Fatalf("Error parsing '3d6x6': %v", err)
}
testResults(*rollSpec, t)
}
func TestBest3of4D6Times6(t *testing.T) {
rollSpec, err := Parse("3,4d6x6")
if err != nil {
t.Fatalf("Error parsing '3,4d6x6': %v", err)
}
testResults(*rollSpec, t)
}
func TestBest3of4D6(t *testing.T) {
rollSpec, err := Parse("3,4d6")
if err != nil {
t.Fatalf("Error parsing '3,4d6': %v", err)
}
testResults(*rollSpec, t)
}
func TestBest2of3D12Plus6(t *testing.T) {
rollSpec, err := Parse("2,3d12+6")
if err != nil {
t.Fatalf("Error parsing '2,3d12+6': %v", err)
}
testResults(*rollSpec, t)
}
func TestBest2of3D100Plus5TenTimes(t *testing.T) {
rollSpec, err := Parse("2,3d100+5x10")
if err != nil {
t.Fatalf("Error parsing '2,3d100+5x10': %v", err)
}
testResults(*rollSpec, t)
}