From 7c2b272861f207fff9cb5dbdc80e3c47cd8bf095 Mon Sep 17 00:00:00 2001 From: "Colin B." Date: Wed, 10 Jul 2024 11:04:18 -0700 Subject: [PATCH] Add basic googletest * Basic test case, load toy.cpp and check line 17 is found --- CMakeLists.txt | 1 + libcoz/CMakeLists.txt | 6 ++++-- libcoz/inspect.h | 8 ++++---- test/CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ test/load_basic_test.cpp | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/load_basic_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cd5ec8..5e931c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ if(INSTALL_COZ) endif() add_subdirectory(libcoz) +add_subdirectory(test) option(BUILD_BENCHMARKS "Build benchmarks" OFF) if(BUILD_BENCHMARKS) diff --git a/libcoz/CMakeLists.txt b/libcoz/CMakeLists.txt index a3a9ea9..b671f4d 100644 --- a/libcoz/CMakeLists.txt +++ b/libcoz/CMakeLists.txt @@ -2,7 +2,6 @@ set(sources ${PROJECT_SOURCE_DIR}/include/coz.h inspect.cpp inspect.h - libcoz.cpp perf.cpp perf.h profiler.cpp @@ -14,7 +13,8 @@ set(sources util.h ) -add_library(coz MODULE ${sources} ${public_headers}) +add_library(coz MODULE libcoz.cpp ${sources} ${public_headers}) +add_library(coz_nopre SHARED ${sources} ${public_headers}) if(CONAN_PACKAGE_VERSION) set_target_properties(coz PROPERTIES VERSION ${CONAN_PACKAGE_VERSION}) endif() @@ -23,6 +23,7 @@ target_include_directories(coz $ $) target_link_libraries(coz PRIVATE ${CMAKE_DL_LIBS} rt Threads::Threads libelfin::libelfin) +target_link_libraries(coz_nopre PRIVATE ${CMAKE_DL_LIBS} rt Threads::Threads libelfin::libelfin) add_library(coz-instrumentation INTERFACE) target_include_directories(coz-instrumentation @@ -39,3 +40,4 @@ if(INSTALL_COZ) RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) endif() + diff --git a/libcoz/inspect.h b/libcoz/inspect.h index 31d1c12..c52614f 100644 --- a/libcoz/inspect.h +++ b/libcoz/inspect.h @@ -145,6 +145,10 @@ class memory_map { static memory_map& get_instance(); + /// Find a debug version of provided file and add all of its in-scope lines to the map + bool process_file(const std::string& name, uintptr_t load_address, + const std::unordered_set& source_scope); + private: memory_map() : _files(std::map>()), _ranges(std::map>()) {} @@ -164,10 +168,6 @@ class memory_map { void add_range(std::string filename, size_t line_no, interval range); - /// Find a debug version of provided file and add all of its in-scope lines to the map - bool process_file(const std::string& name, uintptr_t load_address, - const std::unordered_set& source_scope); - /// Add entries for all inlined calls void process_inlines(const dwarf::die& d, const dwarf::line_table& table, diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..c58038f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.13.0) +project(coz VERSION 0.2.2 LANGUAGES C CXX) + +enable_testing() + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CONAN_CMAKE_SILENT_OUTPUT ON) + +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG 58d77fa8070e8cec2dc1ed015d66b454c8d78850 # release-1.12.1 +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +add_executable( + load_basic_test + load_basic_test.cpp +) +target_link_libraries( + load_basic_test + PRIVATE + GTest::gtest_main + #libcoz.so + coz_nopre +) + +include(GoogleTest) +gtest_discover_tests(load_basic_test) diff --git a/test/load_basic_test.cpp b/test/load_basic_test.cpp new file mode 100644 index 0000000..2e02898 --- /dev/null +++ b/test/load_basic_test.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +#include + +#include "../libcoz/inspect.h" + +using namespace std; + +TEST(BasicTestCases, TestMapForPrebuiltToy) { + auto toy_file = "../benchmarks/toy/toy"; + unordered_set source_scope{"%/toy.cpp"}; + auto& mm = memory_map::get_instance(); + mm.process_file(toy_file, 0, source_scope); + auto x = mm.find_line("toy.cpp:17"); + EXPECT_EQ(x->get_line(), 17); +}