Simplify tests
This commit is contained in:
parent
0288722985
commit
5c32905944
3 changed files with 28 additions and 96 deletions
|
@ -94,7 +94,6 @@ if(BUILD_TESTING)
|
||||||
ament_add_gtest(test_utils test/test_utils.cpp)
|
ament_add_gtest(test_utils test/test_utils.cpp)
|
||||||
if(TARGET test_utils)
|
if(TARGET test_utils)
|
||||||
target_link_libraries(test_utils ${PROJECT_NAME} -rdynamic)
|
target_link_libraries(test_utils ${PROJECT_NAME} -rdynamic)
|
||||||
target_include_directories(test_utils PRIVATE test/)
|
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
|
@ -20,17 +20,19 @@
|
||||||
#include "tracetools/utils.hpp"
|
#include "tracetools/utils.hpp"
|
||||||
#include "test_utils.hpp"
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
int function_int_int(int num)
|
class SomeClassWithCallback
|
||||||
{
|
{
|
||||||
return num + 1;
|
public:
|
||||||
}
|
SomeClassWithCallback() {}
|
||||||
|
|
||||||
void function_generic_shared(const std::shared_ptr<int> p)
|
void my_callback(int some_number, std::string some_string)
|
||||||
{
|
{
|
||||||
(void)p;
|
(void)some_number;
|
||||||
|
(void)some_string;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void function_generic_unique(const std::unique_ptr<int> p)
|
void function_shared(const std::shared_ptr<int> p)
|
||||||
{
|
{
|
||||||
(void)p;
|
(void)p;
|
||||||
}
|
}
|
||||||
|
@ -39,29 +41,26 @@ void function_generic_unique(const std::unique_ptr<int> p)
|
||||||
Testing address and symbol resolution for std::function objects.
|
Testing address and symbol resolution for std::function objects.
|
||||||
*/
|
*/
|
||||||
TEST(TestUtils, valid_address_symbol) {
|
TEST(TestUtils, valid_address_symbol) {
|
||||||
std::function<int(int)> f = &function_int_int;
|
// Function pointer
|
||||||
std::function<int(int)> lambda = [](int num) {return num + 1;};
|
std::function<void(std::shared_ptr<int>)> f = &function_shared;
|
||||||
std::function<int(int)> l = lambda;
|
// Address for one with an actual underlying function should be non-zero
|
||||||
|
ASSERT_GT(get_address(f), (void *)0) << "get_address() for function not valid";
|
||||||
ASSERT_EQ(f(69), l(69));
|
ASSERT_STREQ(get_symbol(get_address(f)), "function_shared(std::shared_ptr<int>)") << "invalid function name";
|
||||||
|
|
||||||
|
// Lambda
|
||||||
|
std::function<int(int)> l = [](int num) {return num + 1;};
|
||||||
// Address for an std::function with an underlying lambda should be nullptr
|
// Address for an std::function with an underlying lambda should be nullptr
|
||||||
ASSERT_EQ(get_address(l), nullptr) << "get_address() for lambda std::function not 0";
|
ASSERT_EQ(get_address(l), nullptr) << "get_address() for lambda std::function not 0";
|
||||||
// But address for one with an actual underlying function should be non-zero
|
// TODO symbol
|
||||||
ASSERT_GT(get_address(f), (void *)0) << "get_address() for function not valid";
|
|
||||||
|
|
||||||
ASSERT_STREQ(get_symbol(get_address(f)), "function_int_int(int)") << "invalid function name";
|
// Bind (to member function)
|
||||||
|
SomeClassWithCallback scwc;
|
||||||
// Generic
|
std::function<void(int, std::string)> fscwc = std::bind(
|
||||||
std::function<void (const std::shared_ptr<int>)> fg_shared = &function_generic_shared;
|
&SomeClassWithCallback::my_callback,
|
||||||
SomeGenericClass<int> gc_shared;
|
&scwc,
|
||||||
gc_shared.set(fg_shared);
|
std::placeholders::_1,
|
||||||
ASSERT_GT(gc_shared.get_address_(), (void *)0) << "generic -- address invalid";
|
std::placeholders::_2
|
||||||
ASSERT_STREQ(gc_shared.get_symbol_(), "function_generic_shared(std::shared_ptr<int>)") << "generic -- symbol invalid";
|
);
|
||||||
|
ASSERT_EQ(get_address(fscwc), nullptr) << "get_address() for std::bind std::function not 0";
|
||||||
std::function<void (const std::unique_ptr<int>)> fg_unique = &function_generic_unique;
|
// TODO symbol
|
||||||
SomeGenericClass<int> gc_unique;
|
|
||||||
gc_unique.set(fg_unique);
|
|
||||||
ASSERT_GT(gc_unique.get_address_(), (void *)0) << "generic -- address invalid";
|
|
||||||
ASSERT_STREQ(gc_unique.get_symbol_(), "function_generic_unique(std::unique_ptr<int, std::default_delete<int> >)") << "generic -- symbol invalid";
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
#include <memory>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include "tracetools/utils.hpp"
|
|
||||||
#include "function_traits.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
template<typename ParamT>
|
|
||||||
class SomeGenericClass
|
|
||||||
{
|
|
||||||
using SharedPtrCallback = std::function<void (const std::shared_ptr<ParamT>)>;
|
|
||||||
using UniquePtrCallback = std::function<void (const std::unique_ptr<ParamT>)>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SomeGenericClass()
|
|
||||||
: my_callback_shared_(nullptr), my_callback_unique_(nullptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
template<
|
|
||||||
typename TheType,
|
|
||||||
typename std::enable_if<
|
|
||||||
rclcpp::function_traits::same_arguments<
|
|
||||||
TheType,
|
|
||||||
SharedPtrCallback
|
|
||||||
>::value
|
|
||||||
>::type * = nullptr
|
|
||||||
>
|
|
||||||
void set(TheType callback)
|
|
||||||
{
|
|
||||||
my_callback_shared_ = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<
|
|
||||||
typename TheType,
|
|
||||||
typename std::enable_if<
|
|
||||||
rclcpp::function_traits::same_arguments<
|
|
||||||
TheType,
|
|
||||||
UniquePtrCallback
|
|
||||||
>::value
|
|
||||||
>::type * = nullptr
|
|
||||||
>
|
|
||||||
void set(TheType callback)
|
|
||||||
{
|
|
||||||
my_callback_unique_ = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
void * get_address_()
|
|
||||||
{
|
|
||||||
if (my_callback_shared_) {
|
|
||||||
return get_address(my_callback_shared_);
|
|
||||||
} else if (my_callback_unique_) {
|
|
||||||
return get_address(my_callback_unique_);
|
|
||||||
} else {
|
|
||||||
return (void *)0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const char * get_symbol_()
|
|
||||||
{
|
|
||||||
return get_symbol(get_address_());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
SharedPtrCallback my_callback_shared_;
|
|
||||||
UniquePtrCallback my_callback_unique_;
|
|
||||||
};
|
|
Loading…
Add table
Add a link
Reference in a new issue