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
test_blockpool.cpp File Reference
#include <stk_config.h>
#include <stk.h>
#include <memory/stk_memory_blockpool.h>
#include <assert.h>
#include <string.h>
#include "stktest_context.h"
Include dependency graph for test_blockpool.cpp:

Go to the source code of this file.

Classes

class  stk::test::blockpool::TryAllocFreeTask< _AccessMode >
 Verifies TryAlloc returns a valid block, Free recycles it, and pool accounting stays consistent throughout. More...
class  stk::test::blockpool::ExhaustPoolTask< _AccessMode >
 Drains the entire pool via TryAlloc, verifies IsFull() and that a further TryAlloc returns nullptr, then frees every block. More...
class  stk::test::blockpool::BlockingAllocTask< _AccessMode >
 Task 0 holds all blocks; Task 1 blocks in Alloc(); Task 0 frees one block and verifies Task 1 unblocks and completes successfully. More...
class  stk::test::blockpool::TimedAllocTimeoutTask< _AccessMode >
 Task 0 holds all blocks; Task 1 calls TimedAlloc with a short timeout that must expire, returning nullptr within the expected window. More...
class  stk::test::blockpool::TimedAllocSuccessTask< _AccessMode >
 Task 0 holds all blocks; Task 1 calls TimedAlloc with a generous timeout; Task 0 frees a block before the timeout so Task 1 succeeds. More...
class  stk::test::blockpool::ConcurrentAllocFreeTask< _AccessMode >
 All tasks race to alloc a block, increment a shared counter inside the block, copy it out and free; total counter must equal iterations * tasks. More...
struct  stk::test::blockpool::TestRecord
class  stk::test::blockpool::TypedAllocTask< _AccessMode >
 Verifies that the typed wrappers AllocT<T>(), TryAllocT<T>(), and TimedAllocT<T>() return correctly typed pointers and that written fields are preserved until Free(). More...
class  stk::test::blockpool::FreeNullTask< _AccessMode >
 Ensures Free(nullptr) returns false and does not corrupt the pool. More...
class  stk::test::blockpool::AlignBlockSizeTask< _AccessMode >
 Verifies AlignBlockSize() rounds up to BLOCK_ALIGN multiples and never returns a value smaller than BLOCK_ALIGN. More...
class  stk::test::blockpool::StorageModeTask< _AccessMode >
 Creates a second pool using heap storage and verifies accessors report correct values for both storage modes. More...
class  stk::test::blockpool::StressTask< _AccessMode >
 All tasks hammer the pool with a mix of TryAlloc, blocking Alloc, and TimedAlloc operations; verifies the pool never leaks or deadlocks. More...

Namespaces

namespace  stk
 Namespace of STK package.
namespace  stk::test
 Namespace of the test inventory.
namespace  stk::test::blockpool
 Namespace of BlockMemoryPool test.

Macros

#define _STK_POOL_TEST_TASKS_MAX   5
#define _STK_POOL_TEST_TIMEOUT   1000
#define _STK_POOL_TEST_SHORT_SLEEP   10
#define _STK_POOL_TEST_LONG_SLEEP   100
#define _STK_POOL_BLOCK_SIZE   32U
#define _STK_POOL_CAPACITY   8U
#define _STK_POOL_STACK_SIZE   256
#define STK_TASK   static
#define RUN(TestClass, name, param)

Functions

void STK_ASSERT_HANDLER (const char *message, const char *file, int32_t line)
static void stk::test::blockpool::ResetTestState ()
static bool NeedsTwoTasks (const char *test_name)
static bool NeedsAllTasks (const char *test_name)
template<class TaskType>
static int32_t RunTest (const char *test_name, int32_t param=0)
int main (int argc, char **argv)

Variables

static volatile int32_t stk::test::blockpool::g_TestResult = 0
static volatile int32_t stk::test::blockpool::g_InstancesDone = 0
static volatile int32_t stk::test::blockpool::g_SharedCounter = 0
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC|(STK_TICKLESS_IDLE ? KERNEL_TICKLESS :0), 5, SwitchStrategyRR, PlatformDefaultstk::test::blockpool::g_Kernel
static uint8_t stk::test::blockpool::g_PoolStorage [8U *stk::memory::BlockMemoryPool::AlignBlockSize(32U)]
static stk::memory::BlockMemoryPoolstk::test::blockpool::g_Pool = nullptr

Macro Definition Documentation

◆ _STK_POOL_BLOCK_SIZE

◆ _STK_POOL_CAPACITY

◆ _STK_POOL_STACK_SIZE

#define _STK_POOL_STACK_SIZE   256

Definition at line 33 of file test_blockpool.cpp.

◆ _STK_POOL_TEST_LONG_SLEEP

#define _STK_POOL_TEST_LONG_SLEEP   100

◆ _STK_POOL_TEST_SHORT_SLEEP

◆ _STK_POOL_TEST_TASKS_MAX

◆ _STK_POOL_TEST_TIMEOUT

#define _STK_POOL_TEST_TIMEOUT   1000

Definition at line 24 of file test_blockpool.cpp.

◆ RUN

#define RUN ( TestClass,
name,
param )
Value:
do { \
if (RunTest<TestClass<ACCESS_PRIVILEGED>>(name, param) \
total_failures++; \
else \
total_success++; \
} while (0)
static int32_t RunTest(const char *test_name, int32_t param=0)
@ SUCCESS_EXIT_CODE
exit code for exit() to denote the success of the test

Referenced by main().

◆ STK_TASK

#define STK_TASK   static

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 811 of file test_blockpool.cpp.

812{
813 (void)argc;
814 (void)argv;
815
817
818 int total_failures = 0, total_success = 0;
819
820 printf("--------------\n");
821
823
824#define RUN(TestClass, name, param) \
825 do { \
826 if (RunTest<TestClass<ACCESS_PRIVILEGED>>(name, param) \
827 != TestContext::SUCCESS_EXIT_CODE) \
828 total_failures++; \
829 else \
830 total_success++; \
831 } while (0)
832
833#ifndef __ARM_ARCH_6M__
834
835 // Test 1: TryAlloc / Free basic cycle
837
838 // Test 2: Exhaust pool, verify IsFull, free all
840
841 // Test 3: Blocking Alloc unblocked by Free from another task
842 RUN(stk::test::blockpool::BlockingAllocTask, "BlockingAlloc", 0);
843
844 // Test 4: TimedAlloc expires when pool remains full
845 RUN(stk::test::blockpool::TimedAllocTimeoutTask, "TimedAllocTimeout", 0);
846
847 // Test 5: TimedAlloc succeeds when block freed within timeout
848 RUN(stk::test::blockpool::TimedAllocSuccessTask, "TimedAllocSuccess", 0);
849
850 // Test 6: Concurrent alloc/free counter integrity (20 iterations per task)
851 RUN(stk::test::blockpool::ConcurrentAllocFreeTask, "ConcurrentAllocFree", 20);
852
853 // Test 7: Typed helpers AllocT / TryAllocT / TimedAllocT
855
856 // Test 8: Free(nullptr) is a safe no-op
858
859 // Test 9: AlignBlockSize static helper
860 RUN(stk::test::blockpool::AlignBlockSizeTask, "AlignBlockSize", 0);
861
862 // Test 10: External vs heap storage constructors + accessors
864
865#endif // __ARM_ARCH_6M__
866
867 // Test 11: Stress test (ARM Cortex-M0 compatible)
869
870#undef RUN
871
872 int32_t final_result = (total_failures == 0
875
876 printf("##############\n");
877 printf("Total tests: %d\n", total_failures + total_success);
878 printf("Failures: %d\n", (int)total_failures);
879
881 return final_result;
882}
#define RUN(TestClass, name, param)
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC|(STK_TICKLESS_IDLE ? KERNEL_TICKLESS :0), 5, SwitchStrategyRR, PlatformDefault > g_Kernel
Verifies TryAlloc returns a valid block, Free recycles it, and pool accounting stays consistent throu...
Drains the entire pool via TryAlloc, verifies IsFull() and that a further TryAlloc returns nullptr,...
Task 0 holds all blocks; Task 1 blocks in Alloc(); Task 0 frees one block and verifies Task 1 unblock...
Task 0 holds all blocks; Task 1 calls TimedAlloc with a short timeout that must expire,...
Task 0 holds all blocks; Task 1 calls TimedAlloc with a generous timeout; Task 0 frees a block before...
All tasks race to alloc a block, increment a shared counter inside the block, copy it out and free; t...
Verifies that the typed wrappers AllocT<T>(), TryAllocT<T>(), and TimedAllocT<T>() return correctly t...
Ensures Free(nullptr) returns false and does not corrupt the pool.
Verifies AlignBlockSize() rounds up to BLOCK_ALIGN multiples and never returns a value smaller than B...
Creates a second pool using heap storage and verifies accessors report correct values for both storag...
All tasks hammer the pool with a mix of TryAlloc, blocking Alloc, and TimedAlloc operations; verifies...
static void ShowTestSuitePrologue()
Show text string as prologue before tests start.
@ DEFAULT_FAILURE_EXIT_CODE
default exit code for exit() to denote failure of the test
static void ShowTestSuiteEpilogue(int32_t result)
Show text string as epilogue after tests end.

References stk::test::TestContext::DEFAULT_FAILURE_EXIT_CODE, stk::test::blockpool::g_Kernel, RUN, stk::test::TestContext::ShowTestSuiteEpilogue(), stk::test::TestContext::ShowTestSuitePrologue(), and stk::test::TestContext::SUCCESS_EXIT_CODE.

Here is the call graph for this function:

◆ NeedsAllTasks()

bool NeedsAllTasks ( const char * test_name)
static

Definition at line 740 of file test_blockpool.cpp.

741{
742 return (strcmp(test_name, "ConcurrentAllocFree") == 0) ||
743 (strcmp(test_name, "Stress") == 0);
744}

Referenced by RunTest().

Here is the caller graph for this function:

◆ NeedsTwoTasks()

bool NeedsTwoTasks ( const char * test_name)
static

Definition at line 726 of file test_blockpool.cpp.

727{
728 return (strcmp(test_name, "BlockingAlloc") == 0) ||
729 (strcmp(test_name, "TimedAllocTimeout") == 0) ||
730 (strcmp(test_name, "TimedAllocSuccess") == 0) ||
731 (strcmp(test_name, "ConcurrentAllocFree")== 0) ||
732 (strcmp(test_name, "Stress") == 0);
733}

Referenced by RunTest().

Here is the caller graph for this function:

◆ RunTest()

template<class TaskType>
int32_t RunTest ( const char * test_name,
int32_t param = 0 )
static

Definition at line 754 of file test_blockpool.cpp.

755{
756 using namespace stk;
757 using namespace stk::test;
758 using namespace stk::test::blockpool;
759
760 printf("Test: %s\n", test_name);
761
763
768 sizeof(g_PoolStorage));
769
770 g_Pool = &pool;
771
772 STK_TASK TaskType task0(0, param);
773 STK_TASK TaskType task1(1, param);
774 TaskType task2(2, param);
775 TaskType task3(3, param);
776 TaskType task4(4, param);
777
778 g_Kernel.AddTask(&task0);
779
780 if (NeedsTwoTasks(test_name))
781 g_Kernel.AddTask(&task1);
782
783 if (NeedsAllTasks(test_name))
784 {
785 g_Kernel.AddTask(&task2);
786 g_Kernel.AddTask(&task3);
787 g_Kernel.AddTask(&task4);
788 }
789
790 g_Kernel.Start();
791
792 g_Pool = nullptr;
793
794 int32_t result = (g_TestResult
797
798 printf("Result: %s\n", result == TestContext::SUCCESS_EXIT_CODE ? "PASS" : "FAIL");
799 printf("--------------\n");
800
801 return result;
802}
static bool NeedsTwoTasks(const char *test_name)
static bool NeedsAllTasks(const char *test_name)
#define _STK_POOL_BLOCK_SIZE
#define _STK_POOL_CAPACITY
#define STK_TASK
Namespace of STK package.
BlockMemoryPool(size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name=nullptr)
Construct a pool backed by caller-supplied (external) storage.
Namespace of the test inventory.
Namespace of BlockMemoryPool test.
static void ResetTestState()
static stk::memory::BlockMemoryPool * g_Pool
static volatile int32_t g_TestResult
static uint8_t g_PoolStorage[8U *stk::memory::BlockMemoryPool::AlignBlockSize(32U)]
static TestTask< ACCESS_PRIVILEGED > task3(2)
static TestTask< ACCESS_PRIVILEGED > task2(1)

References _STK_POOL_BLOCK_SIZE, _STK_POOL_CAPACITY, stk::memory::BlockMemoryPool(), stk::test::TestContext::DEFAULT_FAILURE_EXIT_CODE, stk::test::blockpool::g_Kernel, stk::test::blockpool::g_Pool, stk::test::blockpool::g_PoolStorage, stk::test::blockpool::g_TestResult, NeedsAllTasks(), NeedsTwoTasks(), stk::test::blockpool::ResetTestState(), STK_TASK, and stk::test::TestContext::SUCCESS_EXIT_CODE.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ STK_ASSERT_HANDLER()

void STK_ASSERT_HANDLER ( const char * message,
const char * file,
int32_t line )
extern

Definition at line 21 of file test_blockpool.cpp.

References STK_TEST_DECL_ASSERT.