% UTP_MFIR_INDEX a set of UTPs for the mfir/index method
%
% M Hewitson 06-08-08
%
% $Id: utp_mfir_index.m,v 1.4 2009/07/28 17:12:37 ingo Exp $
%

% <MethodDescription>
%
% The index method of the mfir class index into a MFIR vector or
% matrix. This properly captures the history.
%
% </MethodDescription>

function results = utp_mfir_index(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'mfir';
    mthd    = 'index';

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

    % Test MFIR objects
    [fir1,fir2,fir3,fir4,fir5,fir6,firv,firm] = get_test_objects_mfir;

    % 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];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test history is working
    results = [results utp_06];    % Test the modify call works
    results = [results utp_07];    % Test with plist
    results = [results utp_08];    % Test select more objects with an index

    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

  %% 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 ~= 2, atest = false; end
        % Check key
        if ~io(3).plists.isparam('i'), atest = false; end
        if ~io(3).plists.isparam('j'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(3).plists.find('i')), atest = false; end
        if ~isEmptyDouble(io(3).plists.find('j')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('i'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('j'), {[]}), 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_02

  % <TestDescription>
  %
  % Tests that the index method works with a vector of MFIR objects as
  % input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the index method works for a vector of MFIR objects as
    % input. The following indexing should work:
    % I = [ 1 2 3 ] or (I/J) = [(1,1), (1,2), (1,3)]
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      objvec = [fir1, fir2, fir3];
      out1 = objvec.index(1);
      out2 = objvec.index(3);
      out3 = objvec.index(1,2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the index method selects the correct object.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if ~eq(out1, fir1, ple3), atest = false; end
      if ~eq(out2, fir3, ple3), atest = false; end
      if ~eq(out3, fir2, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the index method works with a matrix of MFIR objects as
  % input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the index method works for a matrix of MFIR objects as
    % input. The following indexing should work:
    % I = [ 1 3 5 ] or (I/J) = [(1,1), (1,2), (1,3)]
    %     [ 2 4 6 ]            [(2,1), (2,2), (2,3)]
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      objmat = [fir1, fir2, fir3; ...
                fir3, fir1, fir2];
      out1 = objmat.index(5);
      out2 = objmat.index(4);
      out3 = objmat.index(1,2);
      out4 = objmat.index(2,1);
      out5 = objmat.index(2,2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the index method selects the correct object.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if ~eq(out1, fir3, ple3), atest = false; end
      if ~eq(out2, fir1, ple3), atest = false; end
      if ~eq(out3, fir2, ple3), atest = false; end
      if ~eq(out4, fir3, ple3), atest = false; end
      if ~eq(out5, fir1, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_04

  % <TestDescription>
  %
  % Tests that the index method works with a list of MFIR objects as
  % input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % The index method doesn't work for a list of MFIR objects as input. 
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = index(fir1,fir2,fir3, 4);
      % </SyntaxCode>
      stest = false;
    catch
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to test.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_05

  % <TestDescription>
  %
  % Tests that the index method properly applies history.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the result of index have an additional history step.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      i = 2;
      j = 3;
      objmat = [fir1, fir2, fir3; ...
                fir3, fir1, fir2];
      out  = index(objmat, i, j);
      % </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 'index'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'index'), atest = false; end
      % Check that the history-plist contains the used indices.
      pl = out.hist.plistUsed;
      if pl.find('i') ~= i, atest = false; end
      if pl.find('j') ~= j, atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_06

  % <TestDescription>
  %
  % Tests that the index method works for the modifier command.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Tests that the index method works for the modifier command.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      i = 1;
      j = 1;
      objmat = [fir1, fir2, fir3; ...
                fir3, fir1, fir2];
      out1 = index(objmat, i, j);
      out2 = objmat.index(i,j);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the history-plist contains the used indices.
    % 2) Check that the index method selects the correct object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the history-plist contains the used indices.
      pl = out1.hist.plistUsed;
      if pl.find('i') ~= i, atest = false; end
      if pl.find('j') ~= j, atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out1, fir1, ple3), atest = false; end
      % Check that the history-plist contains the used indices.
      pl = out2.hist.plistUsed;
      if pl.find('i') ~= i, atest = false; end
      if pl.find('j') ~= j, atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out2, fir1, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_07

  % <TestDescription>
  %
  % Control the method with a plist.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the index method can be controled with a plist.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      i1 = 6;
      i2 = 1;
      j2 = 3;
      objmat = [fir1, fir2, fir3; ...
                fir3, fir1, fir2];
      pl1 = plist('i', i1);
      pl2 = plist('i', i2, 'j', j2);
      out1 = index(objmat, pl1);
      out2 = index(objmat, pl2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the history-plist contains the used indices.
    % 2) Check that the index method selects the correct object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the history-plist contains the used indices.
      pl = out1.hist.plistUsed;
      if ~isequal(pl.find('i'), i1), atest = false; end
      if ~isequal(pl.find('j'), []), atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out1, fir2, ple3), atest = false; end
      % Check that the history-plist contains the used indices.
      pl = out2.hist.plistUsed;
      if ~isequal(pl.find('i'), i2), atest = false; end
      if ~isequal(pl.find('j'), j2), atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out2, fir3, ple3), 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>
  %
  % Test that the index method selects more objects if I have more indices.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the index method selects more objects if I have more indices.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      i = [2 6];
      objmat = [fir1, fir2, fir3; ...
                fir3, fir1, fir2];
      pl = plist('i', i);
      out1 = objmat.index(i);
      out2 = objmat.index(pl);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the history-plist contains the used indices.
    % 2) Check that the index method selects the correct object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the history-plist contains the used indices.
      pl = out1(1).hist.plistUsed;
      if ~isequal(pl.find('i'), i), atest = false; end
      if ~isequal(pl.find('j'), []), atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out1(1), fir3, ple3), atest = false; end
      if ~eq(out1(2), fir2, ple3), atest = false; end
      % Check that the history-plist contains the used indices.
      pl = out2(2).hist.plistUsed;
      if ~isequal(pl.find('i'), i), atest = false; end
      if ~isequal(pl.find('j'), []), atest = false; end
      % Check that the index method selects the correct object
      if ~eq(out2(1), fir3, ple3), atest = false; end
      if ~eq(out2(2), fir2, ple3), 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
