diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 16:49:31 -0500 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-02 16:49:31 -0500 |
| commit | f7325bf190db48ae8771947b0fcd25f0bba64679 (patch) | |
| tree | b179175beec0c4c67afa785f717783b29f351dc8 /tests/liballoc_tests.c | |
| parent | 2347c038520bc3dc6f3990a6b088d4aa8fd17c7a (diff) | |
| download | liballoc-f7325bf190db48ae8771947b0fcd25f0bba64679.tar.gz | |
Added tests of the integrity of the headers to alloction testing
Zero all allocated memory and check the headers to ensure that
the sizes and/or magic numbers are still correct. This ensures that
the allocator isn't returning overlapping memory regions.
Diffstat (limited to 'tests/liballoc_tests.c')
| -rw-r--r-- | tests/liballoc_tests.c | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c index 6b4a4d3..fed6080 100644 --- a/tests/liballoc_tests.c +++ b/tests/liballoc_tests.c @@ -32,16 +32,25 @@ END_TEST START_TEST(multiple_allocations) { size_t size = ALIGNMENT*5; + + void *allocations[100]; for (size_t i=0; i<100; i++) { - void *memory = allocate(size); - ck_assert_ptr_nonnull(memory); + allocations[i] = allocate(size); + ck_assert_ptr_nonnull(allocations[i]); - memset(memory, 0, size); + memset(allocations[i], 0, size); - size_t alignment = (size_t) memory % ALIGNMENT; + size_t alignment = (size_t) allocations[i]% ALIGNMENT; ck_assert_int_eq(alignment, 0); } + /* verify the headers */ + for (size_t i=0; i<100; i++) { + header *hdr = allocations[i] - sizeof(header); + ck_assert_int_eq(hdr->size, size); + ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); + } + /* leak the memory--we aren't testing release */ } @@ -70,19 +79,32 @@ END_TEST START_TEST(unaligned_allocation) { size_t unaligned_size = ALIGNMENT + 3; + void *allocations[100]; /* ensure first allocation is aligned */ - void *first_memory = allocate(unaligned_size); - size_t first_alignment = (size_t) first_memory % ALIGNMENT; + allocations[0] = allocate(unaligned_size); + size_t first_alignment = (size_t) allocations[0] % ALIGNMENT; ck_assert_int_eq(first_alignment, 0); + memset(allocations[0], 0, unaligned_size); + /* now allocate several more times--each allocation should be aligned */ - for (size_t i=0; i<10; i++) { - void *memory = allocate(unaligned_size); - size_t alignment = (size_t) memory % ALIGNMENT; + for (size_t i=1; i<100; i++) { + allocations[i] = allocate(unaligned_size); + size_t alignment = (size_t) allocations[i] % ALIGNMENT; ck_assert_int_eq(alignment, 0); + /* verify we can write the the memory */ + memset(allocations[i], 0, unaligned_size); + /* just leak the memory--we aren't testing release here */ } + + /* verify the headers */ + for (size_t i=0; i<100; i++) { + header *hdr = allocations[i] - sizeof(header); + ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); + } + } |