Main Content

Test User-Written C/C++ Code Against MATLAB Code Using MEX Verification

Since R2025a

If you have a user-written C or C++ function that is meant to reproduce the behavior of a MATLAB® function, you can verify that the functions have equivalent behavior by using MEX verification. To do so, enable MATLAB to execute the C/C++ code by generating a MEX function from the C/C++ function. Then, create and run tests that execute the MEX function and the MATLAB function and compare the outputs for equivalence.

Note

Alternatively, if you have MATLAB Coder™, you can generate C/C++ code from the MATLAB source code and test the generated code for equivalence. For more information, see Generate C/C++ Code and Test for Equivalence.

Create MEX Function

First, confirm that your user-written C/C++ file contains a mexFunction function that uses this syntax. This gateway function enables you to create a MEX function from the C/C++ function.

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, 
  const mxArray *prhs[])
The gateway function can directly implement the algorithm, or it can call another C/C++ function that implements the algorithm. For more information, see mexFunction (C) and Components of C MEX File.

Next, create the MEX function for the user written C/C++ function by using the mex function. For example, suppose that you have a MATLAB function called myAdd, which adds two inputs and outputs the result:

function y = myAdd(a,b) %#codegen
y = a + b;
end

You then create a user-written C function in a file called myAdd_c.c that has the same functionality as the myAdd MATLAB function.

#include "mex.h"

/* myAdd function */
void myAdd(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double a, b, y;

    a = mxGetScalar(prhs[0]);
    b = mxGetScalar(prhs[1]);

    y = a + b;

    plhs[0] = mxCreateDoubleScalar(y);
}
/* Gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* Call myAdd function */
    myAdd(nlhs, plhs, nrhs, prhs);
}
To generate a MEX function for the user-written C function, use the mex function.
mex myAdd_c.c
For a C++ example, see C++ MEX Functions.

Create MEX Verification Test

To verify that the C function has the same behavior as the MATLAB function, create a test that executes the MEX function and the MATLAB function and compares the outputs. To create the test:

  1. Execute the MEX function at the command line.

  2. In the Command History panel, right-click a command and select Generate Test.

    If the Command History icon is not in the sidebar, click the Open more panels button and select Command History.

  3. Complete the test by specifying the output of the MATLAB function.

The generated test executes the command and compares the outputs by using the verifyEqual method.

For example, suppose that you executed these commands.

>> a = 1;
>> b = 2;
>> y = myAdd_c(a,b);

Generating a test from the execution of the myAdd_c MEX function creates this test.

% This is an autogenerated sample test for myAdd_c
classdef myAdd_cTest < matlab.unittest.TestCase
    methods (Test)
        function testMyAddC(testCase)
            % Specify the inputs
            a = 1;
            b = 2;

            % Exercise the code
            actualOutput_y = myAdd_c(a,b)

            % Specify the expected outputs
            expectedOutput_y = 

            % Verify the result
            testCase.verifyEqual(actualOutput_y, expectedOutput_y);
        end
    end
end
Complete the test by specifying the expected output as the output of the myAdd function.
% This is an autogenerated sample test for myAdd_c
classdef myAdd_cTest < matlab.unittest.TestCase
    methods (Test)
        function testMyAddC(testCase)
            % Specify the inputs
            a = 1;
            b = 2;

            % Exercise the code
            actualOutput_out = myAdd_c(a,b);

            % Specify the expected outputs
            expectedOutput_out = myAdd(a,b);

            % Verify the result
            testCase.verifyEqual(actualOutput_out, expectedOutput_out);
        end
    end
end
For more information, see Generate Tests for MATLAB Source Code.

Alternatively, you can manually create a test by clicking New > Test Class on the Home tab. Then, complete the test. For more information, see Class-Based Unit Tests.

Test Multiple Input Signatures and Data Types

To test functions that have multiple input signatures or whose inputs can accept multiple data types, create additional tests to verify that the behavior is equivalent for each signature or data type. Alternatively, you can test multiple input signatures and data types in an existing test by editing the test to use test parameters.

For example, this test runs three times with different inputs. The first time, it executes the functions with the inputs set to (1,2), the second time it executes with the inputs set to (2,3), and the third time it executes with the inputs set to (int16(1),int16(2)). The inputs variable in the properties block defines the inputs. The testMyAddC test and the myAdd_c and myAdd functions take the inputs variable as an input.

classdef myAdd_cTest < matlab.unittest.TestCase
    properties (TestParameter)       
        inputs = {{1,2},{2,3},{int16(1),int16(2)}};
    end    
        methods (Test)
            function testMyAddC(testCase,inputs)         
            % Execute functions
            actualOutput_y = myAdd_c(inputs{:});            
            expectedOutput_y = myAdd(inputs{:});

            % Verify the result
            testCase.verifyEqual(actualOutput_y,expectedOutput_y);
        end        
    end
end
This test executes nine times. Each time it runs, it executes a different combination of the possible inputs for a and b.
classdef myAdd_cTest < matlab.unittest.TestCase
    properties (TestParameter)       
        a = {1,2,int16(1)};
        b = {2,3,int16(2)};
    end    
    methods (Test)
        function testMyAddC(testCase,a,b)         
            % Execute functions
            actualOutput_y = myAdd_c(a,b);            
            expectedOutput_y = myAdd(a,b);

            % Verify the result
            testCase.verifyEqual(actualOutput_y,expectedOutput_y);
        end        
    end
end
For more information, see Use Parameters in Class-Based Tests.

Run MEX Verification Tests

To run the test, on the Editor tab, in the Run Tests section, click Run Tests. Alternatively, in the Files pane, right-click the test file and select Run Tests.

You can also run the tests by using the:

For more information, see Run MATLAB Tests.

See Also

Functions

Classes

Topics