Skip to content

Commit fecf574

Browse files
committed
cmake and tests: enable and provide test coverage analysis
Using lcov and genhtml and a little shell script we are now able to generate test coverage results with a single command (assuming one has a build available with GCC)
1 parent 839a370 commit fecf574

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/doc/build
33
/build*
44
/dist
5+
/coverage
6+
*.info
57

68
# Created by http://www.gitignore.io
79

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ set(pfasst_DEPENDEND_LIBS)
7676

7777
if(pfasst_BUILD_TESTS)
7878
enable_testing()
79-
if(pfasst_WITH_GCC_PROF)
80-
add_to_string_list("${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS "-ftest-coverage")
81-
endif(pfasst_WITH_GCC_PROF)
8279
endif(pfasst_BUILD_TESTS)
8380

8481
# Add / include 3rd-party libraries
@@ -108,6 +105,7 @@ if(pfasst_BUILD_TESTS)
108105
endif()
109106
message(STATUS "********************************************************************************")
110107

108+
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")
111109
message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
112110
message(STATUS "C++ Compiler Names: ${CMAKE_CXX_COMPILER_NAMES}")
113111
message(STATUS "C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")

generate_coverage.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/sh
2+
basepath=`pwd`
3+
4+
function print_help {
5+
echo "#######################################################################"
6+
echo "### Generation of Test Coverage Report ###"
7+
echo "# #"
8+
echo "# This only works for builds made with GCC and the following CMake #"
9+
echo "# variables: #"
10+
echo "# -Dpfasst_WITH_GCC_PROF=ON -Dpfasst_BUILD_TESTS=ON #"
11+
echo "# #"
12+
echo "# First (and only) parameter must be the name of the build directory #"
13+
echo "# #"
14+
echo "# Example: #"
15+
echo "# ./generate_coverage.sh build_gcc #"
16+
echo "# #"
17+
echo "#######################################################################"
18+
return 0
19+
}
20+
21+
22+
if [[ $# -ne 1 ]]
23+
then
24+
print_help
25+
echo "ERROR: Please name the build directory as the first parameter."
26+
exit -1
27+
fi
28+
29+
builddir=${1}
30+
cd ${builddir}
31+
32+
rm -rf ${basepath}/coverage
33+
mkdir -p ${basepath}/coverage
34+
35+
for testdir in `find ${basepath}/${builddir} -type d | grep -o 'tests/.*/.*\dir'`
36+
do
37+
testname=`expr "$testdir" : '.*\(test_[a-zA-Z\-_]*\)\.dir'`
38+
echo "Gathering Coverage for ${testname}"
39+
cd $testdir
40+
lcov --zerocounters --directory .
41+
cd ${basepath}/${builddir}
42+
ctest -R $testname
43+
cd $testdir
44+
lcov --directory . --capture --output-file ${testname}.info.tmp
45+
lcov --extract ${testname}.info.tmp "*${basepath}/include/**/*" --output-file ${testname}.info
46+
rm ${testname}.info.tmp
47+
cd ${basepath}/${builddir}
48+
lcov --add-tracefile ${testdir}/${testname}.info --output-file all_tests.info
49+
done
50+
51+
cd ${basepath}
52+
genhtml --output-directory ./coverage \
53+
--demangle-cpp --num-spaces 2 --sort \
54+
--title "PFASST++ Test Coverage" --prefix ${basepath}/include \
55+
--function-coverage --branch-coverage --legend ${basepath}/${builddir}/all_tests.info

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ foreach(test ${TESTS})
1919
set_target_properties(${test}
2020
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests
2121
)
22+
if(pfasst_WITH_GCC_PROF AND ${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
23+
set_target_properties(${test}
24+
PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage -fprofile-arcs"
25+
LINK_FLAGS "-fprofile-arcs"
26+
)
27+
endif()
2228
add_test(NAME ${test}
2329
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/${test} --gtest_output=xml:${test}_out.xml
2430
)

tests/examples/advection_diffusion/CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ foreach(test ${TESTS})
3030
set_target_properties(${test}
3131
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/examples/advection_diffusion
3232
)
33+
if(pfasst_WITH_GCC_PROF AND ${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
34+
set_target_properties(${test}
35+
PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage -fprofile-arcs"
36+
LINK_FLAGS "-fprofile-arcs"
37+
)
38+
endif()
3339
add_test(NAME ${test}
3440
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/examples/advection_diffusion/${test} --gtest_output=xml:${test}_out.xml
3541
)
@@ -44,8 +50,15 @@ if(${pfasst_WITH_MPI})
4450
if(NOT FFTW_FOUND)
4551
add_dependencies(${test} googlemock fftw3)
4652
endif()
47-
if(MPI_COMPILE_FLAGS)
48-
set_target_properties(${test} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
53+
if(MPI_COMPILE_FLAGS)
54+
if(pfasst_WITH_GCC_PROF AND ${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
55+
set_target_properties(${test}
56+
PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS} -ftest-coverage -fprofile-arcs"
57+
LINK_FLAGS "-fprofile-arcs"
58+
)
59+
else()
60+
set_target_properties(${test} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
61+
endif()
4962
endif()
5063
if(MPI_LINK_FLAGS)
5164
set_target_properties(${test} PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")

tests/examples/scalar/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ foreach(test ${TESTS})
2020
set_target_properties(${test}
2121
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/examples/scalar
2222
)
23+
if(pfasst_WITH_GCC_PROF AND ${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
24+
set_target_properties(${test}
25+
PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage -fprofile-arcs"
26+
LINK_FLAGS "-fprofile-arcs"
27+
)
28+
endif()
2329
add_test(NAME ${test}
2430
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/examples/scalar/${test} --gtest_output=xml:${test}_out.xml
2531
)

0 commit comments

Comments
 (0)