Skip to content

Commit

Permalink
tests: scripts: dts: Add tests for string escapes
Browse files Browse the repository at this point in the history
Added tests for escape sequences in string and string-array properties.

Signed-off-by: Joel Spadin <[email protected]>
  • Loading branch information
joelspadin authored and mmahadevan108 committed Oct 4, 2024
1 parent 6edefd8 commit c82799b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
11 changes: 11 additions & 0 deletions dts/bindings/test/vnd,string-array.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions dts/bindings/test/vnd,string.yaml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions tests/lib/devicetree/api/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 >;
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/devicetree/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -3201,19 +3203,31 @@ 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, "");
zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), val, (0.0)),
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) \
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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 */
Expand Down Expand Up @@ -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))[] = { \
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c82799b

Please sign in to comment.