% UTP_AO_DEMUX a set of UTPs for the ao/demux method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_demux.m,v 1.2 2009/07/20 16:50:08 ingo Exp $
%

% <MethodDescription>
%
% The demux method of the ao class splits the input vector of AOs into a
% number of output AOs. This is a very simple method thus most of standard
% test are not possible.
%
% </MethodDescription>

function results = utp_ao_demux(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'demux';

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

    [at1,at2,at3,at4,at5,at6,atvec,atmat] = get_test_objects_ao;

    % 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];    % Test with non complex data
    results = [results utp_03];    % Negative test: Too few outputs
    results = [results utp_04];    % Negative test: Too many 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

  %% 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 ~= 0, atest = false; end
        % Check key
        % Check default value
        % Check options
      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 demux method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Tests that the demux method works with a mix of different shaped AOs
    % as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Singme AO as input
      o1 = demux(at1);
      % Vector of AOs as input
      [ov1,ov2,ov3] = demux(atvec);
      % Matrix of AOs as input
      [om1,om2,om3,om4,om5,om6] = demux(atmat);
      % List of AOs as input
      [ol1,ol2,ol3] = demux(at1, at2, at3);
      % List mixed AOs as input
      [os1,os2,os3,os4] = demux(at1, atvec);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the output objects.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check single object
      if ~eq(o1, at1), atest = false; end
      % Check vector object
      if ~eq(ov1, atvec(1)), atest = false; end
      if ~eq(ov2, atvec(2)), atest = false; end
      if ~eq(ov3, atvec(3)), atest = false; end
      % Check matrix object
      if ~eq(om1, atmat(1)), atest = false; end
      if ~eq(om2, atmat(2)), atest = false; end
      if ~eq(om3, atmat(3)), atest = false; end
      if ~eq(om4, atmat(4)), atest = false; end
      if ~eq(om5, atmat(5)), atest = false; end
      if ~eq(om6, atmat(6)), atest = false; end
      % Check list object
      if ~eq(ol1, at1), atest = false; end
      if ~eq(ol2, at2), atest = false; end
      if ~eq(ol3, at3), atest = false; end
      % Check mixed object
      if ~eq(os1, at1), atest = false; end
      if ~eq(os2, atvec(1)), atest = false; end
      if ~eq(os3, atvec(2)), atest = false; end
      if ~eq(os4, atvec(3)), 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>
  %
  % Negative test.
  % Check that the demux method throwns an error for too few output
  % variables.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Check that the demux method throwns an error for too few output
    % variables.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      o1 = demux(atvec);
      % </SyntaxCode>
      stest = false;
    catch
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to check.
    %
    % </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_03

  %% UTP_04

  % <TestDescription>
  %
  % Negative test.
  % Check that the demux method throwns an error for too many output
  % variables.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Check that the demux method throwns an error for too few output
    % variables.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      [o1,o2,o3,o4,o5] = demux(atvec);
      % </SyntaxCode>
      stest = false;
    catch
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to check.
    %
    % </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

end
