diff --git a/.ci/test.sh b/.ci/test.sh index 35c7da8410a4..576e49424a4a 100755 --- a/.ci/test.sh +++ b/.ci/test.sh @@ -13,6 +13,14 @@ if [[ "${TASK}" == "r-package" ]]; then exit 0 fi +if [[ "$TASK" == "cpp-tests" ]]; then + mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build + cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF .. + make testlightgbm -j4 || exit -1 + ./../testlightgbm || exit -1 + exit 0 +fi + conda create -q -y -n $CONDA_ENV python=$PYTHON_VERSION source activate $CONDA_ENV @@ -71,9 +79,9 @@ fi if [[ $TASK == "if-else" ]]; then conda install -q -y -n $CONDA_ENV numpy mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build && cmake .. && make lightgbm -j4 || exit -1 - cd $BUILD_DIRECTORY/tests/cpp_test && ../../lightgbm config=train.conf convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp && ../../lightgbm config=predict.conf output_result=origin.pred || exit -1 + cd $BUILD_DIRECTORY/tests/cpp_tests && ../../lightgbm config=train.conf convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp && ../../lightgbm config=predict.conf output_result=origin.pred || exit -1 cd $BUILD_DIRECTORY/build && make lightgbm -j4 || exit -1 - cd $BUILD_DIRECTORY/tests/cpp_test && ../../lightgbm config=predict.conf output_result=ifelse.pred && python test.py || exit -1 + cd $BUILD_DIRECTORY/tests/cpp_tests && ../../lightgbm config=predict.conf output_result=ifelse.pred && python test.py || exit -1 exit 0 fi diff --git a/.ci/test_windows.ps1 b/.ci/test_windows.ps1 index 0def6ad6e36b..5a5c97a34e48 100644 --- a/.ci/test_windows.ps1 +++ b/.ci/test_windows.ps1 @@ -16,6 +16,14 @@ if ($env:TASK -eq "r-package") { Exit 0 } +if ($env:TASK -eq "cpp-tests") { + mkdir $env:BUILD_SOURCESDIRECTORY/build; cd $env:BUILD_SOURCESDIRECTORY/build + cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF -A x64 .. + cmake --build . --target testlightgbm --config Debug ; Check-Output $? + Start-Process -FilePath "./../Debug/testlightgbm.exe" -NoNewWindow -Wait ; Check-Output $? + Exit 0 +} + # setup for Python conda init powershell conda activate diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 1c52e40a1a5c..58398673c215 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -20,7 +20,7 @@ include/ @guolinke @chivee @btrotta @shiyu1994 src/ @guolinke @chivee @btrotta @shiyu1994 CMakeLists.txt @guolinke @chivee @Laurae2 @jameslamb @wxchan @henry0312 @StrikerRUS @huanzhang12 @btrotta @shiyu1994 tests/c_api_test/ @guolinke @chivee @btrotta @shiyu1994 -tests/cpp_test/ @guolinke @chivee @btrotta @shiyu1994 +tests/cpp_tests/ @guolinke @chivee @btrotta @shiyu1994 tests/data/ @guolinke @chivee @btrotta @shiyu1994 windows/ @guolinke @chivee @btrotta @StrikerRUS @shiyu1994 diff --git a/.vsts-ci.yml b/.vsts-ci.yml index 9991de15e9c7..30702b31467b 100644 --- a/.vsts-ci.yml +++ b/.vsts-ci.yml @@ -51,6 +51,8 @@ jobs: METHOD: source swig: TASK: swig + cpp_tests: + TASK: cpp-tests steps: - script: | echo "##vso[task.setvariable variable=BUILD_DIRECTORY]$BUILD_SOURCESDIRECTORY" @@ -222,6 +224,8 @@ jobs: TASK: bdist swig: TASK: swig + cpp_tests: + TASK: cpp-tests steps: - script: | echo "##vso[task.setvariable variable=BUILD_DIRECTORY]$BUILD_SOURCESDIRECTORY" @@ -258,6 +262,8 @@ jobs: TASK: bdist swig: TASK: swig + cpp_tests: + TASK: cpp-tests steps: - powershell: | Write-Host "##vso[task.prependpath]$env:CONDA\Scripts" diff --git a/CMakeLists.txt b/CMakeLists.txt index b845f36244d7..41fb21f5e54c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -478,7 +478,21 @@ if(BUILD_CPP_TEST) FetchContent_MakeAvailable(googletest) add_library(GTest::GTest ALIAS gtest) endif() - file(GLOB CPP_TEST_SOURCES tests/cpp_test/*.cpp) + file(GLOB CPP_TEST_SOURCES tests/cpp_tests/*.cpp) + if(MSVC) + set( + CompilerFlags + CMAKE_CXX_FLAGS + CMAKE_CXX_FLAGS_DEBUG + CMAKE_CXX_FLAGS_RELEASE + CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG + CMAKE_C_FLAGS_RELEASE + ) + foreach(CompilerFlag ${CompilerFlags}) + string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}") + endforeach() + endif(MSVC) add_executable(testlightgbm ${CPP_TEST_SOURCES} ${SOURCES}) target_link_libraries(testlightgbm PRIVATE GTest::GTest) endif() diff --git a/docs/Development-Guide.rst b/docs/Development-Guide.rst index 6a5521b82b4e..ec8e1a7003b7 100644 --- a/docs/Development-Guide.rst +++ b/docs/Development-Guide.rst @@ -66,7 +66,7 @@ Code Structure +---------------------+------------------------------------------------------------------------------------------------------------------------------------+ Documents API -~~~~~~~~~~~~~ +------------- Refer to `docs README <./README.rst>`__. @@ -75,6 +75,12 @@ C API Refer to `C API <./C-API.rst>`__ or the comments in `c\_api.h `__ file, from which the documentation is generated. +Tests +----- + +C++ unit tests are located in the ``./tests/cpp_tests`` folder and written with the help of Google Test framework. +To run tests locally first refer to the `Installation Guide <./Installation-Guide.rst#build-c-unit-tests>`__ for how to build tests and then simply run compiled executable file. + High Level Language Package --------------------------- diff --git a/docs/Installation-Guide.rst b/docs/Installation-Guide.rst index fe4a4a723161..2f2d902125f6 100644 --- a/docs/Installation-Guide.rst +++ b/docs/Installation-Guide.rst @@ -683,7 +683,6 @@ The ``.jar`` file will be in ``LightGBM/build`` folder and the ``.dll`` files wi It is recommended to use **VS Build Tools (Visual Studio)** since it has better multithreading efficiency in **Windows** for many-core systems (see `Question 4 <./FAQ.rst#i-am-using-windows-should-i-use-visual-studio-or-mingw-for-compiling-lightgbm>`__ and `Question 8 <./FAQ.rst#cpu-usage-is-low-like-10-in-windows-when-using-lightgbm-on-very-large-datasets-with-many-core-systems>`__). - Also, you may want to read `gcc Tips <./gcc-Tips.rst>`__. Linux @@ -769,6 +768,103 @@ gcc Also, you may want to read `gcc Tips <./gcc-Tips.rst>`__. +Build C++ Unit Tests +~~~~~~~~~~~~~~~~~~~~ + +Windows +^^^^^^^ + +On Windows a C++ unit tests of LightGBM can be built using **CMake** and **VS Build Tools**. + +1. Install `Git for Windows`_, `CMake`_ (3.8 or higher) and `VS Build Tools`_ (**VS Build Tools** is not needed if **Visual Studio** (2015 or newer) is already installed). + +2. Run the following commands: + + .. code:: + + git clone --recursive https://github.com/microsoft/LightGBM + cd LightGBM + mkdir build + cd build + cmake -A x64 -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF .. + cmake --build . --target testlightgbm --config Debug + +The ``.exe`` file will be in ``LightGBM/Debug`` folder. + +Linux +^^^^^ + +On Linux a C++ unit tests of LightGBM can be built using **CMake** and **gcc** or **Clang**. + +1. Install `CMake`_. + +2. Run the following commands: + + .. code:: + + git clone --recursive https://github.com/microsoft/LightGBM + cd LightGBM + mkdir build + cd build + cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF .. + make testlightgbm -j4 + +**Note**: glibc >= 2.14 is required. + +macOS +^^^^^ + +On macOS a C++ unit tests of LightGBM can be built using **CMake** and **Apple Clang** or **gcc**. + +Apple Clang +*********** + +Only **Apple Clang** version 8.1 or higher is supported. + +1. Install `CMake`_ (3.16 or higher): + + .. code:: + + brew install cmake + +2. Run the following commands: + + .. code:: + + git clone --recursive https://github.com/microsoft/LightGBM + cd LightGBM + mkdir build + cd build + cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF .. + make testlightgbm -j4 + +gcc +*** + +1. Install `CMake`_ (3.2 or higher): + + .. code:: + + brew install cmake + +2. Install **gcc**: + + .. code:: + + brew install gcc + +3. Run the following commands: + + .. code:: + + git clone --recursive https://github.com/microsoft/LightGBM + cd LightGBM + export CXX=g++-7 CC=gcc-7 # replace "7" with version of gcc installed on your machine + mkdir build + cd build + cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF .. + make testlightgbm -j4 + .. |download artifacts| image:: ./_static/images/artifacts-not-available.svg :target: https://lightgbm.readthedocs.io/en/latest/Installation-Guide.html diff --git a/tests/cpp_test/predict.conf b/tests/cpp_tests/predict.conf similarity index 100% rename from tests/cpp_test/predict.conf rename to tests/cpp_tests/predict.conf diff --git a/tests/cpp_test/test.py b/tests/cpp_tests/test.py similarity index 100% rename from tests/cpp_test/test.py rename to tests/cpp_tests/test.py diff --git a/tests/cpp_test/test_chunked_array.cpp b/tests/cpp_tests/test_chunked_array.cpp similarity index 100% rename from tests/cpp_test/test_chunked_array.cpp rename to tests/cpp_tests/test_chunked_array.cpp diff --git a/tests/cpp_test/test_main.cpp b/tests/cpp_tests/test_main.cpp similarity index 100% rename from tests/cpp_test/test_main.cpp rename to tests/cpp_tests/test_main.cpp diff --git a/tests/cpp_test/train.conf b/tests/cpp_tests/train.conf similarity index 100% rename from tests/cpp_test/train.conf rename to tests/cpp_tests/train.conf