-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-of-life_test.go
More file actions
216 lines (193 loc) · 4.41 KB
/
Copy pathgame-of-life_test.go
File metadata and controls
216 lines (193 loc) · 4.41 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
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
package main
import (
"github.com/veandco/go-sdl2/sdl"
"math/rand"
"testing"
"time"
)
// Test Game Table Printing
func TestGamePrint(t *testing.T) {
g := NewGame(3, 3)
g.SetCell(0, 0, true)
g.SetCell(1, 1, true)
g.SetCell(2, 2, true)
t.Run("Print Game Table to Console", func(t *testing.T) {
g.DebugPrint()
})
}
// Test Tables
func TestGameTableEquality(t *testing.T) {
g := NewGame(3, 3)
g.SetCell(2, 1, true)
gTarget := NewGame(3, 3)
gTarget.SetCell(2, 1, true)
if !g.Equals(gTarget) {
t.Error("Game tables not equal")
}
}
// Test 4 Rules of Game
// Reference: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules
func TestGameRules(t *testing.T) {
// RULE 1: Any live cell with fewer than two live neighbours dies, as if by underpopulation.
g1 := NewGame(3, 3)
g1.SetCell(1, 1, true)
g1.SetCell(2, 2, true)
/*
0 0 0
0 1 0
0 0 1
*/
g1Target := NewGame(3, 3)
/*
0 0 0
0 0 0
0 0 0
*/
g1.CreatePlan()
g1.RunPlan()
if !g1.Equals(g1Target) {
t.Error("RULE 1 failed")
}
// RULE 2: Any live cell with two or three live neighbours lives on to the next generation.
g2 := NewGame(3, 3)
g2.SetCell(0, 1, true)
g2.SetCell(1, 1, true)
g2.SetCell(1, 2, true)
g2.SetCell(2, 2, true)
/*
0 1 0
0 1 1
0 0 1
*/
g2Target := NewGame(3, 3)
g2Target.SetCell(0, 1, true)
g2Target.SetCell(0, 2, true)
g2Target.SetCell(1, 1, true)
g2Target.SetCell(1, 2, true)
g2Target.SetCell(2, 1, true)
g2Target.SetCell(2, 2, true)
/*
0 1 1
0 1 1
0 1 1
*/
g2.CreatePlan()
g2.RunPlan()
if !g2.Equals(g2Target) {
t.Error("RULE 2 failed")
}
// RULE 3: Any live cell with more than three live neighbours dies, as if by overpopulation.
g3 := NewGame(3, 3)
g3.SetCell(0, 0, true)
g3.SetCell(0, 1, true)
g3.SetCell(0, 2, true)
g3.SetCell(1, 0, true)
g3.SetCell(1, 1, true)
g3.SetCell(1, 2, true)
g3.SetCell(2, 0, true)
g3.SetCell(2, 1, true)
g3.SetCell(2, 2, true)
/*
1 1 1
1 1 1
1 1 1
*/
g3Target := NewGame(3, 3)
g3Target.SetCell(0, 0, true)
g3Target.SetCell(0, 2, true)
g3Target.SetCell(2, 0, true)
g3Target.SetCell(2, 2, true)
/*
1 0 1
0 0 0
1 0 1
*/
g3.CreatePlan()
g3.RunPlan()
if !g3.Equals(g3Target) {
t.Error("RULE 3 failed")
}
// RULE 4: Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
g4 := NewGame(3, 3)
g4.SetCell(2, 0, true)
g4.SetCell(2, 1, true)
g4.SetCell(2, 2, true)
/*
0 0 0
0 0 0
1 1 1
*/
g4Target := NewGame(3, 3)
g4Target.SetCell(1, 1, true)
g4Target.SetCell(2, 1, true)
/*
0 0 0
0 1 0
0 1 0
*/
g4.CreatePlan()
g4.RunPlan()
if !g4.Equals(g4Target) {
t.Error("RULE 4 failed")
}
}
func TestGameRandomize(t *testing.T) {
var gs []game
var equalLen int
var testCount = 256
rand.Seed(time.Now().UnixNano())
for i := 0; i < testCount; i++ {
g := NewGame(64, 64)
g.Randomize()
// Check equality of before generated game tables
for a := range gs {
if gs[a].Equals(g) {
equalLen++
}
}
gs = append(gs, *g)
}
if equalLen > testCount/100 {
t.Errorf("Too match equal game tables. Seeding may be not working well. "+
"Generated tables: %d Equal tables: %d", testCount, equalLen)
}
}
// Test SDL Library
func TestSDL(t *testing.T) {
// These are the flags which may be passed to SDL_Init()
// Reference: https://wiki.libsdl.org/SDL_Init
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
t.Errorf("SDL is not correctly initialized. Error: %s", err)
}
defer sdl.Quit() // Don't forget quit. Prevents memory leak.
window, err := sdl.CreateWindow("Test", 0, 0, 128, 128, sdl.WINDOW_SHOWN)
if err != nil {
t.Errorf("SDL window is not correctly created. Error: %s", err)
}
surface, err := window.GetSurface()
if err != nil {
t.Errorf("Can't get surface of SDL window. Error: %s", err)
}
bg := sdl.Rect{X: 0, Y: 0, W: 128, H: 128}
err = surface.FillRect(&bg, 0xAAAAAAAA)
if err != nil {
t.Errorf("Can't fill surface of SDL window. Error: %s", err)
}
err = window.UpdateSurface()
if err != nil {
t.Errorf("Can't update surface of SDL window. Error: %s", err)
}
rect := sdl.Rect{X: int32(32), Y: int32(32), W: 64, H: 64}
err = surface.FillRect(&rect, 0x00991111)
if err != nil {
t.Errorf("Can't draw rectangle on surface of SDL window. Error: %s", err)
}
err = window.UpdateSurface()
if err != nil {
t.Errorf("Can't update surface of SDL window. Error: %s", err)
}
err = window.Destroy()
if err != nil {
t.Errorf("Can't destroy SDL window. Error: %s", err)
}
}