From c82799bd4c2980f579afcb0b3dc07991f765579d Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Mon, 16 Sep 2024 19:02:14 -0500 Subject: [PATCH] tests: scripts: dts: Add tests for string escapes Added tests for escape sequences in string and string-array properties. Signed-off-by: Joel Spadin --- dts/bindings/test/vnd,string-array.yaml | 11 ++++++ dts/bindings/test/vnd,string.yaml | 11 ++++++ tests/lib/devicetree/api/app.overlay | 51 +++++++++++++++++++++++++ tests/lib/devicetree/api/src/main.c | 49 ++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 dts/bindings/test/vnd,string-array.yaml create mode 100644 dts/bindings/test/vnd,string.yaml diff --git a/dts/bindings/test/vnd,string-array.yaml b/dts/bindings/test/vnd,string-array.yaml new file mode 100644 index 000000000000..a832ba09f286 --- /dev/null +++ b/dts/bindings/test/vnd,string-array.yaml @@ -0,0 +1,11 @@ +# Copyright (c) 2024 Zephyr Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: Test string array property container + +compatible: "vnd,string-array" + +properties: + val: + type: string-array + required: true diff --git a/dts/bindings/test/vnd,string.yaml b/dts/bindings/test/vnd,string.yaml new file mode 100644 index 000000000000..c12b0e94f3ac --- /dev/null +++ b/dts/bindings/test/vnd,string.yaml @@ -0,0 +1,11 @@ +# Copyright (c) 2024 Zephyr Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: Test string property container + +compatible: "vnd,string" + +properties: + val: + type: string + required: true diff --git a/tests/lib/devicetree/api/app.overlay b/tests/lib/devicetree/api/app.overlay index 0b1f46ba859c..0694c9823906 100644 --- a/tests/lib/devicetree/api/app.overlay +++ b/tests/lib/devicetree/api/app.overlay @@ -687,6 +687,20 @@ val = "XA XPLUS XB"; }; + test_str_unquoted_esc_t: string-unquoted-escape-t { + compatible = "vnd,string-unquoted"; + val = "XA\nXPLUS\nXB"; + }; + + /* + * Tests expect all vnd,string-unquoted instances to evaluate to doubles, + * so use vnd,string instead. + */ + test_str_unquoted_esc_s: string-unquoted-escape-s { + compatible = "vnd,string"; + val = "XSTR1 \" plus \" XSTR2"; + }; + test_stra_unquoted_f0: string-array-unquoted-f0 { compatible = "vnd,string-array-unquoted"; val = "1.0e2", "2.0e2", "3.0e2", "4.0e2"; @@ -702,6 +716,43 @@ val = "XA XPLUS XB", "XC XPLUS XD", "XA XMINUS XB", "XC XMINUS XD"; }; + /* + * Tests expect all vnd,string-array-unquoted instances to evaluate to doubles, + * so use vnd,string-array instead. + */ + test_stra_unquoted_esc: string-array-unquoted-escape { + compatible = "vnd,string-array"; + val = "XA\nXPLUS\nXB", "XSTR1 \" plus \" XSTR2"; + }; + + test_str_escape_0: string-escape-0 { + compatible = "vnd,string"; + val = "\a\b\f\n\r\t\v"; + }; + + test_str_escape_1: string-escape-1 { + compatible = "vnd,string"; + val = "\'single\' \"double\""; + }; + + test_str_escape_2: string-escape-2 { + compatible = "vnd,string"; + val = "first\nsecond"; + }; + + test_str_escape_3: string-escape-3 { + compatible = "vnd,string"; + val = "\x01\x7F"; + }; + + test_stra_escape: string-array-escape { + compatible = "vnd,string-array"; + val = "\a\b\f\n\r\t\v", + "\'single\' \"double\"", + "first\nsecond", + "\x01\x7F"; + }; + test-mtd@ffeeddcc { reg = < 0xffeeddcc 0x1000 >; #address-cells = < 1 >; diff --git a/tests/lib/devicetree/api/src/main.c b/tests/lib/devicetree/api/src/main.c index 7616ef54a1f3..12724bfca912 100644 --- a/tests/lib/devicetree/api/src/main.c +++ b/tests/lib/devicetree/api/src/main.c @@ -3190,6 +3190,8 @@ ZTEST(devicetree_api, test_string_unquoted) #define XA 12.0 #define XB 34.0 #define XPLUS + +#define XSTR1 "one" +#define XSTR2 "two" const double f0_expected = 0.1234; const double f1_expected = 0.9e-3; const double delta = 0.1e-4; @@ -3201,6 +3203,10 @@ ZTEST(devicetree_api, test_string_unquoted) f1_expected, delta, ""); zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_t), val), XA XPLUS XB, delta, ""); + zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_esc_t), val), XA XPLUS XB, + delta, ""); + zassert_str_equal(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_esc_s), val), + "one plus two"); /* Test DT_STRING_UNQUOTED_OR */ zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), val, (0.0)), f0_expected, delta, ""); @@ -3208,12 +3214,20 @@ ZTEST(devicetree_api, test_string_unquoted) f1_expected, delta, ""); zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), val, (0.0)), XA XPLUS XB, delta, ""); + zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_t), val, (0.0)), + XA XPLUS XB, delta, ""); + zassert_str_equal(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_s), val, "nak"), + "one plus two"); zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), nak, (0.0)), 0.0, delta, ""); zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), nak, (0.0)), 0.0, delta, ""); zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), nak, (0.0)), 0.0, delta, ""); + zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_t), nak, (0.0)), + 0.0, delta, ""); + zassert_str_equal(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_s), nak, "nak"), + "nak"); /* Test DT_INST_STRING_UNQUOTED */ #define STRING_UNQUOTED_VAR(node_id) _CONCAT(var_, node_id) #define STRING_UNQUOTED_TEST_INST_EXPANSION(inst) \ @@ -3226,6 +3240,8 @@ ZTEST(devicetree_api, test_string_unquoted) f1_expected, delta, ""); zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_t)), XA XPLUS XB, delta, ""); + zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_esc_t)), XA XPLUS XB, + delta, ""); /* Test DT_INST_STRING_UNQUOTED_OR */ #define STRING_UNQUOTED_OR_VAR(node_id) _CONCAT(var_or_, node_id) @@ -3242,15 +3258,21 @@ ZTEST(devicetree_api, test_string_unquoted) f1_expected, delta, ""); zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[0], XA XPLUS XB, delta, ""); + zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_esc_t))[0], + XA XPLUS XB, delta, ""); zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f0))[1], 1.0e10, delta, ""); zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f1))[1], 1.0e10, delta, ""); zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[1], 1.0e10, delta, ""); + zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_esc_t))[1], 1.0e10, + delta, ""); #undef XA #undef XB #undef XPLUS +#undef XSTR1 +#undef XSTR2 } #undef DT_DRV_COMPAT @@ -3263,6 +3285,8 @@ ZTEST(devicetree_api, test_string_idx_unquoted) #define XD 78.0 #define XPLUS + #define XMINUS - +#define XSTR1 "one" +#define XSTR2 "two" const double delta = 0.1e-4; /* DT_STRING_UNQUOTED_BY_IDX */ @@ -3293,6 +3317,11 @@ ZTEST(devicetree_api, test_string_idx_unquoted) zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 3), XC XMINUS XD, delta, ""); + zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_esc), val, 0), + XA XPLUS XB, delta, ""); + zassert_str_equal(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_esc), val, 1), + "one plus two"); + #define STRING_UNQUOTED_BY_IDX_VAR(node_id) _CONCAT(var_, node_id) #define STRING_UNQUOTED_BY_IDX_TEST_INST_EXPANSION(inst) \ double STRING_UNQUOTED_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \ @@ -3335,6 +3364,26 @@ ZTEST(devicetree_api, test_string_idx_unquoted) #undef XD #undef XPLUS #undef XMINUS +#undef XSTR1 +#undef XSTR2 +} + +#undef DT_DRV_COMPAT +ZTEST(devicetree_api, test_string_escape) +{ + zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_0), val), "\a\b\f\n\r\t\v"); + zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_1), val), "\'single\' \"double\""); + zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_2), val), "first\nsecond"); + zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_3), val), "\x01\x7F"); +} + +ZTEST(devicetree_api, test_string_array_escape) +{ + zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 0), "\a\b\f\n\r\t\v"); + zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 1), + "\'single\' \"double\""); + zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 2), "first\nsecond"); + zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 3), "\x01\x7F"); } #undef DT_DRV_COMPAT