tests/kernel/mheap_api_concept: Add sys_multi_heap test
Add a simple coverage test for the multi_heap utility, validating all cases with a simple configuration value that specifies an index in an array of heaps. Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
parent
85e96ff3ca
commit
f3ed0feb47
2 changed files with 62 additions and 0 deletions
|
@ -13,6 +13,7 @@ extern void test_k_aligned_alloc(void);
|
||||||
extern void test_sys_heap_mem_pool_assign(void);
|
extern void test_sys_heap_mem_pool_assign(void);
|
||||||
extern void test_malloc_in_isr(void);
|
extern void test_malloc_in_isr(void);
|
||||||
extern void test_malloc_in_thread(void);
|
extern void test_malloc_in_thread(void);
|
||||||
|
extern void test_multi_heap(void);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,6 +36,7 @@ void test_main(void)
|
||||||
ztest_unit_test(test_k_aligned_alloc),
|
ztest_unit_test(test_k_aligned_alloc),
|
||||||
ztest_unit_test(test_sys_heap_mem_pool_assign),
|
ztest_unit_test(test_sys_heap_mem_pool_assign),
|
||||||
ztest_unit_test(test_malloc_in_isr),
|
ztest_unit_test(test_malloc_in_isr),
|
||||||
|
ztest_unit_test(test_multi_heap),
|
||||||
ztest_unit_test(test_malloc_in_thread));
|
ztest_unit_test(test_malloc_in_thread));
|
||||||
ztest_run_test_suite(mheap_api);
|
ztest_run_test_suite(mheap_api);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <ztest.h>
|
#include <ztest.h>
|
||||||
#include <kernel_internal.h>
|
#include <kernel_internal.h>
|
||||||
#include <irq_offload.h>
|
#include <irq_offload.h>
|
||||||
|
#include <sys/multi_heap.h>
|
||||||
#include "test_mheap.h"
|
#include "test_mheap.h"
|
||||||
|
|
||||||
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
|
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
|
||||||
|
@ -236,3 +237,62 @@ void test_malloc_in_thread(void)
|
||||||
|
|
||||||
k_thread_abort(tid);
|
k_thread_abort(tid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define N_MULTI_HEAPS 4
|
||||||
|
#define MHEAP_BYTES 128
|
||||||
|
|
||||||
|
static struct sys_multi_heap multi_heap;
|
||||||
|
static char heap_mem[N_MULTI_HEAPS][MHEAP_BYTES];
|
||||||
|
static struct sys_heap mheaps[N_MULTI_HEAPS];
|
||||||
|
|
||||||
|
void *multi_heap_choice(struct sys_multi_heap *mheap, void *cfg,
|
||||||
|
size_t align, size_t size)
|
||||||
|
{
|
||||||
|
struct sys_heap *h = &mheaps[(int)(long)cfg];
|
||||||
|
|
||||||
|
return sys_heap_aligned_alloc(h, align, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_multi_heap(void)
|
||||||
|
{
|
||||||
|
char *blocks[N_MULTI_HEAPS];
|
||||||
|
|
||||||
|
sys_multi_heap_init(&multi_heap, multi_heap_choice);
|
||||||
|
for (int i = 0; i < N_MULTI_HEAPS; i++) {
|
||||||
|
sys_heap_init(&mheaps[i], &heap_mem[i][0], MHEAP_BYTES);
|
||||||
|
sys_multi_heap_add_heap(&multi_heap, &mheaps[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate half the buffer from each heap, make sure it works
|
||||||
|
* and that the pointer is in the correct memory
|
||||||
|
*/
|
||||||
|
for (int i = 0; i < N_MULTI_HEAPS; i++) {
|
||||||
|
blocks[i] = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
|
||||||
|
MHEAP_BYTES / 2);
|
||||||
|
|
||||||
|
zassert_not_null(blocks[i], "allocation failed");
|
||||||
|
zassert_true(blocks[i] >= &heap_mem[i][0] &&
|
||||||
|
blocks[i] < &heap_mem[i+1][0],
|
||||||
|
"allocation not in correct heap");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make sure all heaps fail to allocate another */
|
||||||
|
for (int i = 0; i < N_MULTI_HEAPS; i++) {
|
||||||
|
void *b = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
|
||||||
|
MHEAP_BYTES / 2);
|
||||||
|
|
||||||
|
zassert_is_null(b, "second allocation succeeded?");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Free all blocks */
|
||||||
|
for (int i = 0; i < N_MULTI_HEAPS; i++) {
|
||||||
|
sys_multi_heap_free(&multi_heap, blocks[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate again to make sure they're still valid */
|
||||||
|
for (int i = 0; i < N_MULTI_HEAPS; i++) {
|
||||||
|
blocks[i] = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
|
||||||
|
MHEAP_BYTES / 2);
|
||||||
|
zassert_not_null(blocks[i], "final re-allocation failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue