Skip to content

Commit 9df7e29

Browse files
lum1n0usclaude
andcommitted
test(mem-alloc): add mixed allocation tests
Verify normal and aligned allocations can coexist and obj_to_hmu works correctly for both. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 9af538d commit 9df7e29

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,83 @@ test_aligned_alloc_size_not_multiple(void **state)
208208

209209
mem_allocator_destroy(allocator);
210210
}
211+
212+
/* Test: Mixed normal and aligned allocations */
213+
static void
214+
test_mixed_alloc_interleaved(void **state)
215+
{
216+
mem_allocator_t allocator;
217+
char heap_buf[128 * 1024];
218+
void *normal1, *aligned1, *normal2, *aligned2;
219+
220+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
221+
assert_non_null(allocator);
222+
223+
/* Allocate: normal -> aligned -> normal -> aligned */
224+
normal1 = mem_allocator_malloc(allocator, 64);
225+
assert_non_null(normal1);
226+
assert_false(is_aligned_allocation(normal1));
227+
228+
aligned1 = mem_allocator_malloc_aligned(allocator, 128, 64);
229+
assert_non_null(aligned1);
230+
assert_true(is_aligned_allocation(aligned1));
231+
assert_true(is_aligned(aligned1, 64));
232+
233+
normal2 = mem_allocator_malloc(allocator, 96);
234+
assert_non_null(normal2);
235+
assert_false(is_aligned_allocation(normal2));
236+
237+
aligned2 = mem_allocator_malloc_aligned(allocator, 256, 128);
238+
assert_non_null(aligned2);
239+
assert_true(is_aligned_allocation(aligned2));
240+
assert_true(is_aligned(aligned2, 128));
241+
242+
/* Free in mixed order */
243+
mem_allocator_free(allocator, normal1);
244+
mem_allocator_free(allocator, aligned2);
245+
mem_allocator_free(allocator, normal2);
246+
mem_allocator_free(allocator, aligned1);
247+
248+
mem_allocator_destroy(allocator);
249+
}
250+
251+
/* Test: obj_to_hmu works correctly for both types */
252+
static void
253+
test_mixed_obj_to_hmu(void **state)
254+
{
255+
mem_allocator_t allocator;
256+
char heap_buf[64 * 1024];
257+
void *normal, *aligned;
258+
hmu_t *hmu_normal, *hmu_aligned;
259+
260+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
261+
assert_non_null(allocator);
262+
263+
/* Allocate both types */
264+
normal = mem_allocator_malloc(allocator, 128);
265+
assert_non_null(normal);
266+
267+
aligned = mem_allocator_malloc_aligned(allocator, 128, 64);
268+
assert_non_null(aligned);
269+
270+
/* Get HMU pointers */
271+
hmu_normal = obj_to_hmu(normal);
272+
hmu_aligned = obj_to_hmu(aligned);
273+
274+
assert_non_null(hmu_normal);
275+
assert_non_null(hmu_aligned);
276+
277+
/* Both should have HMU_VO type */
278+
assert_int_equal(hmu_get_ut(hmu_normal), HMU_VO);
279+
assert_int_equal(hmu_get_ut(hmu_aligned), HMU_VO);
280+
281+
/* Sizes should be reasonable */
282+
assert_true(hmu_get_size(hmu_normal) >= 128);
283+
assert_true(hmu_get_size(hmu_aligned) >= 128);
284+
285+
/* Free both */
286+
mem_allocator_free(allocator, normal);
287+
mem_allocator_free(allocator, aligned);
288+
289+
mem_allocator_destroy(allocator);
290+
}

tests/unit/mem-alloc/test_runner.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ main(void)
2222
cmocka_unit_test(test_normal_realloc_works),
2323
cmocka_unit_test(test_aligned_alloc_invalid_not_power_of_2),
2424
cmocka_unit_test(test_aligned_alloc_size_not_multiple),
25+
cmocka_unit_test(test_mixed_alloc_interleaved),
26+
cmocka_unit_test(test_mixed_obj_to_hmu),
2527
};
2628

2729
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)