From 77422b2165b349f4c0a1de8ece59786bdd5dcab0 Mon Sep 17 00:00:00 2001 From: Hyunsu Cho Date: Wed, 11 Nov 2020 01:43:15 -0800 Subject: [PATCH] Set up gtest --- .gitignore | 1 + CMakeLists.txt | 24 ++++++++++++++++++++++-- tests/cpp_test/test_main.cpp | 11 +++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/cpp_test/test_main.cpp diff --git a/.gitignore b/.gitignore index 9cbb2e872f03..f65739c4ea7a 100644 --- a/.gitignore +++ b/.gitignore @@ -269,6 +269,7 @@ _Pvt_Extensions *.app /windows/LightGBM.VC.db lightgbm +/testlightgbm # Created by https://www.gitignore.io/api/python diff --git a/CMakeLists.txt b/CMakeLists.txt index f57c55b5b61e..2a53760c1fd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,10 +7,11 @@ OPTION(USE_TIMETAG "Set to ON to output time costs" OFF) OPTION(USE_CUDA "Enable CUDA-accelerated training (EXPERIMENTAL)" OFF) OPTION(USE_DEBUG "Set to ON for Debug mode" OFF) OPTION(USE_SANITIZER "Use santizer flags" OFF) -option(SANITIZER_PATH "Path to sanitizer libs") -set(ENABLED_SANITIZERS "address" "leak" "undefined" CACHE STRING +OPTION(SANITIZER_PATH "Path to sanitizer libs") +SET(ENABLED_SANITIZERS "address" "leak" "undefined" CACHE STRING "Semicolon separated list of sanitizer names. E.g 'address;leak'. Supported sanitizers are address, leak, undefined and thread.") +OPTION(BUILD_CPP_TEST "Build C++ tests with Google Test" OFF) OPTION(BUILD_STATIC_LIB "Build static library" OFF) OPTION(__BUILD_FOR_R "Set to ON if building lib_lightgbm for use with the R package" OFF) OPTION(__INTEGRATE_OPENCL "Set to ON if building LightGBM with the OpenCL ICD Loader and its dependencies included" OFF) @@ -464,6 +465,25 @@ if(__BUILD_FOR_R) endif(MSVC) endif(__BUILD_FOR_R) +#-- Google C++ tests +if(BUILD_CPP_TEST) + find_package(GTest CONFIG) + if(NOT GTEST_FOUND) + message(STATUS "Did not find Google Test in the system root. Fetching Google Test now...") + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.10.0 + ) + FetchContent_MakeAvailable(googletest) + add_library(GTest::GTest ALIAS gtest) + endif() + file(GLOB CPP_TEST_SOURCES tests/cpp_test/*.cpp) + add_executable(testlightgbm ${CPP_TEST_SOURCES} ${SOURCES}) + target_link_libraries(testlightgbm PRIVATE GTest::GTest) +endif() + install(TARGETS lightgbm _lightgbm RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib diff --git a/tests/cpp_test/test_main.cpp b/tests/cpp_test/test_main.cpp new file mode 100644 index 000000000000..b881e09283c4 --- /dev/null +++ b/tests/cpp_test/test_main.cpp @@ -0,0 +1,11 @@ +/*! + * Copyright (c) 2020 Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE file in the project root for license information. + */ +#include + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + testing::FLAGS_gtest_death_test_style = "threadsafe"; + return RUN_ALL_TESTS(); +}