Skip to content

Add equality operators for QoS profile #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/include/rclcpp/qos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class RCLCPP_PUBLIC QoS
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:
Expand Down
29 changes: 29 additions & 0 deletions rclcpp/src/rclcpp/qos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}
Expand Down
77 changes: 77 additions & 0 deletions rclcpp/test/test_qos.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

#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);
}