function testresult = roundoff_test()
% ROUNDOFF_TEST  Unit test definition for roundoff
%  
% This function defines a unittest for the roundoff function.
%
%
%   See also roundoff 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2008 Deltares
%       Kees den Heijer
%
%       Kees.denHeijer@deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tools is part of <a href="http://OpenEarth.Deltares.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 11 Sep 2009
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

MTestCategory.Unit;

%% Description (Name = roundoff unit test)
% The roundoff unittest tests five ways to use roundoff:
%
% * normal
% * ceil
% * floor
% * fix
% * multiple
%
% Each testcase contains several calls to roundoff trying to roundoff pi().
% The last test rounds a sequence between zero and one to multiples of 0.5.

tr(1) = roundoffnormaltest;
tr(2) = roundofffloortest;
tr(3) = roundoffceiltest;
tr(4) = roundofffixtest;
tr(5) = roundoffmultipletest;

testresult = all(tr);
end

function testresult = roundoffnormaltest()
%% $Description (Name = normal & IncludeCode = true & EvaluateCode = false)
% round pi (mode = normal). We round pi at -1 to 5 digits. The result should look like this:

Xround = [0 3 3.1 3.14 3.142 3.1416 3.14159];

%% $RunCode
n = -1:5;
X = pi() * ones(size(n));

res = roundoff(X, n);

testresult = isequal(...
    cellstr(num2str(res', '%g')),...
    cellstr(num2str(Xround', '%g')));
end

function testresult = roundofffloortest()
%% $Description (Name = floor)
% round pi (mode = @floor). We round pi at -1 to 5 digits in floor mode. The result should look like
% this:

Xround = [0 3 3.1 3.14 3.141 3.1415 3.14159];

%% $RunCode
n = -1:5;
X = pi() * ones(size(n));

res = roundoff(X, n, @floor);

testresult = isequal(...
    cellstr(num2str(res', '%g')),...
    cellstr(num2str(Xround', '%g')));

end

function testresult = roundoffceiltest()
%% $Description (Name = ceil)
% round pi (mode = ceil). We round pi at -1 to 5 digits in ceil mode. The result should look like
% this:

Xround = [10 4 3.2 3.15 3.142 3.1416 3.14160];

%% $RunCode
n = -1:5;
X = pi() * ones(size(n));

res = roundoff(X, n, 'ceil');

testresult = isequal(...
    cellstr(num2str(res', '%g')),...
    cellstr(num2str(Xround', '%g')));

end

function testresult = roundofffixtest()
%% $Description (Name = fix)
% round pi (mode = fix). We round pi at -1 to 5 digits in floor mode. The result should look like
% this:

Xround = [-0  -3  -3.1  -3.14  -3.141  -3.1415  -3.14159];

%% $RunCode
n = -1:5;
X = -pi() * ones(size(n));

res = roundoff(X, n, 'fix');

testresult = isequal(...
    cellstr(num2str(res', '%g')),...
    cellstr(num2str(Xround', '%g')));

end

function testresult = roundoffmultipletest()
%% $Description (Name = multiple)
% round 0:0.1:1 to multiples of 0.5. The result should look like this:

Xround = [0  0  0  0.5  0.5  0.5  0.5  0.5  1  1  1];

%% $RunCode
n = .5;
X = 0:.1:1;

res = roundoff(X, n, @round, 'multiple');

testresult = isequal(...
    cellstr(num2str(res', '%g')),...
    cellstr(num2str(Xround', '%g')));

end