Simulink Code Coverage Metrics

If you have a Simulink® Coverage™ license, you can run a SIL or PIL simulation that produces code coverage metrics for generated model code. The simulation performs several types of code coverage analysis.

Statement Coverage for Code Coverage

Statement coverage determines the number of source code statements that execute when the code runs. Use this type of coverage to determine whether every statement in the program has been invoked at least once.

Statement coverage = (Number of executed statements / Total number of statements) *100

Statement Coverage Example

This code snippet contains five statements. To achieve 100% statement coverage, you need at least three test cases. Specifically, tests with positive x values, negative x values, and x values of zero.

if (x > 0)
  printf( "x is positive" );
else if (x < 0)
  printf( "x is negative" );
else
  printf( "x is 0" );

Condition Coverage for Code Coverage

Condition coverage analyzes statements that include conditions in source code. Conditions are C/C++ Boolean expressions that contain relation operators (<, >, <=, or >=), equation operators (!= or ==), or logical negation operators (!), but that do not contain logical operators (&& or ||). This type of coverage determines whether every condition has been evaluated to all possible outcomes at least once.

Condition coverage = (Number of executed condition outcomes / Total number of condition outcomes) *100

Condition Coverage Example

In this expression:

y = x<=5 && x!=7;

there are these conditions:

x<=5
x!=7

Decision Coverage for Code Coverage

Decision coverage analyzes statements that represent decisions in source code. Decisions are Boolean expressions composed of conditions and one or more of the logical C/C++ operators && or ||. Conditions within branching constructs (if/else, while, do-while) are decisions. Decision coverage determines the percentage of the total number of decision outcomes the code exercises during execution. Use this type of coverage to determine whether all decisions, including branches, in your code are tested.

Note

The decision coverage definition for DO-178C compliance differs from the Simulink Coverage definition. For decision coverage compliance with DO-178C, select the Condition Decision structural coverage level for Boolean expressions not containing && or || operators.

Decision coverage = (Number of executed decision outcomes / Total number of decision outcomes) *100

Decision Coverage Example

This code snippet contains three decisions:

y = x<=5 && x!=7;				// decision #1

if( x > 0 )					// decision #2
  printf( "decision #2 is true" );
else if( x < 0 && y )				// decision #3
  printf( "decision #3 is true" ); 
else
  printf( "decisions #2 and #3 are false" );

Modified Condition/Decision Coverage (MCDC) for Code Coverage

Modified condition/decision coverage (MCDC) is the extent to which the conditions within decisions are independently exercised during code execution.

  • All conditions within decisions have been evaluated to all possible outcomes at least once.

  • Every condition within a decision independently affects the outcome of the decision.

MCDC coverage = (Number of conditions evaluated to all possible outcomes affecting the outcome of the decision / Total number of conditions within the decisions) *100

Modified Condition/Decision Coverage Example

For this decision:

X || ( Y && Z )

the following set of test cases delivers 100% MCDC coverage.

 XYZ

Test case #1

001

Test case #2

010

Test case #3

011

Test case #4

101

Cyclomatic Complexity for Code Coverage

Cyclomatic complexity is a measure of the structural complexity of code that uses the McCabe complexity measure. To compute the cyclomatic complexity of code, code coverage uses this formula:

c=1N(on1)

N is the number of decisions in the code. on is the number of outcomes for the nth decision point. Code coverage adds 1 to the complexity number for each C/C++ function.

Coverage Example

For this code snippet, the cyclomatic complexity is 3:

void evalNum(int x)
{
  if (x > 0)                          // decision #1
    printf( "x is positive" );
  else if (x < 0)                     // decision #2
    printf( "x is negative" );
  else
    printf( "x is 0" );
}

The code contains one function that has two decision points. Each decision point has two outcomes. Using the preceding formula, N is 2, o1 is 2, and o2 is 2. Code coverage uses the formula with these decisions and outcomes and adds 1 for the function. The cyclomatic complexity for this code snippet is:

c = (o1 − 1) + (o2 − 1) + 1 = (2 − 1) + (2 − 1) + 1 = 3

Relational Boundary for Code Coverage

Relational boundary code coverage examines code that has relational operations. Relational boundary code coverage metrics align with those for model coverage, as described in Relational Boundary Coverage (Simulink Coverage). Fixed-point values in your model are integers during code coverage.

Related Topics