diff --git a/rclcpp/CMakeLists.txt b/rclcpp/CMakeLists.txt index 625620c..26361a2 100644 --- a/rclcpp/CMakeLists.txt +++ b/rclcpp/CMakeLists.txt @@ -347,6 +347,15 @@ if(BUILD_TESTING) ) target_link_libraries(test_publisher_subscription_count_api ${PROJECT_NAME}) endif() + ament_add_gtest(test_qos test/test_qos.cpp) + if(TARGET test_qos) + ament_target_dependencies(test_qos + "rmw" + ) + target_link_libraries(test_qos + ${PROJECT_NAME} + ) + endif() ament_add_gtest(test_rate test/test_rate.cpp) if(TARGET test_rate) ament_target_dependencies(test_rate diff --git a/rclcpp/include/rclcpp/qos.hpp b/rclcpp/include/rclcpp/qos.hpp index 6f7db78..04e3666 100644 --- a/rclcpp/include/rclcpp/qos.hpp +++ b/rclcpp/include/rclcpp/qos.hpp @@ -151,6 +151,12 @@ private: rmw_qos_profile_t rmw_qos_profile_; }; +/// Check if two QoS profiles are exactly equal in all policy values. +RCLCPP_PUBLIC +bool operator==(const QoS & left, const QoS & right); +RCLCPP_PUBLIC +bool operator!=(const QoS & left, const QoS & right); + class RCLCPP_PUBLIC SensorDataQoS : public QoS { public: diff --git a/rclcpp/src/rclcpp/qos.cpp b/rclcpp/src/rclcpp/qos.cpp index bec92fd..4063fed 100644 --- a/rclcpp/src/rclcpp/qos.cpp +++ b/rclcpp/src/rclcpp/qos.cpp @@ -184,6 +184,35 @@ QoS::avoid_ros_namespace_conventions(bool avoid_ros_namespace_conventions) return *this; } +namespace +{ +/// Check if two rmw_time_t have the same values. +bool operator==(const rmw_time_t & left, const rmw_time_t & right) +{ + return left.sec == right.sec && left.nsec == right.nsec; +} +} // unnamed namespace + +bool operator==(const QoS & left, const QoS & right) +{ + const auto & pl = left.get_rmw_qos_profile(); + const auto & pr = right.get_rmw_qos_profile(); + return pl.history == pr.history && + pl.depth == pr.depth && + pl.reliability == pr.reliability && + pl.durability == pr.durability && + pl.deadline == pr.deadline && + pl.lifespan == pr.lifespan && + pl.liveliness == pr.liveliness && + pl.liveliness_lease_duration == pr.liveliness_lease_duration && + pl.avoid_ros_namespace_conventions == pr.avoid_ros_namespace_conventions; +} + +bool operator!=(const QoS & left, const QoS & right) +{ + return !(left == right); +} + SensorDataQoS::SensorDataQoS(const QoSInitialization & qos_initialization) : QoS(qos_initialization, rmw_qos_profile_sensor_data) {} diff --git a/rclcpp/test/test_qos.cpp b/rclcpp/test/test_qos.cpp new file mode 100644 index 0000000..b80c776 --- /dev/null +++ b/rclcpp/test/test_qos.cpp @@ -0,0 +1,77 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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 + +#include "rclcpp/qos.hpp" + +TEST(TestQoS, equality_history) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + EXPECT_EQ(a, b); + a.keep_last(5); + EXPECT_NE(a, b); + a.keep_all(); + b.keep_all(); + EXPECT_EQ(a, b); +} + +TEST(TestQoS, equality_reliability) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + b.best_effort(); + EXPECT_NE(a, b); +} + +TEST(TestQoS, equality_durability) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + a.transient_local(); + EXPECT_NE(a, b); +} + +TEST(TestQoS, equality_deadline) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + rmw_time_t deadline{0, 1000}; + a.deadline(deadline); + EXPECT_NE(a, b); +} + +TEST(TestQoS, equality_lifespan) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + rmw_time_t lifespan{3, 0}; + a.lifespan(lifespan); + EXPECT_NE(a, b); +} + +TEST(TestQoS, equality_liveliness) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + rmw_time_t duration{0, 1000000}; + a.liveliness_lease_duration(duration); + EXPECT_NE(a, b); + b.liveliness_lease_duration(duration); + EXPECT_EQ(a, b); + a.liveliness(RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE); + EXPECT_NE(a, b); +} + +TEST(TestQoS, equality_namespace) { + rclcpp::QoS a(10); + rclcpp::QoS b(10); + a.avoid_ros_namespace_conventions(true); + EXPECT_NE(a, b); +}