tests/drivers/flash_api: Flash API tests

The commit adds test for flash_get_size.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2021-06-23 08:30:02 +00:00 committed by Benjamin Cabé
commit 852c992da5
4 changed files with 133 additions and 0 deletions

View file

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

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_ZTEST=y
CONFIG_FLASH=y

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/device.h>
/** Test instrumentation **/
/*
* The structure below gathers variables that may be used by the simulated API calls
* to mock behavior of a flash device; the variables may be used however it is desired,
* because it is up to the mocked functions and checks afterwards to relate these variables
* to results.
*/
static struct {
/* Set to value returned from any API call */
int ret;
/* Some size */
uint64_t size;
} simulated_values = {
.ret = 0,
.size = 0,
};
/*** Device definition atd == API Test Dev ***/
static int some_get_size(const struct device *dev, uint64_t *size)
{
__ASSERT_NO_MSG(dev != NULL);
*size = simulated_values.size;
return 0;
}
static int enotsup_get_size(const struct device *dev, uint64_t *size)
{
ARG_UNUSED(dev);
return -ENOTSUP;
}
/** Device objects **/
/* The device state, just to make it "ready" device */
static struct device_state some_dev_state = {
.init_res = 0,
.initialized = 1,
};
/* Device with get_size */
const static struct flash_driver_api size_fun_api = {
.get_size = some_get_size,
};
const static struct device size_fun_dev = {
"get_size",
NULL,
&size_fun_api,
&some_dev_state,
};
/* No functions device */
const static struct flash_driver_api no_fun_api = {0};
const static struct device no_fun_dev = {
"no_fun",
NULL,
&no_fun_api,
&some_dev_state,
};
/* Device with get_size implemented but returning -ENOTSUP */
static struct flash_driver_api enotsup_fun_api = {
.get_size = enotsup_get_size,
};
static struct device enotsup_fun_dev = {
"enotsup",
NULL,
&enotsup_fun_api,
&some_dev_state,
};
ZTEST(flash_api, test_get_size)
{
uint64_t size = 0;
simulated_values.size = 45;
zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success");
zassert_equal(size, simulated_values.size, "Size mismatch");
simulated_values.size = 46;
zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success");
zassert_equal(size, simulated_values.size, "Size mismatch");
zassert_equal(flash_get_size(&no_fun_dev, &size), -ENOSYS);
zassert_equal(flash_get_size(&enotsup_fun_dev, &size), -ENOTSUP);
}
ZTEST_SUITE(flash_api, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,16 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
common:
platform_allow:
- native_sim/native/64
- native_sim
tests:
drivers.flash.api:
tags: drivers flash
drivers.flash.api.userspace:
tags: driver flash userspace
extra_configs:
- CONFIG_USERSPACE=y