Main Content

matlabtest.coder.TestCase Class

Namespace: matlabtest.coder
Superclasses: matlab.unittest.TestCase

Test case for generated C/C++ equivalence tests

Since R2023a

Description

Use the matlabtest.coder.TestCase class to generate C/C++ code by using MATLAB® Coder™ and test the generated code for equivalence with MATLAB source code. 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.TestCase class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Creation

Create a class definition file that inherits from matlabtest.coder.TestCase. Author test methods in a methods block to define the test case.

Methods

expand all

Examples

collapse all

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

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. Verifies the execution of the C code against the execution of the MATLAB function myAdd with the same inputs and 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});
            verifyExecutionMatchesMATLAB(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