tests: cmsis_rtos_v1: Add and enhance test suite

Add tests for kernel managament APIs and enhance memory
pool tests with bulk allocation, free and negative cases.

Signed-off-by: Spoorthi K <spoorthi.k@intel.com>
This commit is contained in:
Spoorthi K 2018-08-16 10:38:27 +05:30 committed by Anas Nashif
parent 74e02e5d4e
commit c9ca19949e
3 changed files with 96 additions and 12 deletions

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <kernel.h>
#include <cmsis_os.h>
#define WAIT_TIME_US 1000
/**
* @brief Test kernel start
*
* @see osKernelInitialize(), osKernelStart(),
* osKernelRunning()
*/
void test_kernel_start(void)
{
if (osFeature_MainThread) {
/* When osFeature_MainThread is 1 the kernel offers to start
* with 'main'. The kernel is in this case already started.
*/
zassert_true(!osKernelInitialize() && !osKernelStart()
&& osKernelRunning(), NULL);
} else {
/* When osFeature_MainThread is 0 the kernel requires
* explicit start with osKernelStart.
*/
zassert_false(osKernelRunning(), NULL);
}
}
/**
* @brief Test kernel system timer
*
* @see osKernelSysTick()
*/
void test_kernel_systick(void)
{
u32_t start_time, stop_time, diff;
start_time = osKernelSysTick();
k_busy_wait(WAIT_TIME_US);
stop_time = osKernelSysTick();
diff = SYS_CLOCK_HW_CYCLES_TO_NS(stop_time -
start_time) / NSEC_PER_USEC;
zassert_true(diff >= WAIT_TIME_US, NULL);
}

View file

@ -8,6 +8,8 @@
#include <kernel.h>
#include <cmsis_os.h>
extern void test_kernel_start(void);
extern void test_kernel_systick(void);
extern void test_thread_prio(void);
extern void test_thread_apis(void);
extern void test_thread_instances(void);
@ -25,6 +27,8 @@ extern void test_signal_events_signalled(void);
void test_main(void)
{
ztest_test_suite(test_cmsis_apis,
ztest_unit_test(test_kernel_start),
ztest_unit_test(test_kernel_systick),
ztest_unit_test(test_thread_apis),
ztest_unit_test(test_thread_prio),
ztest_unit_test(test_thread_instances),

View file

@ -13,26 +13,54 @@ struct mem_block {
int member2;
};
osPoolDef(MemPool, 8, struct mem_block);
#define MAX_BLOCKS 10
osPoolDef(MemPool, MAX_BLOCKS, struct mem_block);
/**
* @brief Test memory pool allocation and free
*
* @see osPoolCreate(), osPoolAlloc(), osPoolFree(),
* osPoolCAlloc(), memcmp()
*/
void test_mempool(void)
{
int i;
osPoolId mempool_id;
osStatus status;
struct mem_block *addr;
struct mem_block *addr_list[MAX_BLOCKS + 1];
osStatus status_list[MAX_BLOCKS];
static struct mem_block zeroblock;
mempool_id = osPoolCreate(osPool(MemPool));
zassert_true(mempool_id != NULL, "mempool creation failed\n");
zassert_true(mempool_id != NULL, "mempool creation failed");
addr = (struct mem_block *)osPoolAlloc(mempool_id);
zassert_true(addr != NULL, "mempool allocation failed\n");
for (i = 0; i < MAX_BLOCKS; i++) {
addr_list[i] = (struct mem_block *)osPoolAlloc(mempool_id);
zassert_true(addr_list[i] != NULL, "mempool allocation failed");
}
status = osPoolFree(mempool_id, addr);
zassert_true(status == osOK, "mempool free failed\n");
/* All blocks in mempool are allocated, any more allocation
* without free should fail
*/
addr_list[i] = (struct mem_block *)osPoolAlloc(mempool_id);
zassert_true(addr_list[i] == NULL, "allocation happened."
" Something's wrong!");
addr = (struct mem_block *)osPoolCAlloc(mempool_id);
zassert_true(addr != NULL, "mempool allocation failed\n");
for (i = 0; i < MAX_BLOCKS; i++) {
status_list[i] = osPoolFree(mempool_id, addr_list[i]);
zassert_true(status_list[i] == osOK, "mempool free failed");
}
status = osPoolFree(mempool_id, addr);
zassert_true(status == osOK, "mempool free failed\n");
for (i = 0; i < MAX_BLOCKS; i++) {
addr_list[i] = (struct mem_block *)osPoolCAlloc(mempool_id);
zassert_true(addr_list[i] != NULL, "mempool allocation failed");
zassert_true(memcmp(addr_list[i], &zeroblock,
sizeof(struct mem_block)) == 0,
"osPoolCAlloc didn't set mempool to 0");
}
for (i = 0; i < MAX_BLOCKS; i++) {
status_list[i] = osPoolFree(mempool_id, addr_list[i]);
zassert_true(status_list[i] == osOK, "mempool free failed");
}
}