Main Content

Types of Coverage for Generated C/C++ Code in Equivalence Tests

When you run C/C++ equivalence tests using software-in-the-loop (SIL) or processor-in-the-loop (PIL) verification with Embedded Coder®, you can collect code coverage information for the generated C/C++ code. For more information about generated C/C++ equivalence tests, see Generate C/C++ Code and Test for Equivalence.

You can access the coverage information programmatically or as a code coverage report. For more information, see Collect Coverage for Generated C/C++ Code in Equivalence Tests.

Statement Coverage

Statement coverage measures the number of generated C/C++ code statements that execute when the equivalence test runs. Use this type of coverage to determine whether every statement in the program has been invoked at least once.

To calculate the percentage of statement coverage, use the equation

C=ESTS×100,

where:

  • C represents the statement coverage percentage.

  • ES represents the number of executed statements.

  • TS represents the total number of statements.

Statement Coverage Example

This code contains five statements:

if (x > 0) {
  printf( "x is positive" );
} else if (x < 0) {
  printf( "x is negative" );
} else{
  printf( "x is 0" );
}
To achieve 100% statement coverage, you need at least one test with positive x values, one test with negative x values, and one test with x values of zero.

Function Coverage

Function coverage measures the number of unique functions in the generated C/C++ code that execute when the equivalence test runs. Use this type of coverage to determine whether every function in the program has been invoked at least once.

To calculate the percentage of function coverage, use the equation

C=EFTF×100,

where:

  • C represents the function coverage percentage.

  • EF represents the number of executed functions.

  • TF represents the total number of functions.

Function Coverage Example

This code contains three functions:

int func1(){return 1;}
int func2(){return 2;}

int foo(int x)
{
    if (x > 0)
        return func1(); 
    else if (x < 0)
        return func2();
    else
        return func1();
}
To achieve 100% function coverage, you need one test that executes foo() with a positive x value, which executes func1(). You also need one test that executes foo() with a negative x value, which executes func2().

Condition Coverage

Condition coverage analyzes statements that include conditions in the generated C/C++ code when the equivalence test runs. 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.

To calculate the percentage of condition coverage, use the equation

C=ECTC×100,

where:

  • C represents the condition coverage percentage.

  • EC represents the number of executed conditions.

  • TC represents the total number of conditions.

Condition Coverage Example

Consider this expression:

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

The expression has two conditions:

x<=5
x!=7

To achieve 100% condition coverage, your equivalence tests need to demonstrate a true and false outcome for both conditions. For example, a test where x is equal to 4 demonstrates a true case for both conditions, and a case where x is equal to 7 demonstrates a false case for both conditions.

Decision Coverage

Decision coverage analyzes statements that represent decisions in the generated C/C++ code when the equivalence test runs. Decisions are Boolean expressions composed of conditions and one or more of the logical C/C++ operators && or ||. Conditions within branching constructs, such as if, else, while, and 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, are tested.

To calculate the percentage of decision coverage, use the equation

C=EDTD×100,

where:

  • C represents the decision coverage percentage.

  • ED represents the number of executed decisions.

  • TD represents the total number of decisions.

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" );

To achieve 100% decision coverage, your test cases must demonstrate a true and false outcome for each decision.

Modified Condition/Decision Coverage (MC/DC)

Modified condition/decision coverage (MC/DC) analyzes whether the conditions within decisions independently affect the decision outcome during execution. To achieve 100% MC/DC, your test cases must demonstrate:

  • 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.

To calculate the percentage of MC/DC, use the equation

C=ECDTCD×100,

where:

  • C represents the MC/DC percentage.

  • ECD represents the number of conditions evaluated to all possible outcomes affecting the outcome of the decision.

  • TCD represents the total number of conditions within the decisions.

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

In order to demonstrate that the conditions Y and Z can independently affect the decision outcome, the condition X must be false for those test cases. If the condition X is true, then the decision is already known to be true. Therefore, the conditions Y and Z do not affect the decision outcome.

See Also

Classes

Related Topics