From ddf5e9ba837401aad3b01b1b49edc16f26aa94ed Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Mon, 8 Jul 2024 10:28:25 -0400 Subject: [PATCH] adding a test hierarchy skeleton with ctest automation --- CMakeLists.txt | 54 ++++++++++++++++++++++++++++++++++++- test/CMakeLists.txt | 1 + test/example/CMakeLists.txt | 7 +++++ test/example/example.cpp | 35 ++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/example/CMakeLists.txt create mode 100644 test/example/example.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 90bff6b..66fce50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ set(STARTER_INSTALL_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") set(STARTER_INSTALL_LIB_DIR "${PROJECT_SOURCE_DIR}/lib") set(STARTER_INSTALL_BIN_DIR "${PROJECT_SOURCE_DIR}/bin") -add_definitions(-D UNIVERSAL_ENABLE_TEST=OFF) +add_definitions(-D STARTER_ENABLE_TEST=ON) # include file for common includes include_directories(${STARTER_INSTALL_INCLUDE_DIR}) @@ -48,4 +48,56 @@ enable_testing() # MTL4 is a C++ header-only library, so we do not need to build anything #add_subdirectory(${STARTER_ROOT_DIR}/ext/stillwater-sc/mtl4 build_mtl4) +option(STARTER_CMAKE_TRACE "Trace cmake build file actions" ON) +option(STARTER_USE_FOLDERS "Enable solution folders in Visual Studio, disable for Express" ON) +if (STARTER_USE_FOLDERS) + set_property(GLOBAL PROPERTY USE_FOLDERS ON) +endif() + +#### +# macro to read all cpp files in a directory +# and create a test target for that cpp file +macro (compile_all testing prefix folder) + # cycle through the sources + # For the according directories, we assume that each cpp file is a separate test + # so, create a executable target and an associated test target + foreach (source ${ARGN}) + get_filename_component (test ${source} NAME_WE) + string(REPLACE " " ";" new_source ${source}) + set(test_name ${prefix}_${test}) + message(STATUS "Add test ${test_name} from source ${new_source}.") + add_executable (${test_name} ${new_source}) + + #add_custom_target(valid SOURCES ${SOURCES}) + set_target_properties(${test_name} PROPERTIES FOLDER ${folder}) + if (${testing} STREQUAL "true") + if (STARTER_CMAKE_TRACE) + message(STATUS "testing: ${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}") + endif() + add_test(${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}) + endif() + endforeach (source) +endmacro (compile_all) + +#### +# macro to create an executable target consisting of all cpp files in a directory +# and create a test target for that cpp file +macro (compile_multifile_target testing test_name folder) + message(STATUS "Add test ${test_name} from source folder ${folder}.") + add_executable (${test_name} ${ARGN}) + + #add_custom_target(valid SOURCES ${SOURCES}) + set_target_properties(${test_name} PROPERTIES FOLDER ${folder}) + if (${testing} STREQUAL "true") + if (STARTER_CMAKE_TRACE) + message(STATUS "testing: ${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}") + endif() + add_test(${test_name} ${RUNTIME_OUTPUT_DIRECTORY}/${test_name}) + endif() +endmacro (compile_multifile_target) + + +# incorporate the specific targets that are contained in the source trees add_subdirectory(src) +add_subdirectory(test) + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..a5c4dce --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(example) diff --git a/test/example/CMakeLists.txt b/test/example/CMakeLists.txt new file mode 100644 index 0000000..23a25be --- /dev/null +++ b/test/example/CMakeLists.txt @@ -0,0 +1,7 @@ +file (GLOB SRCS "./*.cpp") + +# Universal is a C++ header-only library, so we do not need to build anything +include_directories(${STARTER_ROOT_DIR}/ext/stillwater-sc/universal/include) + +# create a ctest target for every individual cpp file in this directory +compile_all("true" "testprefix" "Tests/example" "${SRCS}") diff --git a/test/example/example.cpp b/test/example/example.cpp new file mode 100644 index 0000000..6fcaefb --- /dev/null +++ b/test/example/example.cpp @@ -0,0 +1,35 @@ +#include +#include + +#include + +int main(int argc, char** argv) +try { + using namespace sw::universal; + edecimal d, e, f; + + e = 1.0f; + f = 0xFFFF'FFFF'FFFF'FFFFull; + d = e + f; + + std::cout << d << std::endl; + + return EXIT_SUCCESS; + +} +catch (const char* msg) { + std::cerr << msg << std::endl; + return EXIT_FAILURE; +} +catch (const sw::universal::universal_arithmetic_exception& err) { + std::cerr << "Unprocessed universal arithmetic exception: " << err.what() << std::endl; + return EXIT_FAILURE; +} +catch (const sw::universal::universal_internal_exception& err) { + std::cerr << "Unprocessed universal internal exception: " << err.what() << std::endl; + return EXIT_FAILURE; +} +catch (...) { + std::cerr << "Caught unknown exception" << std::endl; + return EXIT_FAILURE; +}