-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_test.go
340 lines (302 loc) · 9.05 KB
/
sequence_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package animation
import (
"image/color"
"testing"
"time"
)
type testAnimation uint8
type multiRunAnimation int
func (a *testAnimation) Start(startTime time.Time) {
// NOP
}
func (a *testAnimation) Frame(buf []color.RGBA, frameTime time.Time) (output []color.RGBA, done bool) {
output = buf
// fmt.Printf("In Frame for %v buf len %d, time %v\n", *a, len(buf), frameTime.Nanosecond()/1000)
count := uint8(*a)
for idx := range buf {
output[idx] = color.RGBA{count, count + 1, count + 2, 0x00}
count += 3
}
return output, true
}
func (a *multiRunAnimation) Start(startTime time.Time) {
// NOP
}
func (a *multiRunAnimation) Frame(buf []color.RGBA, frameTime time.Time) (output []color.RGBA, done bool) {
*a--
return buf, *a <= 0
}
func TestDeleteStep(t *testing.T) {
s1 := Step{UniverseID: 1}
s2 := Step{UniverseID: 2}
s3 := Step{UniverseID: 3}
stepsArr := [3]*Step{&s1, &s2, &s3}
steps := stepsArr[:]
no0 := deleteStep(steps, 0)
if len(no0) != 2 || no0[0] != &s2 || no0[1] != &s3 {
t.Fatalf("Delete 0 not as expected: %v", no0)
}
if stepsArr != [3]*Step{&s2, &s3, nil} {
t.Fatalf("Underlying array not as expected after delete 0: %v", no0)
}
}
func TestSimpleSingleSequence(t *testing.T) {
const universeID uint = 3
ta := testAnimation(1)
s := &Step{UniverseID: universeID, Effect: &ta}
seq := NewSequence().AddInitialStep("first", s)
sr := NewSequenceRunner([]uint{1, 1, 1, 10})
startTime := time.Unix(0, 0)
sr.InitSequence(seq, startTime)
// Check for right-sized empty buffer
buf := sr.UniverseData(universeID)
if len(buf) != 10 {
t.Fatalf("Unexpected buffer size %d", len(buf))
}
zero := color.RGBA{0, 0, 0, 0}
for idx, p := range buf {
if p != zero {
t.Fatalf("Pixel %d not 0 (%x)", idx, p)
}
}
// Call the effect to generate data
done := sr.ProcessFrame(startTime)
if done {
t.Fatal("Done on first frame")
}
// Check for updated data
count := uint8(1)
buf = sr.UniverseData(universeID)
if len(buf) != 10 {
t.Fatalf("Unexpected buffer size %d", len(buf))
}
for idx, p := range buf {
expected := color.RGBA{count, count + 1, count + 2, 0x00}
if p != expected {
t.Fatalf("Pixel %d value (%v) not expected (%v)", idx, p, expected)
}
count += 3
}
done = sr.ProcessFrame(startTime.Add(time.Millisecond))
if !done {
t.Fatal("Not done on second call to ProcessFrame")
}
}
func TestTwoSimpleSequences(t *testing.T) {
ta1 := testAnimation(1)
s1 := &Step{UniverseID: 3, Effect: &ta1}
ta2 := testAnimation(31)
s2 := &Step{UniverseID: 1, Effect: &ta2}
seq := NewSequence().AddInitialStep("first", s1).AddInitialStep("second", s2)
sr := NewSequenceRunner([]uint{1, 6, 1, 10})
startTime := time.Unix(0, 0)
sr.InitSequence(seq, startTime)
// Check for right-sized empty buffer
buf := sr.UniverseData(1)
if len(buf) != 6 {
t.Fatalf("Unexpected buffer size %d", len(buf))
}
zero := color.RGBA{0, 0, 0, 0}
for idx, p := range buf {
if p != zero {
t.Fatalf("Pixel %d not 0 (%x)", idx, p)
}
}
buf = sr.UniverseData(3)
if len(buf) != 10 {
t.Fatalf("Unexpected buffer size %d", len(buf))
}
for idx, p := range buf {
if p != zero {
t.Fatalf("Pixel %d not 0 (%x)", idx, p)
}
}
// Call the effect to generate data
done := sr.ProcessFrame(startTime)
if done {
t.Fatal("Done on first frame")
}
// Check for updated data
buf = sr.UniverseData(1)
count := uint8(31)
for idx, p := range buf {
expected := color.RGBA{count, count + 1, count + 2, 0x00}
if p != expected {
t.Fatalf("Pixel %d value (%v) not expected (%v)", idx, p, expected)
}
count += 3
}
buf = sr.UniverseData(3)
count = uint8(1)
for idx, p := range buf {
expected := color.RGBA{count, count + 1, count + 2, 0x00}
if p != expected {
t.Fatalf("Pixel %d value (%v) not expected (%v)", idx, p, expected)
}
count += 3
}
done = sr.ProcessFrame(startTime.Add(time.Millisecond))
if !done {
t.Fatal("Not done on second call to ProcessFrame")
}
}
func TestMultiRun(t *testing.T) {
ta1 := multiRunAnimation(3)
s1 := &Step{UniverseID: 3, Effect: &ta1}
ta2 := multiRunAnimation(1)
s2 := &Step{UniverseID: 0, Effect: &ta2}
seq := NewSequence().AddInitialStep("first", s1).AddInitialStep("second", s2)
sr := NewSequenceRunner([]uint{1, 1, 1, 1})
now := time.Unix(0, 0)
sr.InitSequence(seq, now)
if sr.ProcessFrame(now) {
t.Fatal("Done on call 1")
}
if sr.ProcessFrame(now.Add(time.Millisecond)) {
t.Fatal("Done on call 2")
}
if sr.ProcessFrame(now.Add(2 * time.Millisecond)) {
t.Fatal("Done on call 3")
}
if !sr.ProcessFrame(now.Add(3 * time.Millisecond)) {
t.Fatal("Not done on call 4")
}
}
func TestDelay(t *testing.T) {
ta1 := testAnimation(1)
s1 := &Step{UniverseID: 3, Effect: &ta1}
ta2 := testAnimation(31)
s2 := &Step{UniverseID: 1, Effect: &ta2}
seq := NewSequence()
seq.AddStep("first", s1).AddInitialOperation(Operation{"first", 9 * time.Millisecond})
seq.AddInitialStep("second", s2)
sr := NewSequenceRunner([]uint{1, 6, 1, 10})
now := time.Unix(0, 0)
sr.InitSequence(seq, now)
for tick := 0; tick < 10; tick++ {
frameTime := now.Add(time.Duration(tick) * time.Millisecond)
if sr.ProcessFrame(frameTime) {
t.Fatalf("Done on call for tick %d, time %v", tick, frameTime)
}
if sr.UniverseData(3)[0].R != 0 {
t.Fatalf("Delayed effect ran too early tick %d, time %v", tick, frameTime)
}
if sr.UniverseData(1)[0].R == 0 {
t.Fatalf("Immediate effect didn't run tick %d, time %v", tick, frameTime)
}
}
if sr.ProcessFrame(now.Add(10 * time.Millisecond)) {
t.Fatal("Done at time 10")
}
if sr.UniverseData(3)[0].R == 0 {
t.Fatal("Delayed effect didn't run at right time")
}
if !sr.ProcessFrame(now.Add(11 * time.Millisecond)) {
t.Fatal("Not done at time 11")
}
}
func TestScheduleAfter(t *testing.T) {
ta1 := testAnimation(1)
ta2 := testAnimation(31)
s2 := &Step{UniverseID: 3, Effect: &ta2, Next: []Operation{Operation{"first", 1 * time.Millisecond}}}
// Not using a delay is dicey because the execution order in a single clock cycle
// is unpredictable
s1 := &Step{UniverseID: 1, Effect: &ta1}
seq := NewSequence()
seq.AddInitialStep("second", s2)
seq.AddStep("first", s1)
sr := NewSequenceRunner([]uint{1, 1, 1, 1})
now := time.Unix(0, 0)
sr.InitSequence(seq, now)
if sr.UniverseData(1)[0].R != 0 {
t.Fatal("Data initialization failed to clear colors")
}
// On the first tick only the first step should fire
tick := 0
frameTime := now.Add(time.Duration(tick))
if sr.ProcessFrame(frameTime) {
t.Fatalf("Done on call for tick %d, time %v", tick, frameTime)
}
if sr.UniverseData(1)[0].R != 0 {
t.Fatalf("Contingent effect ran too early at tick %d", tick)
}
if sr.UniverseData(3)[0].R == 0 {
t.Fatalf("Immediate effect didn't run at tick %d", tick)
}
tick += 2
if sr.ProcessFrame(now.Add(time.Duration(tick) * time.Millisecond)) {
t.Fatal("Done at time 1")
}
if sr.UniverseData(1)[0].R == 0 {
t.Fatalf("Contingent effect didn't run at right time at tick %d", tick)
}
if sr.UniverseData(3)[0].R == 0 {
t.Fatalf("Immediate effect weas cleared unexpectedly %d", tick)
}
tick += 2
if !sr.ProcessFrame(now.Add(time.Duration(tick) * time.Millisecond)) {
t.Fatal("Not done at time 2")
}
}
func TestScheduleAfterPlusDelay(t *testing.T) {
ta1 := testAnimation(1)
ta2 := testAnimation(31)
s2 := &Step{UniverseID: 3, Effect: &ta2, Next: []Operation{Operation{"s1", 2 * time.Millisecond}}}
s1 := &Step{UniverseID: 1, Effect: &ta1}
seq := NewSequence()
seq.AddInitialStep("s2", s2)
seq.AddStep("s1", s1)
sr := NewSequenceRunner([]uint{1, 1, 1, 1})
now := time.Unix(0, 0)
sr.InitSequence(seq, now)
for tick := 0; tick < 3; tick++ {
frameTime := now.Add(time.Duration(tick) * time.Millisecond)
if sr.ProcessFrame(frameTime) {
t.Fatalf("Done on call for tick %d, time %v", tick, frameTime)
}
if sr.UniverseData(1)[0].R != 0 {
t.Fatal("Contingent effect ran too early")
}
if sr.UniverseData(3)[0].R == 0 {
t.Fatal("Immediate effect didn't run")
}
}
if sr.ProcessFrame(now.Add(3 * time.Millisecond)) {
t.Fatal("Done at time 3")
}
if sr.UniverseData(1)[0].R == 0 {
t.Fatal("Contingent effect didn't run at right time")
}
if !sr.ProcessFrame(now.Add(4 * time.Millisecond)) {
t.Fatal("Not done at time 4")
}
}
func TestScheduleAfterBadStepId(t *testing.T) {
ta1 := testAnimation(1)
ta2 := testAnimation(31)
// Next operation with invalid step should be ignored with warning
s2 := &Step{UniverseID: 3, Effect: &ta2, Next: []Operation{Operation{"s9", 0}}}
s1 := &Step{UniverseID: 1, Effect: &ta1}
seq := NewSequence().AddInitialStep("s2", s2).AddStep("s1", s1)
sr := NewSequenceRunner([]uint{1, 1, 1, 1})
now := time.Unix(0, 0)
sr.InitSequence(seq, now)
for tick := 0; tick < 1; tick++ {
frameTime := now.Add(time.Duration(tick) * time.Millisecond)
if sr.ProcessFrame(frameTime) {
t.Fatalf("Done on call for tick %d, time %v", tick, frameTime)
}
if sr.UniverseData(1)[0].R != 0 {
t.Fatal("Invalid contingent effect ran")
}
if sr.UniverseData(3)[0].R == 0 {
t.Fatal("Immediate effect didn't run")
}
}
if !sr.ProcessFrame(now.Add(1 * time.Millisecond)) {
t.Fatal("Not done at time 1")
}
if sr.UniverseData(1)[0].R != 0 {
t.Fatal("Contingent effect ran")
}
}