1 /*
2  * Copyright (c) 2022 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/sys/util.h>
9 #include <zephyr/kernel.h>
10 
11 /**
12  * @addtogroup sys_util_tests
13  * @{
14  */
15 
16 /**
17  * @brief Test wait_for works as expected with typical use cases
18  *
19  * @see WAIT_FOR()
20  */
21 
ZTEST(sys_util,test_wait_for)22 ZTEST(sys_util, test_wait_for)
23 {
24 	uint32_t start, end, expected;
25 
26 	zassert_true(WAIT_FOR(true, 0, NULL), "true, no wait, NULL");
27 	zassert_true(WAIT_FOR(true, 0, k_yield()), "true, no wait, yield");
28 	zassert_false(WAIT_FOR(false, 0, k_yield()), "false, no wait, yield");
29 	zassert_true(WAIT_FOR(true, 1, k_yield()), "true, 1usec, yield");
30 	zassert_false(WAIT_FOR(false, 1, k_yield()), "false, 1usec, yield");
31 	zassert_true(WAIT_FOR(true, 1000, k_yield()), "true, 1msec, yield");
32 
33 
34 	expected = 1000*(sys_clock_hw_cycles_per_sec()/USEC_PER_SEC);
35 	start = k_cycle_get_32();
36 	zassert_false(WAIT_FOR(false, 1000, k_yield()), "true, 1msec, yield");
37 	end = k_cycle_get_32();
38 	zassert_true(end-start >= expected, "wait for 1ms");
39 }
40 
41 /**
42  * @brief Test NUM_VA_ARGS works as expected with typical use cases
43  *
44  * @see NUM_VA_ARGS()
45  */
46 
ZTEST(sys_util,test_NUM_VA_ARGS)47 ZTEST(sys_util, test_NUM_VA_ARGS)
48 {
49 	zassert_equal(0, NUM_VA_ARGS());
50 	zassert_equal(1, NUM_VA_ARGS(_1));
51 	zassert_equal(2, NUM_VA_ARGS(_1, _2));
52 	/* support up to 63 args */
53 	zassert_equal(63, NUM_VA_ARGS(LISTIFY(63, ~, (,))));
54 }
55 
56 /**
57  * @brief Test NUM_VA_ARGS_LESS_1 works as expected with typical use cases
58  *
59  * @see NUM_VA_ARGS_LESS_1()
60  */
61 
ZTEST(sys_util,test_NUM_VA_ARGS_LESS_1)62 ZTEST(sys_util, test_NUM_VA_ARGS_LESS_1)
63 {
64 	zassert_equal(0, NUM_VA_ARGS_LESS_1());
65 	zassert_equal(0, NUM_VA_ARGS_LESS_1(_1));
66 	zassert_equal(1, NUM_VA_ARGS_LESS_1(_1, _2));
67 	/* support up to 64 args */
68 	zassert_equal(63, NUM_VA_ARGS_LESS_1(LISTIFY(64, ~, (,))));
69 }
70 /**
71  * @}
72  */
73 
74 
75 /**
76  * @defgroup sys_util_tests Sys Util Tests
77  * @ingroup all_tests
78  * @{
79  * @}
80  */
81 
82 ZTEST_SUITE(sys_util, NULL, NULL, NULL, NULL, NULL);
83