Skip to content

Commit

Permalink
Add initial testing with coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Travis F. Collins <travis.collins@analog.com>
  • Loading branch information
tfcollins committed Nov 30, 2023
1 parent f5b7f6a commit 636f47a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,40 @@ add_subdirectory(bindings)
option(WITH_MAN "Generate on-line reference manuals (man pages)" OFF)
add_subdirectory(man)

# Testing
option(TESTS "Enabling unit testing" OFF)
option(TESTS_DEBUG "Enable test debugging" OFF)
option(TESTS_COVERAGE "Enable coverage tracing when testing" OFF)
if(TESTS)
include(CTest)
enable_testing()
add_subdirectory(tests)
if(TESTS_COVERAGE)
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message(FATAL_ERROR "Coverage only supported on Linux")
endif()
# Verify that lcov is installed
find_program(LCOV_PATH lcov)
if(NOT LCOV_PATH)
message(FATAL_ERROR "lcov not found! Aborting...")
endif()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -fprofile-arcs -ftest-coverage")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -W -fprofile-arcs -ftest-coverage")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
target_compile_options(iio PRIVATE --coverage)
target_link_libraries(iio PRIVATE --coverage)

# Add custom target to run tests with coverage
add_custom_target(
coverage
COMMAND ${CMAKE_CTEST_COMMAND} --progress && lcov -c -d ${CMAKE_CURRENT_SOURCE_DIR} -o main_coverage.info && genhtml main_coverage.info -o coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

message(STATUS "Coverage flags enabled")
endif()

endif()

include(cmake/Install.cmake)

# Add uninstall target
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if(TESTS_DEBUG)
add_definitions(-DTESTS_DEBUG)
endif()
add_subdirectory(standalone)
6 changes: 6 additions & 0 deletions tests/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable(TestVersion test_version.c)
target_link_libraries(TestVersion LINK_PRIVATE iio)
add_test(
NAME TestVersion
COMMAND TestVersion
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
22 changes: 22 additions & 0 deletions tests/standalone/test_version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <assert.h>
#include <stdio.h>

#include "iio/iio.h"

int main() {

char *expected_version = "1.0";
char actual_version[10];

sprintf(actual_version, "%u.%u", iio_context_get_version_major(NULL),
iio_context_get_version_minor(NULL));

#ifdef TESTS_DEBUG
printf("Expected version: %s\n", expected_version);
printf("Actual version: %s\n", actual_version);
#endif

assert(strcmp(expected_version, actual_version) == 0);

return 0;
}

0 comments on commit 636f47a

Please sign in to comment.