%% Determine Individual Integer Overflow Outcomes
% This example shows how to use |overflowsaturationinfo| to determine
% whether or not integer overflow occurs for a block in a model.

% Copyright 2020 The MathWorks, Inc.

%% Generate coverage data
% Load the |slvnvdemo_saturation_on_overflow_coverage| example model.

load_system('slvnvdemo_saturation_on_overflow_coverage');

%%
% Set coverage setting using a |Simulink.SimulationInput| object. Turn
% coverage on and configure Simulink(R) to output a |cvdata| object into
% the workspace.

covSet = Simulink.SimulationInput('slvnvdemo_saturation_on_overflow_coverage');
covSet = covSet.setModelParameter('CovEnable','on');
covSet = covSet.setModelParameter('CovSaveSingleToWorkspaceVar','on');
covSet = covSet.setModelParameter('CovSaveName','covData');
covSet = covSet.setModelParameter('CovScope','EntireSystem');

%% Extract saturation on integer overflow results
% Retrieve saturation on integer overflow coverage results and description
% for the |Sum| block in the |Controller| subsystem of the |Test Unit|
% subsystem.

covSet = covSet.setModelParameter('CovMetricSaturateOnIntegerOverflow','on');
simOut = sim(covSet);
[covResults, covDesc] = overflowsaturationinfo(covData, ...
		['slvnvdemo_saturation_on_overflow_coverage/Test Unit /' ...
		'Controller/Sum'])
percentSaturation = 100 * covResults(1)/covResults(2)

%%
% One out of two saturation on integer overflow decision outcomes were
% satisfied for the Sum block, so it received 50% saturation on integer
% overflow coverage.
%

%%
% Review the number of times the |Sum| block evaluated to each saturation
% on integer overflow outcome during simulation.
%

outcome1 = covDesc.decision.outcome(1)
outcome2 = covDesc.decision.outcome(2)

%%
% During simulation, integer overflow did _not_ occur in the |Sum| block
% because the |'true'| outcome has an execution count of |0|.
%

%%
% If integer overflow is not possible for a block in your model, consider
% clearing the *Saturate on integer overflow* block parameter to optimize
% efficiency of your generated code.
%