view testing/utp_1.1/utps/smodel/utp_smodel_addParameters.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
line wrap: on
line source

% UTP_SMODEL_ADDPARAMETERS a set of UTPs for the smodel/addParameters method
%
% M Hewitson 06-08-08
%
% $Id: utp_smodel_addParameters.m,v 1.1 2011/04/17 15:47:57 ingo Exp $
%

% <MethodDescription>
%
% The addParameters method of the smodel class sets the 'params' and
% 'values' properties.
%
% </MethodDescription>

function results = utp_smodel_addParameters(varargin)
  
  % Check the inputs
  if nargin == 0
    
    % Some keywords
    cl      = 'smodel';
    mthd    = 'addParameters';
    
    disp('******************************************************');
    disp(['****  Running UTPs for ' cl '/' mthd]);
    disp('******************************************************');
    
    % Test objects
    s1 = smodel('a*x1 + b*x2 + c*x3');
    s1.setParams('a', 'b', 'c');
    s1.setName();
    
    s2 = smodel('a*x1 + c*x2');
    s1.setParams('c');
    s2.setName();
    
    s3 = smodel('a*x1');
    s1.setParams('a');
    s3.setName();
    
    sv = [s1, s2, s1];
    sm = [s1, s2, s1; s1, s2, s1];
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    % The setter method have different possibilities to set a property
    args1 = {{'a', 'b'}};
    args2 = {'a', 'b', 'c'};
    args3 = {{'a', 'b'}, [1 2]};
    args4 = {{'a', 'b'}, {1 2}};
    args5 = {'b', 1};
    args6 = {'a', 1, 'b', 2, 'c', 3};
    args7 = {plist('params', 'a')};
    args8 = {plist('params', {'a', 'b'})};
    args9 = {plist('params', {'c', 'b'}, 'values', [1 2])};
    args10 = {plist('params', {'a', 'e'}, 'values', {1 2})};
    
    % Run the general tests
    results(1)  = utp_01();
    results(2)  = utp_genericAnyShape(mthd, s1, args1, @algoTests);
    results(3)  = utp_genericAnyShape(mthd, s1, args2, @algoTests);
    results(4)  = utp_genericAnyShape(mthd, s1, args3, @algoTests);
    results(5)  = utp_genericAnyShape(mthd, s1, args4, @algoTests);
    results(6)  = utp_genericAnyShape(mthd, s1, args5, @algoTests);
    results(7)  = utp_genericAnyShape(mthd, s1, args6, @algoTests);
    results(8)  = utp_genericAnyShape(mthd, s1, args7, @algoTests);
    results(9)  = utp_genericAnyShape(mthd, s1, args8, @algoTests);
    results(10) = utp_genericAnyShape(mthd, s1, args9, @algoTests);
    results(11) = utp_genericAnyShape(mthd, s1, args10,@algoTests);
    results(12) = utp_genericAnyShape(mthd, sv, args5, @algoTests);
    results(13) = utp_genericAnyShape(mthd,sm, args10,@algoTests);
    results(14) = utp_genericList(mthd, s1, s2, s3, args4, @algoTests);
    results(15) = utp_genericHistory(mthd, s1, args5, ple2);
    results(10) = utp_genericModify(mthd, s1, args5, @algoTests, ple1);
    results(11) = utp_genericOutput(mthd, s1, s2, args2, @algoTests, ple2);        % test the outputs
    
    disp('Done.');
    disp('******************************************************');
    
  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end
  
  % Check that the property have the correct value
  function atest = algoTests(in, out, idx, argsin)
    atest = true;
    % Check that the input and output objects are the same except the
    % properties 'params', 'values' and the history
    ple = plist('EXCEPTIONS', {'created', 'proctime', 'UUID', 'hist', 'params', 'values'});
    if ~eq(in(idx), out(idx), ple), atest = false; end
    if ~eq(in(idx).hist, out(idx).hist.inhists), atest = false; end
    % Check that the parameter gets the correct value.
    values = {};
    if isa(argsin{1}, 'plist') && argsin{1}.isparam('params')
      argsin = argsin{1};
      % The value was set with a plist.
      names  = cellstr(argsin.find('params'));
      values = argsin.find('values');
      if ~iscell(values)
        values = num2cell(values);
      end
    else
      if numel(argsin) == 1 && iscell(argsin{1})
        names = argsin{1};
      elseif ~isempty(argsin) && iscellstr(argsin)
        names = argsin;
      elseif numel(argsin) == 2 && iscell(argsin{1}) && isnumeric(argsin{2})
        names  = argsin{1};
        values = num2cell(argsin{2});
      elseif numel(argsin) == 2 && iscell(argsin{1}) && iscell(argsin{2})
        names  = argsin{1};
        values = argsin{2};
      else
        nArgsin = numel(argsin);
        names  = argsin(1:2:nArgsin);
        values = argsin(2:2:nArgsin);
      end
    end
    
    p = in(idx).params;
    v = in(idx).values;
    
    if ~isempty(values) && isempty(v)
      v = cell(size(p));
    end
    
    for ll = 1:numel(names)
      namesIdx = strcmp(p, names{ll});
      if any(namesIdx)
        % Change at the index the value
        if ~isempty(values)
          v{namesIdx} = values{ll};
        end
      else
        % Append the alias key-value pair
        p = [p names(ll)];
        if ~isempty(values)
          v = [v values(ll)];
        end
      end
    end
    
    % Check 'params' and 'values'
    if ~isequal(out(idx).params, p), atest = false; end
    if ~isequal(out(idx).values, v), atest = false; end
    
    % Check the numbe of values
    if ~isempty(out(idx).values)
      if numel(out(idx).values) ~= numel(out(idx).params), atest = false; end
    end
  end
  
  %% UTP_01
  
  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01
    
    
    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = feval('smodel.getInfo', mthd, 'none');
      % Call for all sets
      io(2) = feval('smodel.getInfo', mthd);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = feval('smodel.getInfo', mthd, io(2).sets{kk});
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        % SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        % Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        % SET 'Default'
        if io(3).plists.nparams ~= 2, atest = false; end
        % Check key
        if ~io(3).plists.isparam('params'), atest = false; end
        if ~io(3).plists.isparam('values'), atest = false; end
        % Check default value
        if ~isEmptyCell(io(3).plists.find('params')), atest = false; end
        if ~isEmptyCell(io(3).plists.find('values')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('params'),  {{}}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('values'), {{}}), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
end