Skip to content

Commit 870bf00

Browse files
lum1n0usclaude
andcommitted
test(mem-alloc): add aligned allocation validation test
Verify aligned allocations work for powers of 2. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 13f59f4 commit 870bf00

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,36 @@ test_normal_alloc_basic(void **state)
5858

5959
mem_allocator_destroy(allocator);
6060
}
61+
62+
/* Test: Valid alignment powers of 2 */
63+
static void
64+
test_aligned_alloc_valid_alignments(void **state)
65+
{
66+
mem_allocator_t allocator;
67+
char heap_buf[128 * 1024];
68+
void *ptr;
69+
70+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
71+
assert_non_null(allocator);
72+
73+
/* Test each valid alignment */
74+
int alignments[] = {8, 16, 32, 64, 128, 256, 512, 1024};
75+
for (int i = 0; i < sizeof(alignments) / sizeof(alignments[0]); i++) {
76+
int align = alignments[i];
77+
78+
/* Allocate with size = multiple of alignment */
79+
ptr = mem_allocator_malloc_aligned(allocator, align * 2, align);
80+
assert_non_null(ptr);
81+
82+
/* Verify alignment */
83+
assert_true(is_aligned(ptr, align));
84+
85+
/* Verify marked as aligned */
86+
assert_true(is_aligned_allocation(ptr));
87+
88+
/* Free */
89+
mem_allocator_free(allocator, ptr);
90+
}
91+
92+
mem_allocator_destroy(allocator);
93+
}

tests/unit/mem-alloc/test_runner.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ main(void)
1717
{
1818
const struct CMUnitTest tests[] = {
1919
cmocka_unit_test(test_normal_alloc_basic),
20+
cmocka_unit_test(test_aligned_alloc_valid_alignments),
2021
};
2122

2223
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)