diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-09 23:03:21 +0100 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-15 19:17:57 +0100 |
| commit | d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2 (patch) | |
| tree | 2d4f86ec87eb87a77f6663924aaaa9286756ce3e /spec/01-time_spec.lua | |
| parent | d4222ce6da2a2d7179fc79f9d0cc65fd6c09a686 (diff) | |
| download | luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.gz luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.bz2 luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.zip | |
feat(*): add environment variable and random functions
Diffstat (limited to 'spec/01-time_spec.lua')
| -rw-r--r-- | spec/01-time_spec.lua | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/01-time_spec.lua b/spec/01-time_spec.lua new file mode 100644 index 0000000..80faa75 --- /dev/null +++ b/spec/01-time_spec.lua | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | local system = require 'system.core' | ||
| 2 | |||
| 3 | describe('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 | wait_for_second_rollover() | ||
| 20 | local expected_time = wait_for_second_rollover() | ||
| 21 | local received_time = system.gettime() | ||
| 22 | assert.is.near(expected_time, received_time, 0.02) | ||
| 23 | |||
| 24 | wait_for_second_rollover() | ||
| 25 | assert.is.near(1, system.gettime() - received_time, 0.02) | ||
| 26 | end) | ||
| 27 | |||
| 28 | end) | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | describe("monotime()", function() | ||
| 33 | |||
| 34 | it('returns monotonically increasing time', function() | ||
| 35 | local starttime = system.monotime() | ||
| 36 | local endtime = system.monotime() | ||
| 37 | local delta = endtime - starttime | ||
| 38 | assert.is_true(starttime > 0) | ||
| 39 | assert.is_true(delta >= 0) | ||
| 40 | assert.is_true(system.monotime() - endtime >= 0) | ||
| 41 | end) | ||
| 42 | |||
| 43 | end) | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | describe("sleep()", function() | ||
| 48 | |||
| 49 | it("should sleep for the specified time", function() | ||
| 50 | local start_time = system.gettime() | ||
| 51 | system.sleep(1, 1) | ||
| 52 | local end_time = system.gettime() | ||
| 53 | local elapsed_time = end_time - start_time | ||
| 54 | assert.is.near(elapsed_time, 1, 0.01) | ||
| 55 | end) | ||
| 56 | |||
| 57 | |||
| 58 | it("should sleep for the specified time; fractional", function() | ||
| 59 | local start_time = system.gettime() | ||
| 60 | system.sleep(0.5, 1) | ||
| 61 | local end_time = system.gettime() | ||
| 62 | local elapsed_time = end_time - start_time | ||
| 63 | assert.is.near(0.5, elapsed_time, 0.01) | ||
| 64 | end) | ||
| 65 | |||
| 66 | |||
| 67 | it("should return immediately for a non-positive sleep time", function() | ||
| 68 | local start_time = system.gettime() | ||
| 69 | system.sleep(-1) | ||
| 70 | local end_time = system.gettime() | ||
| 71 | local elapsed_time = end_time - start_time | ||
| 72 | assert.is.near(elapsed_time, 0, 0.01) | ||
| 73 | end) | ||
| 74 | |||
| 75 | end) | ||
| 76 | |||
| 77 | end) | ||
