diff mbox

[API-NEXT,v7,7/9] validation: time: align tests with current time API

Message ID 1449251494-23210-8-git-send-email-ivan.khoronzhuk@linaro.org
State Accepted
Commit 1b025f2dd7dd82a329b200d385c41435e4c6ddf5
Headers show

Commit Message

Ivan Khoronzhuk Dec. 4, 2015, 5:51 p.m. UTC
Add test for odp_time_sum, odp_time_cmp, odp_time_to_u64 APIs.
Sophisticate a little tests for odp_time_diff, odp_time_local_from_ns,
odp_time_local_to_ns APIs. Check time on monotony.

Reviewed-by: Petri Savolainen <petri.savolainen@nokia.com>
Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
---
 test/validation/time/time.c | 228 +++++++++++++++++++++++++++++++++++++++-----
 test/validation/time/time.h |   7 +-
 2 files changed, 210 insertions(+), 25 deletions(-)
diff mbox

Patch

diff --git a/test/validation/time/time.c b/test/validation/time/time.c
index 3d05a6f..0303eb3 100644
--- a/test/validation/time/time.c
+++ b/test/validation/time/time.c
@@ -9,7 +9,107 @@ 
 #include "time.h"
 
 #define TOLERANCE 1
-#define BUSY_LOOP_CNT 100
+#define BUSY_LOOP_CNT		30000000    /* used for t > min resolution */
+#define BUSY_LOOP_CNT_LONG	12000000000 /* used for t > 4 sec */
+
+
+/* check that related conversions come back to the same value */
+void time_test_odp_conversion(void)
+{
+	uint64_t ns1, ns2;
+	odp_time_t time;
+	uint64_t upper_limit, lower_limit;
+
+	ns1 = 100;
+	time = odp_time_local_from_ns(ns1);
+
+	ns2 = odp_time_to_ns(time);
+
+	/* need to check within arithmetic tolerance that the same
+	 * value in ns is returned after conversions */
+	upper_limit = ns1 + TOLERANCE;
+	lower_limit = ns1 - TOLERANCE;
+	CU_ASSERT((ns2 <= upper_limit) && (ns2 >= lower_limit));
+
+	ns1 = 60 * 11 * ODP_TIME_SEC_IN_NS;
+	time = odp_time_local_from_ns(ns1);
+
+	ns2 = odp_time_to_ns(time);
+
+	/* need to check within arithmetic tolerance that the same
+	 * value in ns is returned after conversions */
+	upper_limit = ns1 + TOLERANCE;
+	lower_limit = ns1 - TOLERANCE;
+	CU_ASSERT((ns2 <= upper_limit) && (ns2 >= lower_limit));
+}
+
+void time_test_monotony(void)
+{
+	volatile unsigned long count = 0;
+	odp_time_t t1, t2, t3;
+	uint64_t ns1, ns2, ns3;
+
+	t1 = odp_time_local();
+
+	while (count < BUSY_LOOP_CNT) {
+		count++;
+	};
+
+	t2 = odp_time_local();
+
+	while (count < BUSY_LOOP_CNT_LONG) {
+		count++;
+	};
+
+	t3 = odp_time_local();
+
+	ns1 = odp_time_to_ns(t1);
+	ns2 = odp_time_to_ns(t2);
+	ns3 = odp_time_to_ns(t3);
+
+	CU_ASSERT(ns2 > ns1);
+	CU_ASSERT(ns3 > ns2);
+}
+
+void time_test_odp_cmp(void)
+{
+	/* volatile to stop optimization of busy loop */
+	volatile int count = 0;
+	odp_time_t t1, t2, t3;
+
+	t1 = odp_time_local();
+
+	while (count < BUSY_LOOP_CNT) {
+		count++;
+	};
+
+	t2 = odp_time_local();
+
+	while (count < BUSY_LOOP_CNT * 2) {
+		count++;
+	};
+
+	t3 = odp_time_local();
+
+	CU_ASSERT(odp_time_cmp(t2, t1) > 0);
+	CU_ASSERT(odp_time_cmp(t3, t2) > 0);
+	CU_ASSERT(odp_time_cmp(t3, t1) > 0);
+	CU_ASSERT(odp_time_cmp(t1, t2) < 0);
+	CU_ASSERT(odp_time_cmp(t2, t3) < 0);
+	CU_ASSERT(odp_time_cmp(t1, t3) < 0);
+	CU_ASSERT(odp_time_cmp(t1, t1) == 0);
+	CU_ASSERT(odp_time_cmp(t2, t2) == 0);
+	CU_ASSERT(odp_time_cmp(t3, t3) == 0);
+
+	t2 = odp_time_local_from_ns(60 * 10 * ODP_TIME_SEC_IN_NS);
+	t1 = odp_time_local_from_ns(3);
+
+	CU_ASSERT(odp_time_cmp(t2, t1) > 0);
+	CU_ASSERT(odp_time_cmp(t1, t2) < 0);
+
+	t1 = odp_time_local_from_ns(0);
+	CU_ASSERT(odp_time_cmp(t1, ODP_TIME_NULL) == 0);
+}
 
 /* check that a time difference gives a reasonable result */
 void time_test_odp_diff(void)
