Skip to content

Commit 4c32e21

Browse files
Jordan Yatesnashif
authored andcommitted
tests: devicetree: test supported devices API
Add tests for the supported devices API. Signed-off-by: Jordan Yates <[email protected]>
1 parent b01e41c commit 4c32e21

File tree

1 file changed

+58
-17
lines changed
  • tests/lib/devicetree/devices/src

1 file changed

+58
-17
lines changed

tests/lib/devicetree/devices/src/main.c

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ static bool check_handle(device_handle_t hdl,
7474
return false;
7575
}
7676

77-
struct requires_context {
77+
struct visitor_context {
7878
uint8_t ndevs;
7979
const struct device *rdevs[2];
8080
};
8181

82-
static int requires_visitor(const struct device *dev,
83-
void *context)
82+
static int device_visitor(const struct device *dev,
83+
void *context)
8484
{
85-
struct requires_context *ctx = context;
85+
struct visitor_context *ctx = context;
8686
const struct device **rdp = ctx->rdevs;
8787

8888
while (rdp < (ctx->rdevs + ctx->ndevs)) {
@@ -102,22 +102,22 @@ static void test_requires(void)
102102
size_t nhdls = 0;
103103
const device_handle_t *hdls;
104104
const struct device *dev;
105-
struct requires_context ctx = { 0 };
105+
struct visitor_context ctx = { 0 };
106106

107107
/* TEST_GPIO: no req */
108108
dev = device_get_binding(DT_LABEL(TEST_GPIO));
109109
zassert_equal(dev, DEVICE_DT_GET(TEST_GPIO), NULL);
110110
hdls = device_required_handles_get(dev, &nhdls);
111111
zassert_equal(nhdls, 0, NULL);
112-
zassert_equal(0, device_required_foreach(dev, requires_visitor, &ctx),
112+
zassert_equal(0, device_required_foreach(dev, device_visitor, &ctx),
113113
NULL);
114114

115115
/* TEST_I2C: no req */
116116
dev = device_get_binding(DT_LABEL(TEST_I2C));
117117
zassert_equal(dev, DEVICE_DT_GET(TEST_I2C), NULL);
118118
hdls = device_required_handles_get(dev, &nhdls);
119119
zassert_equal(nhdls, 0, NULL);
120-
zassert_equal(0, device_required_foreach(dev, requires_visitor, &ctx),
120+
zassert_equal(0, device_required_foreach(dev, device_visitor, &ctx),
121121
NULL);
122122

123123
/* TEST_DEVA: TEST_I2C GPIO */
@@ -129,23 +129,23 @@ static void test_requires(void)
129129
zassert_true(check_handle(DEV_HDL(TEST_GPIO), hdls, nhdls), NULL);
130130

131131
/* Visit fails if not enough space */
132-
ctx = (struct requires_context){
132+
ctx = (struct visitor_context){
133133
.ndevs = 1,
134134
};
135-
zassert_equal(-ENOSPC, device_required_foreach(dev, requires_visitor, &ctx),
135+
zassert_equal(-ENOSPC, device_required_foreach(dev, device_visitor, &ctx),
136136
NULL);
137137

138138
/* Visit succeeds if enough space. */
139-
ctx = (struct requires_context){
139+
ctx = (struct visitor_context){
140140
.ndevs = 2,
141141
};
142-
zassert_equal(2, device_required_foreach(dev, requires_visitor, &ctx),
142+
zassert_equal(2, device_required_foreach(dev, device_visitor, &ctx),
143143
NULL);
144144
zassert_true((ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_I2C)))
145-
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_I2C))),
145+
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_I2C))),
146146
NULL);
147147
zassert_true((ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_GPIO)))
148-
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_GPIO))),
148+
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_GPIO))),
149149
NULL);
150150

151151
/* TEST_GPIOX: TEST_I2C */
@@ -154,10 +154,10 @@ static void test_requires(void)
154154
hdls = device_required_handles_get(dev, &nhdls);
155155
zassert_equal(nhdls, 1, NULL);
156156
zassert_true(check_handle(DEV_HDL(TEST_I2C), hdls, nhdls), NULL);
157-
ctx = (struct requires_context){
157+
ctx = (struct visitor_context){
158158
.ndevs = 3,
159159
};
160-
zassert_equal(1, device_required_foreach(dev, requires_visitor, &ctx),
160+
zassert_equal(1, device_required_foreach(dev, device_visitor, &ctx),
161161
NULL);
162162
zassert_true(ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_I2C)),
163163
NULL);
@@ -171,6 +171,46 @@ static void test_requires(void)
171171
zassert_true(check_handle(DEV_HDL(TEST_GPIOX), hdls, nhdls), NULL);
172172
}
173173

174+
static void test_supports(void)
175+
{
176+
size_t nhdls = 0;
177+
const device_handle_t *hdls;
178+
const struct device *dev;
179+
struct visitor_context ctx = { 0 };
180+
181+
/* TEST_DEVB: None */
182+
dev = DEVICE_DT_GET(TEST_DEVB);
183+
hdls = device_supported_handles_get(dev, &nhdls);
184+
zassert_equal(nhdls, 0, NULL);
185+
186+
/* TEST_GPIO: TEST_DEVA */
187+
dev = DEVICE_DT_GET(TEST_GPIO);
188+
hdls = device_supported_handles_get(dev, &nhdls);
189+
zassert_equal(nhdls, 1, NULL);
190+
zassert_true(check_handle(DEV_HDL(TEST_DEVA), hdls, nhdls), NULL);
191+
192+
/* Visit fails if not enough space */
193+
ctx = (struct visitor_context){
194+
.ndevs = 0,
195+
};
196+
zassert_equal(-ENOSPC, device_supported_foreach(dev, device_visitor, &ctx), NULL);
197+
198+
/* Visit succeeds if enough space. */
199+
ctx = (struct visitor_context){
200+
.ndevs = 1,
201+
};
202+
zassert_equal(1, device_supported_foreach(dev, device_visitor, &ctx), NULL);
203+
zassert_true(ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_DEVA)), NULL);
204+
205+
/* TEST_I2C: TEST_DEVA TEST_GPIOX TEST_DEVB */
206+
dev = DEVICE_DT_GET(TEST_I2C);
207+
hdls = device_supported_handles_get(dev, &nhdls);
208+
zassert_equal(nhdls, 3, NULL);
209+
zassert_true(check_handle(DEV_HDL(TEST_DEVA), hdls, nhdls), NULL);
210+
zassert_true(check_handle(DEV_HDL(TEST_GPIOX), hdls, nhdls), NULL);
211+
zassert_true(check_handle(DEV_HDL(TEST_DEVB), hdls, nhdls), NULL);
212+
}
213+
174214
void test_main(void)
175215
{
176216
size_t ndevs;
@@ -180,7 +220,8 @@ void test_main(void)
180220

181221
ztest_test_suite(devicetree_driver,
182222
ztest_unit_test(test_init_order),
183-
ztest_unit_test(test_requires)
184-
);
223+
ztest_unit_test(test_requires),
224+
ztest_unit_test(test_supports)
225+
);
185226
ztest_run_test_suite(devicetree_driver);
186227
}

0 commit comments

Comments
 (0)