Main Content

Test Generated C/C++ Code

After generating code for your MATLAB® code, verify the run-time behavior of the generated code. To see the generated code and identify potential issues, access the code generation report.

You can test the generated code to verify code behavior, depending on your build type:

  • Test MEX code to verify behavior.

  • Test standalone code by using software-in-the-loop and processor-in-the-loop execution (requires Embedded Coder®).

Test MEX Code to Verify Behavior

If you use the app to generate a MEX function, you can test the MEX function in the app.

  1. On the Generate Code page, click Verify Code.

  2. Type or select the test file name.

  3. To run the test file without replacing calls to the original MATLAB function with calls to the MEX function, for Run using, select MATLAB code. Click Run Generated Code.

  4. To run the test file, replacing calls to the original MATLAB function with calls to the MEX function, for Run using, select Generated code. Click Run Generated Code.

  5. Compare the results of running the original MATLAB function to the results of running the MEX function.

If you have Embedded Coder, you can verify the numeric behavior of generated C/C++ code by using software-in-the-loop (SIL) or processor-in-the-loop (PIL) execution. You can also produce a profile of execution times.

Test Standalone Code by Using Software-in-the-Loop and Processor-in-the-Loop

To test the generated standalone code on your target hardware, you can run unit tests on the generated code. To run unit tests on standalone code in a separate process outside of MATLAB, use software-in-the-loop (SIL) or processor-in-the-loop (PIL) execution. To use SIL or PIL execution, you must have Embedded Coder.

See Software-in-the-Loop Execution with the MATLAB Coder App (Embedded Coder) and Processor-in-the-Loop Execution with the MATLAB Coder App (Embedded Coder).

Tips

Test MEX Files at the Command Line

If you use codegen to generate a MEX function, use the -test option. For example:

codegen myfunction -test 'myfunction_test'

You can also test the MEX function by using coder.runTest. For example:

coder.runTest('myfunction_test', 'myfunction')

Unit Test Generated Code

See Unit Test Generated Code with MATLAB Coder.

To unit test external code, see Unit Test External C Code with MATLAB Coder.

See Also

Related Topics