forked from wilson1yan/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_test.lua
More file actions
209 lines (180 loc) · 6.1 KB
/
timer_test.lua
File metadata and controls
209 lines (180 loc) · 6.1 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
--[[ Copyright (C) 2017-2019 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]
local asserts = require 'testing.asserts'
local helpers = require 'common.helpers'
local test_runner = require 'testing.test_runner'
local Timer = require 'common.timer'
local tests = {}
-- Create and remove a timer multiple times.
function tests.testRemove()
local fired = false
local timer = Timer.new()
for i = 1, 3 do
-- Create.
assert(not timer:exists("testTimer"))
timer:start{
name = "testTimer",
time = 2,
callback = function() fired = true end,
}
assert(not fired)
assert(timer:exists("testTimer"))
-- Remove.
timer:remove("testTimer")
assert(not fired)
assert(not timer:exists("testTimer"))
end
end
-- Set up one timer and verify it fires when time is exactly correct.
function tests.testSingleExact()
local timer = Timer.new()
-- Create timer to fire at second 1.
-- Ensure it doesn't fire immediately.
assert(not timer:exists("testTimer"))
local fired = false
timer:start{
name = "testTimer",
time = 1,
callback = function() fired = true end,
}
assert(not fired)
assert(timer:exists("testTimer"))
asserts.EQ(timer:timeRemaining("testTimer"), 1)
-- Update time to exactly 1 and verify that timer fired.
timer:update(1)
assert(fired)
assert(not timer:exists("testTimer"))
asserts.EQ(timer:timeRemaining("testTimer"), 0)
end
-- Set up one timer and verify it fires when time is exactly correct.
function tests.testSingleOver()
local timer = Timer.new()
-- Create timer to fire at second 1.
assert(not timer:exists("testTimer"))
local fired = false
timer:start{
name = "testTimer",
time = 1,
callback = function() fired = true end,
}
-- Nothing should happen here, since we're < the time for testTimer.
timer:update(0.99999)
assert(not fired)
assert(timer:exists("testTimer"))
asserts.EQ(timer:timeRemaining("testTimer"), 1 - 0.99999)
-- Timer should fire now.
timer:update(1000)
assert(fired)
assert(not timer:exists("testTimer"))
asserts.EQ(timer:timeRemaining("testTimer"), 0)
end
-- Set up one timer and verify it fires over and over again when persisting.
function tests.testPersist()
local timer = Timer.new()
-- Create timer to fire at second 1.
assert(not timer:exists("testTimer"))
local fired = false
local persist = true
timer:start{
name = "testTimer",
time = 1,
callback = function()
fired = true
return persist -- Indicate if this timer should be set again.
end,
}
-- Assert the timer fires but remains active.
timer:update(1)
assert(fired)
assert(timer:exists("testTimer"))
asserts.EQ(timer:timeRemaining("testTimer"), 1)
-- Update some more and assert it fires again.
fired = false
timer:update(2.0001)
assert(fired)
-- Update not quite enough and assert it doesn't fire.
fired = false
timer:update(2.9999)
assert(not fired)
-- Update again, this time requesting it not to persist.
persist = false
timer:update(3)
assert(fired)
assert(not timer:exists("testTimer"))
end
-- `timerTimes` (array) times that timers are fired off at.
-- `updateTimes` (array) times used in call to timer:update(). Must be
-- increasing.
local function testMultiple(kwargs)
local timer = Timer.new()
-- Create timers.
local fired = {}
for i = 1, #kwargs.timerTimes do
fired[i] = false
timer:start{
name = "testTimer" .. tostring(i),
time = kwargs.timerTimes[i],
callback = function() fired[i] = true end,
}
end
-- Repeatedly call timer:update() and verify that the appropriate timers
-- have been fired.
for j = 1, #kwargs.updateTimes do
local currentTime = kwargs.updateTimes[j]
timer:update(currentTime)
for i = 1, #kwargs.timerTimes do
local timerTime = kwargs.timerTimes[i]
local shouldBeComplete = timerTime <= currentTime
asserts.EQ(fired[i], shouldBeComplete)
asserts.EQ(timer:exists("testTimer" .. tostring(i)), not shouldBeComplete)
end
end
end
-- Set up multiple timers that complete in the order they're created.
function tests.testMultipleInOrder()
testMultiple{
timerTimes = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
updateTimes = {0, 1, 3, 3.1, 5, 8.1, 11},
}
end
-- Set up multiple timers that complete in the reverse order they're created.
function tests.testMultipleReverseOrder()
testMultiple{
timerTimes = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
updateTimes = {0, 1, 3, 3.1, 5, 8.1, 11},
}
end
-- Set up multiple timers that complete in a mixed up order.
function tests.testMultipleMixedOrder()
testMultiple{
timerTimes = {4.2, 3.3, 8, 2, 2, 3.1, 5, 8, 4.2, 10},
updateTimes = {0, 1, 3, 3.1, 5, 8.1, 11},
}
end
-- Test formatting.
function tests.testSecondsToString()
asserts.EQ(helpers.secondsToTimeString(0), "0")
asserts.EQ(helpers.secondsToTimeString(3), "3")
asserts.EQ(helpers.secondsToTimeString(21), "21")
asserts.EQ(helpers.secondsToTimeString(59), "59")
asserts.EQ(helpers.secondsToTimeString(60), "1:00")
asserts.EQ(helpers.secondsToTimeString(61), "1:01")
asserts.EQ(helpers.secondsToTimeString(61.1), "1:02")
asserts.EQ(helpers.secondsToTimeString(60 * 2), "2:00")
asserts.EQ(helpers.secondsToTimeString(60 * 60), "1:00:00")
asserts.EQ(helpers.secondsToTimeString(60 * 60 + 1), "1:00:01")
asserts.EQ(helpers.secondsToTimeString(60 * 60 + 61), "1:01:01")
asserts.EQ(helpers.secondsToTimeString(100 * 60 * 60), "100:00:00")
end
return test_runner.run(tests)