matlabtest.coder.TestCase.build - Generate C/C++ code in equivalence tests - MATLAB (original) (raw)
Class: matlabtest.coder.TestCase
Namespace: matlabtest.coder
Generate C/C++ code in equivalence tests
Since R2023a
Syntax
Description
[buildResults](#mw%5F55715ce4-e39e-48db-9ea0-d3d44a0051d4) = build([testCase](#mw%5F94a32781-c31a-4b71-a9ce-74465ffb35bc%5Fsep%5Fmw%5Fcc46feec-90aa-472c-a090-245972d4db9a),[fcnName](#mw%5F94a32781-c31a-4b71-a9ce-74465ffb35bc%5Fsep%5Fmw%5F20d0469d-6475-4460-b5a6-439b7edccc0c))
generates C code using MATLAB® for a MEX target from a function that requires no inputs, fcnName
, in the equivalence test case testCase
. In addition to generating the C code,build
compiles the code and generates an executable.
[buildResults](#mw%5F55715ce4-e39e-48db-9ea0-d3d44a0051d4) = build(___,[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments in addition to the input arguments in previous syntaxes.
Input Arguments
Name of function to generate code for, specified as a string scalar or character vector.
Example: "myAdd"
Name-Value Arguments
Specify optional pairs of arguments asName1=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: Inputs={1,2}
Code generation options, specified as a cell array of string scalars or character vectors. Use values from the options (MATLAB Coder) input argument of the codegen (MATLAB Coder) function.
Example: CodeGenerationArguments={"-o","customMex"}
Example: CodeGenerationArguments={"-lang:c++"}
Build-time inputs, specified as a cell array. Each element in the cell array corresponds to an input to the function.
Example: Inputs={1,2}
Folder to preserve generated files in, specified as a string scalar or character vector. The folder path can be relative or absolute. By default, the test case generates code in a temporary directory.
When you specify this name-value argument, MATLAB preserves the generated files regardless of the test result.
Note
When the equivalence test fails, MATLAB preserves the generated files regardless of whether or not you use this name-value argument.
Example: PreserveInFolder="equivalenceTests\artifacts"
Example: PreserveInFolder="C:\users\jdoe\MATLAB\equivalenceTests\artifacts"
Output Arguments
Examples
This example shows how to generate C code from a MATLAB function that has no inputs and test for equivalence by using MATLAB Coder™.
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 matlabtest.coder.TestCase
. The test case in the methods
block defines a test case that:
- Builds C code from the
helloWorld
function - Executes the C code with no inputs
- Verifies the execution of the C code against the execution of the MATLAB function
helloWorld
classdef tEquivalence < matlabtest.coder.TestCase
methods (Test)
function tHelloWorld(testCase)
buildResults = build(testCase,"helloWorld");
executionResults = execute(testCase,buildResults);
verifyExecutionMatchesMATLAB(testCase,executionResults)
end
end
end
Run the tHelloWorld
test.
runtests("tEquivalence", ... procedureName="tHelloWorld")
Running tHelloWorld .. Done tHelloWorld
ans = TestResult with properties:
Name: 'tEquivalence/tHelloWorld'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 2.8921
Details: [1×1 struct]
Totals: 1 Passed, 0 Failed, 0 Incomplete. 2.8921 seconds testing time.
This example shows how to generate C code from a MATLAB function and test for equivalence by using MATLAB Coder.
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
This class definition file defines an equivalence test case that inherits from matlabtest.coder.TestCase
. The test case in the methods
block defines a test case that:
- Builds C code from the
myAdd
function with inputs set to(0,0)
- Executes the C code with inputs set to
(1,2)
- Verifies the execution of the C code against the execution of the MATLAB function
myAdd
with the same inputs and an absolute tolerance of0.01
classdef tEquivalence< matlabtest.coder.TestCase
methods (Test)
function tMyAdd(testCase)
buildResults = build(testCase,"myAdd", ...
Inputs={0,0});
executionResults = execute(testCase,buildResults, ...
Inputs={1,2});
verifyExecutionMatchesMATLAB(testCase,executionResults, ...
AbsTol=0.01)
end
end
end
Run the tMyAdd
test.
runtests("tEquivalence", ... procedureName="tMyAdd")
Running tMyAdd .. Done tMyAdd
ans = TestResult with properties:
Name: 'tEquivalence/tMyAdd'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 2.6670
Details: [1×1 struct]
Totals: 1 Passed, 0 Failed, 0 Incomplete. 2.667 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.
Version History
Introduced in R2023a