Documentation

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 function slcovmex.

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:

load_system('sldemo_lct_bus');
open_system('sldemo_lct_bus/TestCounter');

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'};
def.IncPaths      = {'sldemo_lct_src'};
def.SrcPaths      = {'sldemo_lct_src'};

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/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src -I/tmp/BR2017bd_694400_188374/publish_examples0/tp664be03a -c /tmp/BR2017bd_694400_188374/publish_examples0/tp29821a8f_b81a_4f6d_9cd5_799c940fed0f/counterbus.c -outdir /tmp/BR2017bd_694400_188374/publish_examples0/tp48eb98bb_f400_4807_acb4_cc2571e4a985
Building with 'gcc'.
MEX completed successfully.
mex -I/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src -I/tmp/BR2017bd_694400_188374/publish_examples0/tp664be03a /tmp/BR2017bd_694400_188374/publish_examples0/tp29821a8f_b81a_4f6d_9cd5_799c940fed0f/tp2c0b3526_258e_4388_8fec_a94b1e9b5e03.c /tmp/BR2017bd_694400_188374/publish_examples0/tp48eb98bb_f400_4807_acb4_cc2571e4a985/counterbus.o -L/mathworks/devel/bat/BR2017bd/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldemo_sfun_counterbus
Building with 'gcc'.
MEX completed successfully.
mex -I/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src -I/tmp/BR2017bd_694400_188374/publish_examples0/tp664be03a -c /mathworks/devel/bat/BR2017bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src/counterbus.c -outdir /tmp/BR2017bd_694400_188374/publish_examples0/tp48eb98bb_f400_4807_acb4_cc2571e4a985
Building with 'gcc'.
MEX completed successfully.
mex -I/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src -I/tmp/BR2017bd_694400_188374/publish_examples0/tp664be03a /tmp/BR2017bd_694400_188374/publish_examples0/tp29821a8f_b81a_4f6d_9cd5_799c940fed0f/sldemo_sfun_counterbus.c /tmp/BR2017bd_694400_188374/publish_examples0/tp29821a8f_b81a_4f6d_9cd5_799c940fed0f/tpc5f1edef_d208_44ae_804b_0a5376ac9700.c /tmp/BR2017bd_694400_188374/publish_examples0/tp29821a8f_b81a_4f6d_9cd5_799c940fed0f/tpbc3740b1_7a88_4bd2_a76e_56bf29ae0533.c /tmp/BR2017bd_694400_188374/publish_examples0/tp48eb98bb_f400_4807_acb4_cc2571e4a985/counterbus.o -L/mathworks/devel/bat/BR2017bd/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:

set_param('sldemo_lct_bus',...
          'CovMetricSettings', 'dcme',...
          'RecordCoverage', 'on',...
          'CovHtmlReporting', '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, the model coverage tool produces an HTML report of coverage information, which is displayed in the built-in MATLAB® web browser.

sim('sldemo_lct_bus', 'StopTime', '20');

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 =

    75

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

Clean Up

To complete the demo, close all models.

close_system('sldemo_lct_bus',0)
Was this topic helpful?