%% Configuring S-Function for Test Case Generation
% This example shows how to compile an S-Function to be compatible with
% Simulink(R) Design Verifier(TM) 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 <matlab:help('slcovmex') slcovmex>, with the option |-sldv| passed.

%   Copyright 2015 The MathWorks, Inc.

%% Compile S-Function to Be Compatible with Simulink(R) Design Verifier(TM)
% The handwritten S-Function is found in the file 
% <matlab:sldvexSFunctionHelper('edit','sldvexSFunctionHandlingSFcn.c') sldvexSFunctionHandlingSFcn.c>,
% and the user source code for the lookup table is found in the file
% <matlab:sldvexSFunctionHelper('edit','sldvexSFunctionHandlingSource.c') sldvexSFunctionHandlingSource.c>.
% Call the function <matlab:help('slcovmex') 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') ...
        )
        
%% Create Test Suite
% The example model <matlab:sldvexSFunctionHandlingExample 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(R) Design Verifier(TM) 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';
opts.MaxProcessTime = 2*opts.MaxProcessTime;

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

%% Verifying Complete Coverage
% The <matlab:doc('sldvruntest') |sldvruntest|> function verifies that the test suite achieves complete 
% model coverage. The <matlab:doc('cvhtml') |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);


displayEndOfDemoMessage(mfilename)
