Block coverage

Code Coverage for C

When running the Code Coverage feature on C source code, OneTest Embedded can provide the following coverage types for code blocks:

Statement Blocks (or Simple Blocks)

Simple blocks are the C function main blocks, blocks introduced by decision instructions:

/* 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.

Decisions (Implicit Blocks)

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.

Loops (Logical Blocks)

A typical FOR or WHILE loop can reach three different conditions:

In a DO...WHILE loop, because the output condition is tested after the block has been executed, two further branches are created:

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 TypesAbout Code CoverageCode Coverage settings