-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
114 lines (109 loc) · 2.39 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
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
package main
import (
"testing"
"time"
)
func TestWallIntersections(t *testing.T) {
walls := []*Line{
&Line{&Vector2d{0, 0}, &Vector2d{0, 1}},
&Line{&Vector2d{0, 1}, &Vector2d{1, 1}},
&Line{&Vector2d{1, 1}, &Vector2d{1, 0}},
&Line{&Vector2d{1, 0}, &Vector2d{0, 0}},
}
line := &Line{
&Vector2d{0.1, 0.5},
&Vector2d{0.4, 0.6},
}
intersections := WallIntersections(walls, line)
for _, intersection := range intersections {
if intersection.IntersectAt > 0 && intersection.IntersectAt < 1 {
t.Errorf("intersection we have, captain")
}
}
line = &Line{
&Vector2d{0.5, 0.5},
&Vector2d{0.5, 1.5},
}
intersections = WallIntersections(walls, line)
for i, intersection := range intersections {
if i == 2 {
if !(intersection.IntersectAt > 0) ||
!(intersection.IntersectAt < 1) {
t.Errorf(
"%v should intersect %v",
line,
intersection.Wall,
)
}
} else {
if intersection.IntersectAt > 0 &&
intersection.IntersectAt < 1 {
t.Errorf("intersection we have, captain")
}
}
}
}
func TestUpdateBall(t *testing.T) {
fakeUniverse := map[string]Object{
"Ball": Object{
W: 0.1,
H: 0.1,
X: 0.5,
Y: 0.5,
DX: 0.1,
DY: 0.1,
},
}
fakeUniverseChan := make(chan map[string]Object, 1)
fakeUniverseChan <- fakeUniverse
errChan := make(chan error, 1)
d := time.Second
UpdateBall(fakeUniverseChan, errChan, d)
select {
case err := <-errChan:
t.Error("UpdateBall caused error", err)
default:
// continue
}
fakeUniverse = <-fakeUniverseChan
pos := &Vector2d{fakeUniverse["Ball"].X, fakeUniverse["Ball"].Y}
should := &Vector2d{0.6, 0.6}
if !pos.Equals(should) {
t.Errorf(
"Ball(%v, %v) is not at [%v, %v]",
fakeUniverse["Ball"].X,
fakeUniverse["Ball"].Y,
0.6, 0.6,
)
}
}
func TestUpdateBallWall(t *testing.T) {
fakeUniverse := map[string]Object{
"Ball": Object{
X: 0.5,
Y: 0.9,
DX: 0.1,
DY: 0.2,
},
}
fakeUniverseChan := make(chan map[string]Object, 1)
fakeUniverseChan <- fakeUniverse
errChan := make(chan error, 1)
d := time.Second
UpdateBall(fakeUniverseChan, errChan, d)
select {
case err := <-errChan:
t.Error("UpdateBall caused error", err)
default:
// continue
}
fakeUniverse = <-fakeUniverseChan
if !(fakeUniverse["Ball"].X == 0.6 && fakeUniverse["Ball"].Y == 0.9) {
t.Errorf(
"Ball(%v, %v) is not at [%v, %v]",
fakeUniverse["Ball"].X,
fakeUniverse["Ball"].Y,
0.6, 0.6,
)
}
}