Main Content

matlabtest.coder.MATLABCoderTester Class

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.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Create a class definition file that inherits from matlab.unittest.TestCase and then author test methods in a methods block that imports matlabtest.coder.MATLABCoderTester. Construct an instance of matlabtest.coder.MATLABCoderTester by using one of these methods:

Properties

expand all

Code generation target build type, returned as 'MEX', 'LIB', or 'DLL'.

Attributes:

GetAccess
public
SetAccess
private

Data Types: char

Code generation configuration parameter object, returned as a coder.MexCodeConfig (MATLAB Coder) or coder.EmbeddedCodeConfig (MATLAB Coder) object.

Attributes:

GetAccess
public
SetAccess
private

Build results for generated C/C++ code in equivalence tests, returned as a matlabtest.coder.results.BuildResults object.

Attributes:

GetAccess
public
SetAccess
private

Execution results for generated C/C++ code in equivalence tests, returned as a matlabtest.coder.results.ExecutionResults object.

Attributes:

GetAccess
public
SetAccess
private

Data Types:

Code generation options, specified as a cell array. You can use values from the options (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

expand all

Examples

collapse all

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