No BSD License  

Highlights from
MUnit: a unit testing framework in Matlab

MUnit: a unit testing framework in Matlab

by

 

05 Jun 2006 (Updated )

A complete unit testing framework in Matlab

StestTestSet03(in)
% This is a simple testset collectable!
function out = StestTestSet03(in)
out = str2func(in);

% ------------------------ LOCAL FUNCTIONS ------------------------

function tso = MUnitSetUp(ts,event)
% Generating initial linear systems

% Setting up a linear system:
sys = [1,1,1;3,2,1;2,1,3];
% A coefs vector:
y = [1,2,3]';
% A new figure is opened:
fig = figure;

% Returning the testset with the new env:
tso = set(ts,'env',{sys,y,fig});

% -----------------------------------------------------------------

function tso = MUnitShoutDown(ts,event)
% The opened fig must be closed... after a while!

% Get the figure:
env = get(ts,'env');
fig = env{size(env,2)};

% Sleeping:
pause(1);

% Setting the title:
title('Done');

% Sleeping:
pause(1);

% Closing the figure:
close(fig);
tso = ts;

% -----------------------------------------------------------------

function tuo = MUnitReset(tu,event)
% On reset the image must be shown:

% Get the env:
tdata = get(tu,'data');
env = tdata{1};

% Select the figure:
sys = env{1};
y = env{2};
fig = env{3};
figure(fig); hold off;

% Check the test name:
switch get(tu,'name')
    case 'Solution'
        plot([sys(1,1)/sys(3,1),sys(1,2)/sys(3,2),sys(1,3)/sys(3,3)], ...
             [sys(2,1)/sys(3,1),sys(2,2)/sys(3,2),sys(2,3)/sys(3,3)],'r-');
        plot(y(1)/y(3),y(2)/y(3),'b.');
    case 'Eig'
        plot([sys(1,1)/sys(3,1),sys(1,2)/sys(3,2),sys(1,3)/sys(3,3)], ...
             [sys(2,1)/sys(3,1),sys(2,2)/sys(3,2),sys(2,3)/sys(3,3)],'r-');
    otherwise
        error('Unknown test name!');
end
hold on;

% Returning
tuo = tu;

% -----------------------------------------------------------------

function tuo = MUnitDone(tu,event)
% Showing the final result

% Get the env:
tdata = get(tu,'data');
env = tdata{1};

% Select the figure:
x = env{4};
fig = env{3};
figure(fig);

% Check the test name:
switch get(tu,'name')
    case 'Solution'
        plot(x(1)/x(3),x(2)/x(3),'go');
    case 'Eig'
        for ind=1:3
            plot([0,x(ind,1)/x(ind,3)],[0,x(ind,2)/x(ind,3)],'g-');
            plot(x(ind,1)/x(ind,3),x(ind,2)/x(ind,3),'go');
        end
    otherwise
        error('Unknown test name!');
end

% Returning
tuo = tu;

% =================================================================

function [tuo,status,tdata] = MUnitTest_Solution(tu,tdata)
% Check the system solution finding

% Get the env:
env = tdata{1};
sys = env{1};
y = env{2};

% Init the status:
status = true;

% A list of asserts:
try
    % Compute the solution of the linear system:
    try x = sys\y;
    catch failure(tu,'Cannot solve the linear system!'); end
    % Check that this is a solution:
    assertTrue(tu,sum(sys*x==y)==3,'The solution isn''t a solution!');
catch
    % Bad!!!
    status = false;
end

% Saving results:
env = {env{:},x};
tdata{1} = env;
tuo = set(tu,'data',tdata);

% -----------------------------------------------------------------

function [tuo,status,tdata] = MUnitTest_Eig(tu,tdata)
% Check the eigensolver

% Get the env:
env = tdata{1};
sys = env{1};

% Init the status:
status = true;

% A list of asserts:
try
    % Compute the eigenvectors:
    try [EV,E] = eig(sys);
    catch failure(tu,'Cannot compute eigenvectors!');  end
    
    % Check eigenvalues/eigenvectors relation:
    eps = 0.0001;
    assertTrue(tu, ...
        sum(abs(sys*EV(:,1)-E(1,1)*EV(:,1)))<eps && ...
        sum(abs(sys*EV(:,2)-E(2,2)*EV(:,2)))<eps && ...
        sum(abs(sys*EV(:,3)-E(3,3)*EV(:,3)))<eps, ...
        'Eigenvectors lacks eigenproperties!');
catch
    % Bad!!!
    status = false;
end

% Saving results:
env = {env{:},EV};
tdata{1} = env;
tuo = set(tu,'data',tdata);

% -----------------------------------------------------------------

Contact us