4

When checking statement coverage for my code:

 double programme(double x, double y)
 {
    double z 
    if(x>=5){
      z = 15;
    }
    else if(x>=3){
      z= 10;
    }
    else {
      z=0;
    }
    if (y>z)
    {
      z=y;
    }

    return z;
}

using two test cases (eg test 1: x = 6, y = 10 and test 2: = 3, y =5)

I'm not sure if the statement coverage is equal to 100% or 66% based on the fact that I'm not sure if you count the last if statement as it is false both times.

Cormac Hallinan
  • 187
  • 1
  • 12

1 Answers1

4

There are eight statements in your method - three conditionals, four assignments, and a return:

  1. if (x>=5)
  2. z=15
  3. if (x>=3)
  4. z=10
  5. z=0
  6. if (y>z)
  7. z=y
  8. return

The first test case covers statements 1, 2, 6, and 8. The second test case covers 1, 3, 4, 6, and 8. Therefore, statements 1, 2, 3, 4, 6, and 8 are covered, for 6 out of 8 or 75% coverage.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Is that not branch coverage though? – Cormac Hallinan Mar 22 '15 at 20:08
  • 1
    @CormacHallinan In this case it's close, because each branch has a single statement. Statement coverage could get squeed, though, when a single covered branch contains lots of statements, while several non-covered branches have a single statement each. – Sergey Kalinichenko Mar 22 '15 at 20:37
  • I'm not 100% sure what you mean by this. Could you give an example based on the code I made up? – Cormac Hallinan Mar 22 '15 at 20:39
  • 1
    @CormacHallinan Imagine that the first branch of `if(x>=5)` has 92 additional assignment statements. Now the code has 100 statements, with 98 statements covered, for an extremely healthy 98% coverage. Branch coverage, however, does not change at all - it remains 60% (3 out of 5 branches are covered), so counting branch coverage gives you a better measurement of code coverage. – Sergey Kalinichenko Mar 22 '15 at 20:45