%% Using Command Line Functions to Run Test Cases
% This demonstration shows how to use Simulink Design Verifier command
% line functions to run generated test cases programmatically.

%   Copyright 2007-2013 The MathWorks, Inc.

%% The sldvruntest Example Model
% This demo uses the model |sldvdemo_runtest|, which
% contains one input variable and one parameter. 
% The parameter indicates
% the control mode, and may be either 1 or 2.  

modelName = 'sldvdemo_runtest';
open_system(modelName);

%% Simulink Design Verifier Options
% Simulink Design Verifier functions use options objects created with the
% |sldvoptions| function to control all aspects of analysis and output.
%
% In this example, we will run Simulink Design Verifier in test generation
% mode for a maximum of 300 seconds.  We will disable the report generation
% and harness model creation, since we will be running the generated test
% cases on the original model.
%
% The default values of the remaining options are set correctly to generate 
% tests. You can use the |get| command to display all the options and 
% values.

opts = sldvoptions(modelName);
opts.Mode = 'TestGeneration';
opts.MaxProcessTime = 300;
opts.SaveHarnessModel = 'off';
opts.SaveReport = 'off';

get(opts)

%% Run Test Generation from the Command Line
% The |sldvrun| function generates test cases and saves them to a data
% file.  

[status, filenames] = sldvrun(modelName, opts);

%% Load and View Test Cases
% We can load the data generated by Simulink Design Verifier from the data
% file directly.  This data structure contains all of the generated test
% cases, including parameter values.  

load(filenames.DataFile);
sldvData.TestCases(1)
sldvData.TestCases(1).paramValues

%% Run All the Test Cases Without Coverage
sldvruntest(modelName, filenames.DataFile, [], false); 

%% Load and Run Individual Test Cases
% We can also use the |sldvruntest| command to run individual test cases.
% For example, we can look at cumulative model coverage on all test cases where
% the "control_mode" parameter is 1.  

%%
% First, we turn on cumulative coverage on the original model:
set_param(modelName,'RecordCoverage','on');
set_param(modelName,'CovCumulativeReport', 'on');

%%
% Loop through the test cases, and filter out ones whose parameter value is
% not equal to 1.
for testnum = 1:numel(sldvData.TestCases)
    for p = 1:numel(sldvData.TestCases(testnum).paramValues)
            if strcmp(sldvData.TestCases(testnum).paramValues(p).name,'control_mode')
                if sldvData.TestCases(testnum).paramValues(p).value == 1
                    sldvruntest(modelName, filenames.DataFile, testnum);
                end
            end
    end
end

%% 
% Now, to see the rest of the coverage, we can run the other test cases.
for testnum = 1:numel(sldvData.TestCases)
    sldvruntest(modelName, filenames.DataFile, testnum);
end
%%
% Now, run all the tests with coverage.
sldvruntest(modelName, filenames.DataFile, [], true);




