From a9526ae663500ef49e4bea18f6d2a1812d204048 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Fri, 16 Dec 2022 12:11:46 +0100 Subject: [PATCH] Add support for unit tests --- test/CMakeLists.txt | 1 + test/macros.cmake | 46 +++++++++++++++++++++++++++++++++++++++- test/unit/CMakeLists.txt | 4 ++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/unit/CMakeLists.txt diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e19df6d..b5e12c2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,5 +6,6 @@ set(OVNI_TEST_BUILD_DIR "${CMAKE_BINARY_DIR}/test") include(macros.cmake) +add_subdirectory(unit) add_subdirectory(emu) add_subdirectory(rt) diff --git a/test/macros.cmake b/test/macros.cmake index 670d81a..e64090d 100644 --- a/test/macros.cmake +++ b/test/macros.cmake @@ -3,8 +3,52 @@ include(CMakeParseArguments) +function(unit_test source) + set(switches "") + set(single "" NAME) + set(multi "") + + cmake_parse_arguments( + OVNI_TEST "${switches}" "${single}" "${multi}" ${ARGN}) + + if(OVNI_TEST_NAME) + set(test_name "${OVNI_TEST_NAME}") + else() + set(test_name "${source}") + endif() + + # Compute the test name from the source and path + cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR + BASE_DIRECTORY "${OVNI_TEST_SOURCE_DIR}" + OUTPUT_VARIABLE name_prefix) + set(full_path "${name_prefix}/${test_name}") + string(REGEX REPLACE "\.c$" "" full_path_noext "${full_path}") + string(REPLACE "/" "-" name "${full_path_noext}") + + set(OVNI_TEST_NAME ${name}) + set(OVNI_TEST_NAME ${OVNI_TEST_NAME} PARENT_SCOPE) + set(OVNI_TEST_SOURCE ${source}) + + include_directories( + "${CMAKE_SOURCE_DIR}/src/include" + "${CMAKE_SOURCE_DIR}/src" + "${CMAKE_SOURCE_DIR}/include" + ) + add_executable("${OVNI_TEST_NAME}" "${OVNI_TEST_SOURCE}") + target_link_libraries("${OVNI_TEST_NAME}" PRIVATE ovni emu) + + add_test(NAME "${OVNI_TEST_NAME}" + COMMAND "${OVNI_TEST_NAME}" + WORKING_DIRECTORY "${OVNI_TEST_BUILD_DIR}") + + set_tests_properties("${OVNI_TEST_NAME}" + PROPERTIES + RUN_SERIAL TRUE + WORKING_DIRECTORY "${OVNI_TEST_BUILD_DIR}") +endfunction(unit_test) + function(ovni_test source) - set(switches MP SHOULD_FAIL SORT) + set(switches MP SHOULD_FAIL SORT UNIT) set(single NPROC REGEX NAME) set(multi ENV) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt new file mode 100644 index 0000000..acffe39 --- /dev/null +++ b/test/unit/CMakeLists.txt @@ -0,0 +1,4 @@ +# Copyright (c) 2022 Barcelona Supercomputing Center (BSC) +# SPDX-License-Identifier: GPL-3.0-or-later + +# Example: unit_test(abc.c)