No BSD License  

Highlights from
MATLAB/Simulink Unit Test Framework

from MATLAB/Simulink Unit Test Framework by Carl Kritzinger
A unit test framework for matlab and simulink components

create_example_unit_tests.m
disp('The purpose of this demo is to show how to set up a unit test.');

%First test
%--------------------------------------------------------------------------

disp('The first test will test the functionaliy of the built in product block.');
disp('The product block requires 2 inputs and delivers one output');
disp('--------------------------------------------------');pause;

disp('First input : create a random vector');
disp(' ');
t1_in_1 = rand(10,1);

disp('Second input : another random vector');
disp(' ');
t1_in_2 = rand(10,1);

disp('The input is a cell containing the separate inputs to the test case');
disp(' ');
t_1_in = {t1_in_1,t1_in_2};

disp('Of course the output must be the product of the inputs, so in this case it is easy to compute');
disp('Note that the inputs and outputs must all be in cell arrays, even if there is a single output.');
disp(' ');
t1_out = {t1_in_1.*t1_in_2};

disp('Now make the first test case : input, output and comparator.  In this caes we want the product to be exactly accurate');
disp(' ');
test_1 = make_unit_test_case(t_1_in,t1_out,'exactly equal');



disp('Second test case : test the dynamic range of the operation - scale the inputs by 1000');
disp('--------------------------------------------------');pause;

t2_in_1 = 1000*rand(10,1);
t2_in_2 = 1000*rand(10,1);

test_2 = make_unit_test_case({t2_in_1,t2_in_2},{t2_in_1.*t2_in_2},'exactly equal');

disp('Collate the test cases');
disp(' ');
test_cases = [test_1 test_2];

disp('now we construct the test case file');
disp('the product block takes no parameters so we leave this as an empty struct.');
disp('The third argument is zero since this is a simple block we are testing.  The other case will be demonstrated shortly.');
disp(' ');

make_simulink_unit_test_file(...
    'product_block_unit_test.mat',...
    'built-in/Product',...
    0,...
    {},...
    test_cases)


%Second test
%--------------------------------------------------------------------------
disp('The second unit test is basically identical to the first except that we test the sum block');
disp('--------------------------------------------------');pause;

clear

t1_in_1 = rand(10,1);
t1_in_2 =  rand(10,1);

t2_in_1 =  rand(10,1);
t2_in_2 =  rand(10,1);

test_cases = [...
    make_unit_test_case({t1_in_1,t1_in_2},{t1_in_1+t1_in_2},'exactly equal')...
    make_unit_test_case({t2_in_1,t2_in_2},{t2_in_1-t2_in_2},'exactly equal')...
    ];

make_simulink_unit_test_file(...
    'sum_block_unit_test.mat',...
    'built-in/Sum',...
    0,...
    {},...
    test_cases)


%Third test
%--------------------------------------------------------------------------
disp('In the third test we make provision for a block which is created from a subsystem block using a function.');
disp('I use this kind of block a great deal, see my model creation toolbox (which is a pre-requisite for this toolbox :-) ).');
disp('Your mileage may vary');
disp('The general adder is a block idesigned which can take an arbitrary number of inputs and add them together.');
disp('It is created as a simulink sub-system, by the function make_general_adder');
disp('In this case we are going to use 20 inputs');
disp('The number of inputs is a parameter to this block');
disp('We also use the "approximately equal" comparator, since the block makes small rounding errors.');
disp('We will use a tolerance of 1e-10');
disp('--------------------------------------------------');pause;

n_inputs = 20;
test_cases =[];
tolerance = 1e-10;

for jj = 1:10
    
    inputs = rand(10,n_inputs);
    out_1= {sum(inputs,2)};

    for kk = 1:n_inputs
        in_1{kk} = inputs(:,kk); 
    end
    test_cases = [test_cases make_unit_test_case(in_1,out_1,'approximately equal',tolerance)];

end

make_simulink_unit_test_file(...
    'general_adder_block_unit_test.mat',...
    'make_general_adder',...
    1,...
    {n_inputs},...
    test_cases)


%fourth test
%--------------------------------------------------------------------------
disp('In the fourth test we are going to test a simple matlab function.');
disp('The beamformer.m function is included in this package.');
disp('--------------------------------------------------');pause;

n_inputs = 20;
test_cases =[];
taps = rand(1,n_inputs);
num_samples = 10;

for jj = 1:10
    
    inputs = rand(num_samples,n_inputs);
    out_1= {inputs*taps'};

    for kk = 1:n_inputs
        in_1{kk} = inputs(:,kk); 
    end
    test_cases = [test_cases make_unit_test_case(in_1,out_1,'approximately equal',1e-10)];

end

make_matlab_unit_test_file(...
    'beamformer_unit_test.mat',...
    'beamformer',...
    {taps},...
    test_cases)


Contact us at files@mathworks.com