@@ -17,7 +117,10 @@  void time_test_odp_diff(void)
 	/* volatile to stop optimization of busy loop */
 	volatile int count = 0;
 	odp_time_t diff, t1, t2;
+	uint64_t nsdiff, ns1, ns2, ns;
+	uint64_t upper_limit, lower_limit;
 
+	/* test timestamp diff */
 	t1 = odp_time_local();
 
 	while (count < BUSY_LOOP_CNT) {
@@ -29,43 +132,122 @@  void time_test_odp_diff(void)
 
 	diff = odp_time_diff(t2, t1);
 	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);
-}
 
-/* check that a negative time difference gives a reasonable result */
-void time_test_odp_negative_diff(void)
-{
-	odp_time_t diff, t1, t2;
+	ns1 = odp_time_to_ns(t1);
+	ns2 = odp_time_to_ns(t2);
+	ns = ns2 - ns1;
+	nsdiff = odp_time_to_ns(diff);
 
-	t1 = odp_time_local_from_ns(10);
-	t2 = odp_time_local_from_ns(5);
+	upper_limit = ns + TOLERANCE;
+	lower_limit = ns - TOLERANCE;
+	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
+
+	/* test timestamp and interval diff */
+	ns1 = 54;
+	t1 = odp_time_local_from_ns(ns1);
+	ns = ns2 - ns1;
+
+	diff = odp_time_diff(t2, t1);
+	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);
+	nsdiff = odp_time_to_ns(diff);
+
+	upper_limit = ns + TOLERANCE;
+	lower_limit = ns - TOLERANCE;
+	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
+
+	/* test interval diff */
+	ns2 = 60 * 10 * ODP_TIME_SEC_IN_NS;
+	ns = ns2 - ns1;
+
+	t2 = odp_time_local_from_ns(ns2);
 	diff = odp_time_diff(t2, t1);
 	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);
+	nsdiff = odp_time_to_ns(diff);
+
+	upper_limit = ns + TOLERANCE;
+	lower_limit = ns - TOLERANCE;
+	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
+
+	/* same time has to diff to 0 */
+	diff = odp_time_diff(t2, t2);
+	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) == 0);
+
+	diff = odp_time_diff(t2, ODP_TIME_NULL);
+	CU_ASSERT(odp_time_cmp(t2, diff) == 0);
 }
 
