From 7a042377f48fb53e42f8e580cfc59faf9caa7625 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Tue, 29 Mar 2022 10:46:24 +0200 Subject: [PATCH] tests: drivers: can: utilities: add tests for DLC utility functions Add tests for can_dlc_to_bytes() and can_bytes_to_dlc() utility functions. Signed-off-by: Henrik Brix Andersen --- tests/drivers/can/utilities/src/main.c | 50 ++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/tests/drivers/can/utilities/src/main.c b/tests/drivers/can/utilities/src/main.c index c4be2c4d412..c6ce9a30720 100644 --- a/tests/drivers/can/utilities/src/main.c +++ b/tests/drivers/can/utilities/src/main.c @@ -1,4 +1,5 @@ /* + * Copyright (c) 2022 Vestas Wind Systems A/S * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 @@ -148,13 +149,58 @@ static void test_zcan_filter_to_can_filter(void) zassert_equal(filter.can_mask, expected.can_mask, "CAN mask not same"); } +/** + * @brief Test of @a can_dlc_to_bytes() + */ +static void test_can_dlc_to_bytes(void) +{ + uint8_t dlc; + + /* CAN 2.0B/CAN-FD DLC, 0 to 8 data bytes */ + for (dlc = 0; dlc <= 8; dlc++) { + zassert_equal(can_dlc_to_bytes(dlc), dlc, "wrong number of bytes for DLC %u", dlc); + } + + /* CAN-FD DLC, 12 to 64 data bytes in steps */ + zassert_equal(can_dlc_to_bytes(9), 12, "wrong number of bytes for DLC 9"); + zassert_equal(can_dlc_to_bytes(10), 16, "wrong number of bytes for DLC 10"); + zassert_equal(can_dlc_to_bytes(11), 20, "wrong number of bytes for DLC 11"); + zassert_equal(can_dlc_to_bytes(12), 24, "wrong number of bytes for DLC 12"); + zassert_equal(can_dlc_to_bytes(13), 32, "wrong number of bytes for DLC 13"); + zassert_equal(can_dlc_to_bytes(14), 48, "wrong number of bytes for DLC 14"); + zassert_equal(can_dlc_to_bytes(15), 64, "wrong number of bytes for DLC 15"); +} + +/** + * @brief Test of @a can_bytes_to_dlc() + */ +static void test_can_bytes_to_dlc(void) +{ + uint8_t bytes; + + /* CAN 2.0B DLC, 0 to 8 data bytes */ + for (bytes = 0; bytes <= 8; bytes++) { + zassert_equal(can_bytes_to_dlc(bytes), bytes, "wrong DLC for %u byte(s)", bytes); + } + + /* CAN-FD DLC, 12 to 64 data bytes in steps */ + zassert_equal(can_bytes_to_dlc(12), 9, "wrong DLC for 12 bytes"); + zassert_equal(can_bytes_to_dlc(16), 10, "wrong DLC for 16 bytes"); + zassert_equal(can_bytes_to_dlc(20), 11, "wrong DLC for 20 bytes"); + zassert_equal(can_bytes_to_dlc(24), 12, "wrong DLC for 24 bytes"); + zassert_equal(can_bytes_to_dlc(32), 13, "wrong DLC for 32 bytes"); + zassert_equal(can_bytes_to_dlc(48), 14, "wrong DLC for 48 bytes"); + zassert_equal(can_bytes_to_dlc(64), 15, "wrong DLC for 64 bytes"); +} + void test_main(void) { ztest_test_suite(can_utilities_tests, ztest_unit_test(test_can_frame_to_zcan_frame), ztest_unit_test(test_zcan_frame_to_can_frame), ztest_unit_test(test_can_filter_to_zcan_filter), - ztest_unit_test(test_zcan_filter_to_can_filter)); - + ztest_unit_test(test_zcan_filter_to_can_filter), + ztest_unit_test(test_can_dlc_to_bytes), + ztest_unit_test(test_can_bytes_to_dlc)); ztest_run_test_suite(can_utilities_tests); }