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;
100 m_systimer_freq = 0;
101 m_sleep_ticks = 0;
102 }
103
105 {
107 g_KernelService = NULL;
108 }
109
110 void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap)
111 {
112 m_event_handler = event_handler;
113 m_service = service;
114 m_started = false;
115 m_resolution = resolution_us;
116 m_exit_trap = exit_trap;
117
118 g_KernelService = service;
119 }
120
121 void Start()
122 {
123 m_started = true;
124
125 EventStart();
126 }
127
128 void Stop()
129 {
130 m_started = false;
131
132 m_event_handler->OnStop();
133 }
134
135 bool InitStack(EStackType type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
136 {
138 return false;
139
140 // if NULL then it is Exit trap is being initialized
141 m_stack_info[type].stack = stack;
142 m_stack_info[type].memory = stack_memory;
143 m_stack_info[type].task = user_task;
144
145 // required to pass assertion checks when switching tasks
147
148 stack->SP = (size_t)stack_memory->GetStack();
149 return true;
150 }
151
152 uint32_t GetTickResolution() const
153 {
154 return m_resolution;
155 }
156
158 {
159 return m_systimer_count;
160 }
161
162 uint32_t GetSysTimerFrequency() const
163 {
164 return m_systimer_freq;
165 }
166
168 {
169 m_event_handler->OnTaskSwitch(GetCallerSP());
171 }
172
173 void Sleep(Timeout ticks)
174 {
175 m_event_handler->OnTaskSleep(GetCallerSP(), ticks);
176 }
177
178 void SleepUntil(Ticks timestamp)
179 {
180 m_event_handler->OnTaskSleepUntil(GetCallerSP(), timestamp);
181 }
182
184 {
185 return m_event_handler->OnTaskWait(GetCallerSP(), sobj, mutex, timeout);
186 }
187
189 {
190 m_hard_fault = true;
191 }
192
194 {
195 Timeout ticks = 1;
196
199 , ticks
200 #endif
201 ))
202 {
204 }
205
206 m_ticks_count += ticks;
207 }
208
210 {
211 m_overrider = overrider;
212 }
213
214 // Events generated by test cases:
215
217 {
219 }
220
221 void EventTaskExit(Stack *stack)
222 {
223 m_event_handler->OnTaskExit(stack);
224 }
225
226 void EventTaskSwitch(size_t caller_SP)
227 {
228 m_event_handler->OnTaskSwitch(caller_SP);
229 }
230
231 void EventTaskSleep(size_t caller_SP, uint32_t sleep_ticks)
232 {
233 m_event_handler->OnTaskSleep(caller_SP, sleep_ticks);
234 }
235
236 IWaitObject *EventTaskWait(size_t caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)
237 {
238 return m_event_handler->OnTaskWait(caller_SP, sync_obj, mutex, timeout);
239 }
240
241 size_t GetCallerSP() const
242 {
243 return m_stack_active->SP;
244 }
245
246 virtual TId GetTid() const
247 {
248 return m_event_handler->OnGetTid(GetCallerSP());
249 }
250
252 {
253 m_event_handler->OnSuspend(true);
254 return m_sleep_ticks;
255 }
256
257 void Resume(Timeout elapsed_ticks)
258 {
259 m_event_handler->OnSuspend(false);
260 }
261
278
279protected:
281};
282
287{
288public:
290 {
291 m_inc_ticks = false;
292 m_switch_to_next = false;
293 m_ticks = 0;
294 m_resolution = 0;
295 m_tid = 0;
297 m_systimer_freq = 0;
298 }
300 {}
301
302 size_t GetTid() const
303 {
304 return m_tid;
305 }
306
307 int64_t GetTicks() const
308 {
309 if (m_inc_ticks)
310 const_cast<int64_t &>(m_ticks) = m_ticks + 1;
311
312 return m_ticks;
313 }
314
315 uint32_t GetTickResolution() const
316 {
317 return m_resolution;
318 }
319
321 {
322 return m_systimer_count;
323 }
324
325 uint32_t GetSysTimerFrequency() const
326 {
327 return m_systimer_freq;
328 }
329
330 void Delay(Timeout ticks)
331 {
332 (void)ticks;
333 }
334
335 void Sleep(Timeout ticks)
336 {
337 (void)ticks;
338 }
339
340 void SleepUntil(Ticks timestamp)
341 {
342 (void)timestamp;
343 }
344
346 {
347 m_switch_to_next = true;
348 }
349
351 {
352 (void)sobj;
353 (void)mutex;
354 (void)timeout;
355 return nullptr;
356 }
357
359 {
360 return 1;
361 }
362
363 void Resume(Timeout elapsed_ticks)
364 {
365 (void)elapsed_ticks;
366 }
367
370 int64_t m_ticks;
372 size_t m_tid;
375};
376
381template <EAccessMode _AccessMode>
382class TaskMock : public Task<STACK_SIZE_MIN, _AccessMode>
383{
384public:
387
389
390private:
391 void Run() {}
392
393 void OnDeadlineMissed(uint32_t duration)
394 {
395 // call base (to achieve full coverage)
397
398 m_deadline_missed = duration;
399 }
400};
401
406template <int32_t _Weight, EAccessMode _AccessMode>
407class TaskMockW : public TaskW<_Weight, STACK_SIZE_MIN, _AccessMode>
408{
409private:
410 void Run() {}
411};
412
413struct MutexMock : public IMutex
414{
415 explicit MutexMock() : m_nesting(0), m_locked(false)
416 {}
417
418 uint32_t m_nesting;
420
421 void Lock()
422 {
423 if (m_nesting == 0)
424 m_locked = true;
425
426 ++m_nesting;
427 }
428
429 void Unlock()
430 {
431 STK_ASSERT(m_nesting != 0);
432
433 if (--m_nesting == 0)
434 m_locked = false;
435 }
436};
437
439{
442};
443
444} // namespace test
445} // namespace stk
446
447#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:128
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:123
EStackType
Stack type.
Definition stk_common.h:70
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:73
uint64_t Cycles
Cycles value.
Definition stk_common.h:133
Word TId
Definition stk_common.h:118
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:219
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Definition stk_common.h:220
Interface for a stack memory region.
Definition stk_common.h:231
virtual Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:270
Synchronization object.
Definition stk_common.h:355
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:415
ISyncObject()
Constructor.
Definition stk_common.h:408
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:425
Interface for mutex synchronization primitive.
Definition stk_common.h:439
Interface for a user task.
Definition stk_common.h:491
Interface for a platform driver.
Definition stk_common.h:648
Interface for a back-end event handler.
Definition stk_common.h:656
Interface for a platform event overrider.
Definition stk_common.h:737
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
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:110
size_t GetCallerSP() const
Get caller's Stack Pointer (SP).
Definition stktest.h:241
IEventHandler * m_event_handler
Definition stktest.h:280
void EventTaskSwitch(size_t caller_SP)
Definition stktest.h:226
void Resume(Timeout elapsed_ticks)
Resume scheduling after a prior Suspend() call.
Definition stktest.h:257
Timeout Suspend()
Suspend scheduling.
Definition stktest.h:251
uint32_t GetSysTimerFrequency() const
Get system timer frequency.
Definition stktest.h:162
void ProcessTick()
Process one tick.
Definition stktest.h:193
void EventTaskExit(Stack *stack)
Definition stktest.h:221
bool InitStack(EStackType type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
Initialize stack memory of the user task.
Definition stktest.h:135
IWaitObject * EventTaskWait(size_t caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)
Definition stktest.h:236
void Sleep(Timeout ticks)
Put calling process into a sleep state.
Definition stktest.h:173
IKernelService * m_service
Definition stktest.h:262
IEventOverrider * m_overrider
Definition stktest.h:271
void EventTaskSleep(size_t caller_SP, uint32_t sleep_ticks)
Definition stktest.h:231
Cycles GetSysTimerCount() const
Get system timer count value.
Definition stktest.h:157
void Start()
Start scheduling.
Definition stktest.h:121
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stktest.h:178
IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)
Definition stktest.h:183
virtual TId GetTid() const
Get thread Id.
Definition stktest.h:246
void SetEventOverrider(IEventOverrider *overrider)
Set platform event overrider.
Definition stktest.h:209
void ProcessHardFault()
Cause a hard fault of the system.
Definition stktest.h:188
void Stop()
Stop scheduling.
Definition stktest.h:128
void SwitchToNext()
Switch to a next task.
Definition stktest.h:167
StackInfo m_stack_info[STACK_EXIT_TRAP+1]
Definition stktest.h:274
uint32_t GetTickResolution() const
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
Definition stktest.h:152
size_t GetTid() const
Get thread Id of the currently running task.
Definition stktest.h:302
void Sleep(Timeout ticks)
Put calling process into a sleep state.
Definition stktest.h:335
void Delay(Timeout ticks)
Delay calling process.
Definition stktest.h:330
void SwitchToNext()
Notify scheduler to switch to the next task (yield).
Definition stktest.h:345
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stktest.h:340
uint32_t GetTickResolution() const
Get number of microseconds in one tick.
Definition stktest.h:315
int64_t GetTicks() const
Get number of ticks elapsed since kernel start.
Definition stktest.h:307
Timeout Suspend()
Suspend scheduling.
Definition stktest.h:358
Cycles GetSysTimerCount() const
Get system timer count value.
Definition stktest.h:320
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:350
void Resume(Timeout elapsed_ticks)
Resume scheduling after a prior Suspend() call.
Definition stktest.h:363
uint32_t GetSysTimerFrequency() const
Get system timer frequency.
Definition stktest.h:325
void OnDeadlineMissed(uint32_t duration)
Default no-op handler. Override in subclass to log or handle missed deadlines.
Definition stktest.h:393
void Run()
Entry point of the user task.
Definition stktest.h:391
uint32_t m_deadline_missed
duration of workload if deadline is missed in HRT mode
Definition stktest.h:388
Task mock for SwitchStrategySmoothWeightedRoundRobin and similar algorithms.
Definition stktest.h:408
void Run()
Entry point of the user task.
Definition stktest.h:410
void Lock()
Lock the mutex.
Definition stktest.h:421
void Unlock()
Unlock the mutex.
Definition stktest.h:429