aboutsummaryrefslogtreecommitdiff
path: root/spec/01-time_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/01-time_spec.lua')
-rw-r--r--spec/01-time_spec.lua76
1 files changed, 76 insertions, 0 deletions
diff --git a/spec/01-time_spec.lua b/spec/01-time_spec.lua
new file mode 100644
index 0000000..1607cca
--- /dev/null
+++ b/spec/01-time_spec.lua
@@ -0,0 +1,76 @@
1local system = require 'system.core'
2
3describe('Test time functions', function()
4
5 -- returns the new second, on the new second
6 local function wait_for_second_rollover()
7 local start_time = math.floor(os.time())
8 local end_time = math.floor(os.time())
9 while end_time == start_time do
10 end_time = math.floor(os.time())
11 end
12 return end_time
13 end
14
15
16 describe("time()", function()
17
18 it('returns current time', function()
19 local expected_time = wait_for_second_rollover()
20 local received_time = system.gettime()
21 assert.is.near(expected_time, received_time, 0.02)
22
23 wait_for_second_rollover()
24 assert.is.near(1, system.gettime() - received_time, 0.02)
25 end)
26
27 end)
28
29
30
31 describe("monotime()", function()
32
33 it('returns monotonically increasing time', function()
34 local starttime = system.monotime()
35 local endtime = system.monotime()
36 local delta = endtime - starttime
37 assert.is_true(starttime > 0)
38 assert.is_true(delta >= 0)
39 assert.is_true(system.monotime() - endtime >= 0)
40 end)
41
42 end)
43
44
45
46 describe("sleep()", function()
47
48 it("should sleep for the specified time", function()
49 local start_time = system.gettime()
50 system.sleep(1, 1)
51 local end_time = system.gettime()
52 local elapsed_time = end_time - start_time
53 assert.is.near(elapsed_time, 1, 0.2) -- large marging of error due to CI priorities
54 end)
55
56
57 it("should sleep for the specified time; fractional", function()
58 local start_time = system.gettime()
59 system.sleep(0.5, 1)
60 local end_time = system.gettime()
61 local elapsed_time = end_time - start_time
62 assert.is.near(0.5, elapsed_time, 0.2) -- large marging of error due to CI priorities
63 end)
64
65
66 it("should return immediately for a non-positive sleep time", function()
67 local start_time = system.gettime()
68 system.sleep(-1)
69 local end_time = system.gettime()
70 local elapsed_time = end_time - start_time
71 assert.is.near(elapsed_time, 0, 0.01)
72 end)
73
74 end)
75
76end)