Skip to content

Commit f039e2b

Browse files
committed
tests: kernel: mem_heap: add k_heap_aligned_alloc() coverage
add test coverage for k_heap_aligned_alloc() k_heap API Signed-off-by: Abderrahmane JARMOUNI <git@jarmouni.me>
1 parent b3ccee4 commit f039e2b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/kernel/mem_heap/k_heap_api/src/test_kheap_api.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,34 @@ ZTEST(k_heap_api, test_k_heap_realloc_fail)
431431

432432
k_heap_free(&k_heap_test, p);
433433
}
434+
435+
/**
436+
* @brief Test k_heap_aligned_alloc() API usage and edge cases
437+
*
438+
* @ingroup k_heap_api_tests
439+
*
440+
* @details Allocates a block with a specific alignment from the heap
441+
* and checks alignment, then tries oversize and invalid alignment.
442+
*
443+
* @see k_heap_aligned_alloc()
444+
*/
445+
ZTEST(k_heap_api, test_k_heap_aligned_alloc)
446+
{
447+
void *p;
448+
449+
/* Allocate 128 bytes aligned to 16 bytes */
450+
p = k_heap_aligned_alloc(&k_heap_test, 16, 128, K_NO_WAIT);
451+
zassert_not_null(p, "k_heap_aligned_alloc failed");
452+
zassert_true(((uintptr_t)p % 16) == 0, "Pointer not 16-byte aligned");
453+
k_heap_free(&k_heap_test, p);
454+
455+
/* Oversize allocation returns NULL */
456+
p = k_heap_aligned_alloc(&k_heap_test, 8, HEAP_SIZE * 2, K_NO_WAIT);
457+
zassert_is_null(p, "k_heap_aligned_alloc with oversize should fail");
458+
459+
/* With non-power-of-two alignment, Zephyr internally rewrites the alignment
460+
* and proceeds, so this should not assert or necessarily fail.
461+
*/
462+
p = k_heap_aligned_alloc(&k_heap_test, 3, 64, K_NO_WAIT);
463+
(void)p;
464+
}

0 commit comments

Comments
 (0)