matlabtest.coder.MATLABCoderTester.build - Generate C/C++ code in formal equivalence tests - MATLAB (original) (raw)

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

Generate C/C++ code in formal equivalence tests

Since R2023a

Syntax

Description

build([tester](#mw%5F3d8605fc-05cb-41b3-9616-aa96bb45817f%5Fsep%5Fmw%5F59cdb4f8-4ecc-4812-b287-3ae8571c9931),[testCase](#mw%5F3d8605fc-05cb-41b3-9616-aa96bb45817f%5Fsep%5Fmw%5Fdd5cbe97-2e27-4cb9-99c7-b4d13bdcf8a3)) generates C code from a MATLAB® function by using MATLAB Coder™ with the build-time inputs and build target type specified bytester 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

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 constraintmatlabtest.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 andmySubtract functions
  5. Executes the C code for the entry-point functionmyAdd 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

Version History

Introduced in R2023a

See Also

Classes

Functions

Topics