matlabtest.coder.MATLABCoderTester - Formal tester for generated C/C++ equivalence tests - MATLAB (original) (raw)
Namespace: matlabtest.coder
Formal tester for generated C/C++ equivalence tests
Since R2023a
Description
Use the matlabtest.coder.MATLABCoderTester
class to generate C/C++ code by using MATLAB® Coder™ and test the generated code for equivalence with MATLAB source code. This test class provides similar functionality to matlabtest.coder.TestCase. Use matlabtest.coder.MATLABCoderTester
to generate C/C++ code from multiple-entry point functions and multiple function signatures. You must have MATLAB Coder to run equivalence tests for generated C/C++ code for MEX targets and Embedded Coder® for LIB and DLL targets.
The matlabtest.coder.MATLABCoderTester
class is a handle class.
Properties
Code generation target build type, returned as 'MEX'
,'LIB'
, or 'DLL'
.
Attributes:
GetAccess | public |
---|---|
SetAccess | private |
Data Types: char
Code generation options, specified as a cell array. You can use values from theoptions (MATLAB Coder) input argument for the codegen (MATLAB Coder) function.
Example: tester.CodeGenerationArguments = {'-o','myMEX'}
Attributes:
GetAccess | public |
---|---|
SetAccess | public |
Directory created for code generation, returned as a character vector.
Example: 'C:\Users\jdoe\AppData\Local\Temp\tp68ab0c12_ae1f'
Attributes:
GetAccess | public |
---|---|
SetAccess | private |
Methods
Examples
This example shows how to generate C code from multiple MATLAB functions and test the functions for equivalence by using matlabtest.coder.MATLABCoderTester.
The function myAdd
takes two numbers as inputs, adds them together, and outputs the result.
function y = myAdd(a,b) %#codegen y = a+b; end
The function mySubtract
takes two numbers as inputs, subtracts them, and outputs the result.
function y = mySubtract(a,b) %#codegen y = b-a; end
This class definition file defines an equivalence test case that inherits from matlab.unittest.TestCase
. The test case in the methods
block defines a test case that:
- Imports the equivalence tester matlabtest.coder.MATLABCoderTester and the constraintmatlabtest.constraints.ExecutionMatchesMATLAB
- Instantiates the tester for a MEX build target for the entry-point function
myAdd
with the build-time inputs set to(0,0)
- Adds the entry-point function
mySubtract
with the build-time inputs set to(0,0)
- Builds C code from the
myAdd
andmySubtract
functions - Executes the C code for the entry-point function
myAdd
with the inputs set to(5,5)
- Verifies the execution of the C code against the execution of the MATLAB function
myAdd
classdef tEquivalence < matlab.unittest.TestCase methods(Test) function tMyMath(testCase) import matlabtest.coder.MATLABCoderTester import matlabtest.constraints.ExecutionMatchesMATLAB
tester = MATLABCoderTester.forMEXCoderConfiguration( ...
"myAdd",Inputs={0,0});
addEntryPointFunction(tester,"mySubtract",{0,0});
build(tester,testCase);
execute(tester,testCase,Inputs={5,5}, ...
EntryPoint="myAdd");
verifyThat(testCase,tester,ExecutionMatchesMATLAB)
end
end
end
Run the tMyMath
test.
runtests("tEquivalence", ... procedureName="tMyMath")
Running tMyMath .. Done tMyMath
ans = TestResult with properties:
Name: 'tEquivalence/tMyMath'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 2.7680
Details: [1×1 struct]
Totals: 1 Passed, 0 Failed, 0 Incomplete. 2.768 seconds testing time.
Version History
Introduced in R2023a
See Also
Classes
- matlabtest.constraints.ExecutionMatchesMATLAB | matlabtest.coder.results.BuildResults | matlabtest.coder.results.ExecutionResults | matlabtest.coder.TestCase