Function
Has each function been called?
Any single call is enough. This level finds the functions that were never called, and nothing more.
Function, statement, decision, condition, MC/DC, multiple condition: each level asks a more demanding question than the one before. This guide compares them on the same C example, test by test, to show what each one guarantees and what it lets through.
Coverage levels rest on two words that need to be told apart.
A condition is an elementary Boolean expression, with no logical operator: temp > 90, or simply manual.
A decision is a Boolean expression made of one or more conditions joined by logical operators (&&, ||, !): (temp > 90 && pressure > 5) || manual. It chooses the program's path, in an if, a while or a for.
When a decision contains only one condition, all the levels come together: testing it true, then false, is enough. The differences between levels appear with compound decisions.
A monitoring function raises an alarm when both temperature and pressure are too high, or when the operator requests it.
Its decision contains three conditions:
temp > 90;pressure > 5;manual.The table gives the five tests used in the rest of this guide.
int alarm(int temp, int pressure, int manual)
{
int level = 0;
if ((temp > 90 && pressure > 5) || manual)
level = 2;
return level;
}
| Test | Call | A | B | C | Decision |
|---|---|---|---|---|---|
| T1 | alarm(95, 6, 0) | T | T | – | true |
| T2 | alarm(80, 6, 0) | F | – | F | false |
| T3 | alarm(95, 4, 0) | T | F | F | false |
| T4 | alarm(95, 4, 1) | T | F | T | true |
| T5 | alarm(80, 6, 1) | F | – | T | true |
T: true. F: false. –: condition not evaluated, because of short-circuit evaluation.
For each level: the smallest set of tests that reaches 100% on the alarm function, and what it lets through.
Has each function been called?
Any single call is enough. This level finds the functions that were never called, and nothing more.
Has each statement been executed?
T1 makes the decision true and executes level = 2: every statement is executed.
What gets through: the case where the alarm is not raised has never been tested.
Has each decision been true, then false?
T1 makes the decision true, T2 makes it false. This level includes the previous one: when every decision takes both values, every statement is executed.
What gets through: B has never been false, nor C true. The role of the pressure and that of the manual request have not been tested.
Has each condition been true, then false?
A is true in T1 and false in T2; B is true in T1 and false in T4; C is false in T2 and true in T4.
What gets through: nothing shows that the pressure changes the outcome. If the code ignored the pressure, these three tests would still pass. And in general this level does not even include decision coverage, which is why it is often combined with it, as condition/decision coverage.
Has each condition been shown to change the outcome of the decision on its own?
For each condition, you need two tests in which only that condition changes value, and the decision changes too: A with T1 and T2, B with T1 and T3, C with T3 and T4. Four tests are enough, that is N + 1 for N conditions. MC/DC stands for Modified Condition/Decision Coverage. The method, the variants and the pitfalls: the MC/DC guide.
What changes: T3 raises an alarm if the code ignores the pressure. The defect that escaped the previous levels is detected.
Have all combinations of conditions been tested?
Three conditions give 23 = 8 combinations. In C, because of short-circuit evaluation, only five can occur: T1 to T5.
Its cost: it is the most thorough level, but the number of cases doubles with each added condition.
In C and C++, a decision is not always evaluated in full.
The && and || operators stop as soon as the result is known. If the temperature is low, the pressure is not read: A && B is false whatever happens. If A && B is true, manual is not read: the decision is true.
In T1, C is not evaluated; in T2 and T5, B is not. A condition that is not evaluated is neither true nor false: it does not count for condition coverage.
Of the 8 combinations of A, B and C, only 5 can be told apart at run time. Multiple condition coverage is measured on these 5 cases.
In MC/DC pairs, the other conditions must keep their value. Between T1 and T2, B is not evaluated in T2, but the pressure passed to the function stays the same. The variants of MC/DC differ in how they handle this case.
For writing tests, short-circuit evaluation changes little. For measuring, it matters: two tools that handle it differently give different percentages on the same code.
From the weakest level to the most demanding, on the alarm function:
| Level | Tests | What it adds |
|---|---|---|
| Function | 1 | Finds the functions never called. |
| Statement | 1 | Finds the code never executed. |
| Decision | 2 | Also tests the case where the decision is false, even when no statement depends on it. |
| Condition | 3 | Tests each condition with both values, without showing its effect on the outcome. |
| MC/DC | 4 (N + 1) | Shows that each condition changes the outcome on its own. |
| Multiple condition | 5 (8 without short-circuit) | Tests every combination: up to 2N cases for N conditions. |
Each level includes the previous one, except condition coverage: 100% decision coverage implies 100% statement coverage, and MC/DC implies both decision and condition coverage. With ten conditions, MC/DC requires at least 11 tests, multiple condition coverage up to 1,024.
Other measures exist. Some are common in tools, others are mostly theoretical.
The most common in general-purpose tools. It is close to statement coverage, but one line can hold several statements, or even a whole decision.
Has each function call been executed, at every place where it appears? It is used in integration testing: ISO 26262 lists it, with function coverage, at the software architecture level.
Has each loop been run zero times, once and several times? It targets boundary errors.
Has every possible path through the function been taken? With loops, the number of paths quickly becomes infinite: this measure remains mostly theoretical.
For the most critical airborne software, DO-178C also requires analysing the code added by the compiler that is not directly traceable to the source code.
The level depends on what a failure would cost.
The standard sets the level according to criticality. DO-178C, for example, requires statement coverage at level C, adds decision coverage at level B and MC/DC at level A. The code coverage and standards guide compares the other standards.
Decision coverage is a good starting point: it catches the "else" cases that statement coverage lets through, for a moderate testing effort.
For a complex decision in a sensitive module, MC/DC checks that each condition matters, for a few more tests.
The level can also vary from one part of the code to another: nothing prevents you from requiring MC/DC on a safety module and decision coverage on the rest.
Testwell CTC++ measures every level presented in this guide on C and C++ code (Java as an option): function, statement, decision, condition, MC/DC and multiple condition.
TiCS takes in the coverage measured by Testwell CTC++ or by another coverage tool:
Almost. For an if, the two branches match the two values of the decision. The differences lie in the details: the cases of a switch, or what the tool calls a decision. DO-178C, for example, calls any Boolean expression a decision, even outside an if, as in ok = a && b;
For a decision with N conditions, between N + 1 and 2N tests. That is far fewer than the 2N combinations of multiple condition coverage: at least 11 tests instead of 1,024 for ten conditions.
No. Born in aerospace, it is also recommended by ISO 26262 for the most critical automotive functions, and listed by other functional safety standards, such as IEC 61508.
Because they do not count the same thing: lines or statements, decisions with or without the cases of a switch, conditions with or without short-circuit evaluation. See Testwell CTC++ or gcov?
while loop a decision?Yes. Its condition must be true, to enter the loop, and false, to leave it. Loop coverage goes further: zero, one and several iterations.
No. This level has a cost: it is kept for code whose failure would have the most serious consequences, or for code the standard designates.
A free trial of Testwell CTC++ on your own C or C++ code, on the host or on the embedded target.
Code coverage · Testwell CTC++ · Certification · DO-178C · Standards · Learn
Source: Kelly J. Hayhurst, Dan S. Veerhusen, John J. Chilenski and Leanna K. Rierson, A Practical Tutorial on Modified Condition/Decision Coverage (NASA/TM-2001-210876, 2001) ↗.