%% SIL Testing a Reusable Library Subsystem
% This example shows how to unit test a reusable component in a library. It 
% tests software-in-the-loop (SIL) code generated for a subsystem by using 
% an equivalence test.
%
% The reusable library subsystem must be at the top level of the library
% and must have function interfaces to lock down the subsystem interface.
% For information on reusable subsystem libraries, function
% interfaces, and workflow limitations, see 
% <docid:ecoder_ug#mw_b060e495-1a4c-41bc-bbf8-1792b8946f57 
% Library-Based Code Generation for Reusable Library Subsystems>.
%
% Copyright 2020 The MathWorks, Inc.

%% Set Up the Code Generation Environment
orig = Simulink.fileGenControl('get','CodeGenFolderStructure');
Simulink.fileGenControl('set','CodeGenFolderStructure',...
    Simulink.filegen.CodeGenFolderStructure.TargetEnvironmentSubfolder);
%% Open the Reusable Library
% This library contains a subsystem block at the top level.
ReuseLibSubsysExample
%% Build the Library 
% Build the reusable library to generate code and create the function
% interfaces. After the code generation completes, you can view the
% function interfaces by clicking the lower right corner the library to
% open the Manage Function Interfaces dialog box.
slbuild('ReuseLibSubsysExample');
%% Select the Subsystem Component and Open the Test Manager
% Click on the Subsystem in the library model to select it. Then, open the
% Test Manager.
sltestmgr
%% Open the Test File
% # In the Test Manager, click *Open* and select the 
% |ReuseLibSubsysTest.mldatx| file. 
%% Enable Coverage Collection
% # Select *ReuseLibSubsysTest* in the Test Browser pane.
% # Then, expand the Coverage Settings section in the main pane. 
% # In Coverage to Collect, check *Record coverage for system under test*.
% # In Coverage Metrics, check that *Decision*, *MCDC*, and *Condition* are
% selected.
%%
% <<../rls_coverage.png>>
%% Open the Create Test for Model Component wizard
% # Click the arrow under *New* and select *Test for Model Component* to open
% the Create Test for Model Component wizard.
% # On the first page of the wizard, click the Use currently selected model
% component icon next to the *Component* field. Both the *Component* and 
% *Top Model* fields fill in.
% # Click the refresh icon next to the Select function interface field and
% select Double from the dropdown. 
% # Click Next.
%%
% <<../rls_wizard1.png>>
%% Select the Test Inputs
% # Select *Use Design Verifier to generate test input scenarios*. 
% # Click Next.
%%
% <<../rls_wizard2.png>>
%% Select How to Test the Component
% # Select *Perform back-to-back testing*.
% # Set *Simulation1* to Normal mode.
% # Set *Simulation2* to Software-in-the-Loop (SIL) mode.
% # Click Next.
%%
% <<../rls_wizard3.png>>
%% Specify the Input Source, Data File Format, and Test File
% # Select Inports as the test harness input source. 
% # Select MAT as the file format.
% # Select *Add tests to currently selected test file*.
%%
% <<../rls_wizard4.png>>
%% Generate the Test Case and Return to the Test Manager
% Click Done to generate the Double_harness1 test case for the Double
% function interface. 
%
% After test case generation completes, the Double_harness1 test case 
% appears in the Test Manager. Notice that the *Simulation Mode* for 
% Simulation1 is set to Normal and Simulation2 is set to 
% Software-in-the-Loop (SIL) mode.
%% 
% <<../rls_simmode1.png>>
%% Run the Test
% Click Run.
%% View Equivalence Results
% Expand the Results to view the Equivalence Criteria Result. Notice that
% the Differences plot shows no differences between the two signals, which
% indicates that the Normal and SIL simulations are producing the same 
% results. The SIL code associated with the reusable library subsystem can
% be reused for other SIL tests.
%%
% <<../rls_results.png>>
%% View Coverage Results
% Expand the Aggregated Coverage section to view the coverage results. The
% two coverage results that show 100% are for the equivalence test run for
% the Double function interface. The other two results show 0% or no
% coverage because the Single function interface was not tested.
%%
% <<../rls_coverage_results.png>>
%% Clean up
Simulink.fileGenControl('set','CodeGenFolderStructure',orig);





