Code Coverage for C
When running the Code Coverage feature on C source code, Test RealTime can provide the following coverage types for code blocks:
Statement Blocks
Statement Blocks and Decisions
Statement Blocks, Decisions, and Loops
Simple blocks are the C function main blocks, blocks introduced by decision instructions:
THEN and ELSE FOR IF
FOR, 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>)
blocks following a potentially terminal statement.
/* Power_of_10 Function */ /* -block */
int power_of_10 ( int value, int max )
{
int retval = value, i;
if ( value == 0 ) return 0; /* potentially terminal statement */
for ( i = 0; i < 10; i++ ) /* start of a sequence block */
{
retval = ( max / 10 ) < retval ? retval * 10 : max;
}
return retval;
} /* The power_of_10 function has 6 blocks */
/* 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 ... */
}
} /* The near_color function has at least 3 simple blocks */
Each simple block is a branch. Every C function contains at least one simple block corresponding to its main body.
Implicit blocks are introduced by an IF statement without an ELSE or a SWITCH statement without a DEFAULT.
/* Power_of_10 function */
/* -block=decision */
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.
Because the sum of all possible decision paths includes implicit blocks as well as statement blocks, reports provide the total number of simple and implicit blocks as a figure and as a percentage. Code Coverage places this information in the Decisions report.
A typical FOR or WHILE loop can reach three different conditions:
The statement block contained within the loop is executed zero times, therefore the output condition is True from the start
The statement block is executed exactly once, the output condition is False, then True the next time
The statement block is executed at least twice. (The output condition is False at least twice, and becomes True at the end)
In a DO...WHILE loop, because the output condition is tested after the block has been executed, two further branches are created:
The statement block is executed exactly once. The output is condition True the first time.
The statement block is executed at least twice. (The output condition is False at least once, then true at the end)
In this example, the function try_five_times ( ) must run several times to completely cover the three logical blocks included in the WHILE loop:
/* Try_five_times function */
/* -block=logical */
int try_five_times ( void )
{
int result,i =0;
/*try ()is afunction whose return value depends
on the availability of a system resource, for example */
while ( ( ( result = try ())!=0 )&&
(++i <5 ));
return result;
} /* 3 logical blocks */
Related Topics
Selecting Coverage Types | About Code Coverage | Code Coverage settings