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