% UTP_PARFRAC_PARFRAC a set of UTPs for the parfrac/parfrac method
%
% M Hewitson 06-08-08
%
% $Id: utp_parfrac_parfrac.m,v 1.21 2011/08/22 05:37:13 hewitson Exp $
%

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

function results = utp_parfrac_parfrac(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test objects
    [pf1,pf2,pf3,pfv,pfm] = get_test_objects_parfrac;

    % 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 with empty constructor
    results = [results utp_07];    % Test history is working with copy constructor
    results = [results utp_08];    % Test history is working with MAT file constructor
    results = [results utp_09];    % Test history is working with XML file constructor
    results = [results utp_10];    % Test history is working with struct constructor
    results = [results utp_11];    % Test history is working with rational-object constructor
    results = [results utp_12];    % Test history is working with pzmodel-object constructor
    results = [results utp_13];    % Test history is working with plist(filename) constructor
    results = [results utp_14];    % Test history is working with plist(hostname) constructor
    results = [results utp_15];    % Test history is working with plist(res || poles || dir) constructor
    results = [results utp_16];    % Test history is working with plist(pzmodel) constructor
    results = [results utp_17];    % Test history is working with plist(rational) constructor
    results = [results utp_18];    % Test history is working with plist(plist) constructor
    results = [results utp_19];    % Test history is working with conn+Id constructor
    results = [results utp_20];    % Test history is working with res + poles + dir constructor
    results = [results utp_21];    % Test history is working with res + poles + dir + name constructor
    results = [results utp_22];    % Test history is working with res + poles + dir + name + iunits + ounits 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 Rational')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Pzmodel')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Residuals/Poles/Direct')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Repository')), 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 Built-in Model')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 3, atest = false; end
        % Check key
        % Check default value
        % Check options
        %%%%%%%%%%   SET 'From MAT File'
        pn = 4;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('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('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('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('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 ~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
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('id'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('cid'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('binary'), {'yes', 'no'}), 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('built-in'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('built-in')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('built-in'), {''}), atest = false; end
        %%%%%%%%%%   SET 'From Rational'
        pn = 8;
        if io(pn).plists.nparams ~= 6, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('rational'), atest = false; end
        % Check default value
        if ~eq(io(pn).plists.find('rational'), rational(), ple1), atest = false; end
        % Check options
        %%%%%%%%%%   SET 'From Pzmodel'
        pn = 9;
        if io(pn).plists.nparams ~= 6, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('pzmodel'), atest = false; end
        % Check default value
        if ~eq(io(pn).plists.find('pzmodel'), pzmodel(), ple1), atest = false; end
        % Check options
        %%%%%%%%%%   SET 'From Residuals/Poles/Direct'
        pn = 10;
        if io(pn).plists.nparams ~= 8, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('res'), atest = false; end
        if ~io(pn).plists.isparam('poles'), atest = false; end
        if ~io(pn).plists.isparam('dir'), atest = false; end
        if ~io(pn).plists.isparam('name'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(pn).plists.find('res')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('poles')), atest = false; end
        if ~isequal(io(pn).plists.find('dir'), 0), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('res'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('poles'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('dir'), {0}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('name'), {''}), 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 parfrac method works with a vector of PARFRAC objects as
  % input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the parfrac method works with a vector of PARFRAC objects
    % as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out  = parfrac(pfv);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shape of the output PARFRACs is the same as the
    %    input shape. 
    % 2) Check that each output PARFRAC is a copy of the input PARFRAC.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct shape
      if size(out) ~= size(pfv), atest = false; end
      % Check that the output is a copy.
      for ii = 1:numel(out)
        % Check that the output is the same except the history
        if ~eq(pfv(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pfv(ii).hist, out(ii).hist.inhists), atest = false; end
        % Change the output to make sure that it is a 'real' copy
        out(ii).setDescription('my desc');
        if eq(pfv(ii), out(ii), ple3), atest = false; end
      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 parfrac method works with a matrix of PARFRAC objects as
  % input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the parfrac method works with a matrix of PARFRAC objects
    % as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out  = parfrac(pfm);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shape of the output PARFRACs is the same as the
    %    input shape.
    % 2) Check that each output PARFRAC is a copy of the input PARFRAC.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if size(out) ~= size(pfm), atest = false; end
      % Check that the output is a copy.
      for ii = 1:numel(out)
        % Check that the output is the same except the history
        if ~eq(pfm(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pfm(ii).hist, out(ii).hist.inhists), atest = false; end
        % Change the output to make sure that it is a 'real' copy
        out(ii).setDescription('my desc');
        if eq(pfm(ii), out(ii), ple3), atest = false; end
      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 parfrac method works with a list of PARFRAC objects as
  % input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the parfrac method works with a list of PARFRAC objects as
    % input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = parfrac(pf1,pf2,pf3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same of the
    %    number in the input.
    % 2) Check that each output PARFRAC is a copy of the input PARFRAC.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pfin = [pf1,pf2,pf3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 3, atest = false; end

      % Check that the output is a copy.
      for ii = 1:numel(out)
        % Check that the output is the same except the history
        if ~eq(pfin(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pfin(ii).hist, out(ii).hist.inhists), atest = false; end
        % Change the output to make sure that it is a 'real' copy
        out(ii).setDescription('my desc');
        if eq(pfin(ii), out(ii), ple3), atest = false; end
      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 parfrac method works with a mix of different shaped
  % PARFRACs as input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the parfrac method works with a mix of different shaped
    % PARFRACs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out   = parfrac(pf1,pfv,pf2,pfm,pf3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same of the
    %    number in the input.
    % 2) Check that each output PARFRAC is a copy of the input PARFRAC.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pfin = [pf1, reshape(pfv, 1, []), pf2, reshape(pfm, 1, []), pf3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 3+numel(pfv)+numel(pfm), atest = false; end

      % Check that the output is a copy.
      for ii = 1:numel(out)
        % Check that the output is the same except the history
        if ~eq(pfin(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pfin(ii).hist, out(ii).hist.inhists), atest = false; end
        % Change the output to make sure that it is a 'real' copy
        out(ii).setDescription('my desc');
        if eq(pfin(ii), out(ii), ple3), atest = false; end
      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 parfrac method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

    try
      % <SyntaxCode>
      out = parfrac(pf1);
      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
    %    'parfrac'.
    % 2) Check that the method rebuild produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the rebuilt object
      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>
  %
  % Tests that the parfrac method properly applies history to the copy
  % constructor.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the output can be processed back with the 'rebuild' method.
    % Test the constructor with a different number of inputs.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out1 = parfrac(pf1);
      out2 = parfrac(pf1, pf2);
      out3 = parfrac(pf1, pf2, pf3);
      out1.setName('my name');
      out2(1).setName('my name');
      out2(2).setName('my name');
      out3(1).setName('my name');
      out3(2).setName('my name');
      out3(3).setName('my name');
      mout = rebuild(out1);
      % </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
    %    'parfrac'.
    % 2) Check that the original objects are not changed by the setter
    %    function
    % 3) Check that the method rebuild produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      % It is the method 'setName' because we set it in above
      if ~strcmp(out1.hist.methodInfo.mname, 'setName'), atest = false; end
      % Check next to the last step in the history of 'out'
      if ~strcmp(out1.hist.inhists.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the originals
      if strcmp(pf1, 'my name'), atest = false; end
      if strcmp(pf2, 'my name'), atest = false; end
      if strcmp(pf3, 'my name'), atest = false; end
      % Check the rebuilt object
      if ~eq(mout, out1, 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 that the parfrac method properly applies history to the read
  % MAT-file constructor.
  %
  % </TestDescription>
  function result = utp_08

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

    try
      % <SyntaxCode>
      filename = 'pf.mat';
      pf = parfrac(pf3);
      save(pf, filename);

      out  = parfrac(filename);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the loaded object is the same as the saved object.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the loaded object.
      if ~eq(out, pf), atest = false; end
      % Check the rebuilt object
      if ~eq(mout, out, ple1), atest = false; end
      % </AlgoCode>
      delete(filename);
    else
      atest = false;
    end

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


  %% UTP_09

  % <TestDescription>
  %
  % Tests that the parfrac method properly applies history to the read
  % XML-file constructor.
  %
  % </TestDescription>
  function result = utp_09

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

    try
      % <SyntaxCode>
      filename = 'pfm.xml';
      amat = parfrac(pfm);
      save(amat, filename);

      out  = parfrac(filename);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the loaded object is the same as the saved object.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the algorithm
      for kk = 1:numel(out)
        % Check the loaded object.
        if ~eq(out(kk), amat(kk)), atest = false; end
      end

      % Check the rebuilt object
      for kk = 1:numel(out)
        if ~eq(mout(kk), out(kk), ple2), atest = false; end
      end
      % </AlgoCode>
      delete(filename);
    else
      atest = false;
    end

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

  %% UTP_10

  % <TestDescription>
  %
  % Tests that the parfrac method properly doesn't apply history to the
  % struct constructor.
  %
  % </TestDescription>
  function result = utp_10

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

    try
      % <SyntaxCode>
      spf3         = struct(pf3);
      spf3.iunits  = struct(pf3.iunits);
      spf3.ounits  = struct(pf3.ounits);
      spf3.hist    = struct(pf3.hist);

      out1 = parfrac(struct(pf2));
      out2 = parfrac(spf3);
      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      % </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 'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PARFRAC object
      if ~isa(out1,'parfrac'), atest = false; end
      if ~isa(out2,'parfrac'), atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out1.hist.methodInfo.mname, 'setName'), atest = false; end
      if ~strcmp(out2.hist.methodInfo.mname, 'setName'), atest = false; end
      % Check the rebuilt object
      if ~eq(mout1, out1, ple2), atest = false; end
      if ~eq(mout2, 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_10

  %% UTP_11

  % <TestDescription>
  %
  % Tests that the parfrac method properly applies history to the rational
  % constructor.
  %
  % </TestDescription>
  function result = utp_11

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

    try
      % <SyntaxCode>
      ra   = rational([1 2 3], [4 5 6 7], 'my rational', unit('V'), unit('Hz'));
      out  = parfrac(ra);
      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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PARFRAC object
      if ~isa(out,'parfrac'), atest = false; end
      % Check the values of the parfract object
      [res, poles, dterms, pmul] = utils.math.cpf('INOPT', 'RAT', 'NUM', ra.num, 'DEN', ra.den, 'MODE', 'SYM');
      if ~isequal(out.res, res), atest = false; end
      if ~isequal(out.poles, poles), atest = false; end
      if ~isequal(out.dir, dterms), atest = false; end
      if ~isequal(out.pmul, pmul), atest = false; end
      if ~eq(out.iunits, unit('V')), atest = false; end
      if ~eq(out.ounits, unit('Hz')), atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'rational'), atest = false; end
      % Check the rebuilt object
      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_11

  %% UTP_12

  % <TestDescription>
  %
  % Tests that the parfrac method properly applies history to the pzmodel
  % constructor.
  %
  % </TestDescription>
  function result = utp_12

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

    try
      % <SyntaxCode>
      ps  = [pz(1) pz(200)];
      zs  =  pz(50);
      pzm = pzmodel(1, ps, zs, unit('Hz^2'), unit('V^2'));
      out = parfrac(pzm);
      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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PARFRAC object
      if ~isa(out,'parfrac'), atest = false; end
      % Check the values of the different proerpties
      gs = utils.math.getk(vertcat(zs(:).ri),vertcat(ps(:).ri),pzm.gain);
      [res, poles, dterms, pmul] = utils.math.cpf('INOPT', 'PZ', 'POLES', vertcat(ps(:).ri), 'ZEROS', vertcat(zs(:).ri), 'GAIN', gs, 'MODE', 'SYM');
      if ~isequal(out.res, res),     atest = false; end
      if ~isequal(out.poles, poles), atest = false; end
      if ~isequal(out.dir, dterms),  atest = false; end
      if ~isequal(out.pmul, pmul),   atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'pzmodel'), atest = false; end
      % Check the rebuilt object
      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_12

  %% UTP_13

  % <TestDescription>
  %
  % Tests that the parfrac method properly applies history to the
  % plist(filename) constructor.
  %
  % </TestDescription>
  function result = utp_13

    % <SyntaxDescription>
    %
    % Test that the output can be processed back to an m-file.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      filename1 = 'pf.xml';
      filename2 = 'pf.mat';

      f1 = parfrac(pf3);
      f2 = parfrac(pf2);

      save(f1, filename1);
      save(f2, filename2);

      out1 = parfrac(plist('filename', filename1));
      out2 = parfrac(plist('filename', filename2));

      rout1 = out1.rebuild;
      rout2 = out2.rebuild;
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the save method doesn't change the input object
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the input object of the save method
      if ~eq(f1, pf3, ple3), atest = false; end
      if ~eq(f1.hist.inhists, pf3.hist) , atest = false; end
      if ~eq(f2, pf2, ple3) , atest = false; end
      if ~eq(f2.hist.inhists, pf2.hist)  , atest = false; end
      % The load doesn't have two additionally history steps (save + load)
      if ~eq(out1, f1), atest = false; end
      if ~eq(out2, f2), atest = false; end
      % Rebuild object and check the result
      if ~eq(rout1, out1, ple2), atest = false; end
      if ~eq(rout2, out2, ple2), atest = false; end
      % </AlgoCode>
      % delete test file
      delete(filename1)
      delete(filename2)
    else
      atest = false;
    end

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

  %% UTP_14

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % plist(conn) constructor.
  %
  % </TestDescription>
  function result = utp_14

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

    try
      % <SyntaxCode>
      conn = utpGetConnection();

      sinfo.conn                   = conn;
      sinfo.experiment_title       = 'utp_parfrac_parfrac_14: submit parfrac vector';
      sinfo.experiment_description = 'utp_parfrac_parfrac_14: &description';
      sinfo.analysis_description   = '<utp_parfrac_parfrac_14>';
      sinfo.quantity               = 'none';
      sinfo.keywords               = 'none';
      sinfo.reference_ids          = '';
      sinfo.additional_comments    = 'none';
      sinfo.additional_authors     = 'no one';

      plForAutoTest = plist('no dialog', true, 'use selector', false);
      ids = submit(pfv, sinfo, plForAutoTest);

      out  = parfrac(plist('hostname', utpGetHostname, 'database', utpGetDatabase, 'conn', conn, 'id', ids));
      % </SyntaxCode>
      utpCloseConnection(conn);
      stest = true;
    catch err
      disp(err.message)
      % Close connection
      utpCloseConnection(conn);
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      for kk = 1:numel(out)
        if ~strcmp(out(kk).hist.methodInfo.mname, 'parfrac'), atest = false; end
      end
      % Check data values
      if ~eq(out, pfv, ple3), atest = false; end
      % Rebuild object and check the result
      mout = rebuild(out);
      if ~eq(mout, out, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_15

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % plist(res || poles || dir) constructor.
  %
  % </TestDescription>
  function result = utp_15

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

    try
      % <SyntaxCode>
      res = [1; 2+1i; 2-1i];
      ps  = [6; 1+3i; 1-3i];
      d   = [1; 2];
      pl = plist('res', res, 'poles', ps, 'dir', d);
      out = parfrac(pl);
      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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the properties
      if ~isequal(out.res, res),  atest = false; end
      if ~isequal(out.poles, ps), atest = false; end
      if ~isequal(out.dir, d),    atest = false; end
      % Rebuild object and check the result
      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_15

  %% UTP_16

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % plist(pzmodel) constructor.
  %
  % </TestDescription>
  function result = utp_16

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

    try
      % <SyntaxCode>
      ps   = [pz(1) pz(200)];
      zs   = pz(50);
      pzm  = pzmodel(1, ps, zs, unit('Hz'), unit('V'));
      pl   = plist('pzmodel', pzm);
      out   = parfrac(pl);

      rout = 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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the next to the last step in the history.
      % It must be the history of the pole/zero model (here 'pzmodel')
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'pzmodel'), atest = false; end
      % Check values
      gs = utils.math.getk(vertcat(zs(:).ri),vertcat(ps(:).ri),pzm.gain);
      [res, poles, dterms, pmul] = utils.math.cpf('INOPT', 'PZ', 'POLES', vertcat(ps(:).ri), 'ZEROS', vertcat(zs(:).ri), 'GAIN', gs, 'MODE', 'SYM');
      if ~isequal(out.res, res),     atest = false; end
      if ~isequal(out.poles, poles), atest = false; end
      if ~isequal(out.dir, dterms),  atest = false; end
      if ~isequal(out.pmul, pmul),   atest = false; end
      if ~eq(out.iunits, unit('Hz')), atest = false; end
      if ~eq(out.ounits, unit('V')), atest = false; end
      % Rebuild object and check the result
      if ~eq(rout, out, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_17

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % plist(parfrac) constructor.
  %
  % </TestDescription>
  function result = utp_17

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

    try
      % <SyntaxCode>
      ra  = rational([1 2 3], [4 5 6 7], 'my rational', unit('V'), unit('Hz'));
      pl  = plist('rational', ra);
      out = parfrac(pl);

      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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the next to the last step in the history.
      % It must be the history of the partial fraction model (here 'parfrac')
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'rational'), atest = false; end
      % Check values
      [res, poles, dterms, pmul] = utils.math.cpf('INOPT', 'RAT', 'NUM', ra.num, 'DEN', ra.den, 'MODE', 'SYM');
      if ~isequal(out.res, res), atest = false; end
      if ~isequal(out.poles, poles), atest = false; end
      if ~isequal(out.dir, dterms), atest = false; end
      if ~isequal(out.pmul, pmul), atest = false; end
      if ~eq(out.iunits, unit('V')), atest = false; end
      if ~eq(out.ounits, unit('Hz')), atest = false; end
      % Rebuild object and check the result
      if ~eq(mout, out, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_18

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % plist(<plist-object>) constructor.
  %
  % </TestDescription>
  function result = utp_18

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

    try
      % <SyntaxCode>
      ra  = rational([1 2 3], [4 5 6 7], 'my rational', unit('V'), unit('Hz'));
      pl  = plist('rational', ra);
      out1  = parfrac(plist('plist', pl));
      out2  = parfrac(plist('plist', plist())); % empty plist

      rout1 = rebuild(out1);
      rout2 = rebuild(out2);
      % </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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out1'
      if ~strcmp(out1.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the next to the last step in the history.
      % It must be the history of the partial fraction model (here 'parfrac')
      if ~strcmp(out1.hist.inhists.methodInfo.mname, 'rational'), atest = false; end
      % Check values
      if ~eq(out1.iunits, unit('V')), atest = false; end
      if ~eq(out1.ounits, unit('Hz')), atest = false; end
      % Check the last step in the history of 'out2'
      if ~strcmp(out2.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check the next to the last step in the history.
      if ~isempty(out2.hist.inhists), atest = false; end
      % Rebuild object and check the result
      if ~eq(rout1, out1, ple1), atest = false; end
      if ~eq(rout2, out2, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_19

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the conn+Id
  % constructor.
  %
  % </TestDescription>
  function result = utp_19

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

    conn     = utpGetConnection
    try
      % <SyntaxCode>

      sinfo.conn                   = conn;
      sinfo.experiment_title       = 'utp_parfrac_parfrac_19: submit parfrac';
      sinfo.experiment_description = 'utp_parfrac_parfrac_19: description';
      sinfo.analysis_description   = 'utp_parfrac_parfrac_19';
      sinfo.quantity               = 'none';
      sinfo.keywords               = 'none';
      sinfo.reference_ids          = '';
      sinfo.additional_comments    = 'none';
      sinfo.additional_authors     = 'no one';

      plForAutoTest = plist('no dialog', true, 'use selector', false);
      [ids] = submit(pf3, sinfo, plForAutoTest);

      out = parfrac(conn, ids);
      % </SyntaxCode>
      utpCloseConnection(conn);
      stest = true;
    catch err
      disp(err.message)
      % Close connection
      utpCloseConnection(conn);
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check data values
      if ~eq(out,pf3, ple3), atest = false; end
      % Check the history except the additional 'submit' + 'retrieve' steps
      if ~eq(out.hist.inhists, pf3.hist), atest = false; end
      % Check the rebuilt object
      mout = rebuild(out);
      if ~eq(mout, out, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_20

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % res + poles + direct terms object constructor.
  %
  % </TestDescription>
  function result = utp_20

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

    try
      % <SyntaxCode>
      res = [1; 2+1i; 2-1i];
      ps  = [6; 1+3i; 1-3i];
      d   = [1; 2];
      out = parfrac(res, ps, d);

      rout = 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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check properties
      if ~isequal(out.res, res),  atest = false; end
      if ~isequal(out.poles, ps), atest = false; end
      if ~isequal(out.dir, d),    atest = false; end
      % Rebuild object and check the result
      if ~eq(rout, out, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_21

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % res + poles + direct terms + name object constructor.
  %
  % </TestDescription>
  function result = utp_21

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

    try
      % <SyntaxCode>
      res = [1; 2+1i; 2-1i];
      ps  = [6; 1+3i; 1-3i];
      d   = [1; 2];
      name = 'my new crazy name';
      out = parfrac(res, ps, d, name);

      rout = 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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check properties
      if ~isequal(out.res, res),   atest = false; end
      if ~isequal(out.poles, ps),  atest = false; end
      if ~isequal(out.dir, d),     atest = false; end
      if ~isequal(out.name, name), atest = false; end
      % Rebuild object and check the result
      if ~eq(rout, out, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_22

  % <TestDescription>
  %
  % Tests that the PARFRAC method properly applies history to the
  % res + poles + direct terms + name + iunits + ounits object constructor.
  %
  % </TestDescription>
  function result = utp_22

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

    try
      % <SyntaxCode>
      res  = [1; 2+1i; 2-1i];
      ps   = [6; 1+3i; 1-3i];
      d    = [1; 2];
      name = 'I''m crazy';
      iu   = unit('V^2');
      ou   = 's^2/3';
      out  = parfrac(res, ps, d, name, iu, ou);

      rout = 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
    %    'parfrac'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'parfrac'), atest = false; end
      % Check properties
      if ~isequal(out.res, res),   atest = false; end
      if ~isequal(out.poles, ps),  atest = false; end
      if ~isequal(out.dir, d),     atest = false; end
      if ~isequal(out.name, name), atest = false; end
      if ~eq(out.iunits, iu),      atest = false; end
      if ~eq(out.ounits, unit(ou), ple1), atest = false; end
      % Rebuild object and check the result
      if ~eq(rout, out, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

end