-/* check that related conversions come back to the same value */
-void time_test_odp_conversion(void)
+/* check that a time sum gives a reasonable result */
+void time_test_odp_sum(void)
 {
-	uint64_t ns1, ns2;
-	odp_time_t time;
+	odp_time_t sum, t1, t2;
+	uint64_t nssum, ns1, ns2, ns;
 	uint64_t upper_limit, lower_limit;
 
-	ns1 = 100;
-	time = odp_time_local_from_ns(ns1);
-	CU_ASSERT(odp_time_cmp(time, ODP_TIME_NULL) > 0);
+	/* sum timestamp and interval */
+	t1 = odp_time_local();
+	ns2 = 103;
+	t2 = odp_time_local_from_ns(ns2);
+	ns1 = odp_time_to_ns(t1);
+	ns = ns1 + ns2;
 
-	ns2 = odp_time_to_ns(time);
+	sum = odp_time_sum(t2, t1);
+	CU_ASSERT(odp_time_cmp(sum, ODP_TIME_NULL) > 0);
+	nssum = odp_time_to_ns(sum);
 
-	/* need to check within arithmetic tolerance that the same
-	 * value in ns is returned after conversions */
-	upper_limit = ns1 + TOLERANCE;
-	lower_limit = ns1 - TOLERANCE;
-	CU_ASSERT((ns2 <= upper_limit) && (ns2 >= lower_limit));
+	upper_limit = ns + TOLERANCE;
+	lower_limit = ns - TOLERANCE;
+	CU_ASSERT((nssum <= upper_limit) && (nssum >= lower_limit));
+
+	/* sum intervals */
+	ns1 = 60 * 13 * ODP_TIME_SEC_IN_NS;
+	t1 = odp_time_local_from_ns(ns1);
+	ns = ns1 + ns2;
+
+	sum = odp_time_sum(t2, t1);
+	CU_ASSERT(odp_time_cmp(sum, ODP_TIME_NULL) > 0);
+	nssum = odp_time_to_ns(sum);
+
+	upper_limit = ns + TOLERANCE;
+	lower_limit = ns - TOLERANCE;
+	CU_ASSERT((nssum <= upper_limit) && (nssum >= lower_limit));
+
+	/* test on 0 */
+	sum = odp_time_sum(t2, ODP_TIME_NULL);
+	CU_ASSERT(odp_time_cmp(t2, sum) == 0);
+}
+
+void time_test_odp_to_u64(void)
+{
+	volatile int count = 0;
+	uint64_t val1, val2;
+	odp_time_t t1, t2;
+
+	t1 = odp_time_local();
+
+	val1 = odp_time_to_u64(t1);
+	CU_ASSERT(val1 > 0);
+
+	while (count < BUSY_LOOP_CNT) {
+		count++;
+	};
+
+	t2 = odp_time_local();
+	val2 = odp_time_to_u64(t2);
+	CU_ASSERT(val2 > 0);
+
+	CU_ASSERT(val2 > val1);
+
+	val1 = odp_time_to_u64(ODP_TIME_NULL);
+	CU_ASSERT(val1 == 0);
 }
 
 odp_testinfo_t time_suite_time[] = {
-	ODP_TEST_INFO(time_test_odp_diff),
-	ODP_TEST_INFO(time_test_odp_negative_diff),
 	ODP_TEST_INFO(time_test_odp_conversion),
+	ODP_TEST_INFO(time_test_monotony),
+	ODP_TEST_INFO(time_test_odp_cmp),
+	ODP_TEST_INFO(time_test_odp_diff),
+	ODP_TEST_INFO(time_test_odp_sum),
+	ODP_TEST_INFO(time_test_odp_to_u64),
 	ODP_TEST_INFO_NULL
 };
 
diff --git a/test/validation/time/time.h b/test/validation/time/time.h
index b4e613c..b0c67b7 100644
--- a/test/validation/time/time.h
+++ b/test/validation/time/time.h
@@ -10,9 +10,12 @@ 
 #include <odp_cunit_common.h>
 
 /* test functions: */
-void time_test_odp_diff(void);
 void time_test_odp_conversion(void);
-void time_test_odp_negative_diff(void);
+void time_test_monotony(void);
+void time_test_odp_cmp(void);
+void time_test_odp_diff(void);
+void time_test_odp_sum(void);
+void time_test_odp_to_u64(void);
 
 /* test arrays: */
 extern odp_testinfo_t time_suite_time[];