Skip to content

Commit bab41c8

Browse files
authored
Add timeouts to tests and introduce helper test functions (#35)
## Description Add timeouts to tests and introduce helper test functions. Timeouts are added via the cmake tests config and helper functions are added in the header files. ## Issues <!-- use if this PR fully resolves the issue --> Closes #28 <!-- use if this PR is linked but should not close the issue --> Related: #<issue-number> ## Testing <!-- Steps or notes on how reviewers can verify. -->
1 parent cad0615 commit bab41c8

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

include/malloc_from_scratch/memory_internal.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ MemoryBlock* getMemoryBlockFromAddress(void* address);
4444
size_t getSizeOfAllocatedMemoryBlock(MemoryBlock* block);
4545
void* getErrorCodeInVoidPtr(size_t error_code);
4646

47+
// Test helper functions to inspect allocator state
48+
inline size_t getTotalUsedMemory() { return total_memory_allocated; }
49+
inline size_t getFreeBlockInfo(int type)
50+
{
51+
// type 0 = address of first free block
52+
// type 1 = size of first free block
53+
MemoryBlock* current = block_list_head;
54+
while (current != nullptr)
55+
{
56+
if (!current->allocated_)
57+
{
58+
if (type == 0)
59+
{
60+
return reinterpret_cast<size_t>(getPayloadAddress(current));
61+
}
62+
else if (type == 1)
63+
{
64+
return current->size_;
65+
}
66+
}
67+
current = current->next_;
68+
}
69+
return 0;
70+
}
71+
4772
} // namespace internal
4873
} // namespace mem
4974

tests/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ foreach(test_file ${TEST_SOURCES})
2929
LABELS "${test_category}"
3030
)
3131
endif()
32+
33+
# Set default timeout for all tests (10 seconds)
34+
set_tests_properties(${test_name} PROPERTIES
35+
TIMEOUT 10
36+
)
37+
38+
# Longer timeouts for specific categories that need more time
39+
if(test_category MATCHES "stress")
40+
set_tests_properties(${test_name} PROPERTIES
41+
TIMEOUT 30
42+
)
43+
elseif(test_category MATCHES "concurrency")
44+
set_tests_properties(${test_name} PROPERTIES
45+
TIMEOUT 20
46+
)
47+
endif()
3248
endforeach()
3349

3450
# Add strace tests for all categories except those that would produce false positives

tests/test_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef TEST_UTILS_H
22
#define TEST_UTILS_H
33

4+
#include "malloc_from_scratch/memory_internal.h"
45
#include <cstring>
56
#include <iostream>
67

@@ -56,4 +57,8 @@ inline void test_pass()
5657
#define ASSERT_NOT_NULL(ptr) assert_not_null((ptr), __FILE__, __LINE__)
5758
#define TEST_PASS() test_pass()
5859

60+
// Internal test helpers for convenience
61+
using mem::internal::getFreeBlockInfo;
62+
using mem::internal::getTotalUsedMemory;
63+
5964
#endif // TEST_UTILS_H

0 commit comments

Comments
 (0)