diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 76886c1c0..a23e3ad46 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,32 +27,6 @@ find_package(GTest REQUIRED) include(GoogleTest) -# ---- Declare Tests ---- - -add_executable( - patomic_test - example_add.cpp -) -add_executable(patomic::test ALIAS patomic_test) - -target_link_libraries( - patomic_test - PRIVATE - patomic::patomic - GTest::gtest_main -) - -target_compile_features(patomic_test PRIVATE cxx_std_14) - - -# ---- Discover Tests ---- - -gtest_add_tests( - TARGET patomic_test - TEST_LIST patomic_test_TESTS -) - - # ---- Windows Path Issues ---- include(cmake/WindowsDependenciesPath.cmake) @@ -63,19 +37,11 @@ windows_deps_path( ) # check we're on Windows, and we have SHARED_LIBRARY dependencies -if(NOT deps_path STREQUAL "" AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") - - # set environment variable for each test so that ctest works automatically - if(patomic_test_SET_CTEST_PATH_ENV_WINDOWS) - foreach(test IN LISTS patomic_test_TESTS) - set_property( - TEST "${test}" - PROPERTY ENVIRONMENT "PATH=${deps_path}" - ) - endforeach() - endif() - - # create file with PATH contents for when not running tests through CMake +set(windows_and_shared FALSE) +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" AND NOT deps_path STREQUAL "") + set(windows_and_shared TRUE) + + # create file with PATH contents for when running tests independent of CTest file(GENERATE OUTPUT "${PROJECT_BINARY_DIR}/windows_dependencies_path.txt" CONTENT "${deps_path}" @@ -83,7 +49,22 @@ if(NOT deps_path STREQUAL "" AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") endif() -# ---- Install Rules ---- +# ---- Declare Tests ---- + +add_custom_target(patomic_test) +set_target_properties( + patomic_test + PROPERTIES + WIN_DEPS_PATH ${deps_path} + WIN_AND_SHARED ${windows_and_shared} +) + +# add all test subdirectories +add_subdirectory(bt) +add_subdirectory(ut) + + +# ---- Further Installation and Packaging ---- if(NOT CMAKE_SKIP_INSTALL_RULES) include(cmake/InstallRules.cmake) diff --git a/test/bt/CMakeLists.txt b/test/bt/CMakeLists.txt new file mode 100644 index 000000000..5e14f0f5b --- /dev/null +++ b/test/bt/CMakeLists.txt @@ -0,0 +1,4 @@ +include(../cmake/CreateTest.cmake) + +create_test(BT example_add SOURCE example_add.cpp) +create_test(BT example_sub SOURCE example_sub.cpp) diff --git a/test/bt/example_add.cpp b/test/bt/example_add.cpp new file mode 100644 index 000000000..da5584143 --- /dev/null +++ b/test/bt/example_add.cpp @@ -0,0 +1,8 @@ +#include +#include + + +TEST(suite, name) +{ + ASSERT_EQ(5, patomic_example_add(2, 3)); +} diff --git a/test/bt/example_sub.cpp b/test/bt/example_sub.cpp new file mode 100644 index 000000000..01a8e6f46 --- /dev/null +++ b/test/bt/example_sub.cpp @@ -0,0 +1,8 @@ +#include +#include + + +TEST(suite2, name2) +{ + ASSERT_EQ(5, patomic_example_add(2, 3)); +} diff --git a/test/cmake/CreateTest.cmake b/test/cmake/CreateTest.cmake new file mode 100644 index 000000000..3e6ea1c6e --- /dev/null +++ b/test/cmake/CreateTest.cmake @@ -0,0 +1,115 @@ +# ---- Create Test ---- + +# Creates a test executable and registers it with CTest. +# +# create_test( +# BT|UT +# [INCLUDE ...] +# [SOURCE ...] +# ) +function(create_test) + cmake_parse_arguments( + "ARG" + "" + "BT;UT" + "INCLUDE;SOURCE" + ${ARGN} + ) + + + # validate arguments + + set(args_valid TRUE) + set(func_name "create_test") + + if(NOT ARG_BT AND NOT ARG_UT) + message(WARNING " option needs to be specified when invoking '${func_name}'") + set(args_valid FALSE) + elseif(ARG_BT AND ARG_UT) + message(WARNING "Only one of options may be specified when invoking '${func_name}'") + set(args_valid FALSE) + endif() + + if(DEFINED ARG_UNPARSED_ARGUMENTS) + message(WARNING "The following arguments were not recognised when invoking '${func_name}': ${ARG_UNPARSED_ARGUMENTS}") + set(args_valid FALSE) + endif() + + if(NOT ${args_valid}) + message(FATAL_ERROR "Aborting '${func_name}' due to invalid arguments") + endif() + + + # coalesce arguments + + set(name "${ARG_BT}") + set(kind "bt") + if(ARG_UT) + set(name "${ARG_UT}") + set(kind "ut") + endif() + + + # setup target + + set(target patomic_${kind}_${name}) + + add_executable( + ${target} + ${ARG_INCLUDE} + ${ARG_SOURCE} + ) + + target_link_libraries( + ${target} + PRIVATE + patomic::patomic + GTest::gtest_main + ) + + target_compile_features(${target} PRIVATE cxx_std_14) + + + # setup tests with CTest + + # must be run in same directory scope as target + gtest_add_tests( + TARGET ${target} + TEST_LIST added_tests + ) + + # get these values from root target so we don't recompute every time + get_target_property(deps_path patomic_test WIN_DEPS_PATH) + get_target_property(win_and_shared patomic_test WIN_AND_SHARED) + + # set environment variable for each test so that CTest works automatically + if(win_and_shared AND patomic_test_SET_CTEST_PATH_ENV_WINDOWS) + foreach(test IN LISTS added_tests) + set_property( + TEST "${test}" + PROPERTY ENVIRONMENT "PATH=${deps_path}" + ) + endforeach() + endif() + + + # setup install of target + + set(component patomic_${kind}) + + if(NOT CMAKE_SKIP_INSTALL_RULES) + install( + TARGETS ${target} + RUNTIME # + COMPONENT ${component} + DESTINATION "${CMAKE_INSTALL_TESTDIR}/patomic/${kind}" + EXCLUDE_FROM_ALL + ) + endif() + + + # attach to root target + + add_dependencies(patomic_test ${target}) + +endfunction() diff --git a/test/cmake/InstallRules.cmake b/test/cmake/InstallRules.cmake index 11b8988c8..c2f5f1f82 100644 --- a/test/cmake/InstallRules.cmake +++ b/test/cmake/InstallRules.cmake @@ -1,24 +1,8 @@ -# we don't want the default install location to be in bin -# note: not currently an official CMake variable -set( - CMAKE_INSTALL_TESTDIR "share/test" - CACHE PATH "(unofficial) Default test install location" -) - -# copy target build output artifacts to OS dependent locations -install( - TARGETS patomic_test - RUNTIME # - COMPONENT patomic_test_Runtime - DESTINATION "${CMAKE_INSTALL_TESTDIR}" - EXCLUDE_FROM_ALL -) - # copy file with windows dependencies path if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") install( - FILES "${PROJECT_BINARY_DIR}/windows_dependencies_path.txt" - COMPONENT patomic_test_Runtime + FILES "${PROJECT_BINARY_DIR}/patomic/windows_dependencies_path.txt" + COMPONENT patomic_test DESTINATION "${CMAKE_INSTALL_TESTDIR}" EXCLUDE_FROM_ALL ) diff --git a/test/cmake/OptionVariables.cmake b/test/cmake/OptionVariables.cmake index 690f3ad12..edd9bfa0b 100644 --- a/test/cmake/OptionVariables.cmake +++ b/test/cmake/OptionVariables.cmake @@ -1,3 +1,18 @@ +# ---- Test Install Directory ---- + +# Normally tests would be install in CMAKE_INSTALL_BINDIR by default since +# they're executables. +# This is undesirable, so this variable exists to override the install location +# of test binaries separately. +# It's not prefixed with patomic_test because it's ok for it to be shared and +# overridden by parent projects. +# Note: this is not an official CMake variable +set( + CMAKE_INSTALL_TESTDIR "share/test" + CACHE PATH "(unofficial) Default test install location" +) + + # ---- Windows Tests Path ---- # By default we set PATH for tests run with CTest on Windows in order to prevent diff --git a/test/ut/CMakeLists.txt b/test/ut/CMakeLists.txt new file mode 100644 index 000000000..3720c5cbd --- /dev/null +++ b/test/ut/CMakeLists.txt @@ -0,0 +1,4 @@ +include(../cmake/CreateTest.cmake) + +create_test(UT example_add SOURCE example_add.cpp) +create_test(UT example_sub SOURCE example_sub.cpp) diff --git a/test/ut/example_add.cpp b/test/ut/example_add.cpp new file mode 100644 index 000000000..da5584143 --- /dev/null +++ b/test/ut/example_add.cpp @@ -0,0 +1,8 @@ +#include +#include + + +TEST(suite, name) +{ + ASSERT_EQ(5, patomic_example_add(2, 3)); +} diff --git a/test/ut/example_sub.cpp b/test/ut/example_sub.cpp new file mode 100644 index 000000000..01a8e6f46 --- /dev/null +++ b/test/ut/example_sub.cpp @@ -0,0 +1,8 @@ +#include +#include + + +TEST(suite2, name2) +{ + ASSERT_EQ(5, patomic_example_add(2, 3)); +}