Skip to content

Commit

Permalink
tests/cmsis_rtos_v2: Introduce tests to make use of mempool APIs
Browse files Browse the repository at this point in the history
Some test programs to illustrate the usage of mempool APIs.

Signed-off-by: Rajavardhan Gundi <[email protected]>
  • Loading branch information
rgundi authored and carlescufi committed Dec 20, 2018
1 parent e5c8488 commit 00ffaed
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tests/cmsis_rtos_v2/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern void test_timer(void);
extern void test_mutex(void);
extern void test_mutex_lock_timeout(void);
extern void test_semaphore(void);
extern void test_mempool(void);

void test_main(void)
{
Expand All @@ -23,7 +24,8 @@ void test_main(void)
ztest_unit_test(test_timer),
ztest_unit_test(test_mutex),
ztest_unit_test(test_mutex_lock_timeout),
ztest_unit_test(test_semaphore));
ztest_unit_test(test_semaphore),
ztest_unit_test(test_mempool));

ztest_run_test_suite(test_cmsis_v2_apis);
}
90 changes: 90 additions & 0 deletions tests/cmsis_rtos_v2/src/mempool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <ztest.h>
#include <kernel.h>
#include <cmsis_os2.h>

#define MAX_BLOCKS 10
#define TIMEOUT_TICKS 10

struct mem_block {
int member1;
int member2;
};

static char __aligned(4) sample_mem[sizeof(struct mem_block) * MAX_BLOCKS];
static const osMemoryPoolAttr_t mp_attrs = {
.name = "TestMempool",
.attr_bits = 0,
.cb_mem = NULL,
.cb_size = 0,
.mp_mem = sample_mem,
.mp_size = sizeof(struct mem_block) * MAX_BLOCKS,
};

/**
* @brief Test memory pool allocation and free
*
* @see osMemoryPoolNew(), osMemoryPoolAlloc(), osMemoryPoolFree(),
*/
void test_mempool(void)
{
int i;
osMemoryPoolId_t mp_id;
const char *name;
struct mem_block *addr_list[MAX_BLOCKS + 1];
osStatus_t status;

mp_id = osMemoryPoolNew(MAX_BLOCKS, sizeof(struct mem_block),
&mp_attrs);
zassert_true(mp_id != NULL, "mempool creation failed");

name = osMemoryPoolGetName(mp_id);
zassert_true(strcmp(mp_attrs.name, name) == 0,
"Error getting mempool name");

zassert_equal(osMemoryPoolGetCapacity(mp_id), MAX_BLOCKS,
"Something's wrong with osMemoryPoolGetCapacity!");

zassert_equal(osMemoryPoolGetBlockSize(mp_id),
sizeof(struct mem_block),
"Something's wrong with osMemoryPoolGetBlockSize!");

/* The memory pool should be completely available at this point */
zassert_equal(osMemoryPoolGetCount(mp_id), 0,
"Something's wrong with osMemoryPoolGetCount!");
zassert_equal(osMemoryPoolGetSpace(mp_id), MAX_BLOCKS,
"Something's wrong with osMemoryPoolGetSpace!");

for (i = 0; i < MAX_BLOCKS; i++) {
addr_list[i] = (struct mem_block *)osMemoryPoolAlloc(mp_id,
osWaitForever);
zassert_true(addr_list[i] != NULL, "mempool allocation failed");
}

/* The memory pool should be completely in use at this point */
zassert_equal(osMemoryPoolGetCount(mp_id), MAX_BLOCKS,
"Something's wrong with osMemoryPoolGetCount!");
zassert_equal(osMemoryPoolGetSpace(mp_id), 0,
"Something's wrong with osMemoryPoolGetSpace!");

/* All blocks in mempool are allocated, any more allocation
* without free should fail
*/
addr_list[i] = (struct mem_block *)osMemoryPoolAlloc(mp_id,
TIMEOUT_TICKS);
zassert_true(addr_list[i] == NULL, "allocation happened."
" Something's wrong!");

for (i = 0; i < MAX_BLOCKS; i++) {
status = osMemoryPoolFree(mp_id, addr_list[i]);
zassert_true(status == osOK, "mempool free failed");
}

status = osMemoryPoolDelete(mp_id);
zassert_true(status == osOK, "mempool delete failure");
}

0 comments on commit 00ffaed

Please sign in to comment.