Main Content

Coverage for S-Functions

This example shows how to configure an S-Function generated with the Legacy Code Tool to be compatible with coverage. The model coverage tool supports S-Functions that are:

  • Generated with the Legacy Code Tool, with def.Options.supportCoverage set to true,

  • Generated with the SFunctionBuilder, with Enable support for coverage selected on the Build Info tab of the SFunctionBuilder dialog box, or

  • Compiled with the slcovmex function.

Open Example Model

The example model sldemo_lct_bus contains an S-Function generated with the Legacy Code Tool. The S-Function has constructs that receive decision, condition, and MCDC coverage.

Open sldemo_lct_bus

Configure S-Function to Be Compatible with Model Coverage

The legacy source code in the files counterbus.h, and counterbus.c implements the same algorithm as in sldemo_lct_bus/slCounter. The Legacy Code Tool data structure is defined as follows:


def = legacy_code('initialize');
def.SFunctionName = 'sldemo_sfun_counterbus';
def.OutputFcnSpec = 'void counterbusFcn(COUNTERBUS u1[1], int32 u2, COUNTERBUS y1[1], int32 y2[1])';
def.HeaderFiles   = {'counterbus.h'};
def.SourceFiles   = {'counterbus.c'};

To make this S-Function compatible with model coverage, enable the following option:

def.Options.supportCoverage = true;

Generate and compile the S-Function using the legacy_code function:

legacy_code('generate_for_sim', def);
### Start Compiling sldemo_sfun_counterbus
mex -I/tmp/Bdoc20b_1465442_241683/tp0cb8b3f7/ex71096464 -c /tmp/Bdoc20b_1465442_241683/tp98b520d7_1a19_42f0_a313_0ad39157d747/counterbus.c -outdir /tmp/Bdoc20b_1465442_241683/tpec33374d_f544_47b3_8161_b0167a77339d
Building with 'gcc'.
MEX completed successfully.
mex -I/tmp/Bdoc20b_1465442_241683/tp0cb8b3f7/ex71096464 /tmp/Bdoc20b_1465442_241683/tp98b520d7_1a19_42f0_a313_0ad39157d747/tp2f91b4ec_1a15_4ec4_a213_5d383577a96e.c /tmp/Bdoc20b_1465442_241683/tpec33374d_f544_47b3_8161_b0167a77339d/counterbus.o -L/mathworks/devel/bat/Bdoc20b/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldemo_sfun_counterbus
Building with 'gcc'.
MEX completed successfully.
mex -I/tmp/Bdoc20b_1465442_241683/tp0cb8b3f7/ex71096464 -c /tmp/Bdoc20b_1465442_241683/tp0cb8b3f7/ex71096464/counterbus.c -outdir /tmp/Bdoc20b_1465442_241683/tpec33374d_f544_47b3_8161_b0167a77339d
Building with 'gcc'.
MEX completed successfully.
mex -I/tmp/Bdoc20b_1465442_241683/tp0cb8b3f7/ex71096464 /tmp/Bdoc20b_1465442_241683/tp98b520d7_1a19_42f0_a313_0ad39157d747/sldemo_sfun_counterbus.c /tmp/Bdoc20b_1465442_241683/tp98b520d7_1a19_42f0_a313_0ad39157d747/tp56e7e9a9_a150_4041_96fc_52dace90e826.c /tmp/Bdoc20b_1465442_241683/tp98b520d7_1a19_42f0_a313_0ad39157d747/tp47c942ce_b9de_4d38_b3d9_6ab84bbe6534.c /tmp/Bdoc20b_1465442_241683/tpec33374d_f544_47b3_8161_b0167a77339d/counterbus.o -L/mathworks/devel/bat/Bdoc20b/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldemo_sfun_counterbus
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling sldemo_sfun_counterbus
### Exit

Enable S-Function Coverage

To enable coverage collection for S-Functions, select C/C++ S-Functions in the Coverage pane of the Configurations Parameters dialog box. Alternatively, set the option through the command line:

          'CovMetricSettings', 'dcme',...
          'RecordCoverage', 'on',...
          'CovSFcnEnable', 'on'...

Run Simulation and Produce Coverage Report

Once you enable coverage data collection, coverage information is automatically recorded when you simulate the model. At the end of the simulation, you can generate an HTML report of coverage information, which is displayed in the built-in MATLAB® web browser.

sim('sldemo_lct_bus', 'StopTime', '20');
cvhtml('coverageResults', covdata);

Extract Information from Coverage Data Objects

The cvdata object can be used to extract coverage information for S-Functions, just like any other supported model element. For instance, the decisioninfo command extracts coverage information from a block path or a block handle. The output is a vector containing the satisfied and total outcomes for a single model object.

cov = decisioninfo(covdata, 'sldemo_lct_bus/TestCounter/sldemo_sfun_counterbus')
cov =

     3     4

You then use this coverage information to calculate the percentage of covered model objects:

percentCov = 100 * (cov(1)/cov(2))
percentCov =


S-Function coverage is fully compatible with the model coverage commands, such as decisioninfo, conditioninfo, and mcdcinfo.