-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifekata_test.go
145 lines (123 loc) · 3.08 KB
/
lifekata_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package lifekata
import "testing"
import "reflect"
func TestGame(t *testing.T) {
_ = NewGame()
}
func TestCoord(t *testing.T) {
_ = coord{1, 1}
}
func TestSetCell(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Alive)
if g.GetCell(coord{1, 1}) != Alive {
t.Errorf("Expected coord(1,1) to be Alive")
}
}
func TestDeadCell(t *testing.T) {
g := NewGame()
if g.GetCell(coord{1, 1}) != Dead {
t.Errorf("Expected unset cell to be Dead")
}
}
func TestCountAliveNeighbours(t *testing.T) {
g := NewGame()
if g.CountAliveNeighbours(coord{1, 1}) != 0 {
t.Errorf("Expected no neighbours to be alive")
}
g.SetCell(coord{1, 1}, Alive)
g.SetCell(coord{1, 2}, Alive)
if g.CountAliveNeighbours(coord{2, 1}) != 2 {
t.Errorf("Expected 2 neighbours to be alive")
}
if g.CountAliveNeighbours(coord{1, 1}) != 1 {
t.Errorf("Expected 1 neighbour to be alive")
}
}
func TestNewGeneration(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Alive)
g.SetCell(coord{1, 2}, Alive)
g.SetCell(coord{1, 3}, Alive)
if g.NewGeneration(coord{1, 1}) != Dead {
t.Errorf("Expected coord{1,1} to die")
}
if g.NewGeneration(coord{1, 2}) != Alive {
t.Errorf("Expected coord{1,2} to remain alive")
}
if g.NewGeneration(coord{2, 1}) != Dead {
t.Errorf("Expected coord{2,1} to remain dead")
}
if g.NewGeneration(coord{2, 2}) != Alive {
t.Errorf("Expected coord{2,2} to live")
}
}
func TestOvercrowded(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Alive)
g.SetCell(coord{1, 2}, Alive)
g.SetCell(coord{1, 3}, Alive)
g.SetCell(coord{0, 2}, Alive)
g.SetCell(coord{2, 2}, Alive)
if g.NewGeneration(coord{1, 2}) != Dead {
t.Errorf("Expected coord{1,2} to starve")
}
}
func TestNoCoordsToGenerate(t *testing.T) {
g := NewGame()
if !reflect.DeepEqual(g.CoordsToGenerate(), []coord{}) {
t.Errorf("Expected no coords to generate")
}
}
func TestCoordsToGenerate(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Alive)
crds := g.CoordsToGenerate()
if !reflect.DeepEqual(crds, []coord{
{0, 0},
{0, 1},
{0, 2},
{1, 0},
{1, 1},
{1, 2},
{2, 0},
{2, 1},
{2, 2},
}) {
t.Errorf("Expected coords to generate, got %#v", crds)
}
}
func TestDeadCoordsToGenerate(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Dead)
crds := g.CoordsToGenerate()
if !reflect.DeepEqual(crds, []coord{}) {
t.Errorf("Expected no coords to generate, got %#v", crds)
}
}
func TestCompleteGeneration(t *testing.T) {
g := NewGame()
g.SetCell(coord{1, 1}, Alive)
g.SetCell(coord{1, 2}, Alive)
g.SetCell(coord{1, 3}, Alive)
g.GenerateNextGeneration()
if g.GetCell(coord{1, 1}) != Dead {
t.Errorf("Expected coord{1,1} to die")
}
if g.GetCell(coord{1, 2}) != Alive {
t.Errorf("Expected coord{1,2} to remain alive")
}
if g.GetCell(coord{2, 1}) != Dead {
t.Errorf("Expected coord{2,1} to remain dead")
}
if g.GetCell(coord{2, 2}) != Alive {
t.Errorf("Expected coord{2,2} to live")
}
}
func TestRules(t *testing.T) {
g := NewRulesGame([]amount{1}, []amount{})
g.SetCell(coord{0, 0}, Alive)
if g.NewGeneration(coord{-1, -1}) != Alive {
t.Errorf("Expected coord{-1,-1} to be alive")
}
}