Skip to content

Commit f678b6c

Browse files
lum1n0usclaude
andcommitted
test(mem-alloc): add stress tests
Test many allocations and mixed allocation patterns. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 9df7e29 commit f678b6c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,76 @@ test_mixed_obj_to_hmu(void **state)
288288

289289
mem_allocator_destroy(allocator);
290290
}
291+
292+
/* Test: Many aligned allocations */
293+
static void
294+
test_aligned_alloc_many(void **state)
295+
{
296+
mem_allocator_t allocator;
297+
char heap_buf[512 * 1024];
298+
void *ptrs[100];
299+
int count = 0;
300+
301+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
302+
assert_non_null(allocator);
303+
304+
/* Allocate as many as possible */
305+
for (int i = 0; i < 100; i++) {
306+
int align = (i % 4 == 0) ? 64 : 32;
307+
ptrs[i] = mem_allocator_malloc_aligned(allocator, align * 2, align);
308+
if (ptrs[i]) {
309+
assert_true(is_aligned(ptrs[i], align));
310+
count++;
311+
} else {
312+
break;
313+
}
314+
}
315+
316+
assert_true(count > 10); /* At least some should succeed */
317+
318+
/* Free all */
319+
for (int i = 0; i < count; i++) {
320+
mem_allocator_free(allocator, ptrs[i]);
321+
}
322+
323+
mem_allocator_destroy(allocator);
324+
}
325+
326+
/* Test: Many mixed allocations */
327+
static void
328+
test_mixed_alloc_many(void **state)
329+
{
330+
mem_allocator_t allocator;
331+
char heap_buf[512 * 1024];
332+
void *ptrs[200];
333+
int count = 0;
334+
335+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
336+
assert_non_null(allocator);
337+
338+
/* Alternate normal and aligned */
339+
for (int i = 0; i < 200; i++) {
340+
if (i % 2 == 0) {
341+
/* Normal allocation */
342+
ptrs[i] = mem_allocator_malloc(allocator, 64);
343+
} else {
344+
/* Aligned allocation */
345+
ptrs[i] = mem_allocator_malloc_aligned(allocator, 64, 32);
346+
}
347+
348+
if (ptrs[i]) {
349+
count++;
350+
} else {
351+
break;
352+
}
353+
}
354+
355+
assert_true(count > 20);
356+
357+
/* Free in reverse order */
358+
for (int i = count - 1; i >= 0; i--) {
359+
mem_allocator_free(allocator, ptrs[i]);
360+
}
361+
362+
mem_allocator_destroy(allocator);
363+
}

tests/unit/mem-alloc/test_runner.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ main(void)
2424
cmocka_unit_test(test_aligned_alloc_size_not_multiple),
2525
cmocka_unit_test(test_mixed_alloc_interleaved),
2626
cmocka_unit_test(test_mixed_obj_to_hmu),
27+
cmocka_unit_test(test_aligned_alloc_many),
28+
cmocka_unit_test(test_mixed_alloc_many),
2729
};
2830

2931
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)