Skip to content

Commit

Permalink
GTest and Coverage cleanup and Basic EKF GTest
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilritz authored and bresch committed Oct 28, 2019
1 parent fcea13e commit cac5f3f
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 69 deletions.
77 changes: 36 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ cmake_minimum_required(VERSION 3.0)
project(ECL CXX)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE)
message(STATUS "set build type to ${CMAKE_BUILD_TYPE}")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE)
message(STATUS "set build type to ${CMAKE_BUILD_TYPE}")
endif()

set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage")
Expand All @@ -60,11 +60,11 @@ option(COV_HTML "Display html for coverage" OFF)
option(ECL_ASAN "Enable ECL address sanitizer" OFF)

set(CMAKE_CXX_FLAGS_COVERAGE
"--coverage -fprofile-arcs -ftest-coverage -fno-default-inline -fno-inline -fno-inline-small-functions -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
"--coverage -fprofile-arcs -ftest-coverage -fno-default-inline -fno-inline -fno-inline-small-functions -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
"--coverage -ftest-coverage -lgcov"
CACHE STRING "Flags used for linking binaries during coverage builds" FORCE)
"--coverage -ftest-coverage -lgcov"
CACHE STRING "Flags used for linking binaries during coverage builds" FORCE)
mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE CMAKE_C_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE)


Expand Down Expand Up @@ -92,18 +92,17 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
-pedantic

-Wall
-Wextra
#-Wextra # This was causing an issue in the swig stuff
-Werror

-Wno-missing-field-initializers # ignore for GCC 4.8 support
)
endif()

# testing
include(CTest)
enable_testing()

if(BUILD_TESTING)

include(CTest)

option(ECL_TESTS "Build ECL tests" ON)

add_custom_target(check
Expand All @@ -118,6 +117,8 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
# swig requires -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()


endif()

# fetch latest matrix from github
Expand Down Expand Up @@ -164,28 +165,36 @@ add_subdirectory(geo)
add_subdirectory(geo_lookup)
add_subdirectory(l1)
add_subdirectory(tecs)
add_subdirectory(validation)
if(BUILD_TESTING)
add_subdirectory(validation)
add_subdirectory(test)
endif()

#=============================================================================
# Coverage
#
if (${CMAKE_BUILD_TYPE} STREQUAL "Coverage")

add_custom_target(coverage
COMMAND ${CMAKE_CTEST_COMMAND}
COMMAND lcov --capture --directory . --output-file coverage.info
COMMAND lcov --remove coverage.info '/usr/*' --output-file coverage.info # filter out system
COMMAND lcov --remove coverage.info 'build/coverage_build/EKF/swig/*' --output-file coverage.info
COMMAND lcov --summary coverage.info
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS check
)

add_custom_target(coverage_html
COMMAND genhtml coverage.info --output-directory out
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS coverage
)
add_custom_target(coverage
COMMAND ${CMAKE_CTEST_COMMAND}
COMMAND lcov --capture --directory . --output-file coverage.info
COMMAND lcov --remove coverage.info --output-file coverage.info '/usr/*' '${CMAKE_BINARY_DIR}/*' # filter out system
COMMAND lcov --summary coverage.info
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS check
)

add_custom_target(coverage_html
COMMAND genhtml coverage.info --output-directory out
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS coverage
)

add_custom_target(coverage_html_view
COMMAND x-www-browser out/index.html
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS coverage_html
)

endif()

Expand Down Expand Up @@ -223,17 +232,3 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
endif()
endif()


enable_testing()
include_directories(${GTEST_INCLUDE_DIR})
add_subdirectory("/usr/src/gtest" ${CMAKE_BINARY_DIR}/gtest)

set(SRCS
test/main.cpp
test/test_example.cpp
)
add_executable(TestECL ${SRCS})

target_include_directories(TestECL PRIVATE ${GTEST_INCLUDE_DIRS})
target_link_libraries(TestECL gtest gtest_main)
add_test(NAME TestECL COMMAND TestECL)
12 changes: 6 additions & 6 deletions EKF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ set_target_properties(ecl_EKF PROPERTIES PUBLIC_HEADER "ekf.h")

target_compile_options(ecl_EKF PRIVATE -fno-associative-math)

#if(EKF_PYTHON_TESTS)
# add_subdirectory(swig)
#endif()
if(EKF_PYTHON_TESTS)
add_subdirectory(swig)
endif()

#if(BUILD_TESTING)
# add_subdirectory(tests)
#endif()
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SRC_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
define cmake-build
+@$(eval BUILD_DIR = $(SRC_DIR)/build/$@$(BUILD_DIR_SUFFIX))
+@if [ $(PX4_CMAKE_GENERATOR) = "Ninja" ] && [ -e $(BUILD_DIR)/Makefile ]; then rm -rf $(BUILD_DIR); fi
+@if [ ! -e $(BUILD_DIR)/CMakeCache.txt ]; then mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake $(2) -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) $(3) $(4) || (rm -rf $(BUILD_DIR)); fi
+@if [ ! -e $(BUILD_DIR)/CMakeCache.txt ]; then mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake $(2) -G"$(PX4_CMAKE_GENERATOR)" $(CMAKE_ARGS) $(3) $(4) $(5) || (rm -rf $(BUILD_DIR)); fi
+@(cd $(BUILD_DIR) && $(PX4_MAKE) $(PX4_MAKE_ARGS) $(ARGS))
endef

