Main Content

matlabtest.coder.MATLABCoderTester.forMEXCoderConfiguration

Class: matlabtest.coder.MATLABCoderTester
Namespace: matlabtest.coder

Equivalence test class constructor for MEX code generation

Since R2023a

Description

tester = matlabtest.coder.MATLABCoderTester.forMEXCoderConfiguration(fcnName) creates an instance of the equivalence tester tester configured to build a MEX function for the function specified by fcnName with no build-time inputs.

example

tester = matlabtest.coder.MATLABCoderTester.forMEXCoderConfiguration(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments in previous syntaxes.

example

Input Arguments

expand all

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 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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: Inputs={1,2}

MEX code generation configuration parameter object, specified as a coder.MexCodeConfig (MATLAB Coder) object.

Example: cfg = coder.config("mex");

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

expand all

Equivalence tester object, specified as a matlabtest.coder.MATLABCoderTester object.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

This example shows how to generate C code from a MATLAB function that has no inputs and test for equivalence by using the equivalence tester matlabtest.coder.MATLABCoderTester.

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:

  1. Imports the equivalence tester matlabtest.coder.MATLABCoderTester and the constraint matlabtest.constraints.ExecutionMatchesMATLAB.

  2. Instantiates the tester for a MEX build target for the helloWorld function.

  3. Builds C code from the helloWorld function.

  4. Executes the C code with no inputs.

  5. Verifies the execution of the C code against the execution of the MATLAB function helloWorld.

classdef tEquivalence < matlab.unittest.TestCase
    methods(Test)
        function tHelloWorld(testCase)
            import matlabtest.coder.MATLABCoderTester
            import matlabtest.constraints.ExecutionMatchesMATLAB
 
            tester = MATLABCoderTester.forMEXCoderConfiguration( ...
                "helloWorld");
             
            build(tester,testCase);           
            execute(tester,testCase);           
            verifyThat(testCase,tester,ExecutionMatchesMATLAB);
        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.7680
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   2.768 seconds testing time.

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:

  1. Imports the equivalence tester matlabtest.coder.MATLABCoderTester and the constraint matlabtest.constraints.ExecutionMatchesMATLAB

  2. Instantiates the tester for a MEX build target for the entry-point function myAdd with the build-time inputs set to (0,0)

  3. Adds the entry-point function mySubtract with the build-time inputs set to (0,0)

  4. Builds C code from the myAdd and mySubtract functions

  5. Executes the C code for the entry-point function myAdd with the inputs set to (5,5)

  6. 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