From b4cfba41e132589cee333915ec2cb5384e86c766 Mon Sep 17 00:00:00 2001 From: Christoph Froehlich Date: Wed, 20 Nov 2024 22:04:31 +0000 Subject: [PATCH] Add tests --- CMakeLists.txt | 6 ++ test/control_filters/test_rate_limiter.cpp | 87 +++++++++++++++++++ test/control_filters/test_rate_limiter.hpp | 62 +++++++++++++ .../test_rate_limiter_parameters.yaml | 17 ++++ 4 files changed, 172 insertions(+) create mode 100644 test/control_filters/test_rate_limiter.cpp create mode 100644 test/control_filters/test_rate_limiter.hpp create mode 100644 test/control_filters/test_rate_limiter_parameters.yaml diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f144e88..8242a3ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,6 +133,12 @@ if(BUILD_TESTING) target_link_libraries(test_load_low_pass_filter low_pass_filter low_pass_filter_parameters) ament_target_dependencies(test_load_low_pass_filter ${CONTROL_FILTERS_INCLUDE_DEPENDS}) + add_rostest_with_parameters_gmock(test_rate_limiter test/control_filters/test_rate_limiter.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/control_filters/test_rate_limiter_parameters.yaml + ) + target_link_libraries(test_rate_limiter rate_limiter rate_limiter_parameters) + ament_target_dependencies(test_rate_limiter ${CONTROL_FILTERS_INCLUDE_DEPENDS}) + ament_add_gmock(test_load_rate_limiter test/control_filters/test_load_rate_limiter.cpp) target_link_libraries(test_load_rate_limiter rate_limiter rate_limiter_parameters) ament_target_dependencies(test_load_rate_limiter ${CONTROL_FILTERS_INCLUDE_DEPENDS}) diff --git a/test/control_filters/test_rate_limiter.cpp b/test/control_filters/test_rate_limiter.cpp new file mode 100644 index 00000000..6ba29b60 --- /dev/null +++ b/test/control_filters/test_rate_limiter.cpp @@ -0,0 +1,87 @@ +// Copyright 2024 AIT - Austrian Institute of Technology GmbH +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "test_rate_limiter.hpp" + +TEST_F(RateLimiterTest, TestRateLimiterAllParameters) +{ + std::shared_ptr> filter_ = + std::make_shared>(); + + // should allow configuration and find parameters in sensor_filter_chain param namespace + ASSERT_TRUE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); + + // change a parameter + node_->set_parameter(rclcpp::Parameter("sampling_interval", 0.5)); + // accept second call to configure with valid parameters to already configured filter + ASSERT_TRUE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); +} + + +TEST_F(RateLimiterTest, TestRateLimiterMissingParameter) +{ + std::shared_ptr> filter_ = + std::make_shared>(); + + // should deny configuration as sampling frequency is missing + ASSERT_FALSE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); +} + +TEST_F(RateLimiterTest, TestRateLimiterInvalidThenFixedParameter) +{ + std::shared_ptr> filter_ = + std::make_shared>(); + + // should deny configuration as sampling frequency is invalid + ASSERT_FALSE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); + + // fix the param + node_->set_parameter(rclcpp::Parameter("sampling_interval", 1.0)); + // should allow configuration and pass second call to unconfigured filter + ASSERT_TRUE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); +} + +TEST_F(RateLimiterTest, TestRateLimiterThrowsUnconfigured) +{ + std::shared_ptr> filter_ = + std::make_shared>(); + double in, out; + ASSERT_THROW(filter_->update(in, out), std::runtime_error); +} + +TEST_F(RateLimiterTest, TestRateLimiterCompute) +{ + std::shared_ptr> filter_ = + std::make_shared>(); + + ASSERT_TRUE(filter_->configure("", "TestRateLimiter", + node_->get_node_logging_interface(), node_->get_node_parameters_interface())); + + double in, out; + ASSERT_NO_THROW(filter_->update(in, out)); +} + +int main(int argc, char ** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + rclcpp::init(argc, argv); + int result = RUN_ALL_TESTS(); + rclcpp::shutdown(); + return result; +} diff --git a/test/control_filters/test_rate_limiter.hpp b/test/control_filters/test_rate_limiter.hpp new file mode 100644 index 00000000..c9ba1631 --- /dev/null +++ b/test/control_filters/test_rate_limiter.hpp @@ -0,0 +1,62 @@ +// Copyright 2024 AIT - Austrian Institute of Technology GmbH +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef CONTROL_FILTERS__TEST_RATE_LIMITER_HPP_ +#define CONTROL_FILTERS__TEST_RATE_LIMITER_HPP_ + +#include +#include +#include "gmock/gmock.h" + +#include "control_filters/rate_limiter.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace +{ +static const rclcpp::Logger LOGGER = rclcpp::get_logger("test_rate_limiter"); +} // namespace + +class RateLimiterTest : public ::testing::Test +{ +public: + void SetUp() override + { + auto testname = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + node_ = std::make_shared(testname); + executor_->add_node(node_); + executor_thread_ = std::thread([this]() { executor_->spin(); }); + } + + RateLimiterTest() + { + executor_ = std::make_shared(); + } + + void TearDown() override + { + executor_->cancel(); + if (executor_thread_.joinable()) + { + executor_thread_.join(); + } + node_.reset(); + } + +protected: + rclcpp::Node::SharedPtr node_; + rclcpp::Executor::SharedPtr executor_; + std::thread executor_thread_; +}; + +#endif // CONTROL_FILTERS__TEST_RATE_LIMITER_HPP_ diff --git a/test/control_filters/test_rate_limiter_parameters.yaml b/test/control_filters/test_rate_limiter_parameters.yaml new file mode 100644 index 00000000..6acb9d8a --- /dev/null +++ b/test/control_filters/test_rate_limiter_parameters.yaml @@ -0,0 +1,17 @@ +TestRateLimiterMissingParameter: + ros__parameters: {} + +TestRateLimiterAllParameters: + ros__parameters: + sampling_interval: 1.0 + +TestRateLimiterInvalidThenFixedParameter: + ros__parameters: {} + +TestRateLimiterThrowsUnconfigured: + ros__parameters: + sampling_interval: 1.0 + +TestRateLimiterCompute: + ros__parameters: + sampling_interval: 1.0