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.

Contents

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 -IB:\matlab\toolbox\sldv\sldvdemos\src C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp8b88e411_ece8_4940_9768_9c40965aa01a\tp29b85f7d_303b_46ab_b3b9_79221e531ff3.c C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp8b88e411_ece8_4940_9768_9c40965aa01a\tp0f0de827_6d83_450b_b4a4_8e15f476ffa5.c B:\matlab\extern\lib\win64\microsoft\libmwsl_sfcn_cov_bridge.lib -output sldvexSFunctionHandlingSFcn
Building with 'Microsoft Visual C++ 2015 (C)'.
MEX completed successfully.
mex -IB:\matlab\toolbox\sldv\sldvdemos\src B:\matlab\toolbox\sldv\sldvdemos\src\sldvexSFunctionHandlingSource.c C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp8b88e411_ece8_4940_9768_9c40965aa01a\sldvexSFunctionHandlingSFcn.c C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp8b88e411_ece8_4940_9768_9c40965aa01a\tp2f1bf573_8907_4983_b497_0f019a1fc116.c C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp8b88e411_ece8_4940_9768_9c40965aa01a\tp0321a88b_abf1_4450_92b5_45c9d52cc5aa.c B:\matlab\extern\lib\win64\microsoft\libmwsl_sfcn_cov_bridge.lib -output sldvexSFunctionHandlingSFcn
Building with 'Microsoft Visual C++ 2015 (C)'.
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 = '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 09-Jan-2018 05:06:01...
.............
Generating output files:

    Data file:
    C:\TEMP\BR2018ad_784271_7884\ib4B39A9\8\tp994b3ec5\sldv_output\sldvexSFunctionHandlingExample\sldvexSFunctionHandlingExample_sldvdata.mat

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