[MERGED] osmo-trx[master]: tests: TimevalTest: refactor and avoid double comparison

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Mon Jan 15 11:17:22 UTC 2018


Pau Espin Pedrol has submitted this change and it was merged.

Change subject: tests: TimevalTest: refactor and avoid double comparison
......................................................................


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
---
M tests/CommonLibs/TimevalTest.cpp
1 file changed, 15 insertions(+), 9 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



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

-- 
To view, visit https://gerrit.osmocom.org/5787
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id4324be8ece86d371b1acb46bbd97856dfed241d
Gerrit-PatchSet: 1
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list