Skip to content

Commit 0935435

Browse files
authored
Initial unit tests for include/data (#83)
* initial FluidTensor tests * add assert death test to cmake * more assert death * fix compile tests * Working assertion tests, it seems * Add first FluidTensorView tests * tidy up cmake a bit * factor out EqualRange matcher to own header * Add FluidTensorSupport tests * rollback to Catch2 for compatability with approvaltests.cpp * add tests for FluidDataSet bubba's first approval test ✨ * test print for bigger range as well * tidy cmake * make building tests optional * remove spurious files from tracking * add workflow for CI * make workflow manually triggerable * correct workflow file extention * pleading and bargaining with github * getting event name right * FluidTensorSlice: fix terrible and aged typename mistake * workflow: try and cache whole build folder * wotkflow: unbreak yaml * workflow: disable fail-fast
1 parent e209449 commit 0935435

14 files changed

+1236
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Flucoma Core CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ main, dev ]
7+
pull_request:
8+
branches: [ main, dev ]
9+
10+
env:
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
build:
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
matrix:
18+
os: [ubuntu-18.04, windows-latest, macOS-latest]
19+
fail-fast: false
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
24+
- name: Cache Build Folder
25+
uses: actions/cache@v2
26+
with:
27+
path: ${{github.workspace}}/build
28+
key: ${{ runner.os }}-buildcache
29+
30+
- name: Configure CMake
31+
run: cmake -B ${{github.workspace}}/build -DFLUCOMA_TESTS=On -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
32+
33+
- name: Build
34+
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
35+
36+
- name: Test
37+
working-directory: ${{github.workspace}}/build
38+
run: ctest -C ${{env.BUILD_TYPE}}
39+

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,9 @@ endif()
191191
add_subdirectory(
192192
"${CMAKE_CURRENT_SOURCE_DIR}/examples"
193193
)
194+
195+
enable_testing()
196+
197+
if(FLUCOMA_TESTS)
198+
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/tests")
199+
endif()

include/data/FluidTensor_Support.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ struct FluidTensorSlice
358358
operator()(Dims... dims) const
359359
{
360360
static_assert(sizeof...(Dims) == N, "");
361-
size_t args[N]{Index(dims)...};
361+
size_t args[N]{size_t(dims)...};
362362
return std::inner_product(args, args + N, strides.begin(), index(0));
363363
}
364364

tests/AbortHandler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
///https://discourse.cmake.org/t/tests-that-are-meant-to-abort/537/3
3+
4+
#include <cstdlib>
5+
#include <csignal>
6+
7+
// This is a hack to implement death tests in CTest.
8+
extern "C" void error_test_handle_abort(int) {
9+
std::_Exit(EXIT_FAILURE);
10+
}
11+
12+
struct test_override_abort {
13+
test_override_abort() noexcept {
14+
std::signal(SIGABRT, error_test_handle_abort);
15+
}
16+
};
17+
18+
test_override_abort handler{};

tests/CMakeLists.txt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
cmake_minimum_required (VERSION 3.11)
2+
3+
###### Utils
4+
5+
##### Assert Death Testing
6+
7+
# a handler to catch sigabrt for us on death tests
8+
add_library(AbortHandler OBJECT AbortHandler.cpp)
9+
set_target_properties(AbortHandler PROPERTIES
10+
CXX_STANDARD 14
11+
CXX_STANDARD_REQUIRED ON
12+
CXX_EXTENSIONS OFF
13+
)
14+
15+
# function (add_assert_fail_test testname sourcedir)
16+
# try_compile(
17+
# testname_BUILT "${CMAKE_BINARY_DIR}/tmp/${testname}" "${sourcedir}" FluidTensorDeathTests
18+
# OUTPUT_VARIABLE whathappened
19+
# )
20+
# message(WARNING ${whathappened})
21+
#
22+
# if(NOT testname_BUILT)
23+
# message(ERROR "Could not build assertion death tests in ${sourcedir}")
24+
# endif()
25+
#
26+
# add_custom_command(${testname}_BUILD)
27+
# add_executable(${testname} "${sourcefile}")
28+
# target_link_libraries(${testname} PRIVATE FLUID_DECOMPOSITION)
29+
#
30+
# add_test(NAME ${testname}
31+
# COMMAND ${testname}
32+
# )
33+
# set_tests_properties(${testname} PROPERTIES WILL_FAIL true)
34+
# endfunction()
35+
36+
###### compilation tests
37+
function (add_compile_tests test_name_stub sourcefile)
38+
39+
file(STRINGS ${sourcefile} FAILERS REGEX "#ifdef (FAIL_.+)")
40+
file(STRINGS ${sourcefile} CONFIMERS REGEX "#ifdef (CONFIRM_.+)")
41+
42+
foreach(TESTLINE IN LISTS FAILERS)
43+
string(REGEX MATCH "FAIL_[^ \t\r\n]+" TESTNAME ${TESTLINE})
44+
# message(STATUS "Fluid Tensor build failure tests: ${TESTNAME}")
45+
_add_one_compile_test(${TESTNAME} ${sourcefile} true)
46+
endforeach()
47+
48+
foreach(TESTLINE IN LISTS CONFIMERS)
49+
string(REGEX MATCH "CONFIRM_[^ \t\r\n]+" TESTNAME ${TESTLINE})
50+
# message(STATUS "Fluid Tensor build confirmation tests: ${TESTNAME}")
51+
_add_one_compile_test(${TESTNAME} ${sourcefile} false)
52+
endforeach()
53+
54+
endfunction()
55+
56+
function (_add_one_compile_test testname sourcefile should_fail)
57+
add_executable(${testname} "${sourcefile}")
58+
target_link_libraries(${testname} FLUID_DECOMPOSITION)
59+
set_target_properties(${testname} PROPERTIES
60+
EXCLUDE_FROM_ALL true
61+
EXCLUDE_FROM_DEFAULT_BUILD true
62+
CXX_STANDARD 14
63+
CXX_STANDARD_REQUIRED ON
64+
CXX_EXTENSIONS OFF
65+
POSITION_INDEPENDENT_CODE ON
66+
)
67+
target_compile_definitions(${testname} PRIVATE ${testname})
68+
#test consists of running compiler on source
69+
add_test(NAME ${testname}
70+
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target ${testname} --config $<CONFIGURATION>
71+
)
72+
set_tests_properties(${testname} PROPERTIES WILL_FAIL ${should_fail})
73+
endfunction()
74+
75+
#*********************************************************************
76+
# main body
77+
78+
Include(FetchContent)
79+
80+
# get catch
81+
FetchContent_Declare(
82+
Catch2
83+
GIT_SHALLOW TRUE
84+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
85+
GIT_TAG v2.x
86+
)
87+
FetchContent_MakeAvailable(Catch2)
88+
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib)
89+
90+
# get approvaltests.cpp
91+
FetchContent_Declare(
92+
ApprovalCPP
93+
GIT_SHALLOW TRUE
94+
GIT_REPOSITORY https://github.com/approvals/ApprovalTests.cpp.git
95+
GIT_TAG master
96+
)
97+
FetchContent_MakeAvailable(ApprovalCPP)
98+
99+
add_library(TestUtils INTERFACE)
100+
target_include_directories(TestUtils INTERFACE include)
101+
target_link_libraries(TestUtils INTERFACE
102+
Catch2::Catch2
103+
FLUID_DECOMPOSITION
104+
ApprovalTests::ApprovalTests
105+
)
106+
target_compile_definitions(TestUtils INTERFACE
107+
APPROVAL_TESTS_HIDE_DEPRECATED_CODE=1
108+
)
109+
target_compile_features(TestUtils INTERFACE cxx_std_14)
110+
111+
function(add_test_executable target_name source_file)
112+
add_executable(${target_name} ${source_file})
113+
target_link_libraries(${target_name} PUBLIC TestUtils)
114+
set_target_properties(${target_name} PROPERTIES
115+
CXX_STANDARD 14
116+
CXX_STANDARD_REQUIRED ON
117+
CXX_EXTENSIONS OFF
118+
)
119+
endfunction()
120+
121+
add_test_executable(TestFluidTensor data/TestFluidTensor.cpp)
122+
add_test_executable(TestFluidTensorDeath data/death_tests/TestFluidTensorAsserts.cpp)
123+
target_link_libraries(TestFluidTensorDeath PRIVATE
124+
$<TARGET_OBJECTS:AbortHandler>
125+
)
126+
127+
add_test_executable(TestFluidTensorView data/TestFluidTensorView.cpp)
128+
add_test_executable(TestFluidTensorSupport data/TestFluidTensorSupport.cpp)
129+
add_test_executable(TestFluidDataSet data/TestFluidDataSet.cpp)
130+
131+
include(CTest)
132+
include(Catch)
133+
134+
catch_discover_tests(TestFluidTensor WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
135+
136+
catch_discover_tests(TestFluidTensorDeath
137+
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
138+
PROPERTIES WILL_FAIL true
139+
)
140+
141+
catch_discover_tests(TestFluidTensorView WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
142+
catch_discover_tests(TestFluidTensorSupport WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
143+
catch_discover_tests(TestFluidDataSet WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
144+
145+
add_compile_tests("FluidTensor Compilation Tests" data/compile_tests/TestFluidTensor_Compile.cpp)

0 commit comments

Comments
 (0)