Skip to content

Commit

Permalink
adding a test hierarchy skeleton with ctest automation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Jul 8, 2024
1 parent bffca39 commit ddf5e9b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
54 changes: 53 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)

1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(example)
7 changes: 7 additions & 0 deletions test/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
35 changes: 35 additions & 0 deletions test/example/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <iomanip>

#include <universal/number/edecimal/edecimal.hpp>

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;
}

0 comments on commit ddf5e9b

Please sign in to comment.