Main Content

build

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

Generate C/C++ code in formal equivalence tests

Since R2023a

Description

build(tester,testCase) generates C code from a MATLAB® function by using MATLAB Coder™ with the build-time inputs and build target type specified by tester in the test case testCase. build generates and compiles the code, generates an executable, and stores the results in the BuildResults property of tester.

example

Input Arguments

expand all

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

Test case, specified as a matlab.unittest.TestCase object.

Examples

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

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