diff --git a/dts/bindings/test/vnd,enum-int-array-holder.yaml b/dts/bindings/test/vnd,enum-int-array-holder.yaml new file mode 100644 index 00000000000..f147a0c70aa --- /dev/null +++ b/dts/bindings/test/vnd,enum-int-array-holder.yaml @@ -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 diff --git a/dts/bindings/test/vnd,enum-string-array-holder.yaml b/dts/bindings/test/vnd,enum-string-array-holder.yaml new file mode 100644 index 00000000000..b90ce8e4e67 --- /dev/null +++ b/dts/bindings/test/vnd,enum-string-array-holder.yaml @@ -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 diff --git a/tests/lib/devicetree/api/app.overlay b/tests/lib/devicetree/api/app.overlay index 0694c982390..359f17066a2 100644 --- a/tests/lib/devicetree/api/app.overlay +++ b/tests/lib/devicetree/api/app.overlay @@ -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. diff --git a/tests/lib/devicetree/api/src/main.c b/tests/lib/devicetree/api/src/main.c index 12724bfca91..d4dd12cf733 100644 --- a/tests/lib/devicetree/api/src/main.c +++ b/tests/lib/devicetree/api/src/main.c @@ -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 @@ -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) @@ -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