-2

Given the following fragment of code, how many tests are required for 100% decision coverage?

if width > length then 
    biggest_dimension = width 
    if height > width then
        biggest_dimension = height 
    end_if 
else 
    biggest_dimension = length    
    if height > length then 
        biggest_dimension = height    
    end_if    
end_if

Answer to the above question is 4 . 4 test cases are required for 100% decision coverage. Can anyone tell the answer explanation for the following question?

sanal pillai
  • 21
  • 1
  • 1

3 Answers3

6

control flow

I've constructed a control flow for your code to help with the explanation.

It would take 4 tests in order to achieve 100% decision coverage as both "True" and "False" sides of the "IF" statements have to be exercised by the code. The code written has an "IF" statement on each side of the first "IF" statement, requiring 4 different tests in order to reach each decision made.

Example tests:

  • Test 1: Width = 50cm, Length = 40cm, Height = 40cm

  • Test 2: Width = 50cm, Length = 40cm, Height = 60cm

From these two tests, the decision coverage would now be at 50%, as half of the decision outcomes have been exercised (the "True" side of the first "IF").

  • Test 3: Width = 40cm, Length = 50cm, Height = 40cm

  • Test 4: Width = 40cm, Length = 50cm, Height = 60cm

With these two tests, the "False" side of the first "IF" is exercised. This would make the decision coverage 100%.

DBrooks
  • 61
  • 2
0

There are four test cases because the inner if statements will only be accessible if the outer statements are true, therefore there can only be four combinations:

  • outer = true, inner = true
  • outer = true, inner = false
  • outer = false, inner = true
  • outer = false, inner = false

When the outer statement is false it will move to your else statement.

Hope this makes sense.

0

100% Decision coverage

enter image description here

The code traverses 4 paths/decisions for 100% Decision coverage.

This happens when the decision statements are nested. In this case

"height > width" with execute only if "width > length" is true.

"height > length" with execute only if "width > length" is false.

Hence the minimum number of test cases required to cover 100% decision testing is 4.


100% Statement Coverage

For this same piece of code you can achieve 100% Statement Coverage with just 2 sets of inputs,

Case 1.

Height = 10
Width = 8
Length = 5

In this case,

  • if width > length will be true. Hence next statement biggest_dimension = width will be executed.
  • if height > width will also be true. Hence next statement biggest_dimension = height will be executed.

Case 2.

Height = 7
Width = 5
Length = 6

In this case,

  • if width > length will be false. Hence next statement biggest_dimension = length will be executed.
  • if height > length will be true. Hence next statement biggest_dimension = height will be executed.

So you need a minimum of 2 tests for 100% Statement Coverage.

IAmMilinPatel
  • 413
  • 1
  • 11
  • 19