-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
82 lines (74 loc) · 1.38 KB
/
main_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
package main
import (
"flag"
"os"
"runtime"
"testing"
"time"
"uk.ac.bris.cs/gameoflife/sdl"
"uk.ac.bris.cs/gameoflife/util"
)
var w *sdl.Window
var flipCellChan chan util.Cell
var refreshChan chan struct{}
var clearPixelsChan chan struct{}
func TestMain(m *testing.M) {
runtime.LockOSThread()
var sdlFlag = flag.Bool(
"sdl",
false,
"Enable the SDL window for testing.")
flag.Parse()
done := make(chan int, 1)
test := func() { done <- m.Run() }
if !(*sdlFlag) {
go test()
} else {
w = sdl.NewWindow(512, 512)
flipCellChan = make(chan util.Cell, 1000)
refreshChan = make(chan struct{}, 1)
clearPixelsChan = make(chan struct{}, 1)
fps := 60
ticker := time.NewTicker(time.Second / time.Duration(fps))
dirty := false
go test()
loop:
for {
select {
case code := <-done:
done <- code
w.Destroy()
break loop
case <-ticker.C:
w.PollEvent()
if dirty {
w.RenderFrame()
dirty = false
}
case cell := <-flipCellChan:
w.FlipPixel(cell.X, cell.Y)
case <-clearPixelsChan:
w.ClearPixels()
w.RenderFrame()
case <-refreshChan:
dirty = true
}
}
}
os.Exit(<-done)
}
func flipCell(cell util.Cell) {
if flipCellChan != nil {
flipCellChan <- cell
}
}
func refresh() {
if refreshChan != nil {
refreshChan <- struct{}{}
}
}
func clearPixels() {
if clearPixelsChan != nil {
clearPixelsChan <- struct{}{}
}
}