Main Content

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. Simulink Design Verifier 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 Simulink Design Verifier.

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/Bdoc23a/build/matlab/toolbox/sldv/sldvdemos/src /tmp/Bdoc23a_2213998_2720337/tp9456024d_a425_46a6_a0ca_fc924fc3c02f/tp8cf2f422_b903_4a52_a64b_9c4ce155569f.c /tmp/Bdoc23a_2213998_2720337/tp9456024d_a425_46a6_a0ca_fc924fc3c02f/tp487647d3_d648_4898_a549_53e7402a2ea4.c -L/mathworks/devel/bat/Bdoc23a/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'gcc'.
MEX completed successfully.
mex -I/mathworks/devel/bat/Bdoc23a/build/matlab/toolbox/sldv/sldvdemos/src /mathworks/devel/bat/Bdoc23a/build/matlab/toolbox/sldv/sldvdemos/src/sldvexSFunctionHandlingSource.c /tmp/Bdoc23a_2213998_2720337/tp9456024d_a425_46a6_a0ca_fc924fc3c02f/sldvexSFunctionHandlingSFcn.c /tmp/Bdoc23a_2213998_2720337/tp9456024d_a425_46a6_a0ca_fc924fc3c02f/tp80fe908b_50cc_4e96_be62_0935ab518d67.c /tmp/Bdoc23a_2213998_2720337/tp9456024d_a425_46a6_a0ca_fc924fc3c02f/tpaf9bda5c_81e2_47b1_bc45_9eb20066756e.c -L/mathworks/devel/bat/Bdoc23a/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'gcc'.
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 = 'ConditionDecision';
opts.SaveHarnessModel = 'off';
opts.SaveReport = 'off';
opts.SFcnSupport = 'on';

[status, fileNames] = sldvrun('sldvexSFunctionHandlingExample', opts, true);

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 and Decision 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);