Browse Source

tests: kernel: k_heap: add tests for k_heap_realloc

There was zero test for this new-ish kernel API so add them.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
pull/89914/merge
Benjamin Cabé 4 weeks ago committed by Dan Kalowsky
parent
commit
524e44fd79
  1. 112
      tests/kernel/mem_heap/k_heap_api/src/test_kheap_api.c

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

@ -319,3 +319,115 @@ ZTEST(k_heap_api, test_k_heap_array_get)
} }
zassert_true(test_heap_found); zassert_true(test_heap_found);
} }
/**
* @brief Test k_heap_realloc() API functionality.
*
* @ingroup k_heap_api_tests
*
* @details The test validates k_heap_realloc() API by:
* 1. Allocating memory and reallocating to a larger size
* 2. Reallocating to a smaller size
* 3. Verifying data integrity after reallocation
*
* @see k_heap_realloc()
*/
ZTEST(k_heap_api, test_k_heap_realloc)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p, *p2, *p3;
p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, timeout);
zassert_not_null(p, "k_heap_alloc operation failed");
/* Fill the allocated memory with a pattern */
for (int i = 0; i < ALLOC_SIZE_1; i++) {
p[i] = 'A' + (i % 26);
}
/* Reallocate to a larger size */
p2 = (char *)k_heap_realloc(&k_heap_test, p, ALLOC_SIZE_2, timeout);
zassert_not_null(p2, "k_heap_realloc operation failed");
/* Verify the original data is preserved */
for (int i = 0; i < ALLOC_SIZE_1; i++) {
zassert_equal(p2[i], 'A' + (i % 26), "Data integrity check failed");
}
/* Reallocate to a smaller size */
p3 = (char *)k_heap_realloc(&k_heap_test, p2, ALLOC_SIZE_1 / 2, timeout);
zassert_not_null(p3, "k_heap_realloc operation failed");
/* Verify the data is preserved up to the new size */
for (int i = 0; i < ALLOC_SIZE_1 / 2; i++) {
zassert_equal(p3[i], 'A' + (i % 26), "Data integrity check failed");
}
k_heap_free(&k_heap_test, p3);
}
/**
* @brief Test k_heap_realloc() with NULL pointer.
*
* @ingroup k_heap_api_tests
*
* @details The test validates that k_heap_realloc() behaves like k_heap_alloc()
* when called with a NULL pointer.
*
* @see k_heap_realloc()
*/
ZTEST(k_heap_api, test_k_heap_realloc_null)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p = (char *)k_heap_realloc(&k_heap_test, NULL, ALLOC_SIZE_1, timeout);
zassert_not_null(p, "k_heap_realloc with NULL pointer failed");
k_heap_free(&k_heap_test, p);
}
/**
* @brief Test k_heap_realloc() with size 0.
*
* @ingroup k_heap_api_tests
*
* @details The test validates that k_heap_realloc() behaves like k_heap_free()
* when called with size 0.
*
* @see k_heap_realloc()
*/
ZTEST(k_heap_api, test_k_heap_realloc_zero)
{
char *p, *p2;
p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, K_NO_WAIT);
zassert_not_null(p, "k_heap_alloc operation failed");
/* Realloc with size 0 should free the memory */
p2 = (char *)k_heap_realloc(&k_heap_test, p, 0, K_NO_WAIT);
zassert_is_null(p2, "k_heap_realloc with size 0 should return NULL");
}
/**
* @brief Test k_heap_realloc() failure case.
*
* @ingroup k_heap_api_tests
*
* @details The test validates that k_heap_realloc() returns NULL when
* trying to reallocate to a size larger than the heap.
*
* @see k_heap_realloc()
*/
ZTEST(k_heap_api, test_k_heap_realloc_fail)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p, *p2;
p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, timeout);
zassert_not_null(p, "k_heap_alloc operation failed");
/* Try to reallocate to a size larger than the heap */
p2 = (char *)k_heap_realloc(&k_heap_test, p, HEAP_SIZE + 1, timeout);
zassert_is_null(p2, "k_heap_realloc should fail for size larger than heap");
k_heap_free(&k_heap_test, p);
}

Loading…
Cancel
Save