Skip to content

Commit 7f57759

Browse files
JarmouniAcarlescufi
authored andcommitted
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 <[email protected]>
1 parent 9a1fe30 commit 7f57759

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONFIG_ZTEST=y
22
CONFIG_IRQ_OFFLOAD=y
3+
CONFIG_ZTEST_FATAL_HOOK=y

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
22
* Copyright (c) 2020 Intel Corporation.
3+
* Copyright (c) 2025 Abderrahmane JARMOUNI
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

78
#include <zephyr/ztest.h>
89
#include <zephyr/irq_offload.h>
10+
#include <zephyr/ztest_error_hook.h>
911
#include "test_kheap.h"
1012

1113
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
@@ -431,3 +433,38 @@ ZTEST(k_heap_api, test_k_heap_realloc_fail)
431433

432434
k_heap_free(&k_heap_test, p);
433435
}
436+
437+
/**
438+
* @brief Test k_heap_aligned_alloc() API usage and edge cases
439+
*
440+
* @ingroup k_heap_api_tests
441+
*
442+
* @details Allocates a block with a specific alignment from the heap
443+
* and checks alignment, then tries oversize and invalid alignment.
444+
*
445+
* @see k_heap_aligned_alloc()
446+
*/
447+
ZTEST(k_heap_api, test_k_heap_aligned_alloc)
448+
{
449+
void *p;
450+
451+
/* Allocate 128 bytes aligned to 16 bytes */
452+
p = k_heap_aligned_alloc(&k_heap_test, 16, 128, K_NO_WAIT);
453+
zassert_not_null(p, "k_heap_aligned_alloc failed");
454+
zassert_true(((uintptr_t)p % 16) == 0, "Pointer not 16-byte aligned");
455+
k_heap_free(&k_heap_test, p);
456+
457+
/* Oversize allocation returns NULL */
458+
p = k_heap_aligned_alloc(&k_heap_test, 8, HEAP_SIZE * 2, K_NO_WAIT);
459+
zassert_is_null(p, "k_heap_aligned_alloc with oversize should fail");
460+
461+
ztest_set_fault_valid(true);
462+
/* invalid alignment, should assert */
463+
p = k_heap_aligned_alloc(&k_heap_test, 3, 64, K_NO_WAIT);
464+
(void)p;
465+
/*
466+
* If calling k_heap_aligned_alloc with invalid alignment didn't result in an assert
467+
* then the API isn't working as expected, and the test shall fail.
468+
*/
469+
ztest_test_fail();
470+
}

0 commit comments

Comments
 (0)