MATLAB Examples

Configuring S-Function for Test Case Generation

This example shows how to compile an S-Function to be compatible with Simulink® Design Verifier™ for test case generation. SLDV supports S-Functions that are:

  • Generated with the Legacy Code Tool, with def.Options.supportCoverageAndDesignVerifier set to true,
  • Generated with the SFunctionBuilder, with Enable support for Design Verifier selected on the Build Info tab of the SFunctionBuilder dialog box, or
  • Compiled with the function slcovmex, with the option -sldv passed.


Compile S-Function to Be Compatible with Simulink® Design Verifier™

The handwritten S-Function is found in the file sldvexSFunctionHandlingSFcn.c, and the user source code for the lookup table is found in the file sldvexSFunctionHandlingSource.c. Call the function slcovmex to compile the C-MEX S-Function and make it compatible with SLDV.

slcovmex('-sldv', ...
        '-output', 'sldvexSFunctionHandlingSFcn',...
        ['-I', fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src')], ...
        fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src', 'sldvexSFunctionHandlingSource.c'),...
        fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src', 'sldvexSFunctionHandlingSFcn.c') ...
mex -I/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/sldv/sldvdemos/src /private/tmp/BR2017bd_681905_31816/tpb3a2e0dc_8f34_4e65_8ce3_665031e05e97/tp7e03e1d0_7767_440f_9ef9_96ccbc879aed.c /private/tmp/BR2017bd_681905_31816/tpb3a2e0dc_8f34_4e65_8ce3_665031e05e97/tpf4a152c9_9036_4452_ab30_ab98f225b2f9.c -L/mathworks/devel/bat/BR2017bd/build/matlab/bin/maci64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'Xcode with Clang'.
MEX completed successfully.
mex -I/mathworks/devel/bat/BR2017bd/build/matlab/toolbox/sldv/sldvdemos/src /mathworks/devel/bat/BR2017bd/build/matlab/toolbox/sldv/sldvdemos/src/sldvexSFunctionHandlingSource.c /private/tmp/BR2017bd_681905_31816/tpb3a2e0dc_8f34_4e65_8ce3_665031e05e97/sldvexSFunctionHandlingSFcn.c /private/tmp/BR2017bd_681905_31816/tpb3a2e0dc_8f34_4e65_8ce3_665031e05e97/tp263a0d53_9d50_4383_b66a_4fe37e9de947.c /private/tmp/BR2017bd_681905_31816/tpb3a2e0dc_8f34_4e65_8ce3_665031e05e97/tp064a4754_8f68_4ae8_8586_a89f6ac8977c.c -L/mathworks/devel/bat/BR2017bd/build/matlab/bin/maci64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'Xcode with Clang'.
MEX completed successfully.

Create Test Suite

The example model sldvexSFunctionHandlingExample example contains the handwritten S-Function, which implements a lookup table algorithm. The S-Function block returns the interpolated value at the first output port and returns the status of the interpolation at the second output port. The second output port returns the value -1 if a lower saturation occurs, 1 if a upper saturation occurs, and 0 otherwise. Open the sldvexSFunctionHandlingExample model and configure the analysis options by turning on S-Function support for test generation. On running the analysis, Simulink® Design Verifier™ returns a test suite that satisfies all coverage objectives.


opts = sldvoptions;
opts.Mode = 'TestGeneration';
opts.ModelCoverageObjectives = 'Condition';
opts.SaveHarnessModel = 'off';
opts.SaveReport = 'off';
opts.SFcnSupport = 'on';

[status, fileNames] = sldvrun('sldvexSFunctionHandlingExample', opts);
Checking compatibility for test generation: model 'sldvexSFunctionHandlingExample'
Compiling model...done
Checking compatibility...done

'sldvexSFunctionHandlingExample' is compatible for test generation with Simulink Design Verifier.

Generating tests using compatibility results from 25-Jul-2017 18:07:44...

Completed normally.

Generating output files:

    Data file:

Results generation completed.

Verifying Complete Coverage

The sldvruntest function verifies that the test suite achieves complete model coverage. The cvhtml function produces a coverage report that indicates 100% Condition coverage is achieved with the generated test vectors.

[~, finalCov] = sldvruntest('sldvexSFunctionHandlingExample', fileNames.DataFile, [], true);
cvhtml('Final Coverage', finalCov);

Clean Up

To complete the demo, close all models.

close_system('sldvexSFunctionHandlingExample', 0);