% UTP_PZMODEL_MRDIVIDE a set of UTPs for the pzmodel/mrdivide method
%
% M Hewitson 06-08-08
%
% $Id: utp_pzmodel_mrdivide.m,v 1.2 2009/07/23 18:56:38 ingo Exp $
%

% <MethodDescription>
%
% The mrdivide method of the pzmodel class overloads the division operator
% for PZMODELs.
%
% </MethodDescription>

function results = utp_pzmodel_mrdivide(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'pzmodel';
    mthd    = 'mrdivide';

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

    % 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 with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the I-/O-units

    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 mrdivide method works with a vector of PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the mrdivide method works for a vector of PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-4));
      pz2 = pzmodel(3, pz(2), pz(-3));
      pz3 = pzmodel(4, pz(3), pz(-2));
      pz4 = pzmodel(5, pz(4), pz(-1));
      out1 = mrdivide(pz1, [pz2, pz3, pz4 pz1]);
      out2 = pz1 / [pz2, pz3, pz4 pz1];
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one PZMODEL object.
    % 2) Check the gain of the output
    % 3) Check the poles of the output
    % 4) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if numel(out1) ~= 1, atest = false; end
      if numel(out2) ~= 1, atest = false; end
      % Check 'gain'
      if out1.gain ~= (2 / 3 / 4 / 5 / 2), atest = false; end
      if out2.gain ~= (2 / 3 / 4 / 5 / 2), atest = false; end
      % Check 'poles'
      if ~eq(out1.poles, [pz(1), pz(-3), pz(-2), pz(-1), pz(-4)]), atest = false; end
      if ~eq(out2.poles, [pz(1), pz(-3), pz(-2), pz(-1), pz(-4)]), atest = false; end
      % Check 'zeros'
      if ~eq(out1.zeros, [pz(-4), pz(2), pz(3), pz(4), pz(1)]), atest = false; end
      if ~eq(out2.zeros, [pz(-4), pz(2), pz(3), pz(4), pz(1)]), 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 mrdivide method works with a matrix of PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Tests that the rdivie method works with a matrix of PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-4));
      pz2 = pzmodel(3, pz(2), pz(-3));
      pz3 = pzmodel(4, pz(3), pz(-2));
      pz4 = pzmodel(5, pz(4), pz(-1));
      out1 = mrdivide(pz1, [pz4, pz3, pz2; pz1 pz4 pz3]);
      out2 = pz1 / [pz4, pz3, pz2; pz1 pz4 pz3];
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one PZMODEL object.
    % 2) Check the gain of the output
    % 3) Check the poles of the output
    % 4) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if numel(out1) ~= 1, atest = false; end
      if numel(out2) ~= 1, atest = false; end
      % Check 'gain'
      if out1.gain ~= (2 / 5 / 2 / 4 / 5 / 3 / 4), atest = false; end
      if out2.gain ~= (2 / 5 / 2 / 4 / 5 / 3 / 4), atest = false; end
      % Check 'poles'
      if ~eq(out1.poles, [pz(1), pz(-1), pz(-4), pz(-2), pz(-1), pz(-3), pz(-2)]), atest = false; end
      if ~eq(out2.poles, [pz(1), pz(-1), pz(-4), pz(-2), pz(-1), pz(-3), pz(-2)]), atest = false; end
      % Check 'zeros'
      if ~eq(out1.zeros, [pz(-4), pz(4), pz(1), pz(3), pz(4), pz(2), pz(3)]), atest = false; end
      if ~eq(out2.zeros, [pz(-4), pz(4), pz(1), pz(3), pz(4), pz(2), pz(3)]), 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 mrdivide method works with a list of PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Tests that the mrdivide method works with a list of PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-4));
      pz2 = pzmodel(3, pz(2), pz(-3));
      pz3 = pzmodel(4, pz(3), pz(-2));
      out1 = mrdivide(pz1, pz2, pz3);
      out2 = pz1 / pz2 / pz3;
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one PZMODEL object.
    % 2) Check the gain of the output
    % 3) Check the poles of the output
    % 4) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if numel(out1) ~= 1, atest = false; end
      if numel(out2) ~= 1, atest = false; end
      % Check 'gain'
      if out1.gain ~= (2 / 3 / 4), atest = false; end
      if out2.gain ~= (2 / 3 / 4), atest = false; end
      % Check 'poles'
      if ~eq(out1.poles, [pz(1), pz(-3), pz(-2)]), atest = false; end
      if ~eq(out2.poles, [pz(1), pz(-3), pz(-2)]), atest = false; end
      % Check 'zeros'
      if ~eq(out1.zeros, [pz(-4), pz(2), pz(3)]), atest = false; end
      if ~eq(out2.zeros, [pz(-4), pz(2), pz(3)]), atest = false; end
      % </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 mrdivide method works with a mix of different shaped
  % PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-4));
      pz2 = pzmodel(3, pz(2), pz(-3));
      pz3 = pzmodel(4, pz(3), pz(-2));
      pz4 = pzmodel(5, pz(4), pz(-1));
      out1 = mrdivide(pz1, [pz2 pz3], pz4);
      out2 = pz1 / [pz2 pz3] / pz4;
      mout = rebuild(out2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one PZMODEL object.
    % 2) Check the gain of the output
    % 3) Check the poles of the output
    % 4) Check the zeros of the output
    % 5) Check the rebuilt object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if numel(out1) ~= 1, atest = false; end
      if numel(out2) ~= 1, atest = false; end
      % Check 'gain'
      if out1.gain ~= (2 / 3 / 4 / 5), atest = false; end
      if out2.gain ~= (2 / 3 / 4 / 5), atest = false; end
      % Check 'poles'
      if ~eq(out1.poles, [pz(1), pz(-3), pz(-2), pz(-1)]), atest = false; end
      if ~eq(out2.poles, [pz(1), pz(-3), pz(-2), pz(-1)]), atest = false; end
      % Check 'zeros'
      if ~eq(out1.zeros, [pz(-4), pz(2), pz(3), pz(4)]), atest = false; end
      if ~eq(out2.zeros, [pz(-4), pz(2), pz(3), pz(4)]), atest = false; end
      % Check the rebuilt object
      if ~eq(mout, out2, ple2), 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 mrdivide method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the mrdivide method can be processed
    % back.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-4));
      pz2 = pzmodel(3, pz(2), pz(-3));
      out  = mrdivide(pz1, pz2);
      mout = rebuild(out);
      % </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
    %    'mrdivide'.
    % 2) Check that re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'rdivide'), atest = false; end
      % The rebuilt object must be the same as 'out'
      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_06

  %% UTP_07

  % <TestDescription>
  %
  % Check that the mrdivide method only divide PZMODELs with the same output
  % units.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Check that the mrdivide method only divide PZMODELs with the same
    % output units. Check also the negative case.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, pz(1), pz(-3));
      pz2 = pzmodel(3, pz(2), pz(-2));
      pz3 = pzmodel(4, pz(3), pz(-1));
      pz1.setOunits('Hz Hz^-1/2');
      pz2.setOunits('Hz Hz^-1/2');
      pz3.setOunits('Hz Hz^-1/2');
      pz1.setIunits('m');
      pz2.setIunits('Hz Hz^-1/2');
      pz3.setIunits('Hz Hz^-1/2');
      out = pz1 / pz2 / pz3;
      mout = rebuild(out);
      % Negative case
      pz2.setOunits('m');
      try
        out = pz1 / pz2;
        stest = false;
      catch
        stest = true;
      end
      % </SyntaxCode>
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the I-/O-units
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the I-/O-units
      if ~eq(out.iunits, unit('m')), atest = false; end
      if ~eq(out.ounits, unit('Hz Hz^-1/2')), atest = false; end
      % Check the re-built object.
      if ~eq(mout, mout, 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
