diff --git a/CMakeLists.txt b/CMakeLists.txt index f0a17dd..4d34c76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) +option(MCStepLogger_BUILD_TESTS "Control whether tests are built" OFF) + +include(CTest) + # Install directories SET(INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin) SET(INSTALL_MACRO_DIR ${CMAKE_INSTALL_PREFIX}/macro) @@ -224,3 +228,12 @@ configure_file( install(FILES "${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake" DESTINATION ${INSTALL_CMAKE_DIR}) + +############### +# Build tests # +############### +if(NOT MCStepLogger_BUILD_TESTS) + message(WARNING "No tests are built.") +else() + add_subdirectory(test) +endif(NOT MCStepLogger_BUILD_TESTS) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..750ba31 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,23 @@ +# Build tests + +set(REPO_NAME_TEST MCStepLoggerTestData) + +# Could potentially use CMake's FetchContent module but seems to be overhead since we only need this single line +execute_process(COMMAND git clone https://gitlab.cern.ch/bvolkel/${REPO_NAME_TEST}.git WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} RESULT_VARIABLE res) + +if(NOT res EQUAL "0") + message(WARNING "Test data could not be fetched") + return() +endif(NOT res EQUAL "0") + +# Find boost unit tests dependencies +find_package(Boost REQUIRED COMPONENTS unit_test_framework) +include_directories(${Boost_INCLUDE_DIR}) + +# The basic test only checks core functionality without any backend. +set(CORE_TARGET analysis) +set(TEST_CORE "test${CORE_TARGET}") +set(STEP_TEST_FILE MCStepLoggerOutput.root) +add_executable(${CORE_TARGET} "${CORE_TARGET}.cxx") +target_link_libraries(${CORE_TARGET} MCStepLoggerCore MCStepLoggerAnalysis ${Boost_LIBRARIES}) +add_test(NAME ${TEST_CORE} COMMAND ${CORE_TARGET} -- ${REPO_NAME_TEST}/${CORE_TARGET}/${STEP_TEST_FILE} "./") diff --git a/test/analysis.cxx b/test/analysis.cxx new file mode 100644 index 0000000..36a4cff --- /dev/null +++ b/test/analysis.cxx @@ -0,0 +1,53 @@ +// Test analysis functionality + +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE MCStepLoggerAnalysisCoreTest + +#include + +#include +#include + +#include "MCStepLogger/MCAnalysisManager.h" +#include "MCStepLogger/MCAnalysisFileWrapper.h" +#include "MCStepLogger/SimpleStepAnalysis.h" +#include "MCStepLogger/MCAnalysisFileWrapper.h" + +using namespace o2::mcstepanalysis; + +// checking meta info and whether there is a value container the analysis can be conpared with +BOOST_AUTO_TEST_CASE(testAnalysis) +{ + + // require input and output path + BOOST_REQUIRE(boost::unit_test::framework::master_test_suite().argc == 3); + + new SimpleStepAnalysis(); + auto& anamgr = MCAnalysisManager::Instance(); + anamgr.setLabel("testLabel"); + anamgr.setInputFilepath(boost::unit_test::framework::master_test_suite().argv[1]); + + // everything loaded and ready to run? + BOOST_REQUIRE(anamgr.checkReadiness()); + + // runs all events found and should produce an output file + anamgr.run(); + + // write output + std::string outputDir(boost::unit_test::framework::master_test_suite().argv[2]); + std::string analysisFilepath = outputDir + "/SimpleStepAnalysis/Analysis.root"; + anamgr.write(outputDir); + + // Now check if the analysis file was produced properly + std::ifstream file(analysisFilepath); + BOOST_REQUIRE(file); + + MCAnalysisFileWrapper anaFile; + anaFile.read(analysisFilepath); + BOOST_REQUIRE(anaFile.isSane()); + + // one last check for meta information + auto& anaMetaInfo = anaFile.getAnalysisMetaInfo(); + BOOST_REQUIRE(anaMetaInfo.analysisName.compare("SimpleStepAnalysis") == 0); +}