-
Notifications
You must be signed in to change notification settings - Fork 0
/
gol_test.go
245 lines (204 loc) · 6.01 KB
/
gol_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package gogol
import (
"reflect"
"testing"
)
func TestBoardGeneration(t *testing.T) {
boardSizes := [4]int{10, 42, 100, 200}
randomize := [2]bool{false, true}
for _, rand := range randomize {
for _, n := range boardSizes {
board := GenerateBoard(n, n, rand)
count := 0
if len(board) != n {
t.Error("Incorrect board height.")
}
for i := range board {
if len(board[i]) != n {
t.Error("Incorrect board width.")
}
for j := range board[i] {
if board[i][j] == true {
count++
}
}
}
if !rand && count > 0 {
t.Error("Board should be empty.")
}
if rand && count == 0 {
t.Error("Board should contain living cells.")
}
}
}
}
func TestLivingNeighborCounting(t *testing.T) {
board := Board{
{true, false, false},
{true, false, true},
{false, false, false},
}
positions := [][]int{
{0, 0, 1},
{1, 0, 3},
{2, 0, 1},
{0, 1, 1},
{1, 1, 3},
{2, 1, 0},
{0, 2, 1},
{1, 2, 2},
{2, 2, 1},
}
for _, p := range positions {
count := CountLivingNeighborCells(board, p[0], p[1])
if count != p[2] {
t.Errorf("Wrong number of living neighbors (counted %v, should be %v).", count, p[2])
}
}
}
func TestCalculateEvolution(t *testing.T) {
board := Board{
{true, false, false, false, true},
{false, true, true, false, false},
{true, false, true, false, true},
{false, false, false, true, false},
{false, true, false, false, false},
}
nextGenBoard := CalculateEvolution(board)
if nextGenBoard[4][0] != false {
t.Error("Lonely cells should die.")
}
if nextGenBoard[1][1] != false {
t.Error("Crowded cells should die.")
}
if nextGenBoard[3][2] != true {
t.Error("Cell with three neighbors should become alive.")
}
if nextGenBoard[0][2] != false {
t.Error("Cell with two neighbors should keep its state.")
}
}
func TestStaticObjects(t *testing.T) {
board1 := Board{
{false, false, false, false},
{false, true, true, false},
{false, true, true, false},
{false, false, false, false},
}
if reflect.DeepEqual(board1, CalculateEvolution(board1)) != true {
t.Error("Static square should not change over evolutions.")
}
board2 := Board{
{false, false, false, false, false},
{false, false, true, false, false},
{false, true, false, true, false},
{false, false, true, false, false},
{false, false, false, false, false},
}
if reflect.DeepEqual(board2, CalculateEvolution(board2)) != true {
t.Error("Static plus should not change over evolutions.")
}
board3 := Board{
{false, false, false, false, false},
{false, false, true, false, false},
{false, true, false, true, false},
{false, true, false, true, false},
{false, false, true, false, false},
{false, false, false, false, false},
}
if reflect.DeepEqual(board3, CalculateEvolution(board3)) != true {
t.Error("Static 0 should not change over evolutions.")
}
board4 := Board{
{false, false, false, false, false, false},
{false, false, true, true, false, false},
{false, true, false, false, true, false},
{false, true, false, false, true, false},
{false, false, true, true, false, false},
{false, false, false, false, false, false},
}
if reflect.DeepEqual(board4, CalculateEvolution(board4)) != true {
t.Error("Static circle should not change over evolutions.")
}
board5 := Board{
{false, false, false, false, false, false},
{false, false, true, true, false, false},
{false, true, false, false, true, false},
{false, false, true, false, true, false},
{false, false, false, true, false, false},
{false, false, false, false, false, false},
}
if reflect.DeepEqual(board5, CalculateEvolution(board5)) != true {
t.Error("Static near-circle should not change over evolutions.")
}
board6 := Board{
{false, false, false, false, false, false},
{false, false, true, false, false, false},
{false, true, false, true, false, false},
{false, false, true, false, true, false},
{false, false, false, true, false, false},
{false, false, false, false, false, false},
}
if reflect.DeepEqual(board6, CalculateEvolution(board6)) != true {
t.Error("Static diagonal should not change over evolutions.")
}
}
func TestOscillatingObjects(t *testing.T) {
board1 := Board{
{false, false, false, false, false},
{false, false, true, false, false},
{false, false, true, false, false},
{false, false, true, false, false},
{false, false, false, false, false},
}
nextGenBoard1 := Board{
{false, false, false, false, false},
{false, false, false, false, false},
{false, true, true, true, false},
{false, false, false, false, false},
{false, false, false, false, false},
}
if reflect.DeepEqual(nextGenBoard1, CalculateEvolution(board1)) != true ||
reflect.DeepEqual(board1, CalculateEvolution(nextGenBoard1)) != true {
t.Error("Line of three cells should oscillate.")
}
board2 := Board{
{false, false, false, false, false, false},
{false, false, false, true, false, false},
{false, true, true, false, false, false},
{false, false, false, true, true, false},
{false, false, true, false, false, false},
{false, false, false, false, false, false},
}
nextGenBoard2 := Board{
{false, false, false, false, false, false},
{false, false, true, false, false, false},
{false, false, true, false, true, false},
{false, true, false, true, false, false},
{false, false, false, true, false, false},
{false, false, false, false, false, false},
}
if reflect.DeepEqual(nextGenBoard2, CalculateEvolution(board2)) != true ||
reflect.DeepEqual(board2, CalculateEvolution(nextGenBoard2)) != true {
t.Error("Clock pattern should oscillate.")
}
}
func TestCollectStatistics(t *testing.T) {
board := Board{
{true, false, false, false, true},
{false, true, true, false, false},
{true, false, true, false, true},
{false, false, false, true, false},
{false, true, false, false, false},
}
stats := CollectStatistics(board)
if stats.Alive != 9 {
t.Error("Incorrect count of living cells.")
}
if stats.Dead != 16 {
t.Error("Incorrect count of living cells.")
}
if stats.Total != 25 {
t.Error("Incorrect count of total cells.")
}
}