From e75315b13152701cd00996b5d2bbb9779ab79b69 Mon Sep 17 00:00:00 2001 From: Felix Nagele <43892671+felixnagele@users.noreply.github.com> Date: Fri, 19 Dec 2025 16:38:23 +0100 Subject: [PATCH] Add test helper functions for allocator state inspection and set default test timeouts --- include/malloc_from_scratch/memory_internal.h | 25 +++++++++++++++++++ tests/CMakeLists.txt | 16 ++++++++++++ tests/test_utils.h | 5 ++++ 3 files changed, 46 insertions(+) diff --git a/include/malloc_from_scratch/memory_internal.h b/include/malloc_from_scratch/memory_internal.h index 76f546e..86838d5 100644 --- a/include/malloc_from_scratch/memory_internal.h +++ b/include/malloc_from_scratch/memory_internal.h @@ -44,6 +44,31 @@ MemoryBlock* getMemoryBlockFromAddress(void* address); size_t getSizeOfAllocatedMemoryBlock(MemoryBlock* block); void* getErrorCodeInVoidPtr(size_t error_code); +// Test helper functions to inspect allocator state +inline size_t getTotalUsedMemory() { return total_memory_allocated; } +inline size_t getFreeBlockInfo(int type) +{ + // type 0 = address of first free block + // type 1 = size of first free block + MemoryBlock* current = block_list_head; + while (current != nullptr) + { + if (!current->allocated_) + { + if (type == 0) + { + return reinterpret_cast(getPayloadAddress(current)); + } + else if (type == 1) + { + return current->size_; + } + } + current = current->next_; + } + return 0; +} + } // namespace internal } // namespace mem diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dca58fe..963b707 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,6 +29,22 @@ foreach(test_file ${TEST_SOURCES}) LABELS "${test_category}" ) endif() + + # Set default timeout for all tests (10 seconds) + set_tests_properties(${test_name} PROPERTIES + TIMEOUT 10 + ) + + # Longer timeouts for specific categories that need more time + if(test_category MATCHES "stress") + set_tests_properties(${test_name} PROPERTIES + TIMEOUT 30 + ) + elseif(test_category MATCHES "concurrency") + set_tests_properties(${test_name} PROPERTIES + TIMEOUT 20 + ) + endif() endforeach() # Add strace tests for all categories except those that would produce false positives diff --git a/tests/test_utils.h b/tests/test_utils.h index 4486c3c..4a7b9ba 100644 --- a/tests/test_utils.h +++ b/tests/test_utils.h @@ -1,6 +1,7 @@ #ifndef TEST_UTILS_H #define TEST_UTILS_H +#include "malloc_from_scratch/memory_internal.h" #include #include @@ -56,4 +57,8 @@ inline void test_pass() #define ASSERT_NOT_NULL(ptr) assert_not_null((ptr), __FILE__, __LINE__) #define TEST_PASS() test_pass() +// Internal test helpers for convenience +using mem::internal::getFreeBlockInfo; +using mem::internal::getTotalUsedMemory; + #endif // TEST_UTILS_H