Skip to content

Commit

Permalink
Add coverage example
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Jul 3, 2024
1 parent da6ef81 commit 4d15697
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
27 changes: 27 additions & 0 deletions chapter07/coverage_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vector>

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<int> check_for_divisibilty(int i);

} // namespace coverage_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <coverage_example/coverage_example.h>
#include <vector>

namespace coverage_example {
std::vector<int> check_for_divisibilty(int i) {

std::vector<int> 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
16 changes: 16 additions & 0 deletions chapter07/coverage_example/src/coverage_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <vector>
#include <algorithm>

#include <coverage_example/coverage_example.h>
#include <cassert>

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;
}

0 comments on commit 4d15697

Please sign in to comment.