[fix] TimeVal: add dbg:guard against incorrect subtraction order (#4669)

In principle, any negative subtraction result should be caused by a logical error.
This commit is contained in:
Frans de Jonge
2019-02-27 22:20:47 +01:00
committed by GitHub
parent 163853afdf
commit abba7ba873
3 changed files with 42 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ A simple module to module to compare and do arithmetic with time values.
local tv_duration_seconds_float = tv_duration.sec + tv_duration.usec/1000000
]]
local dbg = require("dbg")
local util = require("ffi/util")
--[[--
@@ -110,6 +111,12 @@ function TimeVal:__sub(time_b)
return diff
end
dbg:guard(TimeVal, '__sub',
function(self, time_b)
assert(self.sec > time_b.sec or (self.sec == time_b.sec and self.usec >= time_b.usec),
"Subtract the first timeval from the latest, not vice versa.")
end)
function TimeVal:__add(time_b)
local sum = TimeVal:new{}