Section 10.20

Branch coverage

Definition

Branch coverage is a coverage metric §10.18 whose elements are the possible outcomes of a program’s decisions. A decision is a point where the program chooses which code runs next, such as the condition of an if or a loop, or a switch. An if has two outcomes, true and false; a switch has one per distinct target. Branch coverage is the fraction of these outcomes that some test produced. Many texts call it decision coverage. Some tools count each condition inside a compound decision separately.

The idea

A message handler should accept a message only if its length is valid and its tag is valid: if (len_ok && tag_ok) accept();. The programmer typed ||, the or operator, instead of &&, the and operator, so a message with a valid length and a forged tag is accepted. Two tests check the handler. Test 1 has both conditions true and expects acceptance; test 2 has both false and expects rejection. With ||, test 1 accepts and test 2 rejects, so both tests pass. The decision came out true once and false once, branch coverage is 100%, and the bug survives.

The two tests changed both conditions at once, so neither condition was shown to matter on its own. The stronger criterion MC/DC (modified condition/decision coverage) requires, for each condition, a pair of tests in which only that condition changes and the outcome changes with it: here, one test with only len_ok false and one with only tag_ok false. Both expect rejection, and the faulty || version accepts both, so each of them finds the bug.

How it works

Branch coverage is measured in three steps.

  1. List the decisions and their outcomes. An if has two outcomes. A loop condition has two: entering the body and leaving the loop. A switch has one outcome for each distinct case target, plus the default, or, when no default is written, the outcome in which no case matches.

  2. Instrument. The compiler adds a counter to each outcome.

  3. Count. Branch coverage is the number of outcomes with a nonzero count, divided by the number of outcomes.

A condition is a Boolean expression that contains none of the Boolean operators && (and), || (or) and ! (not), such as len_ok; a decision is built from one or more conditions. The criteria for compound decisions, from weaker to stronger, are these.

The aviation software standard DO-178C (Software Considerations in Airborne Systems and Equipment Certification) requires MC/DC for software of its Level A, software whose failure could cause or contribute to a catastrophic failure condition for the aircraft. It requires decision coverage for Level B and statement coverage for Level C.

Short-circuit evaluation changes what can be observed. In C, a && b evaluates b only when a is true, and a || b evaluates b only when a is false, so the compiler emits a separate jump for each condition. Tools therefore differ in what they call a branch. Clang’s §12.17 source-based coverage counts the true and false outcomes of each condition, four outcomes for a && b; a tool that counts decision outcomes counts two. The same tests can receive different branch-coverage figures from different tools.

Worked example

Counting. A function contains an if (2 outcomes), a while loop (2 outcomes) and a switch with three case labels and a default (4 outcomes), so it has 2+2+4=82 + 2 + 4 = 8 outcomes. If the tests never make the if condition false and never reach the default, they produce 8−2=68 - 2 = 6 outcomes, and branch coverage is 6/8=0.756 / 8 = 0.75, or 75%.

The compound decision. Take if (len_ok && tag_ok) with test t1 = (len_ok true, tag_ok true) and test t2 = (both false).

Now add t3 = (len_ok false, tag_ok true) and t4 = (len_ok true, tag_ok false); both expect false. The pair t1, t3 differs only in len_ok, and the outcome changes from true to false. The pair t1, t4 differs only in tag_ok, and the outcome changes in the same way. The three tests t1, t3 and t4 achieve MC/DC, which is n+1=3n + 1 = 3 tests for n=2n = 2 conditions; t2 is no longer needed. Every combination would take 22=42^2 = 4 tests. Against the faulty || version, t3 and t4 both return true where false is expected, so the bug is found. The gap widens quickly: for n=10n = 10 independent conditions, unique-cause MC/DC needs at least 11 tests, every combination 210=1,0242^{10} = 1{,}024.

Stated precisely

Let DD be the set of decisions in the measured code, and for each decision dd let O(d)O(d) be its set of possible outcomes. Then branch coverage=|{(d,o):d∈D,o∈O(d),some test made d take outcome o}|∑d∈D|O(d)|,\text{branch coverage} = \frac{|\{(d, o) : d \in D,\ o \in O(d),\ \text{some test made } d \text{ take outcome } o\}|}{\sum_{d \in D} |O(d)|}, where |S||S| is the number of elements of a set SS.

DO-178C defines MC/DC as four requirements that a set of tests must meet together: every point of entry and exit in the program has been invoked at least once; every decision has taken all possible outcomes at least once; every condition in a decision has taken all possible outcomes at least once; and each condition in a decision has been shown to independently affect that decision’s outcome. A condition is shown to independently affect the outcome by varying just that condition while holding fixed all other conditions, or all other conditions that could affect the outcome. When unique-cause MC/DC can be achieved for a decision with nn conditions, it needs at least n+1n + 1 tests.

Limits and common mistakes

Branch coverage counts the outcomes of decisions, not the effect of each condition, as the handler with || shows. Nor is it path coverage: decisions in sequence combine, and covering each decision’s outcomes separately can miss the one combination that fails.

Even every combination of every decision shows only that each combination ran. It does not show that the code inside a taken branch computed the right result, avoided undefined behavior §4.12 or met its deadline. A test oracle §10.5 has to check those.

Compilers and tools disagree on what a branch is. In compiled code, short-circuit operators and the conditional operator ?: become jumps, and optimization can remove jumps by turning them into conditional-move instructions. Compare figures only from the same tool and the same build.

A condition that can never independently affect its decision makes MC/DC impossible for that condition. In (a && b) || (a && !b), the outcome always equals a, so no pair of tests can show an effect of b. That failure is useful: it reveals a redundant or mistaken condition.

A defensive branch that can never be taken, such as a check for an error the hardware never reports, keeps the figure below 100%. The right response is to list it as an exclusion with its reason, not to delete the check.

Questions answered

Does each condition inside a compound branch independently affect the outcome?

Not necessarily, and branch coverage cannot tell. Branch coverage requires only that each decision as a whole came out true and false at least once. For if (len_ok && tag_ok), the two tests (both conditions true) and (both false) reach 100% branch coverage but do not test what happens when the second condition is evaluated and false. A faulty version that uses or (||) instead of and (&&) passes both tests.

That each condition independently affects the outcome is what MC/DC (modified condition/decision coverage) demands. For each condition, MC/DC needs a pair of tests that changes only that condition, holds the other conditions fixed, and changes the outcome of the decision; a masking variant also lets other conditions change where they cannot affect the outcome. For the example, the tests (true, true), (false, true) and (true, false) do this, n+1=3n + 1 = 3 tests for n=2n = 2 conditions, and they catch the faulty or. When a pair is infeasible, look at which inputs the program can actually receive. If two condition occurrences read the same Boolean, a test cannot change one occurrence while holding the other fixed. That is a constraint on the test pair. Deciding whether the code is redundant takes a separate argument.

Related entries: Coverage metric (§10.18), Boundary-value testing (§10.31), Coverage versus correctness (§10.40), Edge coverage (§10.19), Test oracle (§10.5).