Main Content

Test Generation for Custom Code in MATLAB Function Block

Simulink® Design Verifier™ analysis supports models that call custom code from MATLAB® function blocks by using coder.ceval. For such design models, you can generate test cases for model coverage or perform design error detection to find dead logic or detect design errors.

The table summarizes various coder.ceval use-cases that Simulink Design Verifier supports:

Supported coder.ceval use-cases:

coder.ceval usageCustom code sourcesAnalysis

Basic calls - with or without arguments

Source files mentioned in Simulink target in Configuration Parameters.

Supported

Layout - rowMajor, columnMajor

Passing references using coder.ref, coder.wref, coder.rref

Any layout -global

-

Unsupported

-

Source file mentioned by using coder.updateBuildInfo

Unsupported

Generating Tests for Custom code in MATLAB function block

This example demonstrates test generation workflow for model by using coder.ceval.

Consider a model with MATLAB function block calling the custom code by using coder.ceval.

  1. Create the required source files as mentioned in coder.ceval.

    The C-function checkIfSignalsInRange, represents custom code. The function returns 1 if both the signals are in acceptable range else, the function returns 0. The MATLAB function block checkIfSignalsINRangeWrapper, receives sensor inputs and invokes the C-function.

    C file: 
    #include <stdio.h>
    #include <stdlib.h>
    #include "checkIfSignalsInRange.h"
    int checkIfSignalsInRange(double sig1, double sig2) {
        double acceptableMin = 15;
        double acceptableMax = 150;
        if ( ((sig1 > acceptableMin) && (sig1 < acceptableMax)) && ((sig2 > acceptableMin) && (sig2 < acceptableMax)) ) {
            return 1;
        }
        return 0;
    }
    Header file:
    int checkIfSignalsInRange(double sig1, double sig2);
    
    MATLAB function Block:
    function result = checkIfSignalsINRangeWrapper(sig1,sig2)
    result = 0;
    % Check if both the signals are within acceptable range
    result = coder.ceval('checkIfSignalsInRange',sig1,sig2);
    
  2. Navigate to Simulation Target in Configuration Parameters. In the Code information tab add the required files.

  3. Set the Enable custom code analysis option in the Import settings tab.

  4. Set the model coverage objectives to Decision and invoke test generation analysis in Configuration Parameters.

Results

The model has three decision objectives, one for MATLAB function block execution and two for custom code. The two decision objectives correspond in making the outcome of if condition once true and once false. The generated test and report is as follows:

From the report it is inferred that in Step 1 both signals are in acceptable range and in Step 2 the signals are out of range.