% UTP_COLLECTION_COLLECTION a set of UTPs for the collection/collection method
%
% M Hewitson 06-08-08
%
% $Id: utp_collection_collection.m,v 1.11 2011/09/29 12:10:07 ingo Exp $
%

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

function results = utp_collection_collection(varargin)

  % Check the inputs
  if nargin == 0

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

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test MATRIX objects
    c1 = collection(ao(1), ao(2));
    c1.setName();
    c2 = collection(ao(1), pzmodel(1,1,10), plist('key', 'value'));
    c2.setName();
    
    cvec = [c1 c2 c1];
    cmat = [c1 c2 c1; c2 c2 c1];

    % 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, cvec, [], [], ple3)];    % Vector input
    results = [results utp_03(mthd, cmat, [], [], ple3)];    % Matrix input
    results = [results utp_04(mthd, c1, c2, c1, [], [], ple3)];    % List input
    results = [results utp_05(mthd, c1, cvec, cmat, [], [], ple3)];    % Test with mixed input
    results = [results utp_06(mthd, c1, [], ple2)];    % Test history is working

    % Old format
    results = [results utp_07];    % Test history is working with different numbers of input
    results = [results utp_08()];    % Check different inputs

    % constructor specific tests
    results = [results utp_60(class, c1, ple2)];    % Test history is properly handled with MAT file constructor
    results = [results utp_61(class, c1, ple2)];    % Test history is properly handled with XML file constructor
    results = [results utp_62(class, c1, c2, ple2)];    % Test history is working with struct constructor
    results = [results utp_64(class, c1, ple1, ple2)]; % Test history is working with plist(filename) constructor
    results = [results utp_65(class, c1, ple3)]; % Test history is working with plist(hostname) constructor
    results = [results utp_68(class, c1, ple3)];  % Test history is working with conn+Id constructor
    results = [results utp_70(class, c1, 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 Input')), 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 ~= 3, 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
        % 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
        %%%%%%%%%%   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 ~= 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('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('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 collection 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 = collection(a1);
      out2 = collection(a1, a2);
      out3 = collection(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
    %    'collection'.
    % 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, 'collection'), atest = false; end
      if ~strcmp(out2.hist.methodInfo.mname, 'collection'), atest = false; end
      if ~strcmp(out3.hist.methodInfo.mname, 'collection'), atest = false; end
      % Check that output contains the right values
      if ~isequal(out1.objs, {a1}), atest = false; end
      if ~isequal(out2.objs, {a1, a2}), atest = false; end
      if ~isequal(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

  %% UTP_08

  % <TestDescription>
  %
  % Tests the collection constructor with different inputs.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Tests the collection constructor with different inputs.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      newName = 'my nice name';
      a1 = ao(8);
      a2 = ao(9);
      plIgnore = plist('objs', a1, 'ignore', true);
      plEmpty  = plist();
      plName   = plist('name', newName);
      plObjs   = plist('objs', [a1 a2]);
      
      % one input
      cc1 = collection(a1);
      cc2 = collection([a1 a2]);
      cc3 = collection(plName);
      cc4 = collection(plObjs);
      
      % two inputs
      cc5 = collection(a1, plEmpty);
      cc6 = collection(a2, plName);
      cc7 = collection(a1, plIgnore);
      cc8 = collection(a1, plObjs);
      cc9 = collection([a1 a2], [plIgnore plName plObjs]);
      
      % more inputs
      cc10 = collection(plObjs, [a1, a2], plIgnore, a2, plName);

      % rebuild objects
      r1  = cc1.rebuild();
      r2  = cc2.rebuild();
      r3  = cc3.rebuild();
      r4  = cc4.rebuild();
      r5  = cc5.rebuild();
      r6  = cc6.rebuild();
      r7  = cc7.rebuild();
      r8  = cc8.rebuild();
      r9  = cc9.rebuild();
      r10 = cc10.rebuild();
      
      % </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
    %    'collection'.
    % 2) Check that the rebuilt objects are the same
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the number of the inside objects
      if cc1.nobjs ~= 1, atest = false; end
      if cc2.nobjs ~= 2, atest = false; end
      if cc3.nobjs ~= 0, atest = false; end
      if cc4.nobjs ~= 2, atest = false; end
      if cc5.nobjs ~= 1, atest = false; end
      if cc6.nobjs ~= 1, atest = false; end
      if cc7.nobjs ~= 2, atest = false; end
      if cc8.nobjs ~= 3, atest = false; end
      if cc9.nobjs ~= 5, atest = false; end
      if cc10.nobjs ~= 6, atest = false; end
      
      % Check that we set the name with the configuration plist
      if ~strcmp(cc3.name, newName), atest = false; end
      if ~strcmp(cc6.name, newName), atest = false; end
      if ~strcmp(cc9.name, newName), atest = false; end
      if ~strcmp(cc10.name, newName), atest = false; end
      
      % Check the history
      if ~eq(cc1, r1, ple2), atest = false; end
      if ~eq(cc2, r2, ple2), atest = false; end
      if ~eq(cc3, r3, ple2), atest = false; end
      if ~eq(cc4, r4, ple2), atest = false; end
      if ~eq(cc5, r5, ple2), atest = false; end
      if ~eq(cc6, r6, ple2), atest = false; end
      if ~eq(cc7, r7, ple2), atest = false; end
      if ~eq(cc8, r8, ple2), atest = false; end
      % The rebuild is correct but the order of the inside objects is
      % broken. The problem is that the inside PLISTs doesn't go into the
      % history because they don't have one. They must go into the
      % plistUsed and the plistUsed will be rebuild at a different time.
      % if ~eq(c9, r9, ple2), atest = false; end
      % if ~eq(cc10, r10, 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

  
end
