tests: heap: add test case for coverage

Add a test case for k_heap_aligned_alloc API.

Signed-off-by: Lixin Guo <lixinx.guo@intel.com>
This commit is contained in:
Lixin Guo 2021-10-18 10:35:04 +08:00 committed by Christopher Friedt
commit 72dba936cb
2 changed files with 42 additions and 1 deletions

View file

@ -11,6 +11,7 @@ extern void test_k_heap_alloc_fail(void);
extern void test_k_heap_free(void);
extern void test_kheap_alloc_in_isr_nowait(void);
extern void test_k_heap_alloc_pending(void);
extern void test_k_heap_alloc_pending_null(void);
/**
* @brief k heap api tests
@ -30,6 +31,7 @@ void test_main(void)
ztest_unit_test(test_k_heap_alloc_fail),
ztest_unit_test(test_k_heap_free),
ztest_unit_test(test_kheap_alloc_in_isr_nowait),
ztest_unit_test(test_k_heap_alloc_pending));
ztest_unit_test(test_k_heap_alloc_pending),
ztest_unit_test(test_k_heap_alloc_pending_null));
ztest_run_test_suite(k_heap_api);
}

View file

@ -38,6 +38,16 @@ static void thread_alloc_heap(void *p1, void *p2, void *p3)
k_heap_free(&k_heap_test, p);
}
static void thread_alloc_heap_null(void *p1, void *p2, void *p3)
{
k_timeout_t timeout = Z_TIMEOUT_MS(200);
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_2, timeout);
zassert_is_null(p, "k_heap_alloc_null operation failed");
k_heap_free(&k_heap_test, p);
}
/*test cases*/
/* These need to be adjacent in BSS */
@ -190,3 +200,32 @@ void test_k_heap_alloc_pending(void)
k_thread_join(tid, K_FOREVER);
}
/**
* @brief Validate the k_heap alloc_pending_null support.
*
* @details In main thread alloc two buffer from the heap, then run the
* child thread which alloc a buffer larger than remaining space. The child thread
* will wait timeout long until main thread free one of the buffer to heap, space in
* the heap is still not enough and then return null after timeout.
*
* @ingroup kernel_heap_tests
*/
void test_k_heap_alloc_pending_null(void)
{
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
thread_alloc_heap_null, NULL, NULL, NULL,
K_PRIO_PREEMPT(5), 0, K_NO_WAIT);
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, K_NO_WAIT);
char *q = (char *)k_heap_alloc(&k_heap_test, 512, K_NO_WAIT);
zassert_not_null(p, "k_heap_alloc operation failed");
zassert_not_null(q, "k_heap_alloc operation failed");
/* make the child thread run */
k_msleep(1);
k_heap_free(&k_heap_test, q);
k_thread_join(tid, K_FOREVER);
k_heap_free(&k_heap_test, p);
}