Remove incorrect exception on sec < 0 (#527)
* Remove incorrect exception on sec < 0
This commit is contained in:
parent
ea047655d8
commit
6b34bcc94c
2 changed files with 23 additions and 3 deletions
|
@ -88,9 +88,6 @@ Duration::operator=(const Duration & rhs)
|
|||
Duration &
|
||||
Duration::operator=(const builtin_interfaces::msg::Duration & duration_msg)
|
||||
{
|
||||
if (duration_msg.sec < 0) {
|
||||
throw std::runtime_error("cannot store a negative duration point in rclcpp::Duration");
|
||||
}
|
||||
rcl_duration_.nanoseconds = RCL_S_TO_NS(static_cast<int64_t>(duration_msg.sec));
|
||||
rcl_duration_.nanoseconds += duration_msg.nanosec;
|
||||
return *this;
|
||||
|
|
|
@ -97,3 +97,26 @@ TEST(TestDuration, overflows) {
|
|||
EXPECT_THROW(base_d_neg * (-4), std::overflow_error);
|
||||
EXPECT_THROW(base_d_neg * 4, std::underflow_error);
|
||||
}
|
||||
|
||||
TEST(TestDuration, negative_duration) {
|
||||
rclcpp::Duration assignable_duration = rclcpp::Duration(0) - rclcpp::Duration(5, 0);
|
||||
|
||||
{
|
||||
// avoid windows converting a literal number less than -INT_MAX to unsigned int C4146
|
||||
int64_t expected_value = -5000;
|
||||
expected_value *= 1000 * 1000;
|
||||
EXPECT_EQ(expected_value, assignable_duration.nanoseconds());
|
||||
}
|
||||
|
||||
{
|
||||
builtin_interfaces::msg::Duration duration_msg;
|
||||
duration_msg.sec = -4;
|
||||
duration_msg.nanosec = 250000000;
|
||||
|
||||
assignable_duration = duration_msg;
|
||||
// avoid windows converting a literal number less than -INT_MAX to unsigned int C4146
|
||||
int64_t expected_value = -3750;
|
||||
expected_value *= 1000 * 1000;
|
||||
EXPECT_EQ(expected_value, assignable_duration.nanoseconds());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue