Test User-Written C/C++ Code Against MATLAB Code Using MEX Verification
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[])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);
}mex
function.mex myAdd_c.cCreate 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:
Execute the MEX function at the command line.
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.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
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
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
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
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:
runtestsfunction at the MATLAB command lineRun button
in the Test
BrowserRun button
in the MATLAB Test ManagerRun button
in the Code
Quality Dashboard
For more information, see Run MATLAB Tests.