zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2021 Erwin Rol <erwin@erwinrol.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 
7 #include <zephyr/ztest.h>
8 
9 #include <zephyr/kernel.h>
10 
11 #include <zpp/sem.hpp>
12 #include <zpp/thread.hpp>
13 #include <zpp/utils.hpp>
14 
15 ZTEST_SUITE(test_zpp_sem, NULL, NULL, NULL, NULL, NULL);
16 
17 namespace {
18 
19 K_SEM_DEFINE(g_sem, 0, 10);
20 
21 zpp::sem_ref simple_ref_sem(&g_sem);
22 
23 zpp::sem simple_sem(0, 10);
24 
25 } // namespace
26 
27 ZTEST(test_zpp_sem, test_sem_cmp)
28 {
29  bool res;
30 
31  res = simple_sem == simple_ref_sem;
32  zassert_false(res, "unable to compare sem == sem_ref\n");
33 
34  res = simple_sem != simple_ref_sem;
35  zassert_true(res, "unable to compare sem != sem_ref\n");
36 
37  res = simple_ref_sem == simple_sem;
38  zassert_false(res, "unable to compare sem_ref == sem\n");
39 
40  res = simple_ref_sem != simple_sem;
41  zassert_true(res, "unable to compare sem_ref != sem\n");
42 
43  res = simple_ref_sem == &g_sem;
44  zassert_true(res, "unable to compare sem_ref == k_sem*\n");
45 
46  res = simple_ref_sem != &g_sem;
47  zassert_false(res, "unable to compare sem_ref != k_sem*\n");
48 
49  res = simple_sem == &g_sem;
50  zassert_false(res, "unable to compare sem == k_sem*\n");
51 
52  res = simple_sem != &g_sem;
53  zassert_true(res, "unable to compare sem != k_sem*\n");
54 
55  res = &g_sem == simple_ref_sem;
56  zassert_true(res, "unable to compare k_sem* == sem_ref\n");
57 
58  res = &g_sem != simple_ref_sem;
59  zassert_false(res, "unable to compare k_sem* != sem_ref\n");
60 
61  res = &g_sem == simple_sem;
62  zassert_false(res, "unable to compare k_sem* == sem\n");
63 
64  res = &g_sem != simple_sem;
65  zassert_true(res, "unable to compare k_sem* != sem\n");
66 
67 }
68 
69 ZTEST(test_zpp_sem, test_sem_try_take)
70 {
71  simple_sem.reset();
72 
73  for (int i = 0; i < 5; i++) {
74  simple_sem++;
75 
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);
80  }
81 
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",
86  ret_value);
87 
88  auto signal_count = simple_sem.count();
89  zassert_true(signal_count == i,
90  "signal count missmatch Expected %d, got %d\n",
91  i, signal_count);
92  }
93 }
94 
95 ZTEST(test_zpp_sem, test_sem_try_take_fails)
96 {
97  simple_sem.reset();
98 
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");
103 
104  auto signal_count = simple_sem.count();
105  zassert_true(signal_count == 0U,
106  "signal count missmatch Expected 0, got %d\n",
107  signal_count);
108  }
109 }
110 
111 ZTEST(test_zpp_sem, test_sem_try_take_for_fails)
112 {
113  using namespace std::chrono;
114 
115  simple_sem.reset();
116 
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");
121  }
122 }
123 
124 ZTEST(test_zpp_sem, test_sem_try_take_ref)
125 {
126  simple_ref_sem.reset();
127 
128  for (int i = 0; i < 5; i++) {
129  simple_ref_sem++;
130 
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);
135  }
136 
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",
141  ret_value);
142 
143  auto signal_count = simple_ref_sem.count();
144  zassert_true(signal_count == i,
145  "signal count missmatch Expected %d, got %d\n",
146  i, signal_count);
147  }
148 }
149 
150 ZTEST(test_zpp_sem, test_sem_try_take_fails_ref)
151 {
152  simple_ref_sem.reset();
153 
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");
158 
159  auto signal_count = simple_ref_sem.count();
160  zassert_true(signal_count == 0U,
161  "signal count missmatch Expected 0, got %d\n",
162  signal_count);
163  }
164 }
165 
166 ZTEST(test_zpp_sem, test_sem_try_take_for_fails_ref)
167 {
168  using namespace std::chrono;
169 
170  simple_ref_sem.reset();
171 
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");
176  }
177 }
A counting semaphore class borrowing the native sem.
Definition: sem.hpp:264
A counting semaphore class.
Definition: sem.hpp:196
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition: main.cpp:23
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)