Main Content

Generate C Code and Test for Equivalence

In this step, you generate C code from your MATLAB® code and test that the generated code is functionally equivalent to the MATLAB code by writing and running an equivalence test.

Open the MATLABShortestPath project.

openProject("MATLABShortestPath");

Generate and Execute C Code

Use MATLAB Coder™ to generate C code from your MATLAB function.

Use coder.typeof (MATLAB Coder) to define a variable-sized double array with a maximum size of 100-by-100 and a scalar double to use as inputs for the generated code.

mtxType = coder.typeof(ones(100,100),[],1);
scalarDblType = coder.typeof(1);

Generate C code from the shortest_path algorithm with the specified input types.

codegen shortest_path -args {mtxType, scalarDblType, scalarDblType}
Code generation successful.

Execute the generated code with these inputs:

A = [0 1 0 0 1 0;
    1 0 1 0 0 0;
    0 1 0 1 0 0;
    0 0 1 0 1 1;
    1 0 0 1 0 0;
    0 0 0 1 0 0];
startIdx = 1;
endIdx = 6;
pathLength_mex = shortest_path_mex(A,startIdx,endIdx)
pathLength_mex = 3

Execute the MATLAB function with the same inputs and compare the results.

pathLength = shortest_path(A,startIdx,endIdx)
pathLength = 3

Write and Run an Equivalence Test

Write an equivalence test to check that the execution of the generated C code and MATLAB code are functionally equivalent.

This equivalence test uses the same test inputs as graph_unit_tests/check_invalid_start_1. The test defines the build-time inputs, generates C code, executes the C code, and verifies that the execution of the C code matches the execution of the MATLAB function with the same inputs. Save this equivalence test by navigating to the Home tab and clicking New Script. Copy and paste this test code in the script, then click Save.

classdef tShortestPathEquivalence < matlabtest.coder.TestCase
    methods(Test)
        function equivalence_check_invalid_start_1(testCase)             
            adjMatrix = graph_unit_tests.graph_straight_seq();
            startIdx = -1;
            endIdx = 2;
            inputs = {adjMatrix,startIdx,endIdx};
             
            functionToBuild = "shortest_path.m";
            buildOut = build(testCase,functionToBuild,Inputs=inputs);

            executionResults = execute(testCase,buildOut); 
            
            verifyExecutionMatchesMATLAB(testCase,executionResults);
        end
    end
end

Run the equivalence test.

results = runtests("tShortestPathEquivalence")
Running tShortestPathEquivalence

Detected potential differences between generated code and MATLAB function(s). This may result in unexpected behavior when you run or qualify your program. 
For more details, you can refer to the "Code Insights" section in the code generation report.
.
Done tShortestPathEquivalence
__________
results = 
  TestResult with properties:

          Name: 'tShortestPathEquivalence/equivalence_check_invalid_start_1'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 3.4635
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   3.4635 seconds testing time.

The equivalence test indicates that there are potential differences between the generated code and the MATLAB code. However, the test passes, indicating that the generated code and MATLAB code are functionally equivalent for the given inputs.

See Also

Apps

Functions

Classes

Related Topics