-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife_test.go
161 lines (142 loc) · 3.32 KB
/
gameoflife_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
package gameoflife
import (
"context"
"testing"
"time"
)
func TestStateChange(t *testing.T) {
en := NewEngine()
state := State{
[]bool{true, true},
[]bool{true, true},
}
er1 := en.SetInitialState(state)
er2 := en.SetInitialState(state)
if er1 != nil {
t.Error("failed to set state")
}
if er2 == nil {
t.Error("possible to change state")
}
}
func TestTicksOnEmptyState(t *testing.T) {
en := NewEngine()
_, err := en.GetStateAfterTicks(1)
if err == nil {
t.Error("empty engine do ticks")
}
}
func TestTicksPerSecondRespected(t *testing.T) {
en := NewEngine()
en.SetInitialState(State{
[]bool{true, true},
[]bool{true, true},
})
en.Config.TicksPerSecond = 2
todoCtx := context.TODO()
ctx, cancel := context.WithTimeout(todoCtx, time.Second*2)
defer cancel()
channel, _ := en.ListenState(ctx)
counter := 0
expectCounter := 4
for range channel {
counter++
}
if counter != expectCounter {
t.Error("bad tickrate")
}
}
func TestDoTick(t *testing.T) {
en := NewEngine()
state := State{
StateRow{true, false, false},
StateRow{true, false, false},
StateRow{true, false, false},
}
expectedState := State{
StateRow{true, true, true},
StateRow{true, true, true},
StateRow{true, true, true},
}
en.SetInitialState(state)
newState, _ := en.GetStateAfterTicks(1)
if !compareStates(newState, expectedState) {
t.Error("bad life calculation")
}
}
func TestListenEmptyState(t *testing.T) {
en := NewEngine()
_, err := en.ListenState(context.Background())
if err == nil {
t.Error("listen empty state make no sence")
}
}
func TestGetStateAfterSeconds(t *testing.T) {
en1 := NewEngine()
en2 := NewEngine()
state := State{
StateRow{false, true, false},
StateRow{false, true, true},
StateRow{false, true, false},
}
en1.SetInitialState(state)
en2.SetInitialState(state)
en1.Config.TicksPerSecond = 3
r1, _ := en1.GetStateAfterSeconds(1)
r2, _ := en2.GetStateAfterTicks(3)
if !compareStates(r1, r2) {
t.Error("GetStateAfterSeconds fails tickrate")
}
}
func TestChangeWithListeners(t *testing.T) {
en := NewEngine()
en.SetInitialState(State{
StateRow{false, true, false},
StateRow{false, true, true},
StateRow{false, true, false},
})
en.ListenState(context.Background())
_, err := en.GetStateAfterSeconds(1)
if err == nil {
t.Error("can't interupt listeners by direct state change")
}
}
func TestBlinker(t *testing.T) {
en := NewEngine()
en.SetInitialState(State{
StateRow{false, false, false, false, false},
StateRow{false, false, false, false, false},
StateRow{false, true, true, true, false},
StateRow{false, false, false, false, false},
StateRow{false, false, false, false, false},
})
expectedState := State{
StateRow{false, false, false, false, false},
StateRow{false, false, true, false, false},
StateRow{false, false, true, false, false},
StateRow{false, false, true, false, false},
StateRow{false, false, false, false, false},
}
r, _ := en.GetStateAfterTicks(5)
if !compareStates(r, expectedState) {
t.Error("logic bug for common structure")
}
}
func compareStates(s1 State, s2 State) bool {
if len(s1) == len(s2) {
for i := 0; i < len(s1); i++ {
if len(s1[i]) == len(s2[i]) {
for j := 0; j < len(s1[i]); j++ {
if s1[i][j] != s2[i][j] {
return false
}
}
} else {
return false
}
}
} else {
return false
}
return true
}