-
Notifications
You must be signed in to change notification settings - Fork 3
/
food_test.go
72 lines (61 loc) · 1.18 KB
/
food_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
package main
import (
"log"
"testing"
"time"
)
func TestUpdate(t *testing.T) {
tests := [4]bool{}
running := make(chan bool)
go func(tests *[4]bool, running chan bool) {
s := newSnake()
f := newFood()
f.x = windowWidth / 2
f.y = windowHeight / 2
for {
f.update(s)
if f.x < 0 {
log.Println("f.x < 0")
return
} else if f.x >= windowWidth {
log.Printf("f.x >= windowWidth: %v", f.x)
return
} else if f.y < 0 {
log.Println("f.y < 0")
return
} else if f.y >= windowHeight {
log.Printf("f.y >= windowHeight: %v", f.y)
return
}
if f.x == 0 {
tests[0] = true
} else if f.x == windowWidth-cellSize {
tests[1] = true
} else if f.y == 0 {
tests[2] = true
} else if f.y == windowHeight-cellSize {
tests[3] = true
}
if tests[0] && tests[1] && tests[2] && tests[3] {
return
}
running <- true
}
}(&tests, running)
timeout := time.After(5 * time.Second)
for {
select {
case <-running:
// nop
log.Printf("running: %v", tests)
case <-timeout:
log.Printf("evaluating: %v", tests)
for n, b := range tests {
if !b {
t.Fatalf("Test %v failed!", n)
}
}
return
}
}
}