aboutsummaryrefslogtreecommitdiff
path: root/test/validation/time/time.c
blob: 0aac599b63f52ea2c2ef0a5c1e1c5bc115e511af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <odp.h>
#include "odp_cunit_common.h"
#include "time.h"

#define TOLERANCE 1
#define BUSY_LOOP_CNT 100

/* check that a cycles difference gives a reasonable result */
static void time_test_odp_cycles_diff(void)
{
	/* volatile to stop optimization of busy loop */
	volatile int count = 0;
	uint64_t diff, cycles1, cycles2;

	cycles1 = odp_time_cycles();

	while (count < BUSY_LOOP_CNT) {
		count++;
	};

	cycles2 = odp_time_cycles();
	CU_ASSERT(cycles2 > cycles1);

	diff = odp_time_diff_cycles(cycles1, cycles2);
	CU_ASSERT(diff > 0);
}

/* check that a negative cycles difference gives a reasonable result */
static void time_test_odp_cycles_negative_diff(void)
{
	uint64_t diff, cycles1, cycles2;

	cycles1 = 10;
	cycles2 = 5;
	diff = odp_time_diff_cycles(cycles1, cycles2);
	CU_ASSERT(diff > 0);
}

/* check that related conversions come back to the same value */
static void time_test_odp_time_conversion(void)
{
	uint64_t ns1, ns2, cycles;
	uint64_t upper_limit, lower_limit;

	ns1 = 100;
	cycles = odp_time_ns_to_cycles(ns1);
	CU_ASSERT(cycles > 0);

	ns2 = odp_time_cycles_to_ns(cycles);

	/* 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));
}

CU_TestInfo time_suite_time[] = {
	{"cycles diff", time_test_odp_cycles_diff},
	{"negative diff", time_test_odp_cycles_negative_diff},
	{"conversion", time_test_odp_time_conversion},
	 CU_TEST_INFO_NULL
};

static CU_SuiteInfo time_suites[] = {
		{"Time", NULL, NULL, NULL, NULL, time_suite_time},
		 CU_SUITE_INFO_NULL
};

int time_main(void)
{
	return odp_cunit_run(time_suites);
}