bool check_index_in_range(const std::vector<int>&v, size_t index) {
// BUG ON NEXT LINE: SHOULD BE < NOT <=
if(index <= v.size()) {
// BRANCH A
std::cout << "access was safe" << std::endl;
return true;
}
else {
// BRANCH B
std::cout << "access was unsafe" << std::endl;
return false;
}
}
void main()
{
auto v = std::vector<int>();
v.push_back(1);
v.push_back(2);
v.push_back(3);
// Assertion passes, prints that the access is safe.
// Exercises branch A
assert(check_index_in_range(v,1));
// Assertion passes, prints that the access is unsafe
// Exercises branch B
assert(!check_index_in_range(v,5));
// We now have 100% code coverage
// but still failed to catch a trivial bug.
// Even though the buggy line was actually tested twice.
// Code coverage is not a good way to measure testing
}