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_time.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// ============================= PeriodicTimer ================================ //
17// ============================================================================ //
18
20namespace stk
21{
22 static struct KernelServiceMock : public IKernelService
23 {
24 int64_t ticks;
25 int32_t resolution;
26
27 static IKernelService *GetInstance() { return NULL; }
28 TId GetTid() const { return 0; }
29 Ticks GetTicks() const { return ticks; }
30 int32_t GetTickResolution() const { return resolution; }
31 void Delay(Timeout ticks) { (void)ticks; }
32 void Sleep(Timeout ticks) { (void)ticks; }
33 void SleepUntil(Ticks timestamp) { (void)timestamp; }
34 void SwitchToNext() {}
36 {
37 (void)sobj;
38 (void)mutex;
39 (void)timeout;
40 return nullptr;
41 }
42 }
44
45 void SetTimeNowMsec(int64_t now)
46 {
48
49 s_KernelServiceMock.resolution = 1000;
51 }
52}
53
54TEST_GROUP(PeriodicTrigger)
55{
56 enum { PERIOD = 100 };
57
58 void setup()
59 {
61 }
62};
63
64TEST(PeriodicTrigger, MustBeArmed)
65{
66 time::PeriodicTrigger trigger(PERIOD, false);
67
69
70 try
71 {
72 g_TestContext.ExpectAssert(true);
73 CHECK_FALSE(trigger.Poll());
74 CHECK_TRUE(false); // Poll() must fail
75 }
76 catch (TestAssertPassed &pass)
77 {
78 CHECK(true);
79 g_TestContext.ExpectAssert(false);
80 }
81}
82
83TEST(PeriodicTrigger, DoesNotFireBeforePeriod)
84{
85 time::PeriodicTrigger trigger(PERIOD, true);
86
88 CHECK_FALSE(trigger.Poll());
89}
90
91TEST(PeriodicTrigger, FiresAtExactPeriod)
92{
93 time::PeriodicTrigger trigger(PERIOD, true);
94
96 CHECK_TRUE(trigger.Poll());
97}
98
99TEST(PeriodicTrigger, PreservesRemainderAfterFire)
100{
101 time::PeriodicTrigger trigger(PERIOD, true);
102
103 // 1. Move to 150ms.
104 // m_elapsed becomes 150. Poll() returns true.
105 // m_elapsed becomes 150 - 100 = 50ms (the remainder).
107 CHECK_TRUE(trigger.Poll());
108
109 // 2. Move to 190ms.
110 // Time delta is 40ms (190 - 150).
111 // m_elapsed = 50 (remainder) + 40 (delta) = 90ms.
113 CHECK_FALSE(trigger.Poll());
114
115 // 3. Move to 205ms.
116 // Time delta is 15ms (205 - 190).
117 // m_elapsed = 90 + 15 = 105ms. Fires!
119 CHECK_TRUE(trigger.Poll());
120}
121
122TEST(PeriodicTrigger, AccumulatesAcrossMultiplePolls)
123{
124 time::PeriodicTrigger trigger(PERIOD, true);
125
127 CHECK_FALSE(trigger.Poll());
128
130 CHECK_FALSE(trigger.Poll());
131
133 CHECK_TRUE(trigger.Poll()); // Total 110ms accumulated
134}
135
136TEST(PeriodicTrigger, ManualResetSyncsToCurrentTime)
137{
138 time::PeriodicTrigger trigger(PERIOD, true);
139
141 CHECK_FALSE(trigger.Poll()); // Accumulated 80ms
142
143 // Re-constructing or manually resetting m_prev/m_elapsed
144 // allows a "hard reset" to the current mock time.
145 trigger = time::PeriodicTrigger(PERIOD, true);
146
148 // Delta is only 70ms (150 - 80) since m_prev was set to 80 during reset/re-init.
149 CHECK_FALSE(trigger.Poll());
150}
151
152} // namespace stk
153} // namespace test
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
Ticks GetTicksFromMs(int64_t ms, int32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:239
Word TId
Definition stk_common.h:117
Namespace of the test inventory.
TestContext g_TestContext
Global instance of the TestContext.
Definition stktest.cpp:16
IKernelService * g_KernelService
Definition stktest.cpp:18
TEST_GROUP(Kernel)
TEST(Kernel, MaxTasks)
stk::test::stk::KernelServiceMock s_KernelServiceMock
void SetTimeNowMsec(int64_t now)
Namespace of Mutex test.
Wait object.
Definition stk_common.h:212
Synchronization object.
Definition stk_common.h:297
Interface for mutex synchronization primitive.
Definition stk_common.h:381
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
Definition stk_common.h:929
Lightweight periodic trigger: returns true once per configured period when polled.
bool Poll()
Check whether the scheduled trigger time has been reached.
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
void Sleep(Timeout ticks)
Put calling process into a sleep state.
int32_t GetTickResolution() const
Get number of microseconds in one tick.
Ticks GetTicks() const
Get number of ticks elapsed since kernel start.
TId GetTid() const
Get thread Id of the currently running task.
void Delay(Timeout ticks)
Delay calling process.
static IKernelService * GetInstance()
IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
void SwitchToNext()
Notify scheduler to switch to the next task (yield).
Throwable class for catching assertions from STK_ASSERT_HANDLER().
Definition stktest.h:67