Achieve Missing Coverage in Custom Code
This example shows you how to test for missing coverage in custom code. You can use these steps to also test for missing coverage in external C code. If you simulate a model with custom code through C Caller block, C Caller Library, or coder.ceval function, then coverage of the custom code is reported. If the code does not achieve full coverage, you can use Simulink® Design Verifier™ to generate test cases that achieve full coverage. You can then use Simulink® Test Manager™ to perform unit testing by generating test cases only for the custom code.
1. Open the test file.
testFile = 'dTest_TopOffCoverage_mFuncWithPointers.mldatx';
sltest.testmanager.load(testFile);
sltest.testmanager.view;
2. Simulate the test file and observe the coverage value in the Aggregated Coverage Results window.
3. As the coverage of the custom code is not 100%, click Add Tests for Missing Coverage.
4. Simulink Design Verifier generates additional test cases. The custom code file, hFuncWithPointers.c contains two functions: getValue and getValuePointer.
Function 1: The function getValue has vector and scalar inputs. The dimensions are specified for the vector inputs.
The inputs of the harness constructed from this code contain the proper dimensions of the signals. Therefore, Simulink Design Verifier will be able to generate correct test cases for the code.
The harness generated for this code is as shown:
The analysis result is as shown here:
Function 2: The function getValuePointer also has vector and scalar inputs. The dimensions are not specified for the vector inputs.
The inputs of the harness constructed from this code contain the dimensions of the signals to be inherited. In most cases, Simulink Design Verifier may not be able to generate correct test cases for the code.
5. Add new test cases for the function getValue as shown:
From the warning message, you will get a list of functions for which Simulink Design Verifier is not invoked, and the list of corresponding harness names which you need to update manually, to achieve additional coverage. In this example, for the function getValuePointer no additional test case is generated, and hFuncWithPointers_Lib_getValuePointer_harnessTopOff.slx is the corresponding harness that you need to update.
6. To manually add additional test cases for the function, getValuePointer, update the port specification of the C Caller block getValuePointer of the generated library model hFuncWithPointers_Lib. Open the block parameter of the C Caller block getValuePointer, and update the required port dimension and save the model.
7. Add a new test suite for the updated harness, in the existing test file.
8. Simulate the test suite and generate additional test cases by using Add Tests for Missing Coverage. You have now generated test cases for missing coverage in custom code.