Model Coverage for MATLAB Functions - MATLAB & Simulink (original) (raw)

Simulink® Coverage™ analyzes your model and reports model coverage data when you simulate a model with coverage enabled. If your model contains MATLAB® code, such as inside a MATLAB Function block or a call to an external MATLAB function, then Simulink Coverage analyzes that code for the metrics that you select.

Collecting Model Coverage for MATLAB Functions

To analyze your MATLAB Function block or external MATLAB function for coverage, you must first ensure that the MATLAB code inside the block or function is compatible with code generation. For more information about configuring MATLAB code for code generation, see Identify Entry-Point Functions and Check MATLAB Code (MATLAB Coder).

For example, consider the following if statement:

if (x > 0 || y > 0) reset = 1;

The if statement contains a decision with two conditions (x > 0 and y > 0). Simulink Coverage analyzes each decision and condition during the simulation of the model and reports how many times each outcome occurs. For example, if the input signal forx is true at every simulation time step, it reports 50% condition coverage for that condition because the false case does not occur.

When collecting coverage for MATLAB functions, consider:

Types of Model Coverage for MATLAB Functions

The types of model coverage that Simulink Coverage analyzes for MATLAB functions are:

Decision Coverage

During simulation, Simulink Coverage analyzes these MATLAB statements and reports them as decision coverage:

Condition and MCDC Coverage

When you collect condition and MCDC coverage, Simulink Coverage analyzes if statement conditions and logical expressions in assignment statements.

Logical expressions are expressions that contain a logical operator, such as the logical AND (&&) or the logicalOR (||). Model coverage does not analyze simple logical assignments such as:

Or single condition assignment statements, such as:

These MATLAB functions are active in code generation and in Simulink Design Verifier:

When you select the Objectives and Constraints coverage metric in the Coverage pane of the Configuration Parameters dialog box, Simulink Coverage analyzes these functions.

Each of these functions evaluates an expression, such assldv.test(_`expr`_), where_expr_ is a valid Boolean MATLAB expression. Simulink Design Verifier coverage measures the number of time steps that the expression_expr_ evaluates to true.

If expr is true for at least one time step, Simulink Design Verifier coverage for that function is 100%. Otherwise, the reported coverage for that function is 0%.

For an example of coverage data for Simulink Design Verifier functions in a coverage report, see Simulink Design Verifier Coverage.

Saturation on Integer Overflow Coverage

When you select the Saturate on integer overflow parameter,Simulink Coverage analyzes statements in the MATLAB function that contain the integer datatype.

Saturate on integer overflow coverage records the number of times the statement saturates on integer overflow. A test case achieves full coverage when the logical statement saturates on integer overflow at least once and does not saturate at least once.

Relational Boundary Coverage

You can collect relational boundary coverage for MATLAB Function blocks that contain at least one relational operation.

If the MATLAB Function block calls functions containing relational operations multiple times, the relational boundary coverage reports a cumulative result over each instance where the function is called. If a relational operation in the function uses operands of different types in the different calls, relational boundary coverage uses tolerance rules for the stricter operand type. For example, if a relational operation uses int32 operands in one call anddouble operands in another call, relational boundary coverage uses tolerance rules for double operands.

For information on the tolerance rules and the order of strictness of types, seeRelational Boundary Coverage.

Due to run-time optimizations, dead logic using hard-coded constant inputs is not analyzed for relational boundary coverage. For example, consider the function:

function out = myFun(x,y) if nargin < 2 y = 3; end

This function can take one or two input arguments and sets a default if the caller did not provide the second argument. If your model always provides two input arguments when calling this function, the contents of the if statement is dead logic because the if statement is false at every time step. In this case,Simulink Coverage does not analyze the statement for relational boundary coverage.

See Also

cvdata

Topics