From: Dmitry Eremin-Solenikov <dmitry.ereminsoleni...@linaro.org>

ODP test for time API uses clock() to compare time against. However
clock() returns processor time used by program, which can differ between
runs. Use gettimeofday() as a time source to compare against.

Signed-off-by: Dmitry Eremin-Solenikov <dmitry.ereminsoleni...@linaro.org>
---
/** Email created from pull request 377 (lumag:misc-fixes)
 ** https://github.com/Linaro/odp/pull/377
 ** Patch: https://github.com/Linaro/odp/pull/377.patch
 ** Base sha: 49ebafae0edebbc750742d8874ad0a7588286dea
 ** Merge commit sha: 3c1b82834c36bc66d397aedd2f80da45160eae2d
 **/
 test/validation/api/time/time.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/test/validation/api/time/time.c b/test/validation/api/time/time.c
index c8f90dbac..ccbc7e3ba 100644
--- a/test/validation/api/time/time.c
+++ b/test/validation/api/time/time.c
@@ -9,7 +9,7 @@
 #include <odp_api.h>
 #include "odp_cunit_common.h"
 #include "time_test.h"
-#include <time.h>
+#include <sys/time.h>
 
 #define BUSY_LOOP_CNT          30000000    /* used for t > min resolution */
 #define BUSY_LOOP_CNT_LONG     6000000000  /* used for t > 4 sec */
@@ -423,11 +423,12 @@ static void time_test_accuracy(time_cb time_cur, 
time_from_ns_cb time_from_ns)
 {
        int i;
        odp_time_t t1, t2, wait, diff;
-       clock_t c1, c2;
+       struct timeval tv1, tv2, tvdiff;
        double sec_t, sec_c;
        odp_time_t sec = time_from_ns(ODP_TIME_SEC_IN_NS);
 
-       c1 = clock();
+       i = gettimeofday(&tv1, NULL);
+       CU_ASSERT(i == 0);
        t1 = time_cur();
 
        wait = odp_time_sum(t1, sec);
@@ -436,12 +437,21 @@ static void time_test_accuracy(time_cb time_cur, 
time_from_ns_cb time_from_ns)
                wait = odp_time_sum(wait, sec);
        }
 
+       i = gettimeofday(&tv2, NULL);
+       CU_ASSERT(i == 0);
        t2 = time_cur();
-       c2 = clock();
+
+       if (tv2.tv_usec < tv1.tv_usec) {
+               tvdiff.tv_usec = 1000000 + tv2.tv_usec - tv1.tv_usec;
+               tvdiff.tv_sec = tv2.tv_sec - 1 - tv1.tv_sec;
+       } else {
+               tvdiff.tv_usec = tv2.tv_usec - tv1.tv_usec;
+               tvdiff.tv_sec = tv2.tv_sec - tv1.tv_sec;
+       }
 
        diff  = odp_time_diff(t2, t1);
        sec_t = ((double)odp_time_to_ns(diff)) / ODP_TIME_SEC_IN_NS;
-       sec_c = ((double)(c2 - c1)) / CLOCKS_PER_SEC;
+       sec_c = ((double)(tvdiff.tv_usec) / 1000000) + tvdiff.tv_sec;
 
        /* Check that ODP time is within +-5% of system time */
        CU_ASSERT(sec_t < sec_c * 1.05);

Reply via email to