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

Add a basic Catch2 based unittest setup #31

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: cvmfs-contrib/github-action-cvmfs@v2
- uses: aidasoft/run-lcg-view@v3
with:
coverity-cmake-command: 'cmake -C $ILCSOFT/ILCSoft.cmake ..'
coverity-cmake-command: 'cmake -C $ILCSOFT/ILCSoft.cmake -DBUILD_TESTING=OFF ..'
coverity-project: 'iLCSoft%2FMarlinUtil'
coverity-project-token: ${{ secrets.MARLINUTIL_COVERITY_TOKEN }}
github-pat: ${{ secrets.READ_COVERITY_IMAGE }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: cvmfs-contrib/github-action-cvmfs@v2
- uses: aidasoft/run-lcg-view@v3
- uses: cvmfs-contrib/github-action-cvmfs@v3
- uses: aidasoft/run-lcg-view@v4
with:
view-path: "/cvmfs/clicdp.cern.ch/iLCSoft/lcg/${{ matrix.LCG }}/nightly/x86_64-centos7-${{ matrix.COMPILER }}-opt"
setup-script: "init_ilcsoft.sh"
run: |
mkdir build
cd build
cmake -GNinja -C ${ILCSOFT}/ILCSoft.cmake -DCMAKE_CXX_FLAGS=" -fdiagnostics-color=always " -DINSTALL_DOC=ON ..
cmake -GNinja -C ${ILCSOFT}/ILCSoft.cmake -DCMAKE_CXX_FLAGS=" -fdiagnostics-color=always " -DINSTALL_DOC=ON -DUSE_EXTERNAL_CATCH2=OFF ..
ninja -k0
ctest --output-on-failure
ninja install
21 changes: 4 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,11 @@ SET_SOURCE_FILES_PROPERTIES( "./source/src/ann/kd_pr_search.cpp" PROPERTIES COMP
#ADD_SHARED_LIBRARY( ${PROJECT_NAME}_ann ${ann_library_sources} )
#INSTALL_SHARED_LIBRARY( ${PROJECT_NAME}_ann DESTINATION lib )

option(USE_EXTERNAL_CATCH2 "Link against an external Catch2 v3 static library, otherwise build it locally" ON)

# helper macro for adding new tests
# build with 'make tests'
ADD_CUSTOM_TARGET( tests )
MACRO( ADD_MARLINUTIL_TEST _name )
# MarlinUtil test
IF( BUILD_TESTING )
ADD_EXECUTABLE( ${_name} "./source/tests/${_name}.cc" )
ELSE()
ADD_EXECUTABLE( ${_name} EXCLUDE_FROM_ALL "./source/tests/${_name}.cc" )
ENDIF()
ADD_DEPENDENCIES( tests ${_name} )
TARGET_LINK_LIBRARIES( ${_name} ${PROJECT_NAME} )
#INSTALL( TARGETS ${_name} DESTINATION bin )
ENDMACRO()

ADD_MARLINUTIL_TEST( testmarlinutil )

IF( BUILD_TESTING )
ADD_SUBDIRECTORY(source/tests)
ENDIF()

# display some variables and write them to cache
DISPLAY_STD_VARIABLES()
Expand Down
13 changes: 8 additions & 5 deletions source/include/HelixClassT.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
template<typename FloatT>
class HelixClassT {
public:

/**
* The floatint point type used internally. Useful for generic programming
*/
using float_type = FloatT;
/**
* Constructor. Initializations of constants which are used
* to calculate various parameters associated with helix.
Expand Down Expand Up @@ -109,12 +112,12 @@ class HelixClassT {
* Returns momentum of particle at the point of closest approach <br>
* to IP <br>
*/
const FloatT *const getMomentum() const { return _momentum; }
const FloatT* getMomentum() const { return _momentum; }

/**
* Returns reference point of track <br>
*/
const FloatT *const getReferencePoint() const { return _referencePoint; }
const FloatT* getReferencePoint() const { return _referencePoint; }

/**
* Returns Phi angle of the momentum vector <br>
Expand Down Expand Up @@ -242,12 +245,12 @@ class HelixClassT {
/**
* Returns starting point of helix
*/
const FloatT *const getStartingPoint() const {return _xStart;}
const FloatT* getStartingPoint() const {return _xStart;}

/**
* Returns endpoint of helix
*/
const FloatT *const getEndPoint() const {return _xEnd;}
const FloatT* getEndPoint() const {return _xEnd;}

/**
* Returns BZ for the second parameterization
Expand Down
50 changes: 50 additions & 0 deletions source/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# helper macro for adding new tests
# build with 'make tests'
ADD_CUSTOM_TARGET( tests )
MACRO( ADD_MARLINUTIL_TEST _name )
# MarlinUtil test
ADD_EXECUTABLE( ${_name} "${_name}.cc" )
ADD_TEST( NAME ${_name} COMMAND ${_name})
TARGET_LINK_LIBRARIES( ${_name} ${PROJECT_NAME} )
ENDMACRO()

ADD_MARLINUTIL_TEST( testmarlinutil )

#--- Unit test setup. Use existing Catch2 or fetch a suitable version and build it
if(USE_EXTERNAL_CATCH2)
FIND_PACKAGE(Catch2 3.0.0 REQUIRED)
else()
MESSAGE(STATUS "Fetching local copy of Catch2 library for unit-tests...")
# Build Catch2 with the default flags, to avoid generating warnings when we
# build it
SET(CXX_FLAGS_CMAKE_USED ${CMAKE_CXX_FLAGS})
SET(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_DEFAULTS})
INCLUDE(FetchContent)
fETCHCONTENT_DECLARE(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
)
FETCHCONTENT_MAKEAVAILABLE(Catch2)
set(CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras ${CMAKE_MODULE_PATH})

# Disable clang-tidy on external contents
SET_TARGET_PROPERTIES(Catch2 PROPERTIES CXX_CLANG_TIDY "")

# Hack around the fact, that the include directories are not declared as
# SYSTEM for the targets defined this way. Otherwise warnings can still occur
# in Catch2 code when templates are evaluated (which happens quite a bit)
GET_TARGET_PROPERTY(CATCH2_IF_INC_DIRS Catch2 INTERFACE_INCLUDE_DIRECTORIES)
SET_TARGET_PROPERTIES(Catch2 PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${CATCH2_IF_INC_DIRS}")

# Reset the flags
SET(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_USED})
endif()

INCLUDE(Catch)

ADD_EXECUTABLE(unittests unittests/TestHelixClass.cpp)
TARGET_LINK_LIBRARIES(unittests PUBLIC ${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)
CATCH_DISCOVER_TESTS(unittests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
49 changes: 49 additions & 0 deletions source/tests/unittests/TestHelixClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "HelixClass.h"
#include "HelixClass_double.h"

#include "CLHEP/Units/PhysicalConstants.h"

#include <catch2/catch_approx.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <array>
#include <cmath>
#include <tuple>

// The speed of light as used inside the Helix class
constexpr auto c_helix = CLHEP::c_light / (CLHEP::m / CLHEP::ps);

// Using this type list and a TEMPLATE_LIST_TEST_CASE we can test both versions
// of the HelixClass with only one set of code. Inside the test case we can
// refer to the current HelixClass type with TestType
using HelixTypes = std::tuple<HelixClass, HelixClass_double>;

TEMPLATE_LIST_TEST_CASE("Initialize_VP", "[helix-init]", HelixTypes) {
TestType helix;
using FloatT = typename TestType::float_type;
/*const*/ std::array<FloatT, 3> position = {0.1, 0.2, 0.3};
/*const*/ std::array<FloatT, 3> momentum = {10., 20., 30.};

const auto pt = std::hypot(momentum[0], momentum[1]);
const auto magField = 3.0;
const auto charge = -1.0;

helix.Initialize_VP(position.data(), momentum.data(), charge, magField);

REQUIRE(helix.getRadius() == Catch::Approx(pt / (c_helix * magField)));
REQUIRE(helix.getOmega() == Catch::Approx(charge / helix.getRadius()));
REQUIRE(helix.getTanLambda() == Catch::Approx(momentum[2] / pt));
// etc...
}

// TODO: add actually useful HelixClass tests, e.g.
// - Initialize with one of the methods and check whether the resulting Helix
// has the expected properties, like
// - calculating different distances to the Helix and check whether the
// conform to expecations
// - Calculating momenta and see whether that gives the expected values
// - essentially all non-trivial get functionality
// - Other usage examples as found in the "real-world"
// - Also add things that work slightly unexpectedly at the moment, e.g. things
// that are listed in https://github.com/iLCSoft/MarlinUtil/issues/24