Main Content

Analyze Coverage Data Using A Script

This example shows how to load, parse, and query coverage data using a script.

Load Coverage Data

Load the model, then use the helper script setupCoverage. This script creates a simulation scenario with coverage enabled. Use this to simulate the model and generate a Simulink.SimulationOutput object that contains coverage results.

load_system('slvnvdemo_ratelim_harness');
setupCoverage
simOut = sim(covSet);
covData = simOut.covData;

Extract Information from Coverage Data Objects

Retrieve coverage information from a block path or block handle by using decisioninfo. The output is a vector with the achieved and total outcomes for a single model object.

subsysCov = decisioninfo(covData,...
                   'slvnvdemo_ratelim_harness/Adjustable Rate Limiter')
subsysCov =

     5     6

Determine the percentage coverage achieved by using decisioninfo.

percentCov = 100 * (subsysCov(1)/subsysCov(2))
percentCov =

   83.3333

Specify that you want to extract the decision coverage data for the switch block called Apply Limited Gain by using decisioninfo. This returns a structure which contains the decisions and outcomes.

[blockCov,desc] = decisioninfo(covData, ...
         ['slvnvdemo_ratelim_harness/Adjustable Rate Limiter'...
                                       '/Apply limited gain']);
descDecision = desc.decision;
outcome1 = desc.decision.outcome(1)
outcome2 = desc.decision.outcome(2)
outcome1 = 

  struct with fields:

               text: 'false (out = in3)'
     executionCount: 0
         executedIn: []
         isFiltered: 0
        isJustified: 0
    filterRationale: ''


outcome2 = 

  struct with fields:

               text: 'true (out = in1)'
     executionCount: 101
         executedIn: []
         isFiltered: 0
        isJustified: 0
    filterRationale: ''

From the decisioninfo output, you can see that the switch block called Apply Limited Gain was never false because the false case executionCount field has a value of 0. If this behavior is expected, and you did not intend to execute this case with your tests, you can add a filter rule to justify this missing coverage using the slcoverage.Filter class.

First, query for the block instance to be filtered, because we only need to filter the one block instance that received incomplete coverage, and not all instances of that block type. Then use the slcoverage.BlockSelector class with the BlockInstance selector type to designate one block instance for filtering.

id = getSimulinkBlockHandle( ...
   'slvnvdemo_ratelim_harness/Adjustable Rate Limiter/Apply limited gain');
sel = slcoverage.BlockSelector( ...
    slcoverage.BlockSelectorType.BlockInstance,id);

Create a filter object and a filter rule using the slcoverage.Filter and slcoverage.FilterRule classes.

filt = slcoverage.Filter;
rule = slcoverage.FilterRule( ...
    sel,'Edge case',slcoverage.FilterMode.Justify);

Add the rule to the filter using the addRule method. Then save the new filter file with the save method.

filt.addRule(rule);
filt.save('blfilter');

To apply the filter to the coverage data, set the filter property of the cvdata object to the name of the filter file. Use decisioninfo on the filtered coverage data to see that there is now 100% decision coverage because the justified objectives are counted as satisfied.

covData.filter = 'blfilter';
newCov = decisioninfo(covData,...
                   'slvnvdemo_ratelim_harness/Adjustable Rate Limiter')
percentNewCov = 100 * (newCov(1)/newCov(2))
newCov =

     6     6


percentNewCov =

   100

See Also

| | | | | |

Related Topics