1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Tests for include/tst_timer.h
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include "tst_test.h"
11f08c3bdfSopenharmony_ci#include "tst_timer.h"
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#define VAL_MS 1001
14f08c3bdfSopenharmony_ci#define VAL_US 1001000
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic void test_diff(enum tst_ts_type type)
17f08c3bdfSopenharmony_ci{
18f08c3bdfSopenharmony_ci	struct tst_ts ts1, ts2;
19f08c3bdfSopenharmony_ci	long long diff;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci	ts1 = tst_ts_from_ms(type, VAL_MS);
22f08c3bdfSopenharmony_ci	ts2 = tst_ts_from_us(type, VAL_US);
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	diff = tst_ts_diff_ns(ts1, ts2);
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	if (diff == 0)
27f08c3bdfSopenharmony_ci		tst_res(TPASS, "ns_diff = 0");
28f08c3bdfSopenharmony_ci	else
29f08c3bdfSopenharmony_ci		tst_res(TFAIL, "ns_diff = %lli", diff);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	diff = tst_ts_diff_ns(ts1, ts2);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	if (diff == 0)
34f08c3bdfSopenharmony_ci		tst_res(TPASS, "us_diff = 0");
35f08c3bdfSopenharmony_ci	else
36f08c3bdfSopenharmony_ci		tst_res(TFAIL, "us_diff = %lli", diff);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	diff = tst_ts_diff_ms(ts1, ts2);
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (diff == 0)
41f08c3bdfSopenharmony_ci		tst_res(TPASS, "ms_diff = 0");
42f08c3bdfSopenharmony_ci	else
43f08c3bdfSopenharmony_ci		tst_res(TFAIL, "ms_diff = %lli", diff);
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void test_lt(enum tst_ts_type type)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	struct tst_ts ts1, ts2;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	ts1 = tst_ts_from_ms(type, VAL_MS);
51f08c3bdfSopenharmony_ci	ts2 = tst_ts_from_us(type, VAL_US + 1);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	if (tst_ts_lt(ts1, ts2))
54f08c3bdfSopenharmony_ci		tst_res(TPASS, "ts1 < ts2");
55f08c3bdfSopenharmony_ci	else
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "ts1 >= ts2");
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	ts1 = tst_ts_add_us(ts1, 1);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	if (tst_ts_lt(ts1, ts2))
61f08c3bdfSopenharmony_ci		tst_res(TFAIL, "ts1 < ts2");
62f08c3bdfSopenharmony_ci	else
63f08c3bdfSopenharmony_ci		tst_res(TPASS, "ts1 >= ts2");
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	ts1 = tst_ts_add_us(ts1, 1);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (tst_ts_lt(ts1, ts2))
68f08c3bdfSopenharmony_ci		tst_res(TFAIL, "ts1 < ts2");
69f08c3bdfSopenharmony_ci	else
70f08c3bdfSopenharmony_ci		tst_res(TPASS, "ts1 >= ts2");
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic void test_add_sub(enum tst_ts_type type)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	struct tst_ts ts;
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	ts = tst_ts_from_ns(type, 999999000);
78f08c3bdfSopenharmony_ci	ts = tst_ts_add_us(ts, 1);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	long long sec = tst_ts_get_sec(ts);
81f08c3bdfSopenharmony_ci	long long nsec = tst_ts_get_nsec(ts);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	/* Check that result was normalized */
84f08c3bdfSopenharmony_ci	if (sec != 1 || nsec != 0)
85f08c3bdfSopenharmony_ci		tst_res(TFAIL, "sec = %lli, nsec = %lli", sec, nsec);
86f08c3bdfSopenharmony_ci	else
87f08c3bdfSopenharmony_ci		tst_res(TPASS, "sec = %lli, nsec = %lli", sec, nsec);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	ts = tst_ts_from_ms(type, 1000);
90f08c3bdfSopenharmony_ci	ts = tst_ts_sub_us(ts, 1);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	sec = tst_ts_get_sec(ts);
93f08c3bdfSopenharmony_ci	nsec = tst_ts_get_nsec(ts);
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	/* Check that result was normalized */
96f08c3bdfSopenharmony_ci	if (sec != 0 || nsec != 999999000)
97f08c3bdfSopenharmony_ci		tst_res(TFAIL, "sec = %lli, nsec = %lli", sec, nsec);
98f08c3bdfSopenharmony_ci	else
99f08c3bdfSopenharmony_ci		tst_res(TPASS, "sec = %lli, nsec = %lli", sec, nsec);
100f08c3bdfSopenharmony_ci}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_cistatic void do_test(unsigned int n)
103f08c3bdfSopenharmony_ci{
104f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing with type = %i", n);
105f08c3bdfSopenharmony_ci	test_diff(n);
106f08c3bdfSopenharmony_ci	test_lt(n);
107f08c3bdfSopenharmony_ci	test_add_sub(n);
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cistatic struct tst_test test = {
111f08c3bdfSopenharmony_ci	.test = do_test,
112f08c3bdfSopenharmony_ci	.tcnt = 3,
113f08c3bdfSopenharmony_ci};
114