% UTP_MATRIX_MATRIX a set of UTPs for the matrix/matrix method
%
% M Hewitson 06-08-08
%
% $Id: utp_matrix_matrix.m,v 1.6 2009/07/30 17:27:31 ingo Exp $
%

% <MethodDescription>
%
% The matrix method of the matrix class constructs MATRIX objects.
%
% </MethodDescription>

function results = utp_matrix_matrix(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'matrix';
    mthd    = 'matrix';

    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();

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02(mthd, mav, [], [], ple3)];    % Vector input
    results = [results utp_03(mthd, mam, [], [], ple3)];    % Matrix input
    results = [results utp_04(mthd, ma1, ma2, ma3, [], [], ple3)];    % List input
    results = [results utp_05(mthd, ma1, mav, mam, [], [], ple3)];    % Test with mixed input
    results = [results utp_06(mthd, ma1, [], ple2)];    % Test history is working

    % Old format
    results = [results utp_07];    % Test history is working with different numbers of input

    % constructor specific tests
    results = [results utp_60(class, ma3, ple2)];    % Test history is properly handled with MAT file constructor
    results = [results utp_61(class, ma1, ple2)];    % Test history is properly handled with XML file constructor
    results = [results utp_62(class, ma1, ma2, ple2)];    % Test history is working with struct constructor
    results = [results utp_64(class, ma1, ple1, ple2)]; % Test history is working with plist(filename) constructor
    results = [results utp_65(class, ma1, ple3)]; % Test history is working with plist(hostname) constructor
    results = [results utp_68(class, ma1, ple3)];  % Test history is working with conn+Id constructor
    results = [results utp_70(class, ma1, ple2)];  % Test history is working with plist(plist) constructor
    
    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    elseif strcmpi(varargin{1}, 'needs repository')
      results = 2;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  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 ~any(strcmpi(io(2).sets, 'From XML File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From MAT File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Repository')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Built-in Model')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        pn = 3;
        if io(pn).plists.nparams ~= 5, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('shape'), atest = false; end
        if ~io(pn).plists.isparam('objs'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('shape')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('objs')), atest = false; end
        %%%%%%%%%%   SET 'From MAT File'
        pn = 4;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        %%%%%%%%%%   SET 'From XML File'
        pn = 5;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        %%%%%%%%%%   SET 'From Repository'
        pn = 6;
        if io(pn).plists.nparams ~= 10, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('hostname'), atest = false; end
        if ~io(pn).plists.isparam('id'), atest = false; end
        if ~io(pn).plists.isparam('cid'), atest = false; end
        if ~io(pn).plists.isparam('database'), atest = false; end
        if ~io(pn).plists.isparam('binary'), atest = false; end
        if ~io(pn).plists.isparam('username'), atest = false; end
        if ~io(pn).plists.isparam('password'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('id')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('cid')), atest = false; end
        if ~isequal(io(pn).plists.find('binary'), 'yes'), atest = false; end
        %%%%%%%%%%   SET 'From Built-in Model'
        pn = 7;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('built-in'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('built-in')), atest = false; end
        %%%%%%%%%%   SET 'From Input'
        pn = 8;
        if io(pn).plists.nparams ~= 5, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('plotinfo'), atest = false; end
        if ~io(pn).plists.isparam('shape'), atest = false; end
        if ~io(pn).plists.isparam('objs'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('description')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('plotinfo')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('shape')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('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 matrix method properly applies history to the
  % constructor with different numbers of inputs.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the output can be processed back with the rebuild method.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1);
      a2 = ao(2);
      a3 = ao(3);
      out1 = matrix(a1);
      out2 = matrix(a1, a2);
      out3 = matrix(a1, a2, a3);
      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      mout3 = rebuild(out3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'matrix'.
    % 2) Check that the rebuilt objects are the same
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out1.hist.methodInfo.mname, 'matrix'), atest = false; end
      if ~strcmp(out2.hist.methodInfo.mname, 'matrix'), atest = false; end
      if ~strcmp(out3.hist.methodInfo.mname, 'matrix'), atest = false; end
      % Check that output contains the right values
      if ~eq(out1.objs, a1), atest = false; end
      if ~eq(out2.objs, [a1, a2]), atest = false; end
      if ~eq(out3.objs, [a1, a2, a3]), atest = false; end
      % Check the rebuilt object
      if ~eq(mout1, out1, ple2), atest = false; end
      if ~eq(mout2, out2, ple2), atest = false; end
      if ~eq(mout3, out3, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07

  
end