Expand All @@ -98,19 +98,19 @@ doxygen:
.PHONY: test_build test test_EKF

test_build:
@$(call cmake-build,$@,$(SRC_DIR), "-DEKF_PYTHON_TESTS=ON")
@$(call cmake-build,$@,$(SRC_DIR), "-DEKF_PYTHON_TESTS=ON", "-DBUILD_TESTING=ON")

test: test_build
@cmake --build $(SRC_DIR)/build/test_build --target check

test_EKF: test_build
@cmake --build $(SRC_DIR)/build/test_build --target ecl_EKF_pytest-quick

test_EKF_plots: test_build
@cmake --build $(SRC_DIR)/build/test_build --target ecl_EKF_pytest-plots

test_build_asan:
@$(call cmake-build,$@,$(SRC_DIR), "-DECL_ASAN=ON")
@$(call cmake-build,$@,$(SRC_DIR), "-DECL_ASAN=ON", "-DBUILD_TESTING=ON")

test_asan: test_build_asan
@cmake --build $(SRC_DIR)/build/test_build_asan --target check
Expand All @@ -119,21 +119,24 @@ test_asan: test_build_asan
# --------------------------------------------------------------------

coverage_build:
@$(call cmake-build,$@,$(SRC_DIR), "-DCMAKE_BUILD_TYPE=Coverage", "-DEKF_PYTHON_TESTS=ON")
@$(call cmake-build,$@,$(SRC_DIR), "-DCMAKE_BUILD_TYPE=Coverage", "-DEKF_PYTHON_TESTS=ON", "-DBUILD_TESTING=ON")

coverage: coverage_build
@cmake --build $(SRC_DIR)/build/coverage_build --target coverage

coverage_html: coverage
coverage_html: coverage_build
@cmake --build $(SRC_DIR)/build/coverage_build --target coverage_html

coverage_html_view: coverage_build
@cmake --build $(SRC_DIR)/build/coverage_build --target coverage_html_view

# Cleanup
# --------------------------------------------------------------------
.PHONY: clean distclean

clean:
@rm -rf $(SRC_DIR)/build

distclean:
@git clean -ff -x -d .

17 changes: 11 additions & 6 deletions EKF/tests/ringbuffer/CMakeLists.txt → test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2015-2018 ECL Development Team. All rights reserved.
# Copyright (c) 2019 ECL Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -31,9 +31,14 @@
#
############################################################################

add_executable(ecl_EKF_tests_ringbuffer ringbuffer.cpp)
target_link_libraries(ecl_EKF_tests_ringbuffer ecl_EKF)
include(gtest.cmake)

add_test(NAME ecl_EKF_tests_ringbuffer
COMMAND ecl_EKF_tests_ringbuffer
)
set(SRCS
main.cpp
test_EKF_basics.cpp
)
add_executable(ECL_GTESTS ${SRCS})

target_link_libraries(ECL_GTESTS gtest_main ecl_EKF)

add_test(NAME ECL_GTESTS COMMAND ECL_GTESTS)
19 changes: 19 additions & 0 deletions test/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 2.8.4)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
URL https://github.com/google/googletest/archive/8b6d3f9c4a774bef3081195d422993323b6bb2e0.zip
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
# Wrap download, configure and build steps in a script to log output
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
)

50 changes: 50 additions & 0 deletions test/gtest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
############################################################################
#
# Copyright (c) 2019 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

# Download and unpack googletest at configure time
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
if(result1 OR result2)
message(FATAL_ERROR "Preparing googletest failed: ${result1} ${result2}")
endif()

# Add googletest, defines gtest and gtest_main targets
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)

# Remove visibility.h from the compile flags for gtest because of poisoned exit()
get_target_property(GTEST_COMPILE_FLAGS gtest COMPILE_OPTIONS)
list(REMOVE_ITEM GTEST_COMPILE_FLAGS "-include")
list(REMOVE_ITEM GTEST_COMPILE_FLAGS "visibility.h")
set_target_properties(gtest PROPERTIES COMPILE_OPTIONS "${GTEST_COMPILE_FLAGS}")

2 changes: 1 addition & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
std::cout << "init test \n";
std::cout << "Run ECL gtests" << std::endl;
return RUN_ALL_TESTS();
}
Loading

0 comments on commit cac5f3f

Please sign in to comment.