4 ms·
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
by vrighter 3y ago
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
}
- abricq 3y agoOf course code coverage should not be seen as the only way to measure testing. But should it be systematically disregarded ? I rather see it as one of the tool for QAs to know where they stand at : 5% code coverage vs 85% is still an interesting information to have.
- vrighter 3y ago100% coverage just gives you a false sense of security. I'd rather just avoid the metric completely
- Veserv 3y agoWhy does it give a false sense of security? It is not like >99% coverage is the finish line; it is the starting line, the bare minimum. That is like saying you get a false sense of security when a handgun does not pierce your newly developed tank armor.
- pjmlp 3y agoBecause being green on the QA dashboard doesn't mean the unit test makes any sense.
- mathgladiator 3y agoI aim for 100% not for the security but to know my code can be played with and poked at any level.