SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stktest_misc.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#include "stktest.h"
11
12namespace stk {
13namespace test {
14
15// ============================================================================ //
16// =============================== Basic ====================================== //
17// ============================================================================ //
18
20{
21 void setup() {}
22 void teardown() {}
23};
24
25TEST(Basic, MinMax)
26{
27 static_assert(stk::Min(10, 20) == 10, "Min failed at compile time");
28 static_assert(stk::Max(10, 20) == 20, "Max failed at compile time");
29
30 // basic & symmetry
31 CHECK_EQUAL(1, stk::Min(1, 2));
32 CHECK_EQUAL(1, stk::Min(2, 1));
33
34 // negatives
35 CHECK_EQUAL(-5, stk::Min(-5, -2));
36 CHECK_EQUAL(-2, stk::Max(-5, -2));
37
38 // equality
39 CHECK_EQUAL(42, stk::Max(42, 42));
40
41 // boundaries (assuming 32-bit ints)
42 CHECK_EQUAL(2147483647, stk::Max(0, 2147483647));
43
44 // support const vars
45 {
46 const int32_t a = 10;
47 const int32_t b = 20;
48 CHECK_EQUAL(10, stk::Min(a, b));
49 }
50
51 // no duplicate operation
52 {
53 int32_t x = 1;
54 int32_t y = 5;
55
56 // in a macro-based MIN(x++, y), x would become 3, with STK's template-based stk::Min, x should become 2
57 int32_t result = stk::Min(x++, y);
58
59 CHECK_EQUAL(1, result); // the value before increment
60 CHECK_EQUAL(2, x); // x should only have been incremented once
61 CHECK_EQUAL(5, y); // y remains untouched
62 }
63}
64
65// ============================================================================ //
66// ============================= UserTask ===================================== //
67// ============================================================================ //
68
69TEST_GROUP(UserTask)
70{
71 void setup() {}
72 void teardown() {}
73};
74
75TEST(UserTask, GetStackSize)
76{
79
82}
83
84TEST(UserTask, GetStackSizeBytes)
85{
88
89 CHECK_EQUAL(TaskMock<ACCESS_USER>::STACK_SIZE * sizeof(size_t), task.GetStackSizeBytes());
90 CHECK_EQUAL(TaskMock<ACCESS_USER>::STACK_SIZE * sizeof(size_t), taskw.GetStackSizeBytes());
91}
92
93TEST(UserTask, GetWeight)
94{
97
98 // TaskMock inherits Task and Task does not support weights (1)
99 CHECK_EQUAL(1, task.GetWeight());
100
101 // TaskMockW supports weights as it inherits TaskW
102 CHECK_EQUAL(10, taskw.GetWeight());
103}
104
105TEST(UserTask, GetIdAndName)
106{
109
110 CHECK_EQUAL((size_t)&task, task.GetId());
111 CHECK_EQUAL((size_t)&taskw, taskw.GetId());
112
113 // expect NULL name by default
114 CHECK_EQUAL((const char *)NULL, task.GetTraceName());
115 CHECK_EQUAL((const char *)NULL, taskw.GetTraceName());
116}
117
118TEST(UserTask, TaskWUnsupportedHrt)
119{
121
122 try
123 {
124 g_TestContext.ExpectAssert(true);
125 // on next tick kernel will attempt to remove pending task and will check its deadline
126 taskw.OnDeadlineMissed(0);
127 CHECK_TEXT(false, "expecting assertion - task with weights do not support HRT");
128 }
129 catch (TestAssertPassed &pass)
130 {
131 CHECK(true);
132 g_TestContext.ExpectAssert(false);
133 }
134}
135
137{
138 void setup() {}
139 void teardown() {}
140};
141
143{
145 StackMemoryWrapper<STACK_SIZE_MIN> wrapper(&memory);
146
147 CHECK_TRUE(NULL != wrapper.GetStack());
148 CHECK_EQUAL((size_t *)&memory, wrapper.GetStack());
149}
150
152{
154 StackMemoryWrapper<STACK_SIZE_MIN> wrapper(&memory);
155
156 CHECK_EQUAL(STACK_SIZE_MIN, wrapper.GetStackSize());
157 CHECK_EQUAL(sizeof(memory) / sizeof(size_t), wrapper.GetStackSize());
158}
159
160TEST(StackMemoryWrapper, GetStackSizeBytes)
161{
163 StackMemoryWrapper<STACK_SIZE_MIN> wrapper(&memory);
164
165 CHECK_EQUAL(STACK_SIZE_MIN * sizeof(size_t), wrapper.GetStackSizeBytes());
166 CHECK_EQUAL(sizeof(memory), wrapper.GetStackSizeBytes());
167}
168
170{
171 void setup() {}
172 void teardown() {}
173
174 struct ListEntry : public stk::util::DListEntry<ListEntry, true>
175 {
176 int32_t m_id;
177 ListEntry(int32_t id) : m_id(id) {}
178 };
179
181};
182
183TEST(DList, Empty)
184{
185 ListHead list;
186
187 CHECK_EQUAL_ZERO(list.GetSize());
188 CHECK_TRUE(list.IsEmpty());
189 CHECK_TRUE(NULL == list.GetFirst());
190 CHECK_TRUE(NULL == list.GetLast());
191
192 list.Clear();
193}
194
195TEST(DList, LinkFront)
196{
197 ListHead list;
198 ListEntry e1(1), e2(2), e3(3);
199
200 list.LinkFront(e1);
201 CHECK_EQUAL(&e1, list.GetFirst());
202 CHECK_EQUAL(&e1, list.GetLast());
203 CHECK_EQUAL(&list, e1.GetHead());
204
205 list.LinkFront(e2);
206 CHECK_EQUAL(&e2, list.GetFirst());
207 CHECK_EQUAL(&e1, list.GetLast());
208
209 list.LinkFront(e3);
210 CHECK_EQUAL(&e3, list.GetFirst());
211 CHECK_EQUAL(&e1, list.GetLast());
212
213 CHECK_EQUAL(3, list.GetSize());
214}
215
216TEST(DList, LinkBack)
217{
218 ListHead list;
219 ListEntry e1(1), e2(2), e3(3);
220
221 list.LinkBack(e1);
222 CHECK_EQUAL(&e1, list.GetFirst());
223 CHECK_EQUAL(&e1, list.GetLast());
224 CHECK_EQUAL(&list, e1.GetHead());
225
226 list.LinkBack(e2);
227 CHECK_EQUAL(&e1, list.GetFirst());
228 CHECK_EQUAL(&e2, list.GetLast());
229
230 list.LinkBack(e3);
231 CHECK_EQUAL(&e1, list.GetFirst());
232 CHECK_EQUAL(&e3, list.GetLast());
233
234 CHECK_EQUAL(3, list.GetSize());
235}
236
237TEST(DList, PopFront)
238{
239 ListHead list;
240 ListEntry e1(1), e2(2), e3(3);
241
242 list.LinkBack(e1);
243 list.LinkBack(e2);
244 list.LinkBack(e3);
245
246 list.PopFront();
247
248 CHECK_EQUAL(&e2, list.GetFirst());
249 CHECK_EQUAL(&e3, list.GetLast());
250
251 list.PopFront();
252
253 CHECK_EQUAL(&e3, list.GetFirst());
254 CHECK_EQUAL(&e3, list.GetLast());
255
256 list.PopFront();
257
258 CHECK_EQUAL_ZERO(list.GetSize());
259 CHECK_TRUE(list.IsEmpty());
260 CHECK_TRUE(NULL == list.GetFirst());
261 CHECK_TRUE(NULL == list.GetLast());
262}
263
264TEST(DList, PopBack)
265{
266 ListHead list;
267 ListEntry e1(1), e2(2), e3(3);
268
269 list.LinkBack(e1);
270 list.LinkBack(e2);
271 list.LinkBack(e3);
272
273 list.PopBack();
274
275 CHECK_EQUAL(&e1, list.GetFirst());
276 CHECK_EQUAL(&e2, list.GetLast());
277
278 list.PopBack();
279
280 CHECK_EQUAL(&e1, list.GetFirst());
281 CHECK_EQUAL(&e1, list.GetLast());
282
283 list.PopBack();
284
285 CHECK_EQUAL_ZERO(list.GetSize());
286 CHECK_TRUE(list.IsEmpty());
287 CHECK_TRUE(NULL == list.GetFirst());
288 CHECK_TRUE(NULL == list.GetLast());
289}
290
291TEST(DList, Clear)
292{
293 ListHead list;
294 ListEntry e1(1), e2(2), e3(3);
295
296 list.LinkBack(e1);
297 list.LinkBack(e2);
298 list.LinkBack(e3);
299
300 list.Clear();
301
302 CHECK_EQUAL_ZERO(list.GetSize());
303 CHECK_TRUE(list.IsEmpty());
304 CHECK_TRUE(NULL == list.GetFirst());
305 CHECK_TRUE(NULL == list.GetLast());
306}
307
308TEST(DList, Iteration)
309{
310 ListHead list;
311 ListEntry e1(1), e2(2), e3(3);
312
313 list.LinkBack(e1);
314 list.LinkBack(e2);
315 list.LinkBack(e3);
316
317 ListHead::DLEntryType *itr = list.GetFirst();
318 CHECK_EQUAL(&e1, itr);
319
320 itr = itr->GetNext();
321 CHECK_EQUAL(&e2, itr);
322
323 itr = itr->GetNext();
324 CHECK_EQUAL(&e3, itr);
325
326 itr = itr->GetNext();
327 CHECK_EQUAL(&e1, itr);
328}
329
330TEST(DList, Relink)
331{
332 ListHead list;
333 ListEntry e1(1), e2(2), e3(3);
334
335 list.LinkBack(e1);
336 list.LinkBack(e2);
337 list.LinkBack(e3);
338
339 ListHead list2;
340 list.RelinkTo(list2);
341
342 CHECK_EQUAL_ZERO(list.GetSize());
343 CHECK_TRUE(list.IsEmpty());
344 CHECK_TRUE(NULL == list.GetFirst());
345 CHECK_TRUE(NULL == list.GetLast());
346
347 CHECK_EQUAL(3, list2.GetSize());
348 CHECK_EQUAL(&list2, e1.GetHead());
349 CHECK_EQUAL(&list2, e2.GetHead());
350 CHECK_EQUAL(&list2, e3.GetHead());
351}
352
353} // namespace stk
354} // namespace test
Namespace of STK package.
constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
Definition stk_defs.h:536
constexpr T Min(T a, T b) noexcept
Compile-time minimum of two values.
Definition stk_defs.h:530
@ STACK_SIZE_MIN
Minimum stack size in elements of Word. Used as a lower bound for all stack allocations (user task,...
Definition stk_common.h:83
Namespace of the test inventory.
TestContext g_TestContext
Global instance of the TestContext.
Definition stktest.cpp:16
TEST_GROUP(Kernel)
TEST(Kernel, MaxTasks)
virtual const char * GetTraceName() const
Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.
Definition stk_helper.h:76
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
Definition stk_helper.h:56
virtual TId GetId() const
Get object's own address as its Id. Unique per task instance, requires no manual assignment.
Definition stk_helper.h:72
size_t GetStackSize() const
Get number of elements of the stack memory array.
Definition stk_helper.h:55
virtual int32_t GetWeight() const
Default weight of 1. Override in subclass if custom scheduling weight is needed.
Definition stk_helper.h:68
virtual const char * GetTraceName() const
Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.
Definition stk_helper.h:141
size_t GetStackSize() const
Get number of elements of the stack memory array.
Definition stk_helper.h:122
virtual int32_t GetWeight() const
Returns the compile-time weight _Weight.
Definition stk_helper.h:133
virtual void OnDeadlineMissed(uint32_t duration)
Hard Real-Time mode is unsupported for weighted tasks. Triggers an assertion if called.
Definition stk_helper.h:129
virtual TId GetId() const
Get object's own address as its Id. Unique per task instance, requires no manual assignment.
Definition stk_helper.h:137
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
Definition stk_helper.h:123
Adapts an externally-owned stack memory array to the IStackMemory interface.
Definition stk_helper.h:174
size_t GetStackSize() const
Get number of elements in the wrapped stack array.
Definition stk_helper.h:203
StackMemoryDef< _StackSize >::Type MemoryType
The concrete array type that this wrapper accepts, equivalent to StackMemoryDef<_StackSize>::Type.
Definition stk_helper.h:179
size_t GetStackSizeBytes() const
Get size of the wrapped stack array in bytes.
Definition stk_helper.h:207
Word * GetStack() const
Get pointer to the first element of the wrapped stack array.
Definition stk_helper.h:199
Intrusive doubly-linked list container. Manages a collection of DListEntry nodes embedded in host obj...
Intrusive doubly-linked list node. Embed this as a base class in any object (T) that needs to partici...
Throwable class for catching assertions from STK_ASSERT_HANDLER().
Definition stktest.h:67
Task mock for SwitchStrategySmoothWeightedRoundRobin and similar algorithms.
Definition stktest.h:357