diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 533b8ba..7d2fa59 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -83,6 +83,6 @@ gen_test_matrix(test_private "-Wno-missing-declarations") gen_test_matrix(test_public - "test_public_tx.cpp;test_public_rx.cpp;test_public_roundtrip.cpp;test_self.cpp" + "test_public_tx.cpp;test_public_rx.cpp;test_public_roundtrip.cpp;test_self.cpp;test_filters.cpp" "" "-Wmissing-declarations") diff --git a/tests/test_filters.cpp b/tests/test_filters.cpp new file mode 100644 index 0000000..e2f2d6d --- /dev/null +++ b/tests/test_filters.cpp @@ -0,0 +1,54 @@ +// This software is distributed under the terms of the MIT License. +// Copyright (c) 2016-2021 UAVCAN Development Team. + +#include "exposed.hpp" +#include "helpers.hpp" + +TEST_CASE("FilterSubject") +{ + const heartbeat_subject_id = 7509; + CanardFilter heartbeat_config = canardMakeFilterForSubject(heartbeat_subject_id); + REQUIRE(heartbeat_config.extended_can_id & (heartbeat_subject_id << OFFSET_SUBJECT_ID)); + REQUIRE(heartbeat_config.extended_mask & FLAG_SERVICE_NOT_MESSAGE); + REQUIRE(heartbeat_config.extended_mask & FLAG_RESERVED_07); + REQUIRE(heartbeat_config.extended_mask & (CANARD_SUBJECT_ID_MAX << OFFSET_SUBJECT_ID)); +} + +TEST_CASE("FilterService") +{ + const access_service_id = 7509; + const node_id = 42; + CanardFilter access_config = canardMakeFilterForService(access_service_id, node_id); + REQUIRE(access_config.extended_can_id & (access_subject_id << OFFSET_SERVICE_ID)); + REQUIRE(access_config.extended_can_id & (node_id << OFFSET_DST_NODE_ID)); + REQUIRE(access_config.extended_can_id & FLAG_SERVICE_NOT_MESSAGE); + REQUIRE(access_config.extended_mask & FLAG_SERVICE_NOT_MESSAGE); + REQUIRE(access_config.extended_mask & FLAG_RESERVED_23); + REQUIRE(access_config.extended_mask & (CANARD_SERVICE_ID_MAX << OFFSET_SERVICE_ID)); + REQUIRE(access_config.extended_mask & (CANARD_NODE_ID_MAX << OFFSET_DST_NODE_ID)); +} + +TEST_CASE("FilterServices") +{ + const node_id = 42; + access_config = canardMakeFilterForServices(node_id); + REQUIRE(access_config.extended_can_id & (node_id << OFFSET_DST_NODE_ID)); + REQUIRE(access_config.extended_can_id & FLAG_SERVICE_NOT_MESSAGE); + REQUIRE(access_config.extended_mask & FLAG_SERVICE_NOT_MESSAGE); + REQUIRE(access_config.extended_mask & FLAG_RESERVED_23); + REQUIRE(access_config.extended_mask & (CANARD_NODE_ID_MAX << OFFSET_DST_NODE_ID)); +} + +TEST_CASE("Consolidate") +{ + const heartbeat_subject_id = 7509; + CanardFilter heartbeat_config = canardMakeFilterForSubject(heartbeat_subject_id); + + const access_service_id = 7509; + const node_id = 42; + CanardFilter access_config = canardMakeFilterForService(access_service_id, node_id); + + CanardFilter combined = canardConsolidateFilters(&heartbeat_config, &access_config); + REQUIRE((combined.extended_mask | heartbeat_config.extended_mask) == heartbeat_config.extended_mask); + REQUIRE((combined.extended_mask | access_config.extended_mask) == access_config.extended_mask); +}