-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
85 lines (77 loc) · 2.05 KB
/
Copy pathmain_test.go
File metadata and controls
85 lines (77 loc) · 2.05 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
package main
import (
"reflect"
"strings"
"testing"
)
// loadedCoin is a fake coin
type loadedCoin struct {
// Outcome is the list of flips that will be returned.
// Flipping more times than length of Outcome slice will panic.
Outcome []bool
}
func (l *loadedCoin) Toss() bool {
out, remainder := l.Outcome[0], l.Outcome[1:]
l.Outcome = remainder
return out
}
// loadedDice is a fake set of dice
type loadedDice struct {
// Outcome is the list of rolls that will be returned.
// Rolling more times than length of Outcome slice will panic.
Outcome []int
}
func (l *loadedDice) Roll() int {
out, remainder := l.Outcome[0], l.Outcome[1:]
l.Outcome = remainder
return out
}
var testCases = []struct {
coin []bool
dice []int
numTurns int
want []string
}{
{[]bool{true}, []int{2}, 1, []string{
"Heads, rolled 2",
"Black Rook at h3",
"Rook escapes, Black wins",
}},
{[]bool{false}, []int{5}, 1, []string{
"Tails, rolled 5",
"Black Rook at e1",
"Bishop can take rook, White wins",
}},
{[]bool{false}, []int{8}, 1, []string{
"Tails, rolled 8",
"Black Rook at h1",
"Rook escapes, Black wins",
}},
{[]bool{false, true}, []int{3, 2}, 2, []string{
"Tails, rolled 3",
"Black Rook at c1",
"Heads, rolled 2",
"Black Rook at c3",
"Rook takes bishop, Black wins",
}},
}
func TestEvaluateProblemDeterministic(t *testing.T) {
for _, tc := range testCases {
got, err := evaluateProblem(&loadedCoin{Outcome: tc.coin}, &loadedDice{Outcome: tc.dice}, tc.numTurns)
if err != nil {
t.Errorf("evaluateProblem(%v,%v,1) returned err: %v", tc.coin, tc.dice, err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("evaluateProblem(%v,%v,1) = %v, wanted %v", tc.coin, tc.dice, got, tc.want)
}
}
}
func TestEvaluateProblemCompletes(t *testing.T) {
got, err := evaluateProblem(&realCoin{}, &realDice{}, 15)
if err != nil {
t.Errorf("evaluateProblem with random inputs returned error %v", err)
}
if !strings.Contains(got[len(got)-1], "wins") {
t.Errorf("evaluateProblem did not terminate with a winner, messages were %v", got)
}
}