matlabtest.constraints.ExecutionMatchesMATLAB Class
Namespace: matlabtest.constraints
Superclasses: matlab.unittest.constraints.Constraint
Test if execution matches MATLAB in generated C/C++ code equivalence tests
Since R2023a
Description
The matlabtest.constraints.ExecutionMatchesMATLAB
class creates a constraint to test if the execution of
generated C/C++ code in an equivalence test matches the execution of the MATLAB® source code with the same inputs.
The matlabtest.constraints.ExecutionMatchesMATLAB
class is a handle
class.
Creation
Description
c = matlabtest.constraints.ExecutionMatchesMATLAB
creates a constraint to test if the execution of generated C/C++ code in an equivalence
test matches the execution of the MATLAB source code.
c = matlabtest.constraints.ExecutionMatchesMATLAB(___,
specifies options using one or more name-value arguments in addition to the input
arguments in previous syntaxes.Name=Value
)
Input Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: c =
matlabtest.constraints.ExecutionMatchesMATLAB(Within=AbsoluteTolerance(0.05));
Within
— Qualification tolerance
matlab.unittest.constraints.AbsoluteTolerance
object | matlab.unittest.constraints.RelativeTolerance
object | ...
Qualification tolerance, specified as an object of one of these classes:
Example: Within=AbsoluteTolerance(0.05)
Using
— Comparator constraint
matlab.unittest.constraints.CellComparator
object | matlab.unittest.constraints.DictionaryComparator
object | ...
Comparator constraint, specified as an object of one of these classes:
Example: Using=CellComparator(NumericComparator)
Properties
Tolerance
— Qualification tolerance
matlab.unittest.constraints.AbsoluteTolerance
object | matlab.unittest.constraints.RelativeTolerance
object | ...
Qualification tolerance, specified as an object of one of these classes:
Attributes:
GetAccess | public |
SetAccess | protected |
ExecutionOutputComparator
— Comparator constraint
matlab.unittest.constraints.CellComparator
object | matlab.unittest.constraints.DictionaryComparator
object | ...
Comparator constraint, specified as an object of one of these classes:
Attributes:
GetAccess | public |
SetAccess | protected |
Examples
Generate C Code from Multiple Functions and Test for Equivalence
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
functionsExecutes 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.
Generate C Code from Multiple Functions with Different Signatures and Test for Equivalence
This example shows how to generate C code from multiple MATLAB functions with different signatures and test them for equivalence by using
the equivalence tester 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 helloWorld
displays a string of text.
function y = helloWorld y = "Hello World!"; 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
, the constraintmatlabtest.constraints.ExecutionMatchesMATLAB
, and the constraintmatlab.unittest.constraints.AbsoluteTolerance
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
helloWorld
with no inputs.Builds C code from the
myAdd
andhelloWorld
functionsExecutes the C code for the entry-point function
myAdd
with the inputs set to(5,5)
Instantiates the constraint
matlabtest.constraints.ExecutionMatchesMATLAB
with the absolute tolerance set to0.05
Verifies the execution of the C code against the execution of the MATLAB function
myAdd
with the constraint
classdef tEquivalence < matlab.unittest.TestCase methods(Test) function tMyFunctions(testCase) import matlabtest.coder.MATLABCoderTester import matlabtest.constraints.ExecutionMatchesMATLAB import matlab.unittest.constraints.AbsoluteTolerance tester = MATLABCoderTester.forMEXCoderConfiguration( ... "myAdd",Inputs={0,0}); addEntryPointFunction(tester,"helloWorld"); build(tester,testCase); execute(tester,testCase,Inputs={5,5},EntryPoint="myAdd"); c = ExecutionMatchesMATLAB(Within=AbsoluteTolerance(0.05)); verifyThat(testCase,tester,c); end end end
Run the tMyFunctions
test.
runtests("tEquivalence", ... procedureName="tMyFunctions")
Running tMyFunctions .. Done tMyFunctions __________ ans = TestResult with properties: Name: 'tEquivalence/tMyFunctions' 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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)