diff --git a/chapter07/coverage_example/CMakeLists.txt b/chapter07/coverage_example/CMakeLists.txt new file mode 100644 index 0000000..c609041 --- /dev/null +++ b/chapter07/coverage_example/CMakeLists.txt @@ -0,0 +1,27 @@ +# CMakeLists file for the Chapter 7. Example to show how to create a custom build type for a coverage build +# +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.23) + +project( + "ch7_coverage_example" + VERSION 1.0 + DESCRIPTION "A simple C++ project to demonstrate generating coverage information" + LANGUAGES CXX) +#include CTest module for defining tests later +include(CTest) + + +# Add an example library and executable to illustrate the coverage report +add_library(ch7_coverage_example STATIC) +target_include_directories(ch7_coverage_example PUBLIC include) +target_compile_features(ch7_coverage_example PUBLIC cxx_std_11) +target_sources(ch7_coverage_example PRIVATE src/coverage_example/coverage_example.cpp) + + +add_executable(ch7_coverage_example_test src/coverage_test.cpp) +target_link_libraries(ch7_coverage_example_test ch7_coverage_example) + +target_compile_features(ch7_coverage_example_test PRIVATE cxx_std_17) +add_test(NAME code_coverage_test COMMAND ch7_coverage_example_test) \ No newline at end of file diff --git a/chapter07/coverage_example/include/coverage_example/coverage_example.h b/chapter07/coverage_example/include/coverage_example/coverage_example.h new file mode 100644 index 0000000..5d74d45 --- /dev/null +++ b/chapter07/coverage_example/include/coverage_example/coverage_example.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace coverage_example +{ + /// this function checks if a number is dividable by the prime numbers between + /// 1 and 10 and returns the prime numbers as vcector + std::vector check_for_divisibilty(int i); + +} // namespace coverage_example diff --git a/chapter07/coverage_example/src/coverage_example/coverage_example.cpp b/chapter07/coverage_example/src/coverage_example/coverage_example.cpp new file mode 100644 index 0000000..7991713 --- /dev/null +++ b/chapter07/coverage_example/src/coverage_example/coverage_example.cpp @@ -0,0 +1,25 @@ +#include +#include + +namespace coverage_example { +std::vector check_for_divisibilty(int i) { + + std::vector result; + + if (i % 2 == 0) { + result.push_back(2); + } + if (i % 3 == 0) { + result.push_back(3); + } + if (i % 5 == 0) { + result.push_back(5); + } + if (i % 7 == 0) { + result.push_back(7); + } + + return result; +} + +} // namespace coverage_example \ No newline at end of file diff --git a/chapter07/coverage_example/src/coverage_test.cpp b/chapter07/coverage_example/src/coverage_test.cpp new file mode 100644 index 0000000..361814d --- /dev/null +++ b/chapter07/coverage_example/src/coverage_test.cpp @@ -0,0 +1,16 @@ +#include +#include + +#include +#include + +int main(int, char **) { + + auto result = coverage_example::check_for_divisibilty(15); + assert(std::find(result.begin(), result.end(), 2) == result.end()); + assert(std::find(result.begin(), result.end(), 3) != result.end()); + assert(std::find(result.begin(), result.end(), 5) != result.end()); + assert(std::find(result.begin(), result.end(), 7) == result.end()); + + return 0; +} \ No newline at end of file