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
This commit is contained in:
Pau Espin 2018-01-15 11:46:26 +01:00
parent 10d76b6863
commit 28ce315a32
1 changed files with 15 additions and 9 deletions

View File

@ -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++;
}