Code Coverage for C++
When analyzing C++ source code, Code Coverage can provide the following block coverage types:
Statement Blocks
Statement Blocks and Decisions
Statement Blocks, Decisions, and Loops
Statement blocks are the C++ function or method main blocks, blocks introduced by decision instructions:
THEN and ELSE FOR IF, WHILE and DO ... WHILE blocks
non-empty blocks introduced by SWITCH CASE or DEFAULT statements
true and false outcomes of ternary expressions (<expr> ? <expr> : <expr>)
TRY blocks and any associated catch handler
blocks following a potentially terminal statement.
int main ( ) /* -BLOCK */
{
try {
if ( 0 )
{
func ( "Hello" );
}
else
{
throw UnLucky ( );
}
}
catch ( Overflow & o ) {
cout << o.String << '\n';
}
catch ( UnLucky & u ) {
throw u;
} /* potentially terminal statement */
return 0; /* sequence block */
}
Each simple block is a branch. Every C++ function and method contains at least one simple block corresponding to its main body.
Implicit blocks are introduced by IF statements without an ELSE statement, and a SWITCH statements without a DEFAULT statement.
/* Power_of_10 function */
/* -BLOCK=DECISION or -BLOCK=IMPLICIT */
int power_of_10 ( int value, int max )
{
int retval = value, i;
if ( value == 0 ) return 0; else ;
for ( i = 0; i < 10; i++ )
{
retval = ( max / 10 ) < retval ? retval * 10 : max;
}
return retval;
}
/* Near_color function */
ColorType near_color ( ColorType color )
{
switch ( color )
{
case WHITE :
case LIGHT_GRAY :
return WHITE;
case RED :
case PINK :
case BURGUNDY :
return RED;
/* etc ... with no default */
default : ;
}
}
Each implicit block represents a branch.
Since the sum of all possible decision paths includes implicit blocks as well as simple blocks, reports provide the total number of simple and implicit blocks as a figure and a percentage after the term decisions.
Three branches are created in a for or while loop:
The first branch is the simple block contained within the loop, and that is executed zero times (the entry condition is false from the start).
The second branch is the simple block executed exactly once (entry condition true, then false the next time).
The third branch is the simple block executed at least twice (entry condition true at least twice, and false at the end).
Two branches are created in a DO/WHILE loop, as the output condition is tested after the block has been executed:
The first branch is the simple block executed exactly once (output condition true the first time).
The second branch is the simple block executed at least twice (output condition false at least once, then true at the end).
/* myClass::tryFiveTimes method */ /* -BLOCK=LOGICAL */
int myClass::tryFiveTimes ()
{
int result, i = 0;
/* letsgo ( ) is a function whose return value depends
on the availability of a system resource, for example */
while ( ( ( result = letsgo ( ) ) != 0 ) &&
( ++i < 5 ) );
return result;
} /* 3 logical blocks */
You need to execute the method tryFiveTimes ( ) several times to completely cover the three logical blocks included in the while loop.
Related Topics