ros2_tracing/tracetools_test/test/test_utils.cpp

106 lines
2.8 KiB
C++
Raw Normal View History

2019-07-02 14:35:24 +02:00
// Copyright 2019 Robert Bosch GmbH
//
// 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 <functional>
#include <memory>
2019-07-03 10:37:08 +02:00
#include <string>
2019-07-02 14:35:24 +02:00
#include "tracetools/utils.hpp"
2019-07-03 15:21:45 +02:00
void function_shared(const std::shared_ptr<int> p)
{
(void)p;
}
2019-07-03 10:25:15 +02:00
class SomeClassWithCallback
2019-07-02 14:35:24 +02:00
{
2019-07-03 10:25:15 +02:00
public:
SomeClassWithCallback() {}
2019-07-02 14:35:24 +02:00
2019-07-03 10:25:15 +02:00
void my_callback(int some_number, std::string some_string)
{
(void)some_number;
(void)some_string;
}
};
2019-07-02 14:35:24 +02:00
/*
2019-07-03 15:21:45 +02:00
Testing symbol resolution for std::function object created from a function pointer.
2019-07-02 14:35:24 +02:00
*/
2019-07-03 15:21:45 +02:00
TEST(TestUtils, valid_symbol_funcptr) {
2019-07-03 10:25:15 +02:00
std::function<void(std::shared_ptr<int>)> f = &function_shared;
2019-07-03 15:21:45 +02:00
EXPECT_STREQ(get_symbol(f), "function_shared(std::shared_ptr<int>)") <<
"invalid symbol";
}
2019-07-02 14:35:24 +02:00
/*
Testing _get_symbol_funcptr from a null pointer.
*/
TEST(TestUtils, invalid_get_symbol_funcptr) {
EXPECT_STREQ(_get_symbol_funcptr(nullptr), SYMBOL_UNKNOWN);
}
2019-07-03 15:21:45 +02:00
/*
Testing symbol resolution for std::function object created from a lambda.
*/
TEST(TestUtils, valid_symbol_lambda) {
2019-07-03 10:25:15 +02:00
std::function<int(int)> l = [](int num) {return num + 1;};
2019-07-03 15:21:45 +02:00
EXPECT_STREQ(
get_symbol(l),
"TestUtils_valid_symbol_lambda_Test::TestBody()::{lambda(int)#1}") <<
"invalid symbol";
}
2019-07-02 14:35:24 +02:00
/*
Testing symbol resolution lambdas with capture.
*/
TEST(TestUtils, valid_symbol_lambda_capture) {
int num = 1;
auto l = [ = ]() {return num + 1;};
EXPECT_STREQ(
get_symbol(l),
"TestUtils_valid_symbol_lambda_capture_Test::TestBody()::{lambda()#1}") <<
"invalid symbol";
auto m = [&](int other_num) {return num + other_num;};
EXPECT_STREQ(
get_symbol(m),
"TestUtils_valid_symbol_lambda_capture_Test::TestBody()::{lambda(int)#2}") <<
"invalid symbol";
}
2019-07-03 15:21:45 +02:00
/*
Testing symbol resolution for std::function object created from std::bind.
*/
TEST(TestUtils, valid_symbol_bind) {
2019-07-03 10:25:15 +02:00
SomeClassWithCallback scwc;
std::function<void(int, std::string)> fscwc = std::bind(
&SomeClassWithCallback::my_callback,
&scwc,
std::placeholders::_1,
std::placeholders::_2
);
2019-07-03 15:21:45 +02:00
EXPECT_STREQ(
get_symbol(
fscwc),
2019-07-03 15:43:58 +02:00
"std::_Bind<void (SomeClassWithCallback::*(SomeClassWithCallback*, "
"std::_Placeholder<1>, std::_Placeholder<2>))(int, std::__cxx11::basic_string"
"<char, std::char_traits<char>, std::allocator<char> >)>")
2019-07-03 15:21:45 +02:00
<<
"invalid symbol";
2019-07-02 14:35:24 +02:00
}