Main Content

Enhance Model Coverage of Older Release Models

To enhance the model coverage of a model that you created in an older release, use a test generation workflow or a code generation workflow. You can leverage the latest release capabilities of Simulink® Design Verifier™ to generate the test cases for a Model-Based Design.

These workflows enhance model coverage.

Enhance Model Coverage by Generating Test Cases for Older Release Model

This example shows how to upgrade model coverage of a model created in R2015b. You use test generation for supported S-functions available in the latest release.

The example model sldvexSFunctionHandlingExample contains the handwritten S-Function, which implements a lookup table algorithm. The handwritten S-Function is in the file sldvexSFunctionHandlingSFcn.c. The user source code for the lookup table is in the file sldvexSFunctionHandlingSource.c.

  1. In MATLAB R2015b, open the sldvexSFunctionHandlingExample model.

    open_system('sldvexSFunctionHandlingExample');
    

  2. To simulate the model and generate the coverage report, in the Simulink Editor, click the Run button. See View Coverage Results in a Model (Simulink Coverage).

    After the simulation, the coverage report indicates that full coverage is not achieved for sldvexSFunctionHandlingExample model.

  3. In MATLAB R2018b or later releases, open the sldvexSFunctionHandlingExample model. The example model sldvexSFunctionHandlingExample is available in R2015b and later releases, so you can use the same model for test generation workflow.

    open_system('sldvexSFunctionHandlingExample');

    To avoid any potential changes in the model, create a copy of the older release model in the current working folder, and then open the model in R2018b or later releases. To upgrade and improve models that you use in the current release, you can use the upgradeadvisor.

  4. Compile the S-function to be compatible with Simulink Design Verifier for test case generation by using slcovmex (Simulink Coverage). For more information, see Configuring S-Function for Test Case Generation.

    slcovmex('-sldv', ...
            '-output', 'sldvexSFunctionHandlingSFcn',...
            ['-I', fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src')], ...
            fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src',...
            'sldvexSFunctionHandlingSource.c'),...
            fullfile(matlabroot, 'toolbox', 'sldv', 'sldvdemos', 'src',...
             'sldvexSFunctionHandlingSFcn.c'));
  5. Create an opts option for the sldvexSFunctionHandlingExample model.

    opts = sldvoptions;
    opts.Mode = 'TestGeneration';
    opts.ModelCoverageObjectives = 'Condition';
    opts.SaveHarnessModel = 'off';
    opts.SaveReport = 'off';
    opts.SFcnSupport = 'on';
    
  6. To generate test cases by using the specified opts options, use sldvrun to analyze the model.

    [status, fileNames] = sldvrun('sldvexSFunctionHandlingExample', opts);

    After analysis, the software generates a Simulink Design Verifier data file and stores it in the default location <current_folder>\sldv_output\sldvexSFunctionHandlingExample_sldvdata.mat

  7. In R2015b, open the model.

    open_system('sldvexSFunctionHandlingExample');
  8. Load the sldvData file created in R2018b or later releases.

    1. On the Design Verifier tab, click Load Earlier Results and browse to the sldvData MAT-file generated in R2018b or later releases.

    2. Click Open.

  9. In the Simulink Design Verifier Results Summary window, click Simulate tests and produce a model coverage report. The report indicates that 100% coverage is achieved for sldvexSFunctionHandlingExample model.

For more information, see Simulink Design Verifier Data Files and Simulate Tests and Produce Model Coverage Report.

Enhance Model Coverage by Using Generated Code from Older Release

This example shows how to upgrade the model coverage of a model created in R2015b by using code generation workflow.

For this workflow, you must have Simulink Coder™ and Embedded Coder®.

The example model sldvCrossReleaseExample contains the handwritten S-Function, which implements a relational boundary algorithm. The handwritten S-Function is in the file rel_sfcn.c. The user source code is in the file rel_comp.c.

To inline the S-function, use the rel_sfcn.tlc file. For more information, see Inline S-Functions with TLC (Embedded Coder).

  1. Copy the example model sldvCrossReleaseExample and S-Function files, rel_sfcn.c, rel_comp.c, and rel_sfcn.tlc in the current working folder. Copy the header files rel_comp.h into the current working folder. You use the example model and supporting files in R2015b for a Cross-Release Code Integration (Embedded Coder) workflow.

    Note

    The example model sldvCrossReleaseExample is created for example purpose. To perform code generation workflow by using the example model, export sldvCrossReleaseExample model to 15b. Save the model as sldvCrossReleaseExample_15b in the current working folder. For more information, see Export Model to Previous Simulink Version.

  2. In MATLAB R2015b, open sldvCrossReleaseExample_15b model from the current working folder.

    open_system('sldvCrossReleaseExample_15b');
    

  3. Compile the S-function by using the function legacy_code.

    def = legacy_code('initialize');
       def.SFunctionName = 'rel_sfcn';
       def.OutputFcnSpec = 'uint8 y1 = relational_bound(uint8 u1)';
       def.HeaderFiles = {'rel_comp.h'};
       def.SourceFiles = {'rel_comp.c'};
       def.IncPaths = {pwd};
       def.SrcPaths = {pwd};
       def.Options.supportCoverageAndDesignVerifier = true;
       legacy_code('sfcn_cmex_generate', def);
       legacy_code('compile', def);
  4. To simulate the model and generate the coverage report, in the Simulink Editor, click the Run button. See View Coverage Results in a Model (Simulink Coverage).

    After the simulation, the coverage report indicates that 50% coverage is achieved for sldvCrossReleaseExample_15b model.

  5. To generate code using Embedded Coder, from the Apps tab, select Embedded Coder. For more information, see Generate Code Using Embedded Coder® (Embedded Coder).

    In the C Code tab, click Generate Code.

    The model is preconfigured with these code generation settings.

    set_param(sldvCrossReleaseExample_15b,'SystemTargetFile','ert.tlc');
    set_param(sldvCrossReleaseExample_15b,'PortableWordSizes','on');
    set_param(sldvCrossReleaseExample_15b,'SupportNonFinite','off');
    set_param(sldvCrossReleaseExample_15b,'GenCodeOnly','on');
    set_param(sldvCrossReleaseExample_15b,'SolverMode','SingleTasking');
    set_param(sldvCrossReleaseExample_15b,'ProdEqTarget','on');
    

    The software generates C code for the model and saves the files in the default folder location <current_folder>\sldvCrossReleaseExample_15b_ert_rtw.

  6. Save the configuration set of the model sldvCrossReleaseExample_15b to a MAT-file. This ConfigSet is used to set the configuration set of the model in R2018b and later releases.

    config_set = getActiveConfigSet('sldvCrossReleaseExample_15b');
    copiedConfig = config_set.copy;
    save('copiedConfig.mat','copiedConfig');
            
  7. In MATLAB R2018b or later releases, import the components exported from R2015b.

    1. Before you import components in current release, rename or delete rtwtypes.h file available in the folder <current_folder>\sldvCrossReleaseExample_15b_ert_rtw. During cross-release import, MATLAB tries to regenerate a file with same name. If you do not delete or rename the file rtwtypes.h, MATLAB displays an error.

    2. Import the generated component code from R2015b as software-in-the-loop (SIL) block.

       crossReleaseImport('sldvCrossReleaseExample_15b_ert_rtw',...
      'sldvCrossReleaseExample_15b', 'SimulationMode','SIL');

      The crossReleaseImport function creates an untitled model that contains software-in-the-loop (SIL) block sldvCrossReleaseExample_15b_R2015b_sil.

  8. Add Inport and Outport ports to the sldvCrossReleaseExample_15b_R2015b_sil block and save the model as sldvCrossReleaseExample_sil_18b.

  9. Apply the model configuration set similar to R2015b model.

    load('copiedConfig.mat');
    attachConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig, true);
    setActiveConfigSet('sldvCrossReleaseExample_sil_18b', copiedConfig.Name);
  10. Set the simulation mode to Software-in-the-Loop (SIL). To simulate the model, in the Simulink Editor, click the Run button.

  11. To generate test cases for Embedded Coder generated code, on the Design Verifier tab, select Target > Code Generated as Top Model and click Generate Tests. For more information, see Generate Test Cases for Embedded Coder Generated Code.

    After Simulink Design Verifier analysis, the software generates the test cases and saves the sldvData in folder at default location <current_folder>\sldv_output\sldvCrossReleaseExample_sil_18b.

  12. In R2015b, open the model.

    open_system('sldvCrossReleaseExample_15b');
  13. Update the sldvData.ModelInfomation.Name field in sldvData same as the model name in older release. For example, sldvCrossReleaseExample_15b.slx.

  14. Create a harness model by using the sldvData created in R2018b or later releases. This data consists of test cases generated from Embedded Coder generated code. In the dataFile, type the location of the sldvData generated for sldvCrossReleaseExample_sil_18b model.

    sldvmakeharness('sldvCrossReleaseExample_15b.slx','dataFile')

  15. To simulate the model by using all the test cases, click the Run all button .

    The software simulates all the test cases and generates a coverage report. The report indicates that 100% coverage is achieved for sldvCrossReleaseExample_15b model.

Related Topics