From dc2fdc649f7dad27da775c7e0b93b8195d313553 Mon Sep 17 00:00:00 2001 From: "Douglas B. Rumbaugh" Date: Sun, 2 Nov 2025 18:30:08 -0500 Subject: Added large scale stress test --- tests/liballoc_tests.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests') diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c index a54998a..d9794ca 100644 --- a/tests/liballoc_tests.c +++ b/tests/liballoc_tests.c @@ -10,6 +10,7 @@ * */ #include "alloc.h" +#include "alloc_header.h" #include "constants.h" #include @@ -216,6 +217,45 @@ START_TEST(free_list_split_below_threshold) { ck_assert_ptr_null(free_list_head()); } +END_TEST + +START_TEST(stress_test) { + size_t n = 1000; + size_t k = 100000; + void *allocations[n] = {}; + + for (size_t i = 0; i < k; i++) { + size_t idx = rand() % n; + if (allocations[idx]) { + release(allocations[idx]); + allocations[idx] = NULL; + } else { + size_t size = rand() % 4096; + allocations[idx] = allocate(size); + ck_assert_int_eq((size_t)allocations[idx] % ALIGNMENT, 0); + memset(allocations[idx], 0, size); + } + + if (i % 500 == 0) { + for (size_t j = 0; j < n; j++) { + if (allocations[j]) { + header *hdr = (header *)(allocations[j] - sizeof(header)); + ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); + } + } + + free_nd *fl = free_list_head(); + for (free_nd *nd = fl; nd; nd = nd->next) { + header *hdr = (header *)((void *)nd - sizeof(header)); + ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); + ck_assert_int_eq(hdr->size, nd->size); + + ck_assert_ptr_ne(nd->next, ((void *)nd) + nd->size + sizeof(header)); + } + } + } +} +END_TEST Suite *liballoc_suite(void) { Suite *s; @@ -235,6 +275,7 @@ Suite *liballoc_suite(void) { tcase_add_test(unit, free_list_coalesce_backward); tcase_add_test(unit, free_list_split_basic); tcase_add_test(unit, free_list_split_below_threshold); + tcase_add_test(unit, stress_test); suite_add_tcase(s, unit); return s; -- cgit v1.2.3