Skip to content

Commit

Permalink
Make exception mode for tests independent of global configuration (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSeemaier authored Mar 10, 2022
1 parent 01a8487 commit 0146f9b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ find_package(MPI REQUIRED)

add_subdirectory(extern)

add_library(kamping INTERFACE)
target_include_directories(kamping INTERFACE include)
add_library(kamping_base INTERFACE)
target_include_directories(kamping_base INTERFACE include)

# set C++ standard to C++17
target_compile_features(kamping INTERFACE cxx_std_17)
target_link_libraries(kamping INTERFACE MPI::MPI_CXX)
target_compile_features(kamping_base INTERFACE cxx_std_17)
target_link_libraries(kamping_base INTERFACE MPI::MPI_CXX)

list(
APPEND KAMPING_WARNING_FLAGS
Expand Down Expand Up @@ -93,6 +93,10 @@ if(KAMPING_WARNINGS_ARE_ERRORS)
)
endif()

# Target for user-code
add_library(kamping INTERFACE)
target_link_libraries(kamping INTERFACE kamping_base)

# If enabled, use exceptions, otherwise use KASSERT()
option(KAMPING_EXCEPTION_MODE "Use exceptions to report recoverable errors." ON)
if (KAMPING_EXCEPTION_MODE)
Expand Down Expand Up @@ -122,6 +126,9 @@ set(KAMPING_ASSERTION_LEVEL $<IF:$<CONFIG:Debug>,3,1> CACHE STRING "Higher value
set_property(CACHE KAMPING_ASSERTION_LEVEL PROPERTY STRINGS 0 1 2 3 4 5 6)
target_compile_definitions(kamping INTERFACE -DKAMPING_ASSERTION_LEVEL=${KAMPING_ASSERTION_LEVEL})

# Constant for the highest assertion level used by KaMPI.ng
set(KAMPING_HIGHEST_ASSERTION_LEVEL 6)

add_library(kamping::kamping ALIAS kamping)

# Testing and examples are only built if this is the main project
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ kamping_register_compilation_failure_test(
test_mpi_function_wrapper_compilation_failure
FILES mpi_function_wrapper_helpers_compilation_failures_test.cpp
SECTIONS "RECV_BUFFER_NOT_EXTRACTABLE" "RECV_COUNTS_NOT_EXTRACTABLE" "RECV_DISPLACEMENTS_NOT_EXTRACTABLE" "SEND_DISPLACEMENTS_NOT_EXTRACTABLE"
LIBRARIES kamping
LIBRARIES kamping_base
)

kamping_register_compilation_failure_test(
test_named_parameter_selection_compilation_failure
FILES named_parameter_selection_compilation_failures_test.cpp
SECTIONS "REQUESTED_PARAMETER_NOT_GIVEN" "DEFAULT_ARGUMENTS_DO_NOT_MATCH"
LIBRARIES kamping
LIBRARIES kamping_base
)
36 changes: 24 additions & 12 deletions tests/cmake/KampingTestHelper.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
include(KaTestrophe)
include(GoogleTest)

function(kamping_set_kassert_flags KAMPING_TARGET_NAME)
cmake_parse_arguments(
"KAMPING"
"NO_EXCEPTION_MODE"
""
""
${ARGN}
)

# Always run tests with all assertions enabled
target_compile_definitions(${KAMPING_TARGET_NAME} PRIVATE -DKAMPING_ASSERTION_LEVEL=${KAMPING_HIGHEST_ASSERTION_LEVEL})

# Explicitly specify exception mode for tests, default to no exception mode
if (NOT KAMPING_NO_EXCEPTION_MODE)
target_compile_definitions(${KAMPING_TARGET_NAME} PRIVATE -DKAMPING_EXCEPTION_MODE)
endif ()
endfunction()

# Convenience wrapper for adding tests for KaMPI.ng
# this creates the target, links googletest and kamping, enables warnings and registers the test
#
Expand All @@ -17,12 +35,10 @@ function(kamping_register_test KAMPING_TARGET_NAME)
${ARGN}
)
add_executable(${KAMPING_TARGET_NAME} ${KAMPING_FILES})
target_link_libraries(${KAMPING_TARGET_NAME} PRIVATE gtest gtest_main gmock kamping)
target_link_libraries(${KAMPING_TARGET_NAME} PRIVATE gtest gtest_main gmock kamping_base)
target_compile_options(${KAMPING_TARGET_NAME} PRIVATE ${KAMPING_WARNING_FLAGS})
gtest_discover_tests(${KAMPING_TARGET_NAME} WORKING_DIRECTORY ${PROJECT_DIR})
if (NOT KAMPING_NO_EXCEPTION_MODE)
target_compile_definitions(${KAMPING_TARGET_NAME} PRIVATE -DKAMPING_EXCEPTION_MODE)
endif ()
kamping_set_kassert_flags(${KAMPING_TARGET_NAME} ${ARGN})
endfunction()

# Convenience wrapper for adding tests for KaMPI.ng which rely on MPI
Expand All @@ -42,11 +58,9 @@ function(kamping_register_mpi_test KAMPING_TARGET_NAME)
${ARGN}
)
katestrophe_add_test_executable(${KAMPING_TARGET_NAME} FILES ${KAMPING_FILES})
target_link_libraries(${KAMPING_TARGET_NAME} PRIVATE kamping)
target_link_libraries(${KAMPING_TARGET_NAME} PRIVATE kamping_base)
katestrophe_add_mpi_test(${KAMPING_TARGET_NAME} CORES ${KAMPING_CORES} DISCOVER_TESTS)
if (NOT KAMPING_NO_EXCEPTION_MODE)
target_compile_definitions(${KAMPING_TARGET_NAME} PRIVATE -DKAMPING_EXCEPTION_MODE)
endif ()
kamping_set_kassert_flags(${KAMPING_TARGET_NAME} ${ARGN})
endfunction()

# Convenience wrapper for registering a set of tests that should fail to compile and require KaMPI.ng to be linked.
Expand All @@ -67,9 +81,7 @@ function(kamping_register_compilation_failure_test KAMPING_TARGET_NAME)
TARGET ${KAMPING_TARGET_NAME}
FILES ${KAMPING_FILES}
SECTIONS ${KAMPING_SECTIONS}
LIBRARIES kamping
LIBRARIES kamping_base
)
if (NOT KAMPING_NO_EXCEPTION_MODE)
target_compile_definitions(${KAMPING_TARGET_NAME} PRIVATE -DKAMPING_EXCEPTION_MODE)
endif ()
kamping_set_kassert_flags(${KAMPING_TARGET_NAME} ${ARGN})
endfunction()
4 changes: 0 additions & 4 deletions tests/kassert_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// You should have received a copy of the GNU Lesser General Public License along with KaMPI.ng. If not, see
// <https://www.gnu.org/licenses/>.

// overwrite build options and set assertion level to normal
#undef KAMPING_ASSERTION_LEVEL
#define KAMPING_ASSERTION_LEVEL kamping::assert::normal

#include "kamping/kassert.hpp"

#include <gmock/gmock.h>
Expand Down
7 changes: 0 additions & 7 deletions tests/mpi_communicator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
// You should have received a copy of the GNU Lesser General Public License along with KaMPI.ng. If not, see
// <https://www.gnu.org/licenses/>.

// overwrite build options and set assertion level to normal, enable exceptions
#undef KAMPING_ASSERTION_LEVEL
#define KAMPING_ASSERTION_LEVEL kamping::assert::normal
#ifndef KAMPING_EXCEPTION_MODE
#define KAMPING_EXCEPTION_MODE
#endif // KAMPING_EXCEPTION_MODE

#include <gtest/gtest.h>
#include <mpi.h>

Expand Down

0 comments on commit 0146f9b

Please sign in to comment.