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.
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.
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'};
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/BR2020bd_1444674_32127/publish_examples0/tp92a5f7ea/ex71096464 -c /tmp/BR2020bd_1444674_32127/publish_examples0/tp07a6f3ff_e80c_4657_a505_3b1fa1b1b75d/counterbus.c -outdir /tmp/BR2020bd_1444674_32127/publish_examples0/tp92c17ea4_39ff_4f16_b802_62f3c1f14b82 Building with 'gcc'. MEX completed successfully. mex -I/tmp/BR2020bd_1444674_32127/publish_examples0/tp92a5f7ea/ex71096464 /tmp/BR2020bd_1444674_32127/publish_examples0/tp07a6f3ff_e80c_4657_a505_3b1fa1b1b75d/tpa06647c5_a31d_4a48_9a0b_456b0ec30bf1.c /tmp/BR2020bd_1444674_32127/publish_examples0/tp92c17ea4_39ff_4f16_b802_62f3c1f14b82/counterbus.o -L/mathworks/devel/bat/BR2020bd/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldemo_sfun_counterbus Building with 'gcc'. MEX completed successfully. mex -I/tmp/BR2020bd_1444674_32127/publish_examples0/tp92a5f7ea/ex71096464 -c /tmp/BR2020bd_1444674_32127/publish_examples0/tp92a5f7ea/ex71096464/counterbus.c -outdir /tmp/BR2020bd_1444674_32127/publish_examples0/tp92c17ea4_39ff_4f16_b802_62f3c1f14b82 Building with 'gcc'. MEX completed successfully. mex -I/tmp/BR2020bd_1444674_32127/publish_examples0/tp92a5f7ea/ex71096464 /tmp/BR2020bd_1444674_32127/publish_examples0/tp07a6f3ff_e80c_4657_a505_3b1fa1b1b75d/sldemo_sfun_counterbus.c /tmp/BR2020bd_1444674_32127/publish_examples0/tp07a6f3ff_e80c_4657_a505_3b1fa1b1b75d/tp6c47652e_7e2d_42b4_b92c_816ca22ae044.c /tmp/BR2020bd_1444674_32127/publish_examples0/tp07a6f3ff_e80c_4657_a505_3b1fa1b1b75d/tp4c65d9c7_90f9_4fc9_9734_f160bd509a1c.c /tmp/BR2020bd_1444674_32127/publish_examples0/tp92c17ea4_39ff_4f16_b802_62f3c1f14b82/counterbus.o -L/mathworks/devel/bat/BR2020bd/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldemo_sfun_counterbus Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_counterbus ### Exit
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',... 'CovSFcnEnable', 'on'... );
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);
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
.