% UTP_MATRIX_SETOBJS a set of UTPs for the matrix/setObjs method
%
% M Hewitson 06-08-08
%
% $Id: utp_matrix_setObjs.m,v 1.2 2010/12/22 16:05:19 ingo Exp $
%

% <MethodDescription>
%
% The setObjs method of the matrix class sets the objs property.
%
% </MethodDescription>

function results = utp_matrix_setObjs(varargin)
  
  % Check the inputs
  if nargin == 0
    
    % Some keywords
    class   = 'matrix';
    mthd    = 'setObjs';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % Test MATRIX objects
    [ma1,ma2,ma3,ma4,mav,mam] = get_test_objects_matrix;
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    pl_generic = plist('objs', [ao(1) ao(2)]);
    
    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02(mthd, mav, @algo_test_plist, pl_generic, ple2)]; % Vector input
    results = [results utp_03(mthd, mam, @algo_test_plist, pl_generic, ple2)]; % Matrix input
    results = [results utp_04(mthd, ma1, ma2, ma3, @algo_test_plist, pl_generic, ple2)]; % List input
    results = [results utp_05(mthd, ma1, mam, mav, @algo_test_plist, pl_generic, ple2)]; % Mixed input
    results = [results utp_06(mthd, ma1, pl_generic, ple2)];    % Test history is working
    
    % Old format
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Set the property without a plist
    results = [results utp_09];    % Test output of the data
    results = [results utp_11(mthd, ma1, ple1, plist('OBJS', 'foo'))];    % Test plotinfo doesn't disappear
    
    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
  
  function atest = algo_test_plist(in, out, pli)
    atest = true;
    if ~eq(out.objs, pli.find('objs'), 'UUID'), atest = false; 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) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.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 ~= 1, atest = false; end
        % Check key
        if ~io(3).plists.isparam('objs'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(3).plists.find('objs')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('objs'), {[]}), 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
  
  %% UTP_07
  
  % <TestDescription>
  %
  % Tests that the setObjs method can modify the input MATRIX.
  %
  % </TestDescription>
  function result = utp_07
    
    % <SyntaxDescription>
    %
    % Test that the setObjs method can modify the input MATRIX by calling
    % with no output.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      pli = plist('objs', ao(1), ao(2));
      obj_modi = copy(ma1,1);
      obj_eq   = copy(ma1,1);

      out = obj_eq.setObjs(pli);
      obj_modi.setObjs(pli);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that 'out' and 'obj_eq' are now different.
    % 2) Check that 'obj_eq' is not changed
    % 3) Check that the modified input is the setObjs value of the copy
    % 4) Check that out and amodi are the same
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % Check that 'out' and 'obj_eq' are now different.
      if eq(out, obj_eq, ple2), atest = false; end
      % Check that 'obj_eq' is not changed
      if ~eq(obj_eq, ma1, ple1), atest = false; end
      % Check that the modified input is correct
      if ~eq(obj_modi.objs, pli.find('objs'), 'UUID'), atest = false; end
      % Check that out and obj_modi are the same
      if ~eq(out, obj_modi, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07
  
  %% UTP_08
  
  % <TestDescription>
  %
  % Tests that the setObjs method can set the property without a plist.
  %
  % </TestDescription>
  function result = utp_08
    
    % <SyntaxDescription>
    %
    % Test that the setObjs method can modify the property 'objs'
    % without a plist.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      objs = [ao(1), ao(2)];
      out = ma1.setObjs(objs);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that 'out' has the correct objs field
    % 2) Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % Check the field 'objs'
      if ~eq(out.objs, objs), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_08
  
  %% UTP_09
  
  % <TestDescription>
  %
  % Check that the setObjs method pass back the output objects to a list of
  % output variables or to a single variable.
  %
  % </TestDescription>
  function result = utp_09
    
    % <SyntaxDescription>
    %
    % Call the method with a list of output variables and with a single output
    % variable. Additionaly check that the rebuild method works on the output.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      [o1, o2] = setObjs(ma1, ma2, [ao(1), ao(2)]);
      o3  = setObjs(ma1, ma2, [ao(1), ao(2)]);
      mout1 = rebuild(o1);
      mout2 = rebuild(o2);
      mout3 = rebuild(o3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that the output contains the right number of objects
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % Check the number of outputs
      if numel(o1) ~=1, atest = false; end
      if numel(o2) ~=1, atest = false; end
      if numel(o3) ~=2, atest = false; end
      % Check that o1, o2 and o3 have the correct data
      if ~eq(o1.objs, [ao(1) ao(2)], ple1), atest = false; end
      if ~eq(o2.objs, [ao(1) ao(2)], ple1), atest = false; end
      if ~eq(o3.objs, [ao(1) ao(2)], ple1), atest = false; end
      % Check the rebuilding of the object
      if ~eq(o1, mout1, ple2), atest = false; end
      if ~eq(o2, mout2, ple2), atest = false; end
      if ~eq(o3, mout3, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_09
  
end
