-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
11 changes: 11 additions & 0 deletions
11
chapter07/coverage_example/include/coverage_example/coverage_example.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
25 changes: 25 additions & 0 deletions
25
chapter07/coverage_example/src/coverage_example/coverage_example.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |