From 7125fdb13b995426d997f76351821433bac9ea03 Mon Sep 17 00:00:00 2001 From: Pierre Dejoue Date: Tue, 14 Jul 2020 18:16:18 +0200 Subject: [PATCH] Add CMake files for the unit tests - Building unit tests is optional, disabled by default to prevent the library clients from pulling the dependency on boost - Add the unit tests to the Github Actions. - Use boost::filesystem to manipulate paths for better portability --- .github/workflows/linux.yml | 8 ++++++-- CMakeLists.txt | 9 ++++++++- meson.build | 2 +- unittest/CMakeLists.txt | 32 ++++++++++++++++++++++++++++++++ unittest/TriangleTest.cpp | 3 +++ unittest/main.cpp | 14 +++++++++++--- 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 unittest/CMakeLists.txt diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index cc83baf..07cc7a9 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -13,12 +13,13 @@ jobs: image: fedora:32 steps: - uses: actions/checkout@v2 + - name: Install dependencies - run: dnf install -yq cmake ninja-build gcc-c++ clang-tools-extra python3-PyYAML + run: dnf install -yq cmake ninja-build gcc-c++ clang-tools-extra python3-PyYAML boost-devel - name: Build with GCC run: | - cmake -Bbuild -GNinja + cmake -Bbuild -GNinja -DP2T_BUILD_TESTS=ON cmake --build build - name: Build with Clang @@ -28,3 +29,6 @@ jobs: - name: Lint with clang-tidy run: python3 /usr/share/clang/run-clang-tidy.py -header-filter=poly2tri -p=build-clang + + - name: Unit tests + run: cd build && ctest --output-on-failure diff --git a/CMakeLists.txt b/CMakeLists.txt index 693f433..9d9a358 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,15 @@ -cmake_minimum_required(VERSION 3.6) +cmake_minimum_required(VERSION 3.12) project(poly2tri LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) +option(P2T_BUILD_TESTS "Build tests" OFF) + file(GLOB SOURCES poly2tri/common/*.cc poly2tri/sweep/*.cc) add_library(poly2tri ${SOURCES}) target_include_directories(poly2tri INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) + +if(P2T_BUILD_TESTS) + enable_testing() + add_subdirectory(unittest) +endif() diff --git a/meson.build b/meson.build index 44c118d..e84d846 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ lib = static_library('poly2tri', sources : [ ]) thread_dep = dependency('threads') -boost_test_dep = dependency('boost', modules : [ 'unit_test_framework' ], required : false) +boost_test_dep = dependency('boost', modules : [ 'filesystem', 'unit_test_framework' ], required : false) if boost_test_dep.found() test('Unit Test', executable('unittest', [ 'unittest/main.cpp', diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt new file mode 100644 index 0000000..993eedb --- /dev/null +++ b/unittest/CMakeLists.txt @@ -0,0 +1,32 @@ +# Dependencies +if (WIN32) + set(Boost_USE_STATIC_LIBS ON) +endif() +find_package(Boost 1.69 REQUIRED COMPONENTS + filesystem + unit_test_framework +) + +# Build Unit Tests +add_executable(test_poly2tri + main.cpp + TriangleTest.cpp +) + +target_include_directories(test_poly2tri + PRIVATE + ${Boost_INCLUDE_DIRS} +) + +target_compile_definitions(test_poly2tri + PRIVATE + P2T_BASE_DIR="${PROJECT_SOURCE_DIR}" +) + +target_link_libraries(test_poly2tri + PRIVATE + poly2tri + ${Boost_LIBRARIES} +) + +add_test(NAME poly2tri COMMAND test_poly2tri) diff --git a/unittest/TriangleTest.cpp b/unittest/TriangleTest.cpp index bdeeae7..91e4bda 100644 --- a/unittest/TriangleTest.cpp +++ b/unittest/TriangleTest.cpp @@ -1,3 +1,6 @@ +#ifndef WIN32 +#define BOOST_TEST_DYN_LINK +#endif #include #include diff --git a/unittest/main.cpp b/unittest/main.cpp index 671b216..9246533 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -1,5 +1,9 @@ +#ifndef WIN32 #define BOOST_TEST_DYN_LINK +#endif #define BOOST_TEST_MODULE Poly2triTest + +#include #include #include #include @@ -64,9 +68,13 @@ BOOST_AUTO_TEST_CASE(TestbedFilesTest) // Load pointset from file // Parse and tokenize data file std::string line; - const std::string src(__FILE__); // ../unittest/main.cpp - auto folder = src.substr(0, src.find_last_of('/')) + "/../testbed/data/"; - std::ifstream myfile(folder + filename); +#ifndef P2T_BASE_DIR + const auto basedir = boost::filesystem::path(__FILE__).remove_filename().parent_path(); +#else + const auto basedir = boost::filesystem::path(P2T_BASE_DIR); +#endif + const auto datafile = basedir / boost::filesystem::path("testbed/data") / boost::filesystem::path(filename); + std::ifstream myfile(datafile.string()); BOOST_REQUIRE(myfile.is_open()); while (!myfile.eof()) { getline(myfile, line);