From 65b22704d507325da31e983c53c25ab8a1b71e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 21 Aug 2024 11:12:33 +0200 Subject: [PATCH] Add regression test for generation error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Øyvind Rønningstad --- tests/cases/corner_cases.cddl | 5 +++ .../decode/test5_corner_cases/CMakeLists.txt | 1 + tests/decode/test5_corner_cases/src/main.c | 30 +++++++++++++++++ .../encode/test3_corner_cases/CMakeLists.txt | 1 + tests/encode/test3_corner_cases/src/main.c | 32 +++++++++++++++++++ 5 files changed, 69 insertions(+) diff --git a/tests/cases/corner_cases.cddl b/tests/cases/corner_cases.cddl index 56b800c8..baa46a96 100644 --- a/tests/cases/corner_cases.cddl +++ b/tests/cases/corner_cases.cddl @@ -367,3 +367,8 @@ Choice2 = nil / ([ * [tstr] ]) Choice3 = nil / ([ * ([tstr])/nil ]) Choice4 = nil / ([ * ([[Choice3]])/nil ]) Choice5 = nil / ([ * {*int=>[tstr]}/nil ]) + +OptList = [ + optlist_int: int, + ?optlist: [uint], +] diff --git a/tests/decode/test5_corner_cases/CMakeLists.txt b/tests/decode/test5_corner_cases/CMakeLists.txt index c8d88be5..78d78d18 100644 --- a/tests/decode/test5_corner_cases/CMakeLists.txt +++ b/tests/decode/test5_corner_cases/CMakeLists.txt @@ -62,6 +62,7 @@ set(py_command Choice3 Choice4 Choice5 + OptList --decode --git-sha-header --short-names diff --git a/tests/decode/test5_corner_cases/src/main.c b/tests/decode/test5_corner_cases/src/main.c index a1975061..bb930cc8 100644 --- a/tests/decode/test5_corner_cases/src/main.c +++ b/tests/decode/test5_corner_cases/src/main.c @@ -2387,4 +2387,34 @@ ZTEST(cbor_decode_test5, test_nested_choices) } +ZTEST(cbor_decode_test5, test_optlist) +{ + uint8_t optlist_payload1[] = {LIST(1), + 10, + END + }; + uint8_t optlist_payload2[] = {LIST(2), + 11, + LIST(1), 0x18, 100, END + END + }; + struct OptList result; + + size_t num_decode; + + zassert_equal(ZCBOR_SUCCESS, cbor_decode_OptList(optlist_payload1, + sizeof(optlist_payload1), &result, &num_decode)); + + zassert_false(result.uint_present); + zassert_equal(10, result.optlist_int); + + zassert_equal(ZCBOR_SUCCESS, cbor_decode_OptList(optlist_payload2, + sizeof(optlist_payload2), &result, &num_decode)); + + zassert_true(result.uint_present); + zassert_equal(11, result.optlist_int); + zassert_equal(100, result.uint); +} + + ZTEST_SUITE(cbor_decode_test5, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/encode/test3_corner_cases/CMakeLists.txt b/tests/encode/test3_corner_cases/CMakeLists.txt index 34ff5076..6b6e7a0a 100644 --- a/tests/encode/test3_corner_cases/CMakeLists.txt +++ b/tests/encode/test3_corner_cases/CMakeLists.txt @@ -53,6 +53,7 @@ set(py_command Choice3 Choice4 Choice5 + OptList -e ${bit_arg} --short-names diff --git a/tests/encode/test3_corner_cases/src/main.c b/tests/encode/test3_corner_cases/src/main.c index d6e21ab1..61cf5a0c 100644 --- a/tests/encode/test3_corner_cases/src/main.c +++ b/tests/encode/test3_corner_cases/src/main.c @@ -1794,4 +1794,36 @@ ZTEST(cbor_encode_test3, test_nested_choices) } +ZTEST(cbor_encode_test3, test_optlist) +{ + uint8_t optlist_exp_payload1[] = {LIST(1), + 10, + END + }; + uint8_t optlist_exp_payload2[] = {LIST(2), + 11, + LIST(1), 0x18, 100, END + END + }; + struct OptList input; + uint8_t payload[20]; + + input.uint_present = false; + input.optlist_int= 10; + + zassert_equal(ZCBOR_SUCCESS, cbor_encode_OptList(payload, + sizeof(payload), &input, NULL)); + zassert_mem_equal(payload, optlist_exp_payload1, sizeof(optlist_exp_payload1)); + + input.uint_present = true; + input.optlist_int = 11; + input.uint = 100; + + zassert_equal(ZCBOR_SUCCESS, cbor_encode_OptList(payload, + sizeof(payload), &input, NULL)); + zassert_mem_equal(payload, optlist_exp_payload2, sizeof(optlist_exp_payload2)); +} + + + ZTEST_SUITE(cbor_encode_test3, NULL, NULL, NULL, NULL, NULL);