Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for include/data #83

Merged
merged 24 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/flucoma-core-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Flucoma Core CI

on:
workflow_dispatch:
push:
branches: [ main, dev ]
pull_request:
branches: [ main, dev ]

env:
BUILD_TYPE: Release

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-18.04, windows-latest, macOS-latest]
fail-fast: false

steps:
- uses: actions/checkout@v2

- name: Cache Build Folder
uses: actions/cache@v2
with:
path: ${{github.workspace}}/build
key: ${{ runner.os }}-buildcache

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DFLUCOMA_TESTS=On -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -C ${{env.BUILD_TYPE}}

6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,9 @@ endif()
add_subdirectory(
"${CMAKE_CURRENT_SOURCE_DIR}/examples"
)

enable_testing()

if(FLUCOMA_TESTS)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/tests")
endif()
2 changes: 1 addition & 1 deletion include/data/FluidTensor_Support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ struct FluidTensorSlice
operator()(Dims... dims) const
{
static_assert(sizeof...(Dims) == N, "");
size_t args[N]{Index(dims)...};
size_t args[N]{size_t(dims)...};
return std::inner_product(args, args + N, strides.begin(), index(0));
}

Expand Down
18 changes: 18 additions & 0 deletions tests/AbortHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

///https://discourse.cmake.org/t/tests-that-are-meant-to-abort/537/3

#include <cstdlib>
#include <csignal>

// This is a hack to implement death tests in CTest.
extern "C" void error_test_handle_abort(int) {
std::_Exit(EXIT_FAILURE);
}

struct test_override_abort {
test_override_abort() noexcept {
std::signal(SIGABRT, error_test_handle_abort);
}
};

test_override_abort handler{};
145 changes: 145 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
cmake_minimum_required (VERSION 3.11)

###### Utils

##### Assert Death Testing

# a handler to catch sigabrt for us on death tests
add_library(AbortHandler OBJECT AbortHandler.cpp)
set_target_properties(AbortHandler PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)

# function (add_assert_fail_test testname sourcedir)
# try_compile(
# testname_BUILT "${CMAKE_BINARY_DIR}/tmp/${testname}" "${sourcedir}" FluidTensorDeathTests
# OUTPUT_VARIABLE whathappened
# )
# message(WARNING ${whathappened})
#
# if(NOT testname_BUILT)
# message(ERROR "Could not build assertion death tests in ${sourcedir}")
# endif()
#
# add_custom_command(${testname}_BUILD)
# add_executable(${testname} "${sourcefile}")
# target_link_libraries(${testname} PRIVATE FLUID_DECOMPOSITION)
#
# add_test(NAME ${testname}
# COMMAND ${testname}
# )
# set_tests_properties(${testname} PROPERTIES WILL_FAIL true)
# endfunction()

###### compilation tests
function (add_compile_tests test_name_stub sourcefile)

file(STRINGS ${sourcefile} FAILERS REGEX "#ifdef (FAIL_.+)")
file(STRINGS ${sourcefile} CONFIMERS REGEX "#ifdef (CONFIRM_.+)")

foreach(TESTLINE IN LISTS FAILERS)
string(REGEX MATCH "FAIL_[^ \t\r\n]+" TESTNAME ${TESTLINE})
# message(STATUS "Fluid Tensor build failure tests: ${TESTNAME}")
_add_one_compile_test(${TESTNAME} ${sourcefile} true)
endforeach()

foreach(TESTLINE IN LISTS CONFIMERS)
string(REGEX MATCH "CONFIRM_[^ \t\r\n]+" TESTNAME ${TESTLINE})
# message(STATUS "Fluid Tensor build confirmation tests: ${TESTNAME}")
_add_one_compile_test(${TESTNAME} ${sourcefile} false)
endforeach()

endfunction()

function (_add_one_compile_test testname sourcefile should_fail)
add_executable(${testname} "${sourcefile}")
target_link_libraries(${testname} FLUID_DECOMPOSITION)
set_target_properties(${testname} PROPERTIES
EXCLUDE_FROM_ALL true
EXCLUDE_FROM_DEFAULT_BUILD true
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)
target_compile_definitions(${testname} PRIVATE ${testname})
#test consists of running compiler on source
add_test(NAME ${testname}
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target ${testname} --config $<CONFIGURATION>
)
set_tests_properties(${testname} PROPERTIES WILL_FAIL ${should_fail})
endfunction()

#*********************************************************************
# main body

Include(FetchContent)

# get catch
FetchContent_Declare(
Catch2
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.x
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib)

# get approvaltests.cpp
FetchContent_Declare(
ApprovalCPP
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/approvals/ApprovalTests.cpp.git
GIT_TAG master
)
FetchContent_MakeAvailable(ApprovalCPP)

add_library(TestUtils INTERFACE)
target_include_directories(TestUtils INTERFACE include)
target_link_libraries(TestUtils INTERFACE
Catch2::Catch2
FLUID_DECOMPOSITION
ApprovalTests::ApprovalTests
)
target_compile_definitions(TestUtils INTERFACE
APPROVAL_TESTS_HIDE_DEPRECATED_CODE=1
)
target_compile_features(TestUtils INTERFACE cxx_std_14)

function(add_test_executable target_name source_file)
add_executable(${target_name} ${source_file})
target_link_libraries(${target_name} PUBLIC TestUtils)
set_target_properties(${target_name} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
endfunction()

add_test_executable(TestFluidTensor data/TestFluidTensor.cpp)
add_test_executable(TestFluidTensorDeath data/death_tests/TestFluidTensorAsserts.cpp)
target_link_libraries(TestFluidTensorDeath PRIVATE
$<TARGET_OBJECTS:AbortHandler>
)

add_test_executable(TestFluidTensorView data/TestFluidTensorView.cpp)
add_test_executable(TestFluidTensorSupport data/TestFluidTensorSupport.cpp)
add_test_executable(TestFluidDataSet data/TestFluidDataSet.cpp)

include(CTest)
include(Catch)

catch_discover_tests(TestFluidTensor WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")

catch_discover_tests(TestFluidTensorDeath
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
PROPERTIES WILL_FAIL true
)

catch_discover_tests(TestFluidTensorView WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
catch_discover_tests(TestFluidTensorSupport WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
catch_discover_tests(TestFluidDataSet WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")

add_compile_tests("FluidTensor Compilation Tests" data/compile_tests/TestFluidTensor_Compile.cpp)
Loading