Skip to content

Commit

Permalink
Devicetree: Devicetree Bindings: Add tests for new DT_ENUM_ macros
Browse files Browse the repository at this point in the history
Test the new c-macros for dt enums. The new macros are already used in
the existing macros. As an example, DT_ENUM_IDX(node_id, prop) uses
DT_ENUM_IDX_BY_IDX(node_id, prop, 0) to get its result. However, this is
insufficient for testing the complete functionality of these macros.
Therefore, additional tests are added to make sure they work
appropriately for other indices besides 0.

(cherry picked from commit 405c671)

Original-Signed-off-by: Joel Hirsbrunner <jhirsbrunner@baumer.com>
GitOrigin-RevId: 405c671
Cr-Build-Id: 8733565196572304993
Cr-Build-Url: https://cr-buildbucket.appspot.com/build/8733565196572304993
Copybot-Job-Name: zephyr-main-copybot-downstream
Change-Id: Ia4a13d25faf8dac1a9e2c1cc424252ea7f15cb17
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5934726
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
Reviewed-by: Dawid Niedźwiecki <dawidn@google.com>
Tested-by: Dawid Niedźwiecki <dawidn@google.com>
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Commit-Queue: Eric Yilun Lin <yllin@google.com>
  • Loading branch information
JHirsbrunner authored and Chromeos LUCI committed Oct 21, 2024
1 parent 915b2d1 commit ac58e22
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
21 changes: 21 additions & 0 deletions dts/bindings/test/vnd,enum-int-array-holder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2020 Linaro Limited
# SPDX-License-Identifier: Apache-2.0

description: Test enum property container

compatible: "vnd,enum-int-array-holder"

include: [base.yaml]

properties:
val:
type: array
enum:
- 7
- 6
- 5
- 4
- 3
- 2
- 1
- 0
17 changes: 17 additions & 0 deletions dts/bindings/test/vnd,enum-string-array-holder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2020 Linaro Limited
# SPDX-License-Identifier: Apache-2.0

description: Test enum property container

compatible: "vnd,enum-string-array-holder"

include: [base.yaml]

properties:
val:
type: string-array
enum:
- foo
- bar
- baz
- zoo
10 changes: 10 additions & 0 deletions tests/lib/devicetree/api/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@
compatible = "vnd,enum-required-false-holder-inst";
};

test_enum_string_array: enum-8 {
compatible = "vnd,enum-string-array-holder";
val = "foo", "zoo", "foo";
};

test_enum_int_array: enum-9 {
compatible = "vnd,enum-int-array-holder";
val = <4 3 4 0>;
};

/*
* disabled/reserved should be the only nodes with their
* compatible in the tree.
Expand Down
70 changes: 70 additions & 0 deletions tests/lib/devicetree/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,41 @@ ZTEST(devicetree_api, test_enums)
zassert_true(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 5), "");
zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 6), "");
zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 7), "");

/* DT_ENUM_IDX_BY_IDX and DT_ENUM_HAS_VALUE_BY_IDX on string-array enum */
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0), 0);
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1), 3);
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2), 0);
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, foo));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, bar));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, baz));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, zoo));
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, zoo));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, foo));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, bar));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, baz));
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, foo));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, baz));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, bar));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, zoo));

/* DT_ENUM_IDX_BY_IDX and DT_ENUM_HAS_VALUE_BY_IDX on int-array enum */
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0), 3);
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1), 4);
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2), 3);
zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3), 7);
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 4));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 5));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 6));
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 3));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 0));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 1));
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 4));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 3));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 7));
zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 0));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 2));
zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 1));
}
#undef TO_MY_ENUM
#undef TO_MY_ENUM_2
Expand All @@ -2028,6 +2063,18 @@ ZTEST(devicetree_api, test_enums_required_false)
zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_1),
val, 4),
4, "");
/* DT_ENUM_IDX_OR on string-array value */
zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_string_array), val, 0, 2),
0, "");
zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_string_array), val, 5, 2),
2, "");
/* DT_ENUM_IDX_OR on int-array value */
zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_int_array),
val, 0, 7),
3, "");
zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_int_array),
val, 4, 7),
7, "");
}

ZTEST(devicetree_api, test_inst_enums)
Expand All @@ -2046,6 +2093,29 @@ ZTEST(devicetree_api, test_inst_enums)
zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, zero), "");
zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, one), "");
zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, two), "");

/* Also add tests for these:
* DT_INST_ENUM_IDX_BY_IDX
* DT_INST_ENUM_IDX_BY_IDX_OR
* DT_INST_ENUM_HAS_VALUE_BY_IDX
*/
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_enum_string_array_holder
zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 0), 0, "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 1), 3, "");
zassert_true(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 0, foo), "");
zassert_false(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 0, zoo), "");
zassert_true(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 1, zoo), "");
zassert_false(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 2, baz), "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 0, 10), 0, "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 4, 10), 10, "");

#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_enum_int_array_holder
zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 0), 3, "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 3), 7, "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 1, 10), 4, "");
zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 123654, 10), 10, "");
}

#undef DT_DRV_COMPAT
Expand Down

0 comments on commit ac58e22

Please sign in to comment.