Types of Coverage for Generated C/C++ Code in Equivalence Tests - MATLAB & Simulink (original) (raw)

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

where:

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

where:

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 executesfoo() 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

where:

Condition Coverage Example

Consider this expression:

The expression has two conditions:

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

where:

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:

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

where:

Modified Condition/Decision Coverage Example

For this decision:

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

| | X | Y | Z | | | ------------ | - | - | - | | Test case #1 | 0 | 0 | 1 | | Test case #2 | 0 | 1 | 0 | | Test case #3 | 0 | 1 | 1 | | Test case #4 | 1 | 0 | 1 |

In order to demonstrate that the conditions Y andZ 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 andZ do not affect the decision outcome.

See Also

Classes

Topics