complexityinfo - Retrieve cyclomatic complexity coverage information from cvdata
object - MATLAB ([original](https://in.mathworks.com/help/slcoverage/ref/complexityinfo.html)) ([raw](?raw))
Retrieve cyclomatic complexity coverage information from cvdata
object
Syntax
Description
[complexity](#bswieqq-1-complexity) = complexityinfo([cvdo](#bswieqq-1%5Fsep%5Fbrxo2jj-1-cvdo),[modelObject](#bswieqq-1%5Fsep%5Fbswieqq-1-object))
returns complexity coverage results from the cvdata
objectcvdo
for the model component modelObject
.
[complexity](#bswieqq-1-complexity) = complexityinfo([cvdo](#bswieqq-1%5Fsep%5Fbrxo2jj-1-cvdo),[modelObject](#bswieqq-1%5Fsep%5Fbswieqq-1-object),[simMode](#bswieqq-1%5Fsep%5Fbswieqq-1-mode))
returns complexity coverage results from the cvdata
objectcvdo
for the model component modelObject
for the simulation mode simMode
.
Examples
This example shows how to retrieve cyclomatic complexity information for the Gain subsystem of the slvnvdemo_cv_small_controller
model.
Load the slvnvdemo_cv_small_controller
model.
modelName = 'slvnvdemo_cv_small_controller'; load_system(modelName);
Create a test specification object and enable decision, condition, and MCDC coverage. Then, simulate the model using cvsim
.
testObj = cvtest(modelName); testObj.settings.decision = 1; testObj.settings.condition = 1; testObj.settings.mcdc = 1; covData = cvsim(testObj);
Retrieve cyclomatic complexity information for the Gain subsystem.
gainPath = [modelName,'/Gain']; gainComplexity = complexityinfo(covData,gainPath)
The Gain subsystem itself does not record cyclomatic complexity, but the contents of the subsystem do. This can be seen in the results because the total complexity is 1, which includes the subsystem and all of its descendants. In contrast, the local complexity is 0, indicating that the one point of complexity comes from one of the descendants, in this case a Switch block.
switchPath = [modelName,'/Gain/Switch']; switchComplexity = complexityinfo(covData,switchPath)
Open the example and load the model.
openExample("slcoverage/RetrieveCodeCoverageResultsExample");Use the
setupSILCoverage
function to configure the model for code coverage analysis. The output is aSimulink.SimulationInput
object.
simIn = setupSILCoverage("slvnvdemo_counter_harness");Simulate the model.
To retrieve cyclomatic complexity for a function in the generated code, use a cell array that contains the generated source file name and function name. For example, retrieve the cyclomatic complexity for the function
slvnvdemo_counter
located in the source fileslvnvdemo_counter.c
.
complexitySILFunction = complexityinfo(covDataSIL,...
{"slvnvdemo_counter.c","slvnvdemo_counter"})
complexitySILFunction =3 3
The function slvnvdemo_counter
has a cyclomatic complexity of 3.
5. Similarly, retrieve the cyclomatic complexity for the entire system by using only the main generated file, slvnvdemo_counter.c
. In this case, you do not need to use a cell array.
complexitySILSystem = complexityinfo(covDataSIL,...
"slvnvdemo_counter.c")
complexitySILSystem =
4 0
The first number represents the cyclomatic complexity of the object and its descendants, so the system has a cyclomatic complexity of 4. The second number represents cyclomatic complexity of only the object or file given as the input argument, so the file slvnvdemo_counter.c
does not contribute complexity to the system. This makes sense, because the source file is constructed of functions, and the functions report their own cyclomatic complexity.
- Open the example and load data into the workspace.
openExample("slcoverage/RetrieveSFunCoverageResultsExample");
load slcoverage_lct_data.mat - Use the
setupSFcnCoverage
function to configure the model for coverage analysis. The output is aSimulink.SimulationInput
object.
simIn = setupSFcnCoverage("slcoverage_lct_bus"); - Simulate the model.
- To retrieve cyclomatic complexity for an S-function, use the
complexityinfo
function and specify a cell array that contains the handle or full path of the block, followed by the file name of the S-function source file. Optionally, you can also specify a specific function inside that source file. For this example, retrieve the cyclomatic complexity for the function namedcounterbusFcn
in the S-function filecounterbus.c
, which the S-Function blockslcoverage_sfun_counterbus
calls. The S-Function block is inside theTestCounter
subsystem in the modelslcoverage_lct_bus
.
covDataSFcn = simOut.covdata;
complexitySFun = complexityinfo(covDataSFcn,...
{"slcoverage_lct_bus/TestCounter/slcoverage_sfun_counterbus",...
"counterbus.c","counterbusFcn"})
Input Arguments
Coverage data, specified as a cvdata
object.
Data Types: cvdata
Model object, specified as a character array, string array, Simulink handle, Stateflow ID, or cell array.
To specify a model object, such as a block or a Stateflow chart, use one of these formats:
Object Specification | Description |
---|---|
BlockPath | Full path to a model or block |
BlockHandle | Handle to a model or block |
slObj | Handle to a Simulink API object |
sfID | Stateflow ID |
sfObj | Handle to a Stateflow API object from a singly instantiated Stateflow chart |
{BlockPath,sfID} | Cell array with the path to a Stateflow chart or atomic subchart and the ID of an object contained in that chart or subchart |
{BlockPath,sfObj} | Cell array with the path to a Stateflow chart or subchart and a Stateflow object API handle contained in that chart or subchart |
{BlockHandle,sfID} | Cell array with a handle to a Stateflow chart or atomic subchart and the ID of an object contained in that chart or subchart |
To specify an S-Function block or its contents, use one of these formats:
Object Specification | Description |
---|---|
{BlockPath,fileName} | Cell array with the path to an S-Function block and the name of a source file |
{BlockHandle,fileName} | Cell array with an S-Function block handle and the name of a source file |
{BlockPath,fileName,functionName} | Cell array with the path to an S-Function block, the name of a source file, and a function name |
{BlockHandle,fileName,functionName} | Cell array with an S-Function block handle, the name of a source file, and a function name |
To specify a code coverage result, such as coverage data collected during software-in-the-loop (SIL) or processor-in-the-loop (PIL) analysis, use one of these formats:
Object Specification | Description |
---|---|
{fileName,functionName} | Cell array with the name of a source file and a function name |
{Model,fileName} | Cell array with a model name or model handle and the name of a source file |
{Model,fileName,functionName} | Cell array with a model name or model handle, the name of a source file, and a function name |
Data Types: char
| string
| cell
| Stateflow.State
| Stateflow.Transition
Simulation mode during coverage analysis, specified as one of these options:
Object Specification | Description |
---|---|
"Normal" | Model in normal simulation mode. |
"SIL" or "PIL" | Model in software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation mode. |
"ModelRefSIL" or "ModelRefPIL" | Model reference in SIL or PIL simulation mode. |
"ModelRefTopSIL" or "ModelRefTopPIL" | Model reference in SIL or PIL simulation mode with the code interface set to top model. |
Data Types: char
| string
Output Arguments
Cyclomatic complexity, returned as a two-element array of the form[total_complexity,local_complexity]
if cvdo contains cyclomatic complexity coverage results, or an empty array if it does not.
total_complexity | Cyclomatic complexity coverage for modelObject and its descendants (if any) |
---|---|
local_complexity | Cyclomatic complexity coverage formodelObject |
If modelObject
has variable-size signals,complexity
also contains the variable complexity.
Data Types: double
Alternatives
Use the coverage settings to collect and display cyclomatic complexity coverage results in the coverage report:
- Open the model.
- In the Simulink Editor, select Model Settings on theModeling tab.
- On the Coverage pane of the Configuration Parameters dialog box, select Enable coverage analysis.
- Under Coverage metrics, select MCDC as the structural coverage level.
- Click OK to close the Configuration Parameters dialog box and save your changes.
- Simulate the model.
- In the docked Coverage Details pane, the coverage report shows the cyclomatic complexity for the model as well as each model object.
Version History
Introduced in R2011a