-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathclock_test.go
131 lines (122 loc) · 2.64 KB
/
clock_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package engo
import (
"testing"
"time"
)
// testTime is the time interface where testTime.Now() is controllable using
// testTime.curTime
type testTime struct {
curTime int64
}
func (t testTime) Now() int64 {
return t.curTime
}
func TestClockTick(t *testing.T) {
theTimer = testTime{0}
clock := NewClock()
before := clock.counter
clock.Tick()
after := clock.counter
if after != before+1 {
t.Errorf("Tick did not increase the counter, before: %v, after: %v", before, after)
}
theTimer = testTime{1000000000}
clock.Tick()
if clock.counter != 0 {
t.Error("Clock's count did not reset to 0 after waiting over 1 second")
}
}
func TestClockFPS(t *testing.T) {
data := []struct {
fps int
}{
{5},
{10},
{20},
{30},
{60},
}
for _, d := range data {
theTimer = testTime{0}
clock := NewClock()
tickTime := 1000000000 / d.fps
curTime := int64(0)
for i := 0; i < d.fps; i++ {
curTime += int64(tickTime) + 1
theTimer = testTime{curTime}
clock.Tick()
}
if clock.FPS() != float32(d.fps) {
t.Errorf("Clock's FPS did not match %v fps, was %v", d.fps, clock.FPS())
}
}
}
func TestClockDelta(t *testing.T) {
data := []struct {
delta int64
}{
{6000000000},
{16666667},
{33333333},
{66666666},
{60},
}
for _, d := range data {
exp := float32(d.delta) / 1000000000.0
theTimer = testTime{0}
clock := NewClock()
theTimer = testTime{d.delta}
clock.Tick()
if clock.Delta() != exp {
t.Errorf("Clock's Delta did not match %v, was %v", exp, clock.Delta())
}
}
}
func TestClockTime(t *testing.T) {
data := []struct {
time int64
}{
{6000000000},
{16666667},
{33333333},
{66666666},
{60},
}
for _, d := range data {
exp := float32(d.time) / 1000000000.0
theTimer = testTime{0}
clock := NewClock()
theTimer = testTime{d.time}
if clock.Time() != exp {
t.Errorf("Clock's duration from Time() did not match %v seconds, was %v", exp, clock.Time())
}
}
}
func TestTheTimerNow(t *testing.T) {
theTimer = realTime{}
res := time.Now().UnixNano() - theTimer.Now()
if res < -1000000 { //this is an arbitrary choice, could be smaller or larger depending on machine testing on
t.Error("theTimer when it's a realTime did not produce time.Now().UnixNano()")
}
}
func TestClockPause(t *testing.T) {
clock := NewClock()
wait := time.NewTicker(time.Second / 100)
clock.Tick()
<-wait.C
clock.Tick()
if clock.Delta() <= 0 {
t.Error("Clock did not increase delta after wait")
}
clock.Pause()
if clock.Delta() != 0 {
t.Error("Clock did not pause!")
}
clock.Unpause()
clock.Tick()
<-wait.C
clock.Tick()
if clock.Delta() <= 0 {
t.Error("Clock did not increase delta after unpausing")
}
}