What I'm going to do next is to introduce a few additional
coverage criteria using a slightly more complex example, but still a
pretty simple one. What I'm showing here is a program that
reads two real numbers, x and y. And then, if x
is equal to 0 or y is greater than 0, it
computes y as y divided by x. Otherwise, it computes x
as y plus 2, then it writes the value of x,
the value of y, and it terminates. Let's also introduce a CFG
for the program. As you can see, the CFG represents the statements
in the code and their control flow. And in this case, I made
explicit over the branches what are the conditions under which those branches
are taken to make it simpler to look at the example. Let's assume
that we have two tests for this code that are shown here.
For the first one, the inputs are 5 and 5. For the second
one, 5 and minus 5. If we consider branch coverage for this code
and we consider the two test cases, for the first one this condition
is true. Because x is not equal to 0 but y
is greater than 0. And therefore, we will follow this tree branch.
For the second one, the condition is false. Because x is not
equal to 0 and y is not greater than 0. Therefore, the
negation of it is true and we will follow this branch. In
other words, these two test cases achieve 100% branch coverage on this
code. If we look at the code though, we can see that
there is the possibility of making this code fail. Consider this statement,
if x is equal to 0, we could have a division
by 0. However, these two test cases, despite the fact that
they achieved 100% branch coverage, will not rebuild this problem. So
how can we be more thorough? I'll let you think about it
for a second, so think about how can we test more
thoroughly, in a more complete way, this code. So, in a way
that goes beyond branch coverage. And the answer is that we
can make each condition true and false. Instead of just considering the
whole predicate here. And that's exactly what is required by
the next criteria that we're going to consider which is condition
coverage. We're going to define it as usual in terms of
test requirements and coverage measure. In this case, the test
requirements for condition coverage are the individual conditions in the
program. So we want each condition in the program to
be both true and false first time execution. So the
way in which we can measure that is by measuring the
number of conditions that were both true and false
when we executed our tests over the total number
of conditions. And that gives us the percentage of
coverage that we achieved for condition coverage. Again, if you
want to look at this criteria in the form
of a question. The question would be, has each boolean
sub-expression, which means every condition in every predicate, evaluated
both to true and false when we run our tests.