-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_test.go
86 lines (81 loc) · 1.86 KB
/
count_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
package main
import (
"encoding/csv"
"fmt"
"os"
"strconv"
"testing"
"time"
"uk.ac.bris.cs/gameoflife/gol"
"uk.ac.bris.cs/gameoflife/util"
)
// TestAlive will automatically check the 512x512 cell counts for the first 5 messages.
// You can manually check your counts by looking at CSVs provided in check/alive
func TestAlive(t *testing.T) {
p := gol.Params{
Turns: 100000000,
Threads: 8,
ImageWidth: 512,
ImageHeight: 512,
}
alive := readAliveCounts(p.ImageWidth, p.ImageHeight)
events := make(chan gol.Event)
gol.Run(p, events, nil)
implemented := make(chan bool)
go func() {
timer := time.After(5 * time.Second)
select {
case <-timer:
t.Fatal("no AliveCellsCount events received in 5 seconds")
case <-implemented:
return
}
}()
i := 0
for event := range events {
switch e := event.(type) {
case gol.AliveCellsCount:
var expected int
if e.CompletedTurns <= 10000 {
expected = alive[e.CompletedTurns]
} else if e.CompletedTurns%2 == 0 {
expected = 5565
} else {
expected = 5567
}
actual := e.CellsCount
if expected != actual {
t.Fatalf("At turn %v expected %v alive cells, got %v instead", e.CompletedTurns, expected, actual)
} else {
fmt.Println(event)
if i == 0 {
implemented <- true
}
i++
}
}
if i >= 5 {
return
}
}
t.Fatal("not enough AliveCellsCount events received")
}
func readAliveCounts(width, height int) map[int]int {
f, err := os.Open("check/alive/" + fmt.Sprintf("%vx%v.csv", width, height))
util.Check(err)
reader := csv.NewReader(f)
table, err := reader.ReadAll()
util.Check(err)
alive := make(map[int]int)
for i, row := range table {
if i == 0 {
continue
}
completedTurns, err := strconv.Atoi(row[0])
util.Check(err)
aliveCount, err := strconv.Atoi(row[1])
util.Check(err)
alive[completedTurns] = aliveCount
}
return alive
}