-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgol_test.go
More file actions
82 lines (75 loc) · 2.08 KB
/
gol_test.go
File metadata and controls
82 lines (75 loc) · 2.08 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
package main
import (
"fmt"
"testing"
"uk.ac.bris.cs/gameoflife/gol"
"uk.ac.bris.cs/gameoflife/util"
)
// TestGol tests 16x16, 64x64 and 512x512 images on 0, 1 and 100 turns using 1-16 worker threads.
func TestGol(t *testing.T) {
tests := []gol.Params{
{ImageWidth: 16, ImageHeight: 16},
{ImageWidth: 64, ImageHeight: 64},
{ImageWidth: 512, ImageHeight: 512},
}
for _, p := range tests {
for _, turns := range []int{0, 1, 100} {
p.Turns = turns
expectedAlive := util.ReadAliveCells(
"check/images/"+fmt.Sprintf("%vx%vx%v.pgm", p.ImageWidth, p.ImageHeight, turns),
p.ImageWidth,
p.ImageHeight,
)
for threads := 1; threads <= 16; threads++ {
p.Threads = threads
testName := fmt.Sprintf("%dx%dx%d-%d", p.ImageWidth, p.ImageHeight, p.Turns, p.Threads)
t.Run(testName, func(t *testing.T) {
events := make(chan gol.Event)
gol.Run(p, events, nil)
var cells []util.Cell
for event := range events {
switch e := event.(type) {
case gol.FinalTurnComplete:
cells = e.Alive
}
}
assertEqualBoard(t, cells, expectedAlive, p)
})
}
}
}
}
func boardFail(t *testing.T, given, expected []util.Cell, p gol.Params) bool {
errorString := fmt.Sprintf("-----------------\n\n FAILED TEST\n %vx%v\n %d Workers\n %d Turns\n", p.ImageWidth, p.ImageHeight, p.Threads, p.Turns)
if p.ImageWidth == 16 && p.ImageHeight == 16 {
errorString = errorString + util.AliveCellsToString(given, expected, p.ImageWidth, p.ImageHeight)
}
t.Error(errorString)
return false
}
func assertEqualBoard(t *testing.T, given, expected []util.Cell, p gol.Params) bool {
givenLen := len(given)
expectedLen := len(expected)
if givenLen != expectedLen {
return boardFail(t, given, expected, p)
}
visited := make([]bool, expectedLen)
for i := 0; i < givenLen; i++ {
element := given[i]
found := false
for j := 0; j < expectedLen; j++ {
if visited[j] {
continue
}
if expected[j] == element {
visited[j] = true
found = true
break
}
}
if !found {
return boardFail(t, given, expected, p)
}
}
return true
}