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 ***/
some_get_size(const struct device * dev,uint64_t * size)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
enotsup_get_size(const struct device * dev,uint64_t * size)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 static DEVICE_API(flash, 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 static DEVICE_API(flash, 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 DEVICE_API(flash, 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
ZTEST(flash_api,test_get_size)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);
99