Block coverage

Code Coverage for C++

When analyzing C++ source code, Code Coverage can provide the following block coverage types:

Statement Blocks

Statement blocks are the C++ function or method main blocks, blocks introduced by decision instructions:

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.

Decisions (Implicit Blocks)

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.

Loops (Logical Blocks)

Three branches are created in a for or while loop:

Two branches are created in a DO/WHILE loop, as the output condition is tested after the block has been executed:

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

Selecting coverage typesCode Coverage settings