In this example, you record coverage for a model which contains a combination of code blocks and other Simulink® blocks.
Open the model.
open_system('ex_cc_cruise_control_doublepress_sfun');
The model is a cruise control system that consists of test cases
and input signals from a Signal Builder block. The
signals from the Signal Builder act as inputs to the Stateflow® chart ComputeTargetSpeed
,
which engages or disengages the cruise control system and sets the
target speed, tspeed
.
In the Simulink Editor, select Model Settings on the
Modeling tab. Before setting code coverage options, on the
Code Generation pane in the Configuration
Parameters dialog box, set the System target file in
the Target selection menu to
ert.tlc
. Navigate to the
Verification tab of the Code
Generation pane. From the Code coverage for SIL
or PIL tab, select None (use Simulink
Coverage)
as the code coverage tool.
In the Coverage pane, set the options for coverage calculated during simulation.
Select Enable coverage analysis.
In the Include in analysis section, ensure that C/C++ S-Functions is selected.
In the Coverage metrics section, select Modified Condition
Decision (MCDC)
as the Structural
coverage level. Apply the changes by clicking
Apply.
Open the RejectDoublePress
S-Function Builder block. In the Build
options of the Build Info tab, select
Enable support for coverage. To build the
S-Function, click Build .
Note
To build the S-Function, you must have a compiler installed. For more information on supported compilers for various platforms, see Supported and Compatible Compilers.
Open the Signal Builder block.
open_system('ex_cc_cruise_control_doublepress_sfun/Signal Builder');
The Signal Builder consists of eight signal groups with
five signals each. In this example, we simulate all the signal groups and
record coverage. Click
Run all and produce coverage to start recording
coverage. At the end of the simulation, the Coverage Results Explorer opens,
showing the results for the latest coverage analysis. The blocks in the
model are highlighted in different colors corresponding to the level of
coverage achieved by each block.
The Coverage Results Explorer offers several options for displaying
and reporting coverage results. Select the Not_Engaged_with_Enable
group
in the Current Cumulative Data tab of the left
pane. Click the Generate report link at
the bottom of the Coverage Results Explorer to generate an HTML coverage
report in the built-in MATLAB® web browser. The coverage report
lists model coverage for Simulink model blocks and code coverage
for code blocks.
Scroll down to view the coverage metrics for the S-Function block in the coverage report. Click the Detailed Report link to open the code coverage report for the S-Function block. For more details on the code coverage report for S-Function blocks, seeView Coverage Results for Custom C/C++ Code in S-Function Blocks.
In this example, we justify coverage for one input signal group by creating a coverage filter.
In the code coverage report for the S-Function block created in Review Results by Generating a Coverage Report, scroll down to
Decision/Condition 2.1 !(CoastSetSwIn[0] &&
AccelResSwIn[0])
. This condition is never False
for
the current test case. We can therefore justify this condition in our coverage
analysis.
Click the Justify or Exclude link
under the detailed results for this condition. The Filter tab
of the Coverage Results Explorer opens, and the rule filtering this
transition is added. Change the Mode for this
rule to Justified
and enter a description
for the Rationale, such as “expression
cannot be false”. Click Apply to apply
the changes.
After you click Apply, the Generate
report link becomes available. Click the link to generate the
report with the updated coverage filter. The new code coverage report for
the RejectDoublePress
S-Function block lists the excluded condition under
Objects Filtered from Coverage Analysis. The
detailed results for the condition !(CoastSetSwIn[0] &&
AccelResSwIn[0])
show that missing coverage for this condition
has been justified. The justified objects are treated as satisfied when
reporting coverage percentages and appear light blue in the Coverage Summary.
For more information on coverage filters, see Coverage Filtering.
Coverage for Custom C/C++ Code in Simulink Models | Creating and Using Coverage Filters | Types of Coverage Reports