-
Notifications
You must be signed in to change notification settings - Fork 411
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
Add coverage for missing API (except executors) #1326
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2020 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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/allocator/allocator_common.hpp" | ||
|
||
TEST(TestAllocatorCommon, retyped_allocate) { | ||
std::allocator<int> allocator; | ||
void * untyped_allocator = &allocator; | ||
void * allocated_mem = | ||
rclcpp::allocator::retyped_allocate<std::allocator<char>>(1u, untyped_allocator); | ||
EXPECT_NE(nullptr, allocated_mem); | ||
|
||
auto code = [&untyped_allocator, allocated_mem]() { | ||
rclcpp::allocator::retyped_deallocate<int, std::allocator<int>>( | ||
allocated_mem, untyped_allocator); | ||
}; | ||
EXPECT_NO_THROW(code()); | ||
|
||
allocated_mem = allocator.allocate(1); | ||
ASSERT_NE(nullptr, allocated_mem); | ||
void * reallocated_mem = | ||
rclcpp::allocator::retyped_reallocate<int, std::allocator<int>>( | ||
allocated_mem, 2u, untyped_allocator); | ||
EXPECT_NE(nullptr, reallocated_mem); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here as to whether it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
auto code2 = [&untyped_allocator, reallocated_mem]() { | ||
rclcpp::allocator::retyped_deallocate<int, std::allocator<int>>( | ||
reallocated_mem, untyped_allocator); | ||
}; | ||
EXPECT_NO_THROW(code2()); | ||
} | ||
|
||
TEST(TestAllocatorCommon, get_rcl_allocator) { | ||
std::allocator<int> allocator; | ||
auto rcl_allocator = rclcpp::allocator::get_rcl_allocator<int>(allocator); | ||
EXPECT_NE(nullptr, rcl_allocator.allocate); | ||
EXPECT_NE(nullptr, rcl_allocator.deallocate); | ||
EXPECT_NE(nullptr, rcl_allocator.reallocate); | ||
EXPECT_NE(nullptr, rcl_allocator.state); | ||
} | ||
|
||
TEST(TestAllocatorCommon, get_void_rcl_allocator) { | ||
std::allocator<void> allocator; | ||
auto rcl_allocator = | ||
rclcpp::allocator::get_rcl_allocator<void, std::allocator<void>>(allocator); | ||
EXPECT_NE(nullptr, rcl_allocator.allocate); | ||
EXPECT_NE(nullptr, rcl_allocator.deallocate); | ||
EXPECT_NE(nullptr, rcl_allocator.reallocate); | ||
EXPECT_EQ(nullptr, rcl_allocator.state); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2020 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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/allocator/allocator_deleter.hpp" | ||
|
||
#include "../../utils/rclcpp_gtest_macros.hpp" | ||
|
||
TEST(TestAllocatorDeleter, construct_destruct) { | ||
std::allocator<int> allocator; | ||
|
||
rclcpp::allocator::AllocatorDeleter<std::allocator<int>> deleter; | ||
EXPECT_EQ(nullptr, deleter.get_allocator()); | ||
deleter.set_allocator(&allocator); | ||
EXPECT_EQ(&allocator, deleter.get_allocator()); | ||
|
||
rclcpp::allocator::AllocatorDeleter<std::allocator<int>> deleter2(&allocator); | ||
EXPECT_EQ(&allocator, deleter2.get_allocator()); | ||
|
||
rclcpp::allocator::AllocatorDeleter<std::allocator<int>> deleter3(deleter2); | ||
EXPECT_EQ(&allocator, deleter3.get_allocator()); | ||
} | ||
|
||
TEST(TestAllocatorDeleter, delete) { | ||
std::allocator<int> allocator; | ||
int * some_mem = allocator.allocate(1u); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
rclcpp::allocator::AllocatorDeleter<std::allocator<int>> deleter(&allocator); | ||
EXPECT_NO_THROW(deleter(some_mem)); | ||
} | ||
|
||
TEST(TestAllocatorDeleter, set_allocator_for_deleter_AllocatorDeleter) { | ||
using AllocatorT = std::allocator<int>; | ||
using DeleterT = rclcpp::allocator::AllocatorDeleter<AllocatorT>; | ||
AllocatorT allocator; | ||
DeleterT deleter(&allocator); | ||
|
||
std::allocator<int> allocator2; | ||
rclcpp::allocator::set_allocator_for_deleter<AllocatorT, AllocatorT>(&deleter, &allocator2); | ||
EXPECT_EQ(&allocator2, deleter.get_allocator()); | ||
|
||
auto throwing_statement = [&allocator]() { | ||
DeleterT * null_del_ptr = nullptr; | ||
rclcpp::allocator::set_allocator_for_deleter<AllocatorT, AllocatorT>( | ||
null_del_ptr, &allocator); | ||
}; | ||
RCLCPP_EXPECT_THROW_EQ( | ||
throwing_statement(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brawner out of curiosity, why using lambdas instead of putting the block statement right in here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My compiler was having trouble parsing the templated function inside a macro. |
||
std::invalid_argument("Argument was NULL to set_allocator_for_deleter")); | ||
|
||
auto throwing_statement2 = [&deleter]() { | ||
AllocatorT * null_alloc_ptr = nullptr; | ||
rclcpp::allocator::set_allocator_for_deleter<AllocatorT, AllocatorT>( | ||
&deleter, null_alloc_ptr); | ||
}; | ||
|
||
RCLCPP_EXPECT_THROW_EQ( | ||
throwing_statement2(), | ||
std::invalid_argument("Argument was NULL to set_allocator_for_deleter")); | ||
} | ||
|
||
TEST(TestAllocatorDeleter, set_allocator_for_deleter_std_default_delete) { | ||
using AllocatorT = std::allocator<int>; | ||
using DeleterT = std::default_delete<int>; | ||
auto not_throwing_statement = []() { | ||
AllocatorT allocator; | ||
DeleterT deleter; | ||
rclcpp::allocator::set_allocator_for_deleter<int, int>(&deleter, &allocator); | ||
}; | ||
EXPECT_NO_THROW(not_throwing_statement()); | ||
} | ||
|
||
TEST(TestAllocatorDeleter, set_allocator_for_deleter_unexpected_template) { | ||
class SomeAllocatorClass {}; | ||
class SomeDeleterClass {}; | ||
using AllocatorT = SomeAllocatorClass; | ||
using DeleterT = SomeDeleterClass; | ||
auto throwing_statement = []() { | ||
DeleterT deleter; | ||
AllocatorT allocator; | ||
rclcpp::allocator::set_allocator_for_deleter<AllocatorT, int, DeleterT>(&deleter, &allocator); | ||
}; | ||
RCLCPP_EXPECT_THROW_EQ( | ||
throwing_statement(), | ||
std::runtime_error("Reached unexpected template specialization")); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2020 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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/exceptions/exceptions.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
#include "../../mocking_utils/patch.hpp" | ||
#include "../../utils/rclcpp_gtest_macros.hpp" | ||
|
||
TEST(TestExceptions, throw_from_rcl_error) { | ||
EXPECT_THROW( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_BAD_ALLOC, ""), | ||
rclcpp::exceptions::RCLBadAlloc); | ||
|
||
EXPECT_THROW( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_INVALID_ARGUMENT, ""), | ||
rclcpp::exceptions::RCLInvalidArgument); | ||
|
||
EXPECT_THROW( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_INVALID_ROS_ARGS, ""), | ||
rclcpp::exceptions::RCLInvalidROSArgsError); | ||
|
||
EXPECT_THROW( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_ERROR, ""), | ||
rclcpp::exceptions::RCLError); | ||
|
||
RCLCPP_EXPECT_THROW_EQ( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_OK, ""), | ||
std::invalid_argument("ret is RCL_RET_OK")); | ||
|
||
{ | ||
auto mock = mocking_utils::patch_and_return("lib:rclcpp", rcl_get_error_state, nullptr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forget how the mocking works; do we need to "unmock" after this, or does leaving the scope do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The scope block takes care of that, which is awesome |
||
RCLCPP_EXPECT_THROW_EQ( | ||
rclcpp::exceptions::throw_from_rcl_error(RCL_RET_ERROR, ""), | ||
std::runtime_error("rcl error state is not set")); | ||
} | ||
} | ||
|
||
TEST(TestExceptions, basic_constructors) { | ||
EXPECT_STREQ("node is invalid", rclcpp::exceptions::InvalidNodeError().what()); | ||
rcl_error_state_t error_state{"error", __FILE__, __LINE__}; | ||
EXPECT_STREQ( | ||
"prefix: error not set", | ||
rclcpp::exceptions::RCLInvalidROSArgsError(RCL_RET_ERROR, &error_state, "prefix: ").what()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2015 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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 <string> | ||
|
||
#include "rclcpp/future_return_code.hpp" | ||
|
||
TEST(TestFutureReturnCode, to_string) { | ||
EXPECT_EQ( | ||
std::string("Unknown enum value (-1)"), rclcpp::to_string(rclcpp::FutureReturnCode(-1))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brawner nit: IIRC it's not necessary to explicitly construct an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
EXPECT_EQ( | ||
std::string("SUCCESS (0)"), rclcpp::to_string(rclcpp::FutureReturnCode::SUCCESS)); | ||
EXPECT_EQ( | ||
std::string("INTERRUPTED (1)"), rclcpp::to_string(rclcpp::FutureReturnCode::INTERRUPTED)); | ||
EXPECT_EQ( | ||
std::string("TIMEOUT (2)"), rclcpp::to_string(rclcpp::FutureReturnCode::TIMEOUT)); | ||
EXPECT_EQ( | ||
std::string("Unknown enum value (3)"), rclcpp::to_string(rclcpp::FutureReturnCode(3))); | ||
EXPECT_EQ( | ||
std::string("Unknown enum value (100)"), rclcpp::to_string(rclcpp::FutureReturnCode(100))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an
ASSERT_NE
? That is, what happens during theretyped_deallocate
below if this happens to be nullptr?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Done