Cleanup and reorg of lib/common (#34)

* Cleanup and reorg of lib/common

- remove stale code in helper.cpp
- move helper.hpp to demangle.hpp
- move helper.cpp to demangle.cpp
- update CMakeLists.txt with new filenames
- fix includes in config.cpp

* Remove log.hpp and join.hpp

- replace with glog and fmt

* Update lib/common/environment

- move implementation functions into cpp file

* Common library tests

- tests for demangling
- tests for mpl (template metaprogramming)
- tests for environment

[ROCm/rocprofiler-sdk commit: ba0eb11e96]
This commit is contained in:
Jonathan R. Madsen
2023-08-30 22:31:36 -05:00
committed by GitHub
parent 697c751c62
commit 144d7018e7
13 changed files with 474 additions and 694 deletions
@@ -0,0 +1,23 @@
#
# Tests for the common library
#
project(rocprofiler-tests-common LANGUAGES C CXX)
include(GoogleTest)
set(common_sources demangling.cpp environment.cpp mpl.cpp)
add_executable(common-tests)
target_sources(common-tests PRIVATE ${common_sources})
target_link_libraries(
common-tests
PRIVATE rocprofiler::rocprofiler-headers rocprofiler::rocprofiler-common-library
GTest::gtest GTest::gtest_main)
gtest_add_tests(
TARGET common-tests
SOURCES ${common_sources}
TEST_LIST common-tests_TESTS
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(${common-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests")
@@ -0,0 +1,95 @@
// MIT License
//
// Copyright (c) 2023 ROCm Developer Tools
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/common/demangle.hpp"
#include <gtest/gtest.h>
#include <string_view>
#include <utility>
TEST(common, demangling)
{
// the purpose of this test is to verify the cxx_demangle function.
// We want to make sure that the function produces the right demangling
// when it is a correctly mangled string and does not change the string
// when demangling fails
using strview_pair_t = std::pair<std::string_view, std::string_view>;
for(auto [mangled, demangled] :
{strview_pair_t{"_ZN11rocprofiler8internal18correlation_config20get_unique_record_idEv",
"rocprofiler::internal::correlation_config::get_unique_record_id()"},
strview_pair_t{"_ZN11rocprofiler8internal18get_active_configsEv",
"rocprofiler::internal::get_active_configs()"},
strview_pair_t{"_ZN11rocprofiler8internal22get_registered_configsEv",
"rocprofiler::internal::get_registered_configs()"},
strview_pair_t{
"_ZZN11rocprofiler8internal18correlation_config20get_unique_record_idEvE2_v",
"rocprofiler::internal::correlation_config::get_unique_record_id()::_v"},
strview_pair_t{"_ZZN11rocprofiler8internal18get_active_configsEvE2_v",
"rocprofiler::internal::get_active_configs()::_v"},
strview_pair_t{"_ZZN11rocprofiler8internal22get_registered_configsEvE2_v",
"rocprofiler::internal::get_registered_configs()::_v"}})
{
// verify the demangling works
EXPECT_EQ(rocprofiler::common::cxx_demangle(mangled), demangled)
<< "failed to demangle '" << mangled << "'";
// verify we get same string in when improperly mangled string
auto bad_mangled = std::string{"_Z"} + std::string{mangled};
EXPECT_EQ(rocprofiler::common::cxx_demangle(bad_mangled), bad_mangled)
<< "demangling succeeded for '" << bad_mangled << "'";
}
}
TEST(common, truncate)
{
// this test is verify that the truncate_name function correctly finds the function
// name in a complex template instantiation
auto mangled = std::string_view{"_ZSt16__do_uninit_copyIPKSt4pairINSt7__cxx1112basic_"
"stringIcSt11char_traitsIcESaIcEEES6_EPS7_ET0_T_SC_SB_"};
auto untruncated = std::string_view{
"std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> "
">, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >* "
"std::__do_uninit_copy<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> > > const*, std::pair<std::__cxx11::basic_string<char, "
"std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, "
"std::char_traits<char>, std::allocator<char> > "
">*>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> > > const*, std::pair<std::__cxx11::basic_string<char, "
"std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, "
"std::char_traits<char>, std::allocator<char> > > const*, "
"std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> "
">, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)"};
EXPECT_EQ(rocprofiler::common::cxx_demangle(mangled), untruncated);
EXPECT_EQ(rocprofiler::common::truncate_name(untruncated),
std::string_view{"__do_uninit_copy"});
EXPECT_EQ(rocprofiler::common::truncate_name(rocprofiler::common::cxx_demangle(mangled)),
std::string_view{"__do_uninit_copy"});
}
@@ -0,0 +1,115 @@
// MIT License
//
// Copyright (c) 2023 ROCm Developer Tools
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/common/environment.hpp"
#include <gtest/gtest.h>
TEST(common, environment)
{
using rocprofiler::common::env_config;
using rocprofiler::common::get_env;
enum TestBareEnum : unsigned short // NOLINT(performance-enum-size)
{
BZero = 0,
BOne = 1,
};
enum class TestClassEnum : unsigned short // NOLINT(performance-enum-size)
{
CZero = 0,
COne = 1,
};
//
// int testing section
//
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_INT", 0), 0);
setenv("ROCPROFILER_ENV_TEST_INT", "1", 1);
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_INT", 0), 1);
env_config{"ROCPROFILER_ENV_TEST_INT", "2"}();
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_INT", 0), 1);
env_config{"ROCPROFILER_ENV_TEST_INT", "2", 1}();
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_INT", 0), 2);
//
// enum testing section
//
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_BARE_ENUM", BZero), BZero);
env_config{"ROCPROFILER_ENV_TEST_BARE_ENUM", "1", 1}();
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_BARE_ENUM", BZero), BOne);
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_CLASS_ENUM", TestClassEnum::CZero),
TestClassEnum::CZero);
env_config{"ROCPROFILER_ENV_TEST_CLASS_ENUM", "1", 1}();
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_CLASS_ENUM", TestClassEnum::CZero),
TestClassEnum::COne);
//
// string testing section
//
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_STR", "nostr"), std::string_view{"nostr"});
env_config{"ROCPROFILER_ENV_TEST_STR", "hasstr", 0}();
EXPECT_EQ(get_env("ROCPROFILER_ENV_TEST_STR", "nostr"), std::string_view{"hasstr"});
//
// bool testing section
//
EXPECT_FALSE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "YES", 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "yes", 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "y", 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "true", 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "on", 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "no", 1}();
EXPECT_FALSE(get_env("ROCPROFILER_ENV_TEST_BOOL", true));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "false", 1}();
EXPECT_FALSE(get_env("ROCPROFILER_ENV_TEST_BOOL", true));
env_config{"ROCPROFILER_ENV_TEST_BOOL", "0", 1}();
EXPECT_FALSE(get_env("ROCPROFILER_ENV_TEST_BOOL", true));
for(auto n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
{
env_config{"ROCPROFILER_ENV_TEST_BOOL", std::to_string(n), 1}();
EXPECT_TRUE(get_env("ROCPROFILER_ENV_TEST_BOOL", false));
}
}
@@ -0,0 +1,70 @@
// MIT License
//
// Copyright (c) 2023 ROCm Developer Tools
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/common/mpl.hpp"
#include <gtest/gtest.h>
#include <tuple>
TEST(common, mpl)
{
namespace mpl = ::rocprofiler::common::mpl;
struct Foo;
using test_type_list_t = mpl::type_list<int, long, int, Foo, Foo*>;
using test_tuple_t = std::tuple<int, long, int, Foo, Foo*>;
EXPECT_EQ(mpl::size_of<test_type_list_t>::value, 5);
EXPECT_EQ(mpl::size_of<test_tuple_t>::value, 5);
{
constexpr bool _foo_p_is_one_of = mpl::is_one_of<Foo*, test_type_list_t>::value;
constexpr bool _int_is_one_of = mpl::is_one_of<int, test_type_list_t>::value;
constexpr bool _double_is_one_of = mpl::is_one_of<double, test_type_list_t>::value;
EXPECT_TRUE(_foo_p_is_one_of);
EXPECT_TRUE(_int_is_one_of);
EXPECT_FALSE(_double_is_one_of);
constexpr auto _foo_p_index_of = mpl::index_of<Foo*, test_type_list_t>::value;
constexpr auto _int_index_of = mpl::index_of<int, test_type_list_t>::value;
EXPECT_EQ(_foo_p_index_of, 4);
EXPECT_EQ(_int_index_of, 0);
}
{
constexpr bool _foo_p_is_one_of = mpl::is_one_of<Foo*, test_tuple_t>::value;
constexpr bool _int_is_one_of = mpl::is_one_of<int, test_tuple_t>::value;
constexpr bool _double_is_one_of = mpl::is_one_of<double, test_tuple_t>::value;
EXPECT_TRUE(_foo_p_is_one_of);
EXPECT_TRUE(_int_is_one_of);
EXPECT_FALSE(_double_is_one_of);
constexpr auto _foo_p_index_of = mpl::index_of<Foo*, test_tuple_t>::value;
constexpr auto _int_index_of = mpl::index_of<int, test_tuple_t>::value;
EXPECT_EQ(_foo_p_index_of, 4);
EXPECT_EQ(_int_index_of, 0);
}
}