Main Content

Test Integrated Code

Test Integrated C Code

If you have a model that integrates C code with a C Caller block, you can test the C code with the Test Manager and a test harness. For an example, see C Code Verification with Simulink Test.

The C Caller block uses configuration parameters to define the custom code. If you change the configuration parameters, synchronize the parameters between the test harness and the model. For more information, see Synchronize Changes Between Test Harness and Model and Create Test Harnesses and Select Properties.

  • If you change the test harness configuration parameters, you can push the configuration set to the main model. Click Push Changes, or use sltest.harness.push.

  • If you change the main model configuration parameters in the main model, and you want to update the test harness parameters, the test harness must copy the configuration parameters on rebuild. You can set this property in two ways:

    • When you create the test harness, select Update Configuration Parameters and Model Workspace data on rebuild. You can also select Rebuild Harness on Open, which rebuilds every time the harness opens.

    • For existing test harnesses, in the harness preview, select one or more of Rebuild Harness > Rebuild on Open, , or Rebuild Harness without Compiling Model, and Update Harness Configuration Settings and Model Workspace. The Update Harness Configuration Settings and Model Workspace option updates the settings every time a rebuild occurs.

Test Code in S-Functions

S-Functions are computer language descriptions of Simulink® blocks written in MATLAB®, C, C++ or Fortran. You can test code wrapped in S-Functions using Simulink Test™ test harnesses. Testing code in S-Functions can be helpful for regression testing of legacy code and for testing your code in a system context.

S-Function Testing Example

In this example, you test code in an S-Function block using a test harness. The main model is a controller-plant model of an air conditioning/heat pump unit. Before you begin, change the default working folder to one with write permissions.

Note

This example works only on a 64–bit Windows® platform.

Set Up the Working Environment

  1. Add the example folder to the MATLAB path, and set the example file names.

    ep = fullfile(docroot,'toolbox','sltest','examples');
    addpath(ep);
    
    md = 'sltestHeatpumpSfunExample.slx'
    cb = 'sltestHeatpumpBusPostLoadFcn.mat'
    dt = 'PumpDirection.m'
  2. Open the model.

    open_system(fullfile(ep,md))

In the example model:

  • The controller is an S-Function that accepts room temperature and specified temperature inputs.

  • The controller output is a bus with signals that control the fan, heat pump, and the direction of the heat pump (heat or cool).

  • The plant accepts the control bus. The heat pump and the fan signals are Boolean, and the heat pump direction is specified by +1 for cooling and -1 for heating.

The test covers four temperature conditions. Each condition corresponds to one operating state with fan, pump, and pump direction signal outputs.

Temperature ConditionSystem StateFan CommandPump CommandPump Direction
|Troom_in - Tset| < DeltaT_fanidle000
DeltaT_fan <= |Troom_in - Tset| < DeltaT_pumpfan only100
|Troom_in - Tset| >= DeltaT_pump and Tset < Troom_incooling11-1
|Troom_in - Tset| >= DeltaT_pump and Tset > Troom_inheating111

Create a Test Case

  1. On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test. Then, on the Tests tab, click Simulink Test Manager.

  2. From the Test Manager toolstrip, click New to create a test file. Name and save the test file.

  3. In the test case, under System Under Test, click the button to load the current model into the test case.

Create a Test Harness

  1. In the model, right-click the Controller_sfcn subsystem and select Test Harness > Create for ‘Controller_sfcn’.

  2. Set the harness properties.

    In the Basic Properties tab:

    • Set Name to test_harness_1

    • Set Sources and Sinks to None and Scope

  3. Click OK to create the test harness.

  4. In the test case, under System Under Test, refresh the test harness list and select test_harness_1 for the Harness.

Add Inputs and Set Simulation Parameters

Create inputs in the test harness, with a constant Tset and a time-varying Troom_in.

  1. Connect a Constant block to the Tset input and set the value to 75.

  2. Add a Sine Wave block to the harness model to simulate a temperature signal. Connect the Sine Wave block to the conversion subsystem input Troom_in.

  3. Double-click the Sine Wave block and set the parameters:

    ParameterValue
    Amplitude15
    Bias75
    Frequency2*pi/3600
    Phase (rad)0
    Sample time1

    Select Interpret vector parameters as 1–D.

  4. In the Solver pane of the Simulink toolstrip, set Stop time to 3600.

Obtain Baseline Data

  1. In the test case, in Simulation Outputs, click Add. Highlight the output bus from the controller S-Function.

  2. In the Signal Selection dialog box, click the Add button.

  3. Under Baseline Criteria, click Capture to record a baseline data set from simulating the test harness. Save the baseline data set to the working folder. The baseline signals appear in the table.

Run the Test Case and View Results

  1. Run the test case. The test results appear in the Results and Artifacts pane.

  2. Expand the results to view the baseline criteria result. The baseline test passes because the simulation output is identical to the baseline data.