Skip to content

Commit

Permalink
Add regression test for generation error
Browse files Browse the repository at this point in the history
Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad committed Aug 21, 2024
1 parent bed3782 commit 65b2270
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/cases/corner_cases.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]
1 change: 1 addition & 0 deletions tests/decode/test5_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(py_command
Choice3
Choice4
Choice5
OptList
--decode
--git-sha-header
--short-names
Expand Down
30 changes: 30 additions & 0 deletions tests/decode/test5_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions tests/encode/test3_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(py_command
Choice3
Choice4
Choice5
OptList
-e
${bit_arg}
--short-names
Expand Down
32 changes: 32 additions & 0 deletions tests/encode/test3_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 65b2270

Please sign in to comment.