|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2020 Intel Corporation.
|
| 3 | + * Copyright (c) 2025 Abderrahmane JARMOUNI |
3 | 4 | *
|
4 | 5 | * SPDX-License-Identifier: Apache-2.0
|
5 | 6 | */
|
6 | 7 |
|
7 | 8 | #include <zephyr/ztest.h>
|
8 | 9 | #include <zephyr/irq_offload.h>
|
| 10 | +#include <zephyr/ztest_error_hook.h> |
9 | 11 | #include "test_kheap.h"
|
10 | 12 |
|
11 | 13 | #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
|
@@ -431,3 +433,38 @@ ZTEST(k_heap_api, test_k_heap_realloc_fail)
|
431 | 433 |
|
432 | 434 | k_heap_free(&k_heap_test, p);
|
433 | 435 | }
|
| 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