Choose an Approach for Equivalence Testing Generated C++ Code
You can write equivalence tests that use MATLAB® Coder™ to generate and test C++ code from MATLAB source code. Equivalence testing generated C++ code can help mitigate unexpected behavior when you deploy the generated C++ code.
Alternatively, you can write equivalence tests that use MATLAB Compiler SDK™ to generate and test deployable C++ shared libraries from MATLAB source code. Equivalence testing deployable C++ shared libraries can help mitigate unexpected behavior when you deploy the C++ shared library in a C++ application.
This table compares the benefits of writing equivalence tests that generate C++ code and deployable C++ shared libraries.
Approach for Equivalence Testing C++ Code | Benefits | Required Product | Able to Collect Coverage for C++ Code |
---|---|---|---|
Define a test in a matlabtest.coder.TestCase class |
| MATLAB Coder | Yes, but you must build and test a static library using SIL or PIL verification. You must have an Embedded Coder license. |
Define a test in a matlab.unittest.TestCase class that imports the matlabtest.coder.MATLABCoderTester class |
| MATLAB Coder | Yes, but you must build and test a static library using SIL or PIL verification. You must have an Embedded Coder license. |
Define a test in a matlabtest.compiler.TestCase class |
| MATLAB Compiler SDK | No |
Generate and Test C++ Code
To generate C++ code from MATLAB source code and test for equivalence:
Define a test class that inherits from the
matlabtest.coder.TestCase
class.Create a test in the test class.
In the test, generate the C++ code by using the
build
method. Specify the language to use in the generated code as C++ by specifying theCodeGenerationArguments
as{"-lang:c++"}
.Execute the C++ code by using the
execute
method.Qualify the result by comparing the execution of the C++ code to the execution of the MATLAB source code by using methods such as
verifyExecutionMatchesMATLAB
. For more information, see Table of Qualifications for Equivalence Tests.Run the test and view the results programmatically, or by using the Test Browser or MATLAB Test Manager.
Suppose that you want to generate code for a function called
myAdd
, which adds two inputs and outputs the
result:
function y = myAdd(a,b) %#codegen y = a + b; end
(1,2)
. The test executes the C code with the same inputs used
to build the code and verifies that the execution matches the MATLAB execution of the
function.classdef tEquivalenceCpp < matlabtest.coder.TestCase methods (Test) function tMyAddCpp(testCase) buildResults = build(testCase,"myAdd.m", ... CodeGenerationArguments={"-lang:c++"},Inputs={1,2}); executionResults = execute(testCase,buildResults); verifyExecutionMatchesMATLAB(testCase,executionResults); end end end
To collect coverage for the generated C++ code, configure the tests to generate a static library and use SIL or PIL verification. For more information about running the test, collecting coverage, and viewing the coverage results, see Collect Coverage for Generated C/C++ Code in Equivalence Tests.
Generate and Test C++ Code Using Multiple Entry-Point Functions or Functions with Multiple Signatures
You can generate C++ code from multiple entry-point functions or for functions that have multiple signatures. To generate code in these situations and test for equivalence with the MATLAB source code:
Define a test class that inherits from the
matlab.unittest.TestCase
class.Create a test in the test class.
In the test, import the
matlabtest.coder.MATLABCoderTester
interface and thematlabtest.constraints.ExecutionMatchesMATLAB
constraint.Specify the language to use in the generated code as C++ by using the
coder.config
(MATLAB Coder) function to create acoder.MexCodeConfig
(MATLAB Coder) orcoder.EmbeddedCodeConfig
(MATLAB Coder) object, referred to as a code generation configuration parameter object. Specify theTargetLang
property of the object as"C++"
.Construct an instance of the
matlabtest.coder.MATLABCoderTester
class for your build type and provide one of the functions and build-time inputs by using one of these methods:Pass the code generation configuration parameter object to the equivalence test class constructor by using the
Configuration
name-value argument.Add additional functions or function signatures by using the
addEntryPointFunction
method.Generate the C++ code by using the
build
method.Execute the C++ code by using the
execute
method.Qualify the result against the MATLAB execution of the function with the same inputs by using the
verifyThat
method and thematlabtest.constraints.ExecutionMatchesMATLAB
constraint.Run the test and view the results programmatically, or by using the Test Browser or MATLAB Test Manager.
This SIL equivalence test builds C++ code as a static library, and
executes the code with inputs set to (1,2)
. The test verifies that
the execution matches the MATLAB execution of the
function.
classdef tFormalEquivalenceCpp < matlab.unittest.TestCase methods(Test) function tMyAddCpp(testCase) import matlabtest.coder.MATLABCoderTester import matlabtest.constraints.ExecutionMatchesMATLAB config = coder.config("lib","ecoder",true); config.TargetLang = "C++"; tester = MATLABCoderTester.forLIBCoderConfiguration( ... "myAdd.m",Inputs={0,0},Configuration=config); build(tester,testCase); execute(tester,testCase,Inputs={1,2}); verifyThat(testCase,tester, ... ExecutionMatchesMATLAB); end end end
To collect coverage for the generated C++ code, configure the tests to generate a static library and use SIL or PIL verification. For more information about running the test, collecting coverage, and viewing the coverage results, see Collect Coverage for Generated C/C++ Code in Equivalence Tests.
Generate and Test Deployable C++ Shared Libraries
Since R2024b
To generate deployable C++ shared libraries and test for equivalence with the MATLAB source code:
Define a test class that inherits from the
matlabtest.compiler.TestCase
class.Create a test in the test class.
Generate the deployable C++ shared library by using the
build
method. Specify theartifactType
argument as"cppSharedLibrary"
.Execute the deployed C++ shared library by using the
execute
method.Qualify the result by comparing the execution of the deployed code artifact to the execution of the MATLAB source code by using methods such as
verifyExecutionMatchesMATLAB
. For more information, see Table of Qualifications for Equivalence Tests.Run the test and view the results programmatically, or by using the Test Browser or MATLAB Test Manager.
classdef tEquivalenceCppSharedLibrary < matlabtest.compiler.TestCase methods (Test) function cppSimple(testCase) func = which("myAdd.m"); buildResults = build(testCase,func,"cppSharedLibrary"); executionResults = execute(testCase,buildResults,{1,2}); verifyExecutionMatchesMATLAB(testCase,executionResults); end end end