tests: k_heap api: add tests for k_heap_api

Add test cases to test k_heap_alloc() and k_heap_free() APIs

Fixes #29654

Signed-off-by: iva kik <megatheriumiva@gmail.com>
This commit is contained in:
iva kik 2020-11-05 04:00:55 -08:00 committed by Anas Nashif
commit 10fdf95b94
6 changed files with 142 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(k_heap_api)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

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

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
extern void test_k_heap_alloc(void);
extern void test_k_heap_alloc_fail(void);
extern void test_k_heap_free(void);
/**
* @brief k heap api tests
*
* @defgroup k_heap api Tests
*
* @ingroup all_tests
* @{
* @}
*/
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(k_heap_api,
ztest_unit_test(test_k_heap_alloc),
ztest_unit_test(test_k_heap_alloc_fail),
ztest_unit_test(test_k_heap_free));
ztest_run_test_suite(k_heap_api);
}

View file

@ -0,0 +1,8 @@
/*
* Copyright (c) 2019 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define TIMEOUT 2000
#define HEAP_SIZE 2048

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include "test_kheap.h"
K_HEAP_DEFINE(k_heap_test, HEAP_SIZE);
#define ALLOC_SIZE_1 1024
#define ALLOC_SIZE_2 1536
#define ALLOC_SIZE_3 2049
/*test cases*/
/**
* @brief Test to demonstrate k_heap_alloc() and k_heap_free() API usage
*
* @ingroup kernel_kheap_api_tests
*
* @details The test allocates 1024 bytes from 2048 byte heap,
* and checks if allocation is successful or not
*
* @see k_heap_malloc(), k_heap_Free()
*/
void test_k_heap_alloc(void)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, timeout);
zassert_not_null(p, "k_heap_alloc operation failed");
for (int i = 0; i < ALLOC_SIZE_1; i++) {
p[i] = '0';
}
k_heap_free(&k_heap_test, p);
}
/**
* @brief Test to demonstrate k_heap_alloc() and k_heap_free() API usage
*
* @ingroup kernel_kheap_api_tests
*
* @details The test allocates 2049 bytes, which is greater than the heap
* size(2048 bytes), and checks for NULL return from k_heap_alloc
*
* @see k_heap_malloc(), k_heap_Free()
*/
void test_k_heap_alloc_fail(void)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_3, timeout);
zassert_is_null(p, NULL);
k_heap_free(&k_heap_test, p);
}
/**
* @brief Test to demonstrate k_heap_free() API functionality.
*
* @ingroup kernel_kheap_api_tests
*
* @details The test validates k_heap_free()
* API, by using below steps
* 1. allocate the memory from the heap,
* 2. free the allocated memory
* 3. allocate memory more than the first allocation.
* the allocation in the 3rd step should succeed if k_heap_free()
* works as expected
*
* @see k_heap_alloc, k_heap_free()
*/
void test_k_heap_free(void)
{
k_timeout_t timeout = Z_TIMEOUT_US(TIMEOUT);
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, timeout);
zassert_not_null(p, "k_heap_alloc operation failed");
k_heap_free(&k_heap_test, p);
p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_2, timeout);
zassert_not_null(p, "k_heap_alloc operation failed");
for (int i = 0; i < ALLOC_SIZE_2; i++) {
p[i] = '0';
}
k_heap_free(&k_heap_test, p);
}

View file

@ -0,0 +1,3 @@
tests:
kernel.k_heap_api:
tags: k_heap_api kernel