aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2018-01-15 11:46:26 +0100
committerPau Espin Pedrol <pespin@sysmocom.de>2018-01-15 11:49:10 +0100
commit28ce315a3225188211c2ed52844716f88093035e (patch)
tree5b2305e5709a46267b50f3ec5f9f23ae5f5c7eef
parent10d76b6863cb2168cc5607fd70b191216acc851c (diff)
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
-rw-r--r--tests/CommonLibs/TimevalTest.cpp24
1 files 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 <iostream>
#include <assert.h>
+#include <sys/time.h>
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++;
}