tests: devicetree: test supported devices API

Add tests for the supported devices API.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-08-20 19:30:47 +10:00 committed by Anas Nashif
commit 4c32e21fc7

View file

@ -74,15 +74,15 @@ static bool check_handle(device_handle_t hdl,
return false;
}
struct requires_context {
struct visitor_context {
uint8_t ndevs;
const struct device *rdevs[2];
};
static int requires_visitor(const struct device *dev,
void *context)
static int device_visitor(const struct device *dev,
void *context)
{
struct requires_context *ctx = context;
struct visitor_context *ctx = context;
const struct device **rdp = ctx->rdevs;
while (rdp < (ctx->rdevs + ctx->ndevs)) {
@ -102,14 +102,14 @@ static void test_requires(void)
size_t nhdls = 0;
const device_handle_t *hdls;
const struct device *dev;
struct requires_context ctx = { 0 };
struct visitor_context ctx = { 0 };
/* TEST_GPIO: no req */
dev = device_get_binding(DT_LABEL(TEST_GPIO));
zassert_equal(dev, DEVICE_DT_GET(TEST_GPIO), NULL);
hdls = device_required_handles_get(dev, &nhdls);
zassert_equal(nhdls, 0, NULL);
zassert_equal(0, device_required_foreach(dev, requires_visitor, &ctx),
zassert_equal(0, device_required_foreach(dev, device_visitor, &ctx),
NULL);
/* TEST_I2C: no req */
@ -117,7 +117,7 @@ static void test_requires(void)
zassert_equal(dev, DEVICE_DT_GET(TEST_I2C), NULL);
hdls = device_required_handles_get(dev, &nhdls);
zassert_equal(nhdls, 0, NULL);
zassert_equal(0, device_required_foreach(dev, requires_visitor, &ctx),
zassert_equal(0, device_required_foreach(dev, device_visitor, &ctx),
NULL);
/* TEST_DEVA: TEST_I2C GPIO */
@ -129,23 +129,23 @@ static void test_requires(void)
zassert_true(check_handle(DEV_HDL(TEST_GPIO), hdls, nhdls), NULL);
/* Visit fails if not enough space */
ctx = (struct requires_context){
ctx = (struct visitor_context){
.ndevs = 1,
};
zassert_equal(-ENOSPC, device_required_foreach(dev, requires_visitor, &ctx),
zassert_equal(-ENOSPC, device_required_foreach(dev, device_visitor, &ctx),
NULL);
/* Visit succeeds if enough space. */
ctx = (struct requires_context){
ctx = (struct visitor_context){
.ndevs = 2,
};
zassert_equal(2, device_required_foreach(dev, requires_visitor, &ctx),
zassert_equal(2, device_required_foreach(dev, device_visitor, &ctx),
NULL);
zassert_true((ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_I2C)))
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_I2C))),
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_I2C))),
NULL);
zassert_true((ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_GPIO)))
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_GPIO))),
|| (ctx.rdevs[1] == device_from_handle(DEV_HDL(TEST_GPIO))),
NULL);
/* TEST_GPIOX: TEST_I2C */
@ -154,10 +154,10 @@ static void test_requires(void)
hdls = device_required_handles_get(dev, &nhdls);
zassert_equal(nhdls, 1, NULL);
zassert_true(check_handle(DEV_HDL(TEST_I2C), hdls, nhdls), NULL);
ctx = (struct requires_context){
ctx = (struct visitor_context){
.ndevs = 3,
};
zassert_equal(1, device_required_foreach(dev, requires_visitor, &ctx),
zassert_equal(1, device_required_foreach(dev, device_visitor, &ctx),
NULL);
zassert_true(ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_I2C)),
NULL);
@ -171,6 +171,46 @@ static void test_requires(void)
zassert_true(check_handle(DEV_HDL(TEST_GPIOX), hdls, nhdls), NULL);
}
static void test_supports(void)
{
size_t nhdls = 0;
const device_handle_t *hdls;
const struct device *dev;
struct visitor_context ctx = { 0 };
/* TEST_DEVB: None */
dev = DEVICE_DT_GET(TEST_DEVB);
hdls = device_supported_handles_get(dev, &nhdls);
zassert_equal(nhdls, 0, NULL);
/* TEST_GPIO: TEST_DEVA */
dev = DEVICE_DT_GET(TEST_GPIO);
hdls = device_supported_handles_get(dev, &nhdls);
zassert_equal(nhdls, 1, NULL);
zassert_true(check_handle(DEV_HDL(TEST_DEVA), hdls, nhdls), NULL);
/* Visit fails if not enough space */
ctx = (struct visitor_context){
.ndevs = 0,
};
zassert_equal(-ENOSPC, device_supported_foreach(dev, device_visitor, &ctx), NULL);
/* Visit succeeds if enough space. */
ctx = (struct visitor_context){
.ndevs = 1,
};
zassert_equal(1, device_supported_foreach(dev, device_visitor, &ctx), NULL);
zassert_true(ctx.rdevs[0] == device_from_handle(DEV_HDL(TEST_DEVA)), NULL);
/* TEST_I2C: TEST_DEVA TEST_GPIOX TEST_DEVB */
dev = DEVICE_DT_GET(TEST_I2C);
hdls = device_supported_handles_get(dev, &nhdls);
zassert_equal(nhdls, 3, NULL);
zassert_true(check_handle(DEV_HDL(TEST_DEVA), hdls, nhdls), NULL);
zassert_true(check_handle(DEV_HDL(TEST_GPIOX), hdls, nhdls), NULL);
zassert_true(check_handle(DEV_HDL(TEST_DEVB), hdls, nhdls), NULL);
}
void test_main(void)
{
size_t ndevs;
@ -180,7 +220,8 @@ void test_main(void)
ztest_test_suite(devicetree_driver,
ztest_unit_test(test_init_order),
ztest_unit_test(test_requires)
);
ztest_unit_test(test_requires),
ztest_unit_test(test_supports)
);
ztest_run_test_suite(devicetree_driver);
}