Main Content

fatalAssertExecutionMatchesMATLAB

Class: matlabtest.coder.TestCase
Namespace: matlabtest.coder

Fatally assert that generated C/C++ code execution results match MATLAB results

Since R2023a

Description

example

fatalAssertExecutionMatchesMATLAB(testCase,executionResults) fatally asserts that the execution results specified by executionResults for the C/C++ code generated by MATLAB® Coder™ match the execution of the MATLAB source code in the equivalence test case testCase.

example

fatalAssertExecutionMatchesMATLAB(testCase,executionResults,diagnostic) returns diagnostic information specified by diagnostic.

example

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

Input Arguments

expand all

Test case, specified as a matlabtest.coder.TestCase object.

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

Failure diagnostic information, specified as a:

Example: verifyExecutionMatchesMATLAB(testCase,executionResults,"Equivalence test failed")

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.

Example: AbsTol=0.01

Absolute tolerance, specified as a numeric array. The sizes of AbsTol and expected, where expected is the output of the MATLAB execution of the function, must be the same or compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For an absolute tolerance to be satisfied, abs(expected-actual) <= AbsTol must be true, where actual is ExecutableOutput.

Relative tolerance, specified as a numeric array. The sizes of RelTol and expected, where expected is the output of the MATLAB execution of the function, must be the same or compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For a relative tolerance to be satisfied, abs(expected-actual) <= RelTol.*abs(expected) must be true, where actual is ExecutableOutput.

Examples

expand all

This example shows how to generate C code from a MATLAB function by using MATLAB Coder and fatally assert equivalence.

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:

  1. Builds C code from the myAdd function with inputs set to (0,0)

  2. Executes the C code with inputs set to (1,2)

  3. Fatally asserts that the execution of the C code matches the execution of the MATLAB function myAdd with the same inputs

classdef tEquivalence< matlabtest.coder.TestCase
    methods (Test)
        function tMyAdd(testCase)
            buildResults = build(testCase,"myAdd", ...
                Inputs={0,0});         
            executionResults = execute(testCase,buildResults, ...
                Inputs={1,2});
            fatalAssertExecutionMatchesMATLAB(testCase,executionResults)
        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.

This example shows how to generate C code from a MATLAB function by using MATLAB Coder, fatally assert equivalence, and return a custom diagnostic result.

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:

  1. Builds C code from the myAdd function with inputs set to (0,0)

  2. Executes the C code with inputs set to (1,2)

  3. Fatally asserts that the execution of the C code matches the execution of the MATLAB function myAdd with the same inputs and returns a string if the test fails

classdef tEquivalence< matlabtest.coder.TestCase
    methods (Test)
        function tMyAdd(testCase)
            buildResults = build(testCase,"myAdd", ...
                Inputs={0,0});         
            executionResults = execute(testCase,buildResults, ...
                Inputs={1,2});
            d = "Equivalence test failed.";
            fatalAssertExecutionMatchesMATLAB(testCase, ...
                executionResults,d)
        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.

This example shows how to generate C code from a MATLAB function by using MATLAB Coder and fatally assert equivalence within tolerances.

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:

  1. Builds C code from the myAdd function with inputs set to (0,0)

  2. Executes the C code with inputs set to (1,2)

  3. Fatally asserts that the execution of the C code matches the execution of the MATLAB function myAdd with the same inputs and within an absolute tolerance of 0.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});
            fatalAssertExecutionMatchesMATLAB(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.

Version History

Introduced in R2023a