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 totrue
,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 slcoverage_lct_bus contains an S-Function generated with the Legacy Code Tool. The S-Function has constructs that receive decision, condition, and MCDC coverage.
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 slcoverage_lct_bus/slCounter. The Legacy Code Tool data structure is defined as follows:
load_system('slcoverage_lct_bus'); open_system('slcoverage_lct_bus/TestCounter'); load slcoverage_lct_data.mat def = legacy_code('initialize'); def.SFunctionName = 'slcoverage_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 slcoverage_sfun_counterbus mex -I/tmp/Bdoc25a_2874102_1427903/tpc3a144bd/slcoverage-ex71096464 -c /tmp/Bdoc25a_2874102_1427903/tpaa576e36_dbcb_492a_b7e0_23cca4abfb00/counterbus.c -outdir /tmp/Bdoc25a_2874102_1427903/tp31066f36_f8e7_4b17_95aa_d47b2ced2ac1 Building with 'gcc'. MEX completed successfully. mex -I/tmp/Bdoc25a_2874102_1427903/tpc3a144bd/slcoverage-ex71096464 /tmp/Bdoc25a_2874102_1427903/tpaa576e36_dbcb_492a_b7e0_23cca4abfb00/tp6e3bf6eb_7942_48d7_8a9d_74da6efefdc4.c /tmp/Bdoc25a_2874102_1427903/tp31066f36_f8e7_4b17_95aa_d47b2ced2ac1/counterbus.o -L/mathworks/devel/bat/filer/batfs2566-0/Bdoc25a.2874102.Bdoc25a.2873005.pass/build/runnable/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output slcoverage_sfun_counterbus Building with 'gcc'. MEX completed successfully. mex -I/tmp/Bdoc25a_2874102_1427903/tpc3a144bd/slcoverage-ex71096464 -c /tmp/Bdoc25a_2874102_1427903/tpc3a144bd/slcoverage-ex71096464/counterbus.c -outdir /tmp/Bdoc25a_2874102_1427903/tp31066f36_f8e7_4b17_95aa_d47b2ced2ac1 Building with 'gcc'. MEX completed successfully. mex -I/tmp/Bdoc25a_2874102_1427903/tpc3a144bd/slcoverage-ex71096464 /tmp/Bdoc25a_2874102_1427903/tpaa576e36_dbcb_492a_b7e0_23cca4abfb00/slcoverage_sfun_counterbus.c /tmp/Bdoc25a_2874102_1427903/tpaa576e36_dbcb_492a_b7e0_23cca4abfb00/tpe74aa56c_6dad_4346_849a_ab65dea5925f.c /tmp/Bdoc25a_2874102_1427903/tpaa576e36_dbcb_492a_b7e0_23cca4abfb00/tp2fd01e89_b3c0_468c_9799_3ec1a57bf73a.c /tmp/Bdoc25a_2874102_1427903/tp31066f36_f8e7_4b17_95aa_d47b2ced2ac1/counterbus.o -L/mathworks/devel/bat/filer/batfs2566-0/Bdoc25a.2874102.Bdoc25a.2873005.pass/build/runnable/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output slcoverage_sfun_counterbus Building with 'gcc'. MEX completed successfully. ### Finish Compiling slcoverage_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('slcoverage_lct_bus',... 'CovMetricStructuralLevel', 'MCDC',... 'RecordCoverage', 'on',... 'CovSFcnEnable', 'on',... 'CovSaveSingleToWorkspaceVar','on'... );
Run Simulation and Produce Coverage Report
Simulate the model to record coverage data. At the end of the simulation, use cvhtml
to generate an HTML report of coverage results, and open the report in the HTML Viewer.
sim('slcoverage_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, ... 'slcoverage_lct_bus/TestCounter/slcoverage_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
.