aboutsummaryrefslogtreecommitdiffstats
path: root/tests/liballoc_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/liballoc_tests.c')
-rw-r--r--tests/liballoc_tests.c41
1 files changed, 41 insertions, 0 deletions
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 <check.h>
@@ -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;