Block coverage

Code Coverage for Java

When analyzing Java source code, Code Coverage can provide the following block coverage:

Statement Blocks

Statement blocks are the Java method blocks, blocks introduced by control instructions:

Example

public class StatementBlocks

{

  public static void func( String _message )

  throws UnsupportedOperationException

  {

    throw new UnsupportedOperationException(_message);

  }

  public static void main( String[] args )

  throws Exception

  {

    try {

      if ( false )

      {

        func( "Hello" );

      }

      else

      {

        throw new Exception("bad luck");

      }

    }

    catch ( UnsupportedOperationException _E )

    {

      System.out.println( _E.toString() );

    }

    catch ( Exception _E )

    {

      System.out.println( _E.toString() );

      throw _E ;

    } //potentially terminal statement

    return ; //sequence block

  }

}

 

Each simple block is a branch. Every Java 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 statement without a DEFAULT statement.

Example

public class MathOp

{

  static final int WHITE=0;

  static final int LIGHTGRAY=1;

  static final int RED=2;

  static final int PINK=3;

  static final int BLUE=4;

  static final int GREEN=5;

  // power of 10

  public static int powerOf10( int _value, int _max )

  {

    int result = _value, i;

    if( _value==0 ) return 0; //implicit else

    for( i = 0; i < 10; i++ )

    {

      result = ( _max / 10 ) < result ? 10*result : _max ;

    }

    return result;

  }

  // Near color function

  int nearColor( int _color )

  {

    switch( _color )

    {

      case WHITE:

      case LIGHTGRAY:

        return WHITE ;

      case RED:

      case PINK:

        return RED;

      //implicit default:

    }

    return _color ;

  }

}

 

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:

Example

public class LogicalBlocks

{

  public static int tryFiveTimes()

  {

    int result, i=0;

    while ( ( ( result=resourcesAvailable() )<= 0)

         && ( ++i < 5 ) );

    // while define 3 logical blocks

    return result;

  }

  public static int resourcesAvailable()

  {

    return (_free_resources_++);

  }

 

  public static int _free_resources_=0;

  public static void main( String[] argv )

  {

    //first call: '0 loop' block is reach

    _free_resources_=1;

    tryFiveTimes();

    //second call: '1 loop' blocks are reach

    _free_resources_=0;

    tryFiveTimes();

    //third call: '2 loops or more' blocks are reach

    _free_resources_=-10;

    tryFiveTimes();

  }

}

Related Topics

Selecting coverage typesCode Coverage settings