2019-02-27 21:20:47 +00:00
|
|
|
describe("TimeVal module", function()
|
|
|
|
local TimeVal, dbg, dbg_on
|
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
TimeVal = require("ui/timeval")
|
|
|
|
dbg = require("dbg")
|
|
|
|
dbg_on = dbg.is_on
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
if dbg_on then
|
|
|
|
dbg:turnOn()
|
|
|
|
else
|
|
|
|
dbg:turnOff()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-02-28 14:01:13 +00:00
|
|
|
it("should add", function()
|
|
|
|
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
|
|
|
|
local timev2 = TimeVal:new{ sec = 10, usec = 6000}
|
2019-02-28 16:31:39 +00:00
|
|
|
local timev3 = TimeVal:new{ sec = 10, usec = 50000000}
|
2019-02-28 14:01:13 +00:00
|
|
|
|
|
|
|
assert.is.same({sec = 15,usec = 11000}, timev1 + timev2)
|
2019-02-28 16:31:39 +00:00
|
|
|
assert.is.same({sec = 65,usec = 5000}, timev1 + timev3)
|
2019-02-28 14:01:13 +00:00
|
|
|
end)
|
|
|
|
|
2019-02-27 21:20:47 +00:00
|
|
|
it("should subtract", function()
|
|
|
|
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
|
|
|
|
local timev2 = TimeVal:new{ sec = 10, usec = 6000}
|
|
|
|
|
|
|
|
assert.is.same({sec = 5,usec = 1000}, timev2 - timev1)
|
|
|
|
assert.is.same({sec = -5,usec = -1000}, timev1 - timev2)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should guard against reverse subtraction logic", function()
|
|
|
|
dbg:turnOn()
|
|
|
|
TimeVal = package.reload("ui/timeval")
|
|
|
|
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
|
|
|
|
local timev2 = TimeVal:new{ sec = 10, usec = 5000}
|
|
|
|
|
|
|
|
assert.has.errors(function() return timev1 - timev2 end)
|
|
|
|
end)
|
2019-02-28 14:01:13 +00:00
|
|
|
|
|
|
|
it("should derive sec and usec from more than 1 sec worth of usec", function()
|
|
|
|
local timev1 = TimeVal:new{ sec = 5, usec = 5000000}
|
|
|
|
|
|
|
|
assert.is.same({sec = 10,usec = 0}, timev1)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should compare", function()
|
|
|
|
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
|
|
|
|
local timev2 = TimeVal:new{ sec = 10, usec = 6000}
|
|
|
|
local timev3 = TimeVal:new{ sec = 5, usec = 5000}
|
2019-02-28 16:31:39 +00:00
|
|
|
local timev4 = TimeVal:new{ sec = 5, usec = 6000}
|
2019-02-28 14:01:13 +00:00
|
|
|
|
|
|
|
assert.is_true(timev2 > timev1)
|
2019-02-28 16:31:39 +00:00
|
|
|
assert.is_false(timev2 < timev1)
|
2019-02-28 14:01:13 +00:00
|
|
|
assert.is_true(timev2 >= timev1)
|
|
|
|
|
2019-02-28 16:31:39 +00:00
|
|
|
assert.is_true(timev4 > timev1)
|
|
|
|
assert.is_false(timev4 < timev1)
|
|
|
|
assert.is_true(timev4 >= timev1)
|
|
|
|
|
2019-02-28 14:01:13 +00:00
|
|
|
assert.is_true(timev1 < timev2)
|
2019-02-28 16:31:39 +00:00
|
|
|
assert.is_false(timev1 > timev2)
|
2019-02-28 14:01:13 +00:00
|
|
|
assert.is_true(timev1 <= timev2)
|
|
|
|
|
|
|
|
assert.is_true(timev1 == timev3)
|
2019-02-28 16:31:39 +00:00
|
|
|
assert.is_false(timev1 == timev2)
|
2019-02-28 14:01:13 +00:00
|
|
|
assert.is_true(timev1 >= timev3)
|
|
|
|
assert.is_true(timev1 <= timev3)
|
|
|
|
end)
|
2019-02-27 21:20:47 +00:00
|
|
|
end)
|