7 #include <zephyr/ztest.h>
9 #include <zephyr/kernel.h>
19 K_SEM_DEFINE(g_sem, 0, 10);
27 ZTEST(test_zpp_sem, test_sem_cmp)
31 res = simple_sem == simple_ref_sem;
32 zassert_false(res,
"unable to compare sem == sem_ref\n");
34 res = simple_sem != simple_ref_sem;
35 zassert_true(res,
"unable to compare sem != sem_ref\n");
37 res = simple_ref_sem == simple_sem;
38 zassert_false(res,
"unable to compare sem_ref == sem\n");
40 res = simple_ref_sem != simple_sem;
41 zassert_true(res,
"unable to compare sem_ref != sem\n");
43 res = simple_ref_sem == &g_sem;
44 zassert_true(res,
"unable to compare sem_ref == k_sem*\n");
46 res = simple_ref_sem != &g_sem;
47 zassert_false(res,
"unable to compare sem_ref != k_sem*\n");
49 res = simple_sem == &g_sem;
50 zassert_false(res,
"unable to compare sem == k_sem*\n");
52 res = simple_sem != &g_sem;
53 zassert_true(res,
"unable to compare sem != k_sem*\n");
55 res = &g_sem == simple_ref_sem;
56 zassert_true(res,
"unable to compare k_sem* == sem_ref\n");
58 res = &g_sem != simple_ref_sem;
59 zassert_false(res,
"unable to compare k_sem* != sem_ref\n");
61 res = &g_sem == simple_sem;
62 zassert_false(res,
"unable to compare k_sem* == sem\n");
64 res = &g_sem != simple_sem;
65 zassert_true(res,
"unable to compare k_sem* != sem\n");
69 ZTEST(test_zpp_sem, test_sem_try_take)
73 for (
int i = 0; i < 5; i++) {
76 auto signal_count = simple_sem.count();
77 zassert_true(signal_count == (i + 1),
78 "signal count missmatch Expected %d, got %d\n",
79 (i + 1), signal_count);
82 for (
int i = 4; i >= 0; i--) {
83 auto ret_value = simple_sem.try_take();
84 zassert_true(ret_value ==
true,
85 "unable to do k_sem_take which returned %d\n",
88 auto signal_count = simple_sem.count();
89 zassert_true(signal_count == i,
90 "signal count missmatch Expected %d, got %d\n",
95 ZTEST(test_zpp_sem, test_sem_try_take_fails)
99 for (
int i = 4; i >= 0; i--) {
100 auto ret_value = simple_sem.try_take();
101 zassert_true(ret_value ==
false,
102 "k_sem_take returned when not possible");
104 auto signal_count = simple_sem.count();
105 zassert_true(signal_count == 0U,
106 "signal count missmatch Expected 0, got %d\n",
111 ZTEST(test_zpp_sem, test_sem_try_take_for_fails)
113 using namespace std::chrono;
117 for (
int i = 4; i >= 0; i--) {
118 auto ret_value = simple_sem.try_take_for(100ms);
119 zassert_true(ret_value ==
false,
120 "k_sem_take succeeded when its not possible");
124 ZTEST(test_zpp_sem, test_sem_try_take_ref)
126 simple_ref_sem.reset();
128 for (
int i = 0; i < 5; i++) {
131 auto signal_count = simple_ref_sem.count();
132 zassert_true(signal_count == (i + 1),
133 "signal count missmatch Expected %d, got %d\n",
134 (i + 1), signal_count);
137 for (
int i = 4; i >= 0; i--) {
138 auto ret_value = simple_ref_sem.try_take();
139 zassert_true(ret_value ==
true,
140 "unable to do k_sem_take which returned %d\n",
143 auto signal_count = simple_ref_sem.count();
144 zassert_true(signal_count == i,
145 "signal count missmatch Expected %d, got %d\n",
150 ZTEST(test_zpp_sem, test_sem_try_take_fails_ref)
152 simple_ref_sem.reset();
154 for (
int i = 4; i >= 0; i--) {
155 auto ret_value = simple_ref_sem.try_take();
156 zassert_true(ret_value ==
false,
157 "k_sem_take returned when not possible");
159 auto signal_count = simple_ref_sem.count();
160 zassert_true(signal_count == 0U,
161 "signal count missmatch Expected 0, got %d\n",
166 ZTEST(test_zpp_sem, test_sem_try_take_for_fails_ref)
168 using namespace std::chrono;
170 simple_ref_sem.reset();
172 for (
int i = 4; i >= 0; i--) {
173 auto ret_value = simple_ref_sem.try_take_for(100ms);
174 zassert_true(ret_value ==
false,
175 "k_sem_take succeeded when its not possible");
A counting semaphore class borrowing the native sem.
A counting semaphore class.
ZTEST(zpp_atomic_tests, test_atomic_bitset)
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)