1 /*
2 * Copyright (c) 2023 BayLibre SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9
ZTEST(timepoints,test_timepoint_api)10 ZTEST(timepoints, test_timepoint_api)
11 {
12 k_timepoint_t timepoint;
13 k_timeout_t timeout, remaining;
14
15 timeout = K_NO_WAIT;
16 timepoint = sys_timepoint_calc(timeout);
17 zassert_true(sys_timepoint_expired(timepoint));
18 remaining = sys_timepoint_timeout(timepoint);
19 zassert_true(K_TIMEOUT_EQ(remaining, K_NO_WAIT));
20
21 timeout = K_FOREVER;
22 timepoint = sys_timepoint_calc(timeout);
23 zassert_false(sys_timepoint_expired(timepoint));
24 remaining = sys_timepoint_timeout(timepoint);
25 zassert_true(K_TIMEOUT_EQ(remaining, K_FOREVER));
26
27 timeout = K_SECONDS(1);
28 timepoint = sys_timepoint_calc(timeout);
29 zassert_false(sys_timepoint_expired(timepoint));
30 remaining = sys_timepoint_timeout(timepoint);
31 zassert_true(remaining.ticks <= timeout.ticks && remaining.ticks != 0);
32 k_sleep(K_MSEC(1100));
33 zassert_true(sys_timepoint_expired(timepoint));
34 remaining = sys_timepoint_timeout(timepoint);
35 zassert_true(K_TIMEOUT_EQ(remaining, K_NO_WAIT));
36 }
37
ZTEST(timepoints,test_comparison)38 ZTEST(timepoints, test_comparison)
39 {
40 k_timepoint_t a, b;
41
42 a = sys_timepoint_calc(K_NO_WAIT);
43 b = a;
44 zassert_true(sys_timepoint_cmp(a, b) == 0);
45 zassert_true(sys_timepoint_cmp(b, a) == 0);
46
47 a = sys_timepoint_calc(K_FOREVER);
48 b = a;
49 zassert_true(sys_timepoint_cmp(a, b) == 0);
50 zassert_true(sys_timepoint_cmp(b, a) == 0);
51
52 a = sys_timepoint_calc(K_NO_WAIT);
53 b = sys_timepoint_calc(K_MSEC(1));
54 zassert_true(sys_timepoint_cmp(a, b) < 0);
55 zassert_true(sys_timepoint_cmp(b, a) > 0);
56
57 a = sys_timepoint_calc(K_MSEC(1));
58 b = sys_timepoint_calc(K_FOREVER);
59 zassert_true(sys_timepoint_cmp(a, b) < 0);
60 zassert_true(sys_timepoint_cmp(b, a) > 0);
61
62 a = sys_timepoint_calc(K_MSEC(1));
63 b = a;
64 zassert_true(sys_timepoint_cmp(a, b) == 0);
65 zassert_true(sys_timepoint_cmp(b, a) == 0);
66
67 a = sys_timepoint_calc(K_MSEC(100));
68 b = sys_timepoint_calc(K_MSEC(200));
69 zassert_true(sys_timepoint_cmp(a, b) < 0);
70 zassert_true(sys_timepoint_cmp(b, a) > 0);
71
72 a = sys_timepoint_calc(K_NO_WAIT);
73 b = sys_timepoint_calc(K_FOREVER);
74 zassert_true(sys_timepoint_cmp(a, b) < 0);
75 zassert_true(sys_timepoint_cmp(b, a) > 0);
76 }
77
78 ZTEST_SUITE(timepoints, NULL, NULL, NULL, NULL, NULL);
79