function [results] = simulink_unit_test(component,function_created_sub_system,parameters,test_cases)
% simulink_unit_test_hook - a hook to allow simulink components to be integrated into the unit test framework. The function will intantiate the component, hook it up to the relevant inputs and outputs, run the simulation and save the results.
%
% Inputs:
% component - the component to be tested
% function_created_sub_system - flag to indicate the component is to be instantiated by a script.
% parameters - parameters used to set up the component
% test_cases - structure containing details of the unit test cases
%
% Outputs:
% result - structure containing the result of the testing.
%
% Example:
% [results] = simulink_unit_test_hook(component,function_created_sub_system,parameters,test_cases)
%
% Other m-files required: none
%
% See also: unit_test, run_unit_test_batch, make_simulink_unit_test
% Author: Carl Kritzinger
% KAT DSP Team
% email address: carl@ska.ac.za
% November 2005
%------------- MAIN --------------
%first we create an empty simulink system
sys = 'test_case';
try
close_system(sys,0);
catch
end
new_system(sys);
open_system('test_case');
draw_params = setup_default_draw_parameters;
draw_params.h_pos = 120;
draw_params.v_pos = 20;
draw_params.block_width = 200;
draw_params.block_height = 200;
%drop the component into the system
%if it is a function-created component, create an empty
%sub-system and create the component
if function_created_sub_system
[block] = add_simulink_block(sys,'built-in/Subsystem','UUT',draw_params);
feval(component,block,parameters{:});
%else this is a simple component then just add it
else
[block] = add_simulink_block(sys,component,'UUT',draw_params);
end
%link up the component
ports = get_param(block,'Ports');
n_inputs = ports(1);
n_outputs = ports(2);
%inputs
%----------------
draw_params.h_pos = 20;
draw_params.v_pos = 20;
draw_params.block_width = 40;
draw_params.block_height = 40;
[inputs,draw_params] = add_column_of_blocks(sys,n_inputs,'built-in/From Workspace','inport',draw_params);
for kk = 1:length(inputs)
set_param(inputs{kk},'VariableName',['inputs_' num2str(kk)]);
set_param(inputs{kk},'SampleTime',num2str(1));
set_param(inputs{kk},'OutputAfterFinalValue','Setting to zero');
set_param(inputs{kk},'Interpolate','off');
link_blocks(sys,inputs{kk},1,block,kk);
end
%outputs
%----------------
draw_params.h_pos = 420;
draw_params.v_pos = 20;
[outputs,draw_params] = add_column_of_blocks(sys,n_outputs,'built-in/To Workspace','outport',draw_params);
for kk = 1:length(outputs)
set_param(outputs{kk},'VariableName',['outputs_' num2str(kk)]);
link_blocks(sys,block,kk,outputs{kk},1);
end
results = [];
%now iterate through the test cases
for jj = 1:length(test_cases)
current_test_case = test_cases(jj);
for kk = 1:length(inputs)
eval(['inputs_' num2str(kk) '= current_test_case.inputs{' num2str(kk) '}' ';']);
end
%set the sytem configuration
set_param(sys,'Solver','FixedStepDiscrete');
%run the system - remember to catch exceptions!
sim(sys,current_test_case.simulation_time,simset(simget(sys),'SrcWorkspace','current'));
%capture the outputs
for kk = 1:length(outputs)
eval(['current_test_case.outputs{' num2str(kk) '} = outputs_' num2str(kk) ';']);
end
results = [results current_test_case];
end
try
close_system(sys,0);
catch
end