Skip to content

Commit

Permalink
Add equality operators for QoS profile (#1032)
Browse files Browse the repository at this point in the history
* Add equality operators for QoS profile

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>

* Use == operator for rmw_time_t as well

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>

* Add visibility macros for the new functions

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>

* Add tests for every member of the profile

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>

* Remove dangling space

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
  • Loading branch information
emersonknapp committed Mar 25, 2020
1 parent a985d6d commit 9017efb
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
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);
}

0 comments on commit 9017efb

Please sign in to comment.