tests: kernel: mem_heap: added api and concept tests

<kernel.h> APIs covered:
    k_malloc
    k_free
Concept TESTPOINTs extracted from
https://www.zephyrproject.org/doc/kernel/memory/heap.html#concepts

ZEP-1242

Change-Id: I39edc809119984585d78d6abbe33f5be707c5818
Signed-off-by: Sharron LIU <sharron.liu@intel.com>
This commit is contained in:
Sharron LIU 2017-01-26 18:04:14 +08:00 committed by Anas Nashif
commit de7435a73f
8 changed files with 211 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_HEAP_MEM_POOL_SIZE=256

View file

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

View file

@ -0,0 +1,32 @@
/*
* 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_mheap_malloc_free(void);
extern void test_mheap_malloc_align4(void);
extern void test_mheap_min_block_size(void);
extern void test_mheap_block_desc(void);
/*test case main entry*/
void test_main(void *p1, void *p2, void *p3)
{
ztest_test_suite(test_mheap_api,
ztest_unit_test(test_mheap_malloc_free),
ztest_unit_test(test_mheap_malloc_align4),
ztest_unit_test(test_mheap_min_block_size),
ztest_unit_test(test_mheap_block_desc));
ztest_run_test_suite(test_mheap_api);
}

View file

@ -0,0 +1,12 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#define TIMEOUT 2000
#define BLK_SIZE_MIN 64
#define BLK_NUM_MAX (CONFIG_HEAP_MEM_POOL_SIZE/BLK_SIZE_MIN)
#define BLK_NUM_MIN (CONFIG_HEAP_MEM_POOL_SIZE/(BLK_SIZE_MIN<<2))
#define BLK_SIZE_EXCLUDE_DESC (BLK_SIZE_MIN-16)
#define BLK_SIZE_QUARTER (CONFIG_HEAP_MEM_POOL_SIZE>>2)

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @addtogroup t_mheap
* @{
* @defgroup t_mheap_api test_mheap_api
* @brief TestPurpose: verify heap memory pool APIs.
* @details All TESTPOINTs extracted from API doc
* https://www.zephyrproject.org/doc/api/kernel_api.html#heap-memory-pool
* - API coverage
* -# k_malloc
* -# k_free
* @}
*/
#include <ztest.h>
#include "test_mheap.h"
/*test cases*/
void test_mheap_malloc_free(void)
{
void *block[BLK_NUM_MAX], *block_fail;
for (int i = 0; i < BLK_NUM_MAX; i++) {
/**
* TESTPOINT: This routine provides traditional malloc()
* semantics. Memory is allocated from the heap memory pool.
*/
block[i] = k_malloc(i);
/** TESTPOINT: Address of the allocated memory if successful;*/
assert_not_null(block[i], NULL);
}
block_fail = k_malloc(BLK_SIZE_MIN);
/** TESTPOINT: Return NULL if fail.*/
assert_is_null(block_fail, NULL);
for (int i = 0; i < BLK_NUM_MAX; i++) {
/**
* TESTPOINT: This routine provides traditional free()
* semantics. The memory being returned must have been allocated
* from the heap memory pool.
*/
k_free(block[i]);
}
/** TESTPOINT: If ptr is NULL, no operation is performed.*/
k_free(NULL);
}

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @addtogroup t_mheap
* @{
* @defgroup t_mheap_concept test_mheap_concept
* @brief TestPurpose: verify memory pool concepts.
* @details All TESTPOINTs extracted from kernel documentation.
* https://www.zephyrproject.org/doc/kernel/memory/heap.html#concepts
*
* TESTPOINTs cover testable kernel behaviours that preserve across internal
* implementation change or kernel version change.
* As a black-box test, TESTPOINTs do not cover internal operations.
* @}
*/
#include <ztest.h>
#include "test_mheap.h"
/* request 0 bytes*/
#define TEST_SIZE_0 0
/*test cases*/
void test_mheap_malloc_align4(void)
{
void *block[BLK_NUM_MAX];
/**
* TESTPOINT: The address of the allocated chunk is guaranteed to be
* aligned on a multiple of 4 bytes.
*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
block[i] = k_malloc(i);
assert_not_null(block[i], NULL);
assert_false((int)block[i]%4, NULL);
}
/* test case tear down*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
k_free(block[i]);
}
}
void test_mheap_min_block_size(void)
{
void *block[BLK_NUM_MAX], *block_fail;
/**
* TESTPOINT: The heap memory pool also defines a minimum block
* size of 64 bytes.
* Test steps:
* initial memory heap status (F for free, U for used):
* 64F, 64F, 64F, 64F
* 1. request 4 blocks: each 0-byte plus 16-byte block desc,
* indeed 64-byte allocated
* 2. verify no more free blocks, any further allocation failed
*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
block[i] = k_malloc(TEST_SIZE_0);
assert_not_null(block[i], NULL);
}
/* verify no more free blocks available*/
block_fail = k_malloc(BLK_SIZE_MIN);
assert_is_null(block_fail, NULL);
/* test case tear down*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
k_free(block[i]);
}
}
void test_mheap_block_desc(void)
{
void *block[BLK_NUM_MAX], *block_fail;
/**
* TESTPOINT: The kernel uses the first 16 bytes of any memory block
* allocated from the heap memory pool to save the block descriptor
* information it needs to later free the block. Consequently, an
* applications request for an N byte chunk of heap memory requires a
* block that is at least (N+16) bytes long.
* Test steps:
* initial memory heap status (F for free, U for used):
* 64F, 64F, 64F, 64F
* 1. request 4 blocks: each (64-16) bytes, indeed 64-byte allocated
* 2. verify no more free blocks, any further allocation failed
*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
block[i] = k_malloc(BLK_SIZE_EXCLUDE_DESC);
assert_not_null(block[i], NULL);
}
/* verify no more free blocks available*/
block_fail = k_malloc(BLK_SIZE_MIN);
assert_is_null(block_fail, NULL);
/* test case tear down*/
for (int i = 0; i < BLK_NUM_MAX; i++) {
k_free(block[i]);
}
}

View file

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