From 28ce315a3225188211c2ed52844716f88093035e Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Mon, 15 Jan 2018 11:46:26 +0100 Subject: tests: TimevalTest: refactor and avoid double comparison Before this patch, the experession assert(then_secondws==then.seconds()) was failing in x86 architecture (and passing when adding a fprintf to debug it). Avoid comparing the double values with == as that's usually a bad idea, since the processor can output slightly different results for the same operation depending on how it is optimized. Use timespec() instead to check the invariant. Take the chance to refactor some variables around to make the test easier to read. Change-Id: Id4324be8ece86d371b1acb46bbd97856dfed241d --- tests/CommonLibs/TimevalTest.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/CommonLibs/TimevalTest.cpp b/tests/CommonLibs/TimevalTest.cpp index f579878..61a92c9 100644 --- a/tests/CommonLibs/TimevalTest.cpp +++ b/tests/CommonLibs/TimevalTest.cpp @@ -29,24 +29,30 @@ #include "Timeval.h" #include #include +#include using namespace std; int main(int argc, char *argv[]) { - Timeval then(10000); - assert(then.elapsed() == -10000); - cerr << then << " elapsed: " << then.elapsed() << endl; - double then_seconds = then.seconds(); - double last_now = Timeval().seconds(); long last_remaining = 10000; + Timeval then(last_remaining); + assert(then.elapsed() == -last_remaining); + cerr << then << " elapsed: " << then.elapsed() << endl; + + /* Check that last_remaining parameter affects setting time in the future */ + usleep(10000); + double increased_time_secs = Timeval().seconds(); + assert(increased_time_secs <= then.seconds()); + + struct timespec invariant_time = then.timespec(); int loops = 0; while (!then.passed()) { - double tnow = Timeval().seconds(); - cerr << "now: " << tnow << " then: " << then << " remaining: " << then.remaining() << endl; - assert(last_now <= tnow && last_remaining >= then.remaining()); - assert(then_seconds == then.seconds()); + struct timespec tspecnow = then.timespec(); + cerr << "now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl; + assert(last_remaining >= then.remaining()); + assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec); usleep(500000); loops++; } -- cgit v1.2.3