matlabtest.coder.MATLABCoderTester.build - Generate C/C++ code in formal equivalence tests - MATLAB (original) (raw)
Class: matlabtest.coder.MATLABCoderTester
Namespace: matlabtest.coder
Generate C/C++ code in formal equivalence tests
Since R2023a
Syntax
Description
build([tester](#mw%5F3d8605fc-05cb-41b3-9616-aa96bb45817f%5Fsep%5Fmw%5F59cdb4f8-4ecc-4812-b287-3ae8571c9931),[testCase](#mw%5F3d8605fc-05cb-41b3-9616-aa96bb45817f%5Fsep%5Fmw%5Fdd5cbe97-2e27-4cb9-99c7-b4d13bdcf8a3))
generates C code from a MATLAB® function by using MATLAB Coder™ with the build-time inputs and build target type specified bytester
in the test case testCase
.build
generates and compiles the code, generates an executable, and stores the results in the BuildResults property of tester
.
Input Arguments
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.
Tips
- By default, the test case generates code in a temporary directory. To preserve the generated files, use the
PreserveInFolder
name-value argument in the creation method when you construct an instance of matlabtest.coder.MATLABCoderTester. When the equivalence test fails, MATLAB preserves the generated files regardless of whether or not you use this name-value argument.
Version History
Introduced in R2023a
See Also
Classes
Functions
- matlabtest.coder.MATLABCoderTester.forDLLCoderConfiguration | matlabtest.coder.MATLABCoderTester.forLIBCoderConfiguration | matlabtest.coder.MATLABCoderTester.forMEXCoderConfiguration | execute