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 (Simulink Coverage), 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 (Simulink Coverage) to compile the C-MEX S-Function and make it compatible with Simulink Design Verifier.

slcovmex('-sldv', ...
        '-output', 'sldvexSFunctionHandlingSFcn',...
        'sldvexSFunctionHandlingSource.c','sldvexSFunctionHandlingSFcn.c');
mex /tmp/Bdoc24a_2528353_1741016/tp3365f366_95eb_4bc0_bd7a_6ebe4e379b81/tp5a52d0b2_c39e_487e_8de1_d7182a72347e.c /tmp/Bdoc24a_2528353_1741016/tp3365f366_95eb_4bc0_bd7a_6ebe4e379b81/tp0fa2f42b_6f9d_4021_91c0_b7d52a4138dc.c -L/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/bin/glnxa64 -lmwsl_sfcn_cov_bridge -output sldvexSFunctionHandlingSFcn
Building with 'gcc'.
MEX completed successfully.
mex sldvexSFunctionHandlingSource.c /tmp/Bdoc24a_2528353_1741016/tp3365f366_95eb_4bc0_bd7a_6ebe4e379b81/sldvexSFunctionHandlingSFcn.c /tmp/Bdoc24a_2528353_1741016/tp3365f366_95eb_4bc0_bd7a_6ebe4e379b81/tp65b3b1d5_12d3_45c2_9f5d_2627ce22d300.c /tmp/Bdoc24a_2528353_1741016/tp3365f366_95eb_4bc0_bd7a_6ebe4e379b81/tp703f0f77_3d30_45f7_994c_95ad703f49f5.c -L/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/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.

open_system('sldvexSFunctionHandlingExample');

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 (Simulink Coverage) 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);