Skip to content

Commit 852c992

Browse files
de-nordickartben
authored andcommitted
tests/drivers/flash_api: Flash API tests
The commit adds test for flash_get_size. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 13fa233 commit 852c992

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
cmake_minimum_required(VERSION 3.13.1)
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(flash_api_tests)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})

tests/drivers/flash_api/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
CONFIG_ZTEST=y
7+
CONFIG_FLASH=y

tests/drivers/flash_api/src/main.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/drivers/flash.h>
9+
#include <zephyr/device.h>
10+
11+
/** Test instrumentation **/
12+
/*
13+
* The structure below gathers variables that may be used by the simulated API calls
14+
* to mock behavior of a flash device; the variables may be used however it is desired,
15+
* because it is up to the mocked functions and checks afterwards to relate these variables
16+
* to results.
17+
*/
18+
static struct {
19+
/* Set to value returned from any API call */
20+
int ret;
21+
/* Some size */
22+
uint64_t size;
23+
} simulated_values = {
24+
.ret = 0,
25+
.size = 0,
26+
};
27+
28+
/*** Device definition atd == API Test Dev ***/
29+
static int some_get_size(const struct device *dev, uint64_t *size)
30+
{
31+
__ASSERT_NO_MSG(dev != NULL);
32+
33+
*size = simulated_values.size;
34+
35+
return 0;
36+
}
37+
38+
static int enotsup_get_size(const struct device *dev, uint64_t *size)
39+
{
40+
ARG_UNUSED(dev);
41+
42+
return -ENOTSUP;
43+
}
44+
45+
/** Device objects **/
46+
/* The device state, just to make it "ready" device */
47+
static struct device_state some_dev_state = {
48+
.init_res = 0,
49+
.initialized = 1,
50+
};
51+
52+
/* Device with get_size */
53+
const static struct flash_driver_api size_fun_api = {
54+
.get_size = some_get_size,
55+
};
56+
const static struct device size_fun_dev = {
57+
"get_size",
58+
NULL,
59+
&size_fun_api,
60+
&some_dev_state,
61+
};
62+
63+
/* No functions device */
64+
const static struct flash_driver_api no_fun_api = {0};
65+
const static struct device no_fun_dev = {
66+
"no_fun",
67+
NULL,
68+
&no_fun_api,
69+
&some_dev_state,
70+
};
71+
72+
/* Device with get_size implemented but returning -ENOTSUP */
73+
static struct flash_driver_api enotsup_fun_api = {
74+
.get_size = enotsup_get_size,
75+
};
76+
static struct device enotsup_fun_dev = {
77+
"enotsup",
78+
NULL,
79+
&enotsup_fun_api,
80+
&some_dev_state,
81+
};
82+
83+
ZTEST(flash_api, test_get_size)
84+
{
85+
uint64_t size = 0;
86+
87+
simulated_values.size = 45;
88+
zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success");
89+
zassert_equal(size, simulated_values.size, "Size mismatch");
90+
simulated_values.size = 46;
91+
zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success");
92+
zassert_equal(size, simulated_values.size, "Size mismatch");
93+
zassert_equal(flash_get_size(&no_fun_dev, &size), -ENOSYS);
94+
95+
zassert_equal(flash_get_size(&enotsup_fun_dev, &size), -ENOTSUP);
96+
}
97+
98+
ZTEST_SUITE(flash_api, NULL, NULL, NULL, NULL, NULL);

tests/drivers/flash_api/testcase.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
common:
7+
platform_allow:
8+
- native_sim/native/64
9+
- native_sim
10+
tests:
11+
drivers.flash.api:
12+
tags: drivers flash
13+
drivers.flash.api.userspace:
14+
tags: driver flash userspace
15+
extra_configs:
16+
- CONFIG_USERSPACE=y

0 commit comments

Comments
 (0)