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.h
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#ifndef STKTEST_H_
11#define STKTEST_H_
12
13#include <stdio.h>
14#include <exception>
15
16// lib: cpputest
17#include <CppUTest/TestHarness.h>
18
20extern void (* g_RelaxCpuHandler)();
21
23static inline void __stktest_relax_cpu()
24{
25 if (g_RelaxCpuHandler != NULL)
27}
28
29// lib: stk
30#ifndef _STK_UNDER_TEST
31 #define _STK_UNDER_TEST
32#endif
33#define __stk_relax_cpu __stktest_relax_cpu
34#include <stk_config.h>
35#undef _STK_ARCH_ARM_CORTEX_M
36#undef _STK_ARCH_RISC_V
37#undef _STK_ARCH_X86_WIN32
38#include <stk.h>
40#include <sync/stk_sync.h>
41#include <time/stk_time.h>
42
43#include "stktest_context.h"
44
45namespace stk {
46
50namespace test {
51
52extern IKernelService *g_KernelService;
53
55extern int32_t g_CriticalSectionState;
56
58extern bool g_InsideISR;
59
62
66struct TestAssertPassed : public std::exception
67{
68 const char *what() const noexcept { return "STK test suite exception (TestAssertPassed) thrown!"; }
69};
70
75{
76public:
83
85 {
86 m_event_handler = NULL;
87 m_service = NULL;
88 m_started = false;
89 m_hard_fault = false;
91 m_exit_trap = NULL;
92 m_fail_InitStack = false;
93 m_resolution = 0;
95 m_ticks_count = 0;
96 m_stack_idle = NULL;
97 m_stack_active = NULL;
98 m_overrider = NULL;
99 }
100
102 {
104 g_KernelService = NULL;
105 }
106
107 void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap)
108 {
109 m_event_handler = event_handler;
110 m_service = service;
111 m_started = false;
112 m_resolution = resolution_us;
113 m_exit_trap = exit_trap;
114
115 g_KernelService = service;
116 }
117
118 void Start()
119 {
120 m_started = true;
121
122 EventStart();
123 }
124
125 void Stop()
126 {
127 m_started = false;
128
129 m_event_handler->OnStop();
130 }
131
132 bool InitStack(EStackType type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
133 {
135 return false;
136
137 // if NULL then it is Exit trap is being initialized
138 m_stack_info[type].stack = stack;
139 m_stack_info[type].memory = stack_memory;
140 m_stack_info[type].task = user_task;
141
142 // required to pass assertion checks when switching tasks
144
145 stack->SP = (size_t)stack_memory->GetStack();
146 return true;
147 }
148
149 uint32_t GetTickResolution() const
150 {
151 return m_resolution;
152 }
153
155 {
156 m_event_handler->OnTaskSwitch(GetCallerSP());
158 }
159
160 void Sleep(Timeout ticks)
161 {
162 m_event_handler->OnTaskSleep(GetCallerSP(), ticks);
163 }
164
165 void SleepUntil(Ticks timestamp)
166 {
167 m_event_handler->OnTaskSleepUntil(GetCallerSP(), timestamp);
168 }
169
171 {
172 return m_event_handler->OnTaskWait(GetCallerSP(), sobj, mutex, timeout);
173 }
174
176 {
177 m_hard_fault = true;
178 }
179
181 {
182 Timeout ticks = 1;
183
186 , ticks
187 #endif
188 ))
189 {
191 }
192
193 m_ticks_count += ticks;
194 }
195
197 {
198 m_overrider = overrider;
199 }
200
201 // Events generated by test cases:
202
204 {
206 }
207
208 void EventTaskExit(Stack *stack)
209 {
210 m_event_handler->OnTaskExit(stack);
211 }
212
213 void EventTaskSwitch(size_t caller_SP)
214 {
215 m_event_handler->OnTaskSwitch(caller_SP);
216 }
217
218 void EventTaskSleep(size_t caller_SP, uint32_t sleep_ticks)
219 {
220 m_event_handler->OnTaskSleep(caller_SP, sleep_ticks);
221 }
222
223 IWaitObject *EventTaskWait(size_t caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)
224 {
225 return m_event_handler->OnTaskWait(caller_SP, sync_obj, mutex, timeout);
226 }
227
228 size_t GetCallerSP() const
229 {
230 return m_stack_active->SP;
231 }
232
233 virtual TId GetTid() const
234 {
235 return m_event_handler->OnGetTid(GetCallerSP());
236 }
237
251
252protected:
254};
255
260{
261public:
263 {
264 m_inc_ticks = false;
265 m_switch_to_next = false;
266 m_ticks = 0;
267 m_resolution = 0;
268 m_tid = 0;
269 }
271 { }
272
273 size_t GetTid() const
274 {
275 return m_tid;
276 }
277
278 int64_t GetTicks() const
279 {
280 if (m_inc_ticks)
281 const_cast<int64_t &>(m_ticks) = m_ticks + 1;
282
283 return m_ticks;
284 }
285
286 int32_t GetTickResolution() const
287 {
288 return m_resolution;
289 }
290
291 void Delay(Timeout ticks)
292 {
293 (void)ticks;
294 }
295
296 void Sleep(Timeout ticks)
297 {
298 (void)ticks;
299 }
300
301 void SleepUntil(Ticks timestamp)
302 {
303 (void)timestamp;
304 }
305
307 {
308 m_switch_to_next = true;
309 }
310
312 {
313 (void)sobj;
314 (void)mutex;
315 (void)timeout;
316 return nullptr;
317 }
318
321 int64_t m_ticks;
323 size_t m_tid;
324};
325
330template <EAccessMode _AccessMode>
331class TaskMock : public Task<STACK_SIZE_MIN, _AccessMode>
332{
333public:
336
338
339private:
340 void Run() {}
341
342 void OnDeadlineMissed(uint32_t duration)
343 {
344 // call base (to achieve full coverage)
346
347 m_deadline_missed = duration;
348 }
349};
350
355template <int32_t _Weight, EAccessMode _AccessMode>
356class TaskMockW : public TaskW<_Weight, STACK_SIZE_MIN, _AccessMode>
357{
358private:
359 void Run() {}
360};
361
362struct MutexMock : public IMutex
363{
364 explicit MutexMock() : m_nesting(0), m_locked(false)
365 {}
366
367 uint32_t m_nesting;
369
370 void Lock()
371 {
372 if (m_nesting == 0)
373 m_locked = true;
374
375 ++m_nesting;
376 }
377
378 void Unlock()
379 {
380 STK_ASSERT(m_nesting != 0);
381
382 if (--m_nesting == 0)
383 m_locked = false;
384 }
385};
386
388{
391};
392
393} // namespace test
394} // namespace stk
395
396#endif /* STKTEST_H_ */
Contains common inventory for platform implementation.
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_TICKLESS_IDLE
Enables tickless (dynamic-tick) low-power operation during idle periods.
Definition stk_defs.h:36
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Collection of synchronization primitives (stk::sync namespace).
Collection of time-related primitives (stk::time namespace).
void(* g_RelaxCpuHandler)()
__stk_relax_cpu handler.
Definition stktest.cpp:17
void(* g_RelaxCpuHandler)()
__stk_relax_cpu handler.
Definition stktest.cpp:17
static void __stktest_relax_cpu()
__stk_relax_cpu interceptor.
Definition stktest.h:23
Namespace of STK package.
int64_t Ticks
Ticks value.
Definition stk_common.h:150
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:133
EStackType
Stack type.
Definition stk_common.h:70
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:73
Word TId
Definition stk_common.h:117
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:52
IKernelService * g_KernelService
Definition stktest.cpp:18
int32_t g_CriticalSectionState
Critical section state.
Definition stktest.cpp:19
EKernelPanicId g_PanicValue
Panic value.
Definition stktest.cpp:20
bool g_InsideISR
ISR state.
Definition stktest.cpp:21
Namespace of Mutex test.
static Word * InitStackMemory(IStackMemory *memory)
Initialize stack memory by filling it with STK_STACK_MEMORY_FILLER.
Stack descriptor.
Definition stk_common.h:181
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Definition stk_common.h:182
Interface for a stack memory region.
Definition stk_common.h:193
virtual Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:212
Synchronization object.
Definition stk_common.h:297
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:357
ISyncObject()
Constructor.
Definition stk_common.h:350
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:367
Interface for mutex synchronization primitive.
Definition stk_common.h:381
Interface for a user task.
Definition stk_common.h:433
Interface for a platform driver.
Definition stk_common.h:575
Interface for a back-end event handler.
Definition stk_common.h:583
Interface for a platform event overrider.
Definition stk_common.h:659
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
Definition stk_common.h:929
virtual void OnDeadlineMissed(uint32_t duration)
Default no-op handler. Override in subclass to log or handle missed deadlines.
Definition stk_helper.h:62
Throwable class for catching assertions from STK_ASSERT_HANDLER().
Definition stktest.h:67
const char * what() const noexcept
Definition stktest.h:68
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap)
Initialize scheduler's context.
Definition stktest.h:107
size_t GetCallerSP() const
Get caller's Stack Pointer (SP).
Definition stktest.h:228
IEventHandler * m_event_handler
Definition stktest.h:253
void EventTaskSwitch(size_t caller_SP)
Definition stktest.h:213
void ProcessTick()
Process one tick.
Definition stktest.h:180
void EventTaskExit(Stack *stack)
Definition stktest.h:208
bool InitStack(EStackType type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
Initialize stack memory of the user task.
Definition stktest.h:132
IWaitObject * EventTaskWait(size_t caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)
Definition stktest.h:223
void Sleep(Timeout ticks)
Put calling process into a sleep state.
Definition stktest.h:160
IKernelService * m_service
Definition stktest.h:238
IEventOverrider * m_overrider
Definition stktest.h:247
void EventTaskSleep(size_t caller_SP, uint32_t sleep_ticks)
Definition stktest.h:218
void Start()
Start scheduling.
Definition stktest.h:118
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stktest.h:165
IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)
Definition stktest.h:170
virtual TId GetTid() const
Get thread Id.
Definition stktest.h:233
void SetEventOverrider(IEventOverrider *overrider)
Set platform event overrider.
Definition stktest.h:196
void ProcessHardFault()
Cause a hard fault of the system.
Definition stktest.h:175
void Stop()
Stop scheduling.
Definition stktest.h:125
void SwitchToNext()
Switch to a next task.
Definition stktest.h:154
StackInfo m_stack_info[STACK_EXIT_TRAP+1]
Definition stktest.h:250
uint32_t GetTickResolution() const
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
Definition stktest.h:149
int32_t GetTickResolution() const
Get number of microseconds in one tick.
Definition stktest.h:286
size_t GetTid() const
Get thread Id of the currently running task.
Definition stktest.h:273
void Sleep(Timeout ticks)
Put calling process into a sleep state.
Definition stktest.h:296
void Delay(Timeout ticks)
Delay calling process.
Definition stktest.h:291
void SwitchToNext()
Notify scheduler to switch to the next task (yield).
Definition stktest.h:306
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stktest.h:301
int64_t GetTicks() const
Get number of ticks elapsed since kernel start.
Definition stktest.h:278
IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
Definition stktest.h:311
void OnDeadlineMissed(uint32_t duration)
Default no-op handler. Override in subclass to log or handle missed deadlines.
Definition stktest.h:342
void Run()
Entry point of the user task.
Definition stktest.h:340
uint32_t m_deadline_missed
duration of workload if deadline is missed in HRT mode
Definition stktest.h:337
Task mock for SwitchStrategySmoothWeightedRoundRobin and similar algorithms.
Definition stktest.h:357
void Run()
Entry point of the user task.
Definition stktest.h:359
void Lock()
Lock the mutex.
Definition stktest.h:370
void Unlock()
Unlock the mutex.
Definition stktest.h:378