tests: kernel: added memory slab api test

TestPurpose: verify memory slab APIs.
All TESTPOINTs extracted from kernel-doc comments in <kernel.h>

ZEP-1209

Change-Id: I80bc85e96110e7106b3fc5883b982d71c6a7e50b
Signed-off-by: Sharron LIU <sharron.liu@intel.com>
This commit is contained in:
Sharron LIU 2016-12-30 12:30:59 +08:00
commit 37402b4161
8 changed files with 302 additions and 0 deletions

View file

@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.test

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_IRQ_OFFLOAD=y

View file

@ -0,0 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = main.o test_mslab_api.o test_mslab_extern.o

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ztest.h>
extern void test_mslab_kinit(void);
extern void test_mslab_kdefine(void);
extern void test_mslab_kdefine_extern(void);
extern void test_mslab_alloc_free_thread(void);
extern void test_mslab_alloc_free_isr(void);
extern void test_mslab_alloc_align(void);
extern void test_mslab_alloc_timeout(void);
extern void test_mslab_used_get(void);
/*test case main entry*/
void test_main(void *p1, void *p2, void *p3)
{
ztest_test_suite(test_mslab_api,
ztest_unit_test(test_mslab_kinit),
ztest_unit_test(test_mslab_kdefine),
ztest_unit_test(test_mslab_kdefine_extern),
ztest_unit_test(test_mslab_alloc_free_thread),
ztest_unit_test(test_mslab_alloc_free_isr),
ztest_unit_test(test_mslab_alloc_align),
ztest_unit_test(test_mslab_alloc_timeout),
ztest_unit_test(test_mslab_used_get));
ztest_run_test_suite(test_mslab_api);
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __TEST_MSLAB_H__
#define __TEST_MSLAB_H__
#define TIMEOUT 2000
#define BLK_NUM 3
#define BLK_ALIGN 4
#define BLK_SIZE 8
extern void tmslab_alloc_free(void *data);
#endif /*__TEST_MSLAB_H__*/

View file

@ -0,0 +1,193 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup t_mslab
* @{
* @defgroup t_mslab_api test_mslab_api
* @brief TestPurpose: verify memory slab APIs.
* @details All TESTPOINTs extracted from kernel-doc comments in <kernel.h>
* - API coverage
* - K_MEM_SLAB_DEFINE
* - k_mem_slab_init
* - k_mem_slab_alloc
* - k_mem_slab_free
* - k_mem_slab_num_used_get
* - k_mem_slab_num_free_get
* @}
*/
#include <ztest.h>
#include <irq_offload.h>
#include "test_mslab.h"
/** TESTPOINT: Statically define and initialize a memory slab*/
K_MEM_SLAB_DEFINE(kmslab, BLK_SIZE, BLK_NUM, BLK_ALIGN);
static char __aligned(BLK_ALIGN) tslab[BLK_SIZE * BLK_NUM];
static struct k_mem_slab mslab;
void tmslab_alloc_free(void *data)
{
struct k_mem_slab *pslab = (struct k_mem_slab *)data;
void *block[BLK_NUM];
memset(block, 0, sizeof(block));
/**
* TESTPOINT: The memory slab's buffer contains @a slab_num_blocks
* memory blocks that are @a slab_block_size bytes long.
*/
for (int i = 0; i < BLK_NUM; i++) {
/** TESTPOINT: Allocate memory from a memory slab.*/
/** TESTPOINT: @retval 0 Memory allocated.*/
assert_true(k_mem_slab_alloc(pslab, &block[i], K_NO_WAIT) == 0,
NULL);
/**
* TESTPOINT: The block address area pointed at by @a mem is set
* to the starting address of the memory block.
*/
assert_not_null(block[i], NULL);
}
for (int i = 0; i < BLK_NUM; i++) {
/** TESTPOINT: Free memory allocated from a memory slab.*/
k_mem_slab_free(pslab, &block[i]);
}
}
static void tmslab_alloc_align(void *data)
{
struct k_mem_slab *pslab = (struct k_mem_slab *)data;
void *block[BLK_NUM];
for (int i = 0; i < BLK_NUM; i++) {
assert_true(k_mem_slab_alloc(pslab, &block[i], K_NO_WAIT) == 0,
NULL);
/**
* TESTPOINT: To ensure that each memory block is similarly
* aligned to this boundary
*/
assert_true((uint32_t)block[i] % BLK_ALIGN == 0, NULL);
}
for (int i = 0; i < BLK_NUM; i++) {
k_mem_slab_free(pslab, &block[i]);
}
}
static void tmslab_alloc_timeout(void *data)
{
struct k_mem_slab *pslab = (struct k_mem_slab *)data;
void *block[BLK_NUM], *block_fail;
uint64_t tms;
for (int i = 0; i < BLK_NUM; i++) {
assert_true(k_mem_slab_alloc(pslab, &block[i], K_NO_WAIT) == 0,
NULL);
}
/** TESTPOINT: Use K_NO_WAIT to return without waiting*/
/** TESTPOINT: @retval -ENOMEM Returned without waiting.*/
assert_equal(k_mem_slab_alloc(pslab, &block_fail, K_NO_WAIT), -ENOMEM,
NULL);
/** TESTPOINT: @retval -EAGAIN Waiting period timed out*/
tms = k_uptime_get();
assert_equal(k_mem_slab_alloc(pslab, &block_fail, TIMEOUT), -EAGAIN,
NULL);
/**
* TESTPOINT: @param timeout Maximum time to wait for operation to
* complete (in milliseconds)
*/
assert_true(k_uptime_delta(&tms) >= TIMEOUT, NULL);
for (int i = 0; i < BLK_NUM; i++) {
k_mem_slab_free(pslab, &block[i]);
}
}
static void tmslab_used_get(void *data)
{
struct k_mem_slab *pslab = (struct k_mem_slab *)data;
void *block[BLK_NUM], *block_fail;
for (int i = 0; i < BLK_NUM; i++) {
assert_true(k_mem_slab_alloc(pslab, &block[i], K_NO_WAIT) == 0,
NULL);
/** TESTPOINT: Get the number of used blocks in a memory slab.*/
assert_equal(k_mem_slab_num_used_get(pslab), i+1, NULL);
/**
* TESTPOINT: Get the number of unused blocks in a memory slab.
*/
assert_equal(k_mem_slab_num_free_get(pslab), BLK_NUM-1-i, NULL);
}
assert_equal(k_mem_slab_alloc(pslab, &block_fail, K_NO_WAIT), -ENOMEM,
NULL);
/* free get on allocation failure*/
assert_equal(k_mem_slab_num_free_get(pslab), 0, NULL);
/* used get on allocation failure*/
assert_equal(k_mem_slab_num_used_get(pslab), BLK_NUM, NULL);
assert_equal(k_mem_slab_alloc(pslab, &block_fail, TIMEOUT), -EAGAIN,
NULL);
assert_equal(k_mem_slab_num_free_get(pslab), 0, NULL);
assert_equal(k_mem_slab_num_used_get(pslab), BLK_NUM, NULL);
for (int i = 0; i < BLK_NUM; i++) {
k_mem_slab_free(pslab, &block[i]);
assert_equal(k_mem_slab_num_free_get(pslab), i+1, NULL);
assert_equal(k_mem_slab_num_used_get(pslab), BLK_NUM-1-i, NULL);
}
}
/*test cases*/
void test_mslab_kinit(void)
{
k_mem_slab_init(&mslab, tslab, BLK_SIZE, BLK_NUM);
assert_equal(k_mem_slab_num_used_get(&mslab), 0, NULL);
assert_equal(k_mem_slab_num_free_get(&mslab), BLK_NUM, NULL);
}
void test_mslab_kdefine(void)
{
assert_equal(k_mem_slab_num_used_get(&kmslab), 0, NULL);
assert_equal(k_mem_slab_num_free_get(&kmslab), BLK_NUM, NULL);
}
void test_mslab_alloc_free_thread(void)
{
tmslab_alloc_free(&mslab);
}
void test_mslab_alloc_free_isr(void)
{
irq_offload(tmslab_alloc_free, &mslab);
}
void test_mslab_alloc_align(void)
{
tmslab_alloc_align(&mslab);
tmslab_alloc_align(&kmslab);
}
void test_mslab_alloc_timeout(void)
{
tmslab_alloc_timeout(&mslab);
}
void test_mslab_used_get(void)
{
tmslab_used_get(&mslab);
tmslab_used_get(&kmslab);
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ztest.h>
#include "test_mslab.h"
/**
* TESTPOINT: The memory slab can be accessed outside the module where it is
* defined using: @code extern struct k_mem_slab <name>; @endcode
*/
extern struct k_mem_slab kmslab;
/*test cases*/
void test_mslab_kdefine_extern(void)
{
tmslab_alloc_free(&kmslab);
}

View file

@ -0,0 +1,2 @@
[test]
tags = kernel