% UTP_RATIONAL_RATIONAL a set of UTPs for the rational/rational method
%
% M Hewitson 06-08-08
%
% $Id: utp_rational_rational.m,v 1.18 2011/08/22 05:37:13 hewitson Exp $
%

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

function results = utp_rational_rational(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test objects
    [ra1,ra2,ra3,rav,ram] = get_test_objects_rational;

    % 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 parfrac-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(den || num) 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 num + den constructor
    results = [results utp_21];    % Test history is working with num + den + 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 Pzmodel')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Coefficients')), 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 Parfrac')), 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 Pzmodel'
        pn = 8;
        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 Coefficients'
        pn = 9;
        if io(pn).plists.nparams ~= 7, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('num'), atest = false; end
        if ~io(pn).plists.isparam('den'), atest = false; end
        if ~io(pn).plists.isparam('name'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(pn).plists.find('num')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('den')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('num'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('den'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('name'), {''}), atest = false; end
        %%%%%%%%%%   SET 'From Parfrac'
        pn = 10;
        if io(pn).plists.nparams ~= 6, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('parfrac'), atest = false; end
        % Check default value
        if ~eq(io(pn).plists.find('parfrac'), parfrac(), ple1), atest = false; end
        % 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 rational method works with a vector of RATIONAL objects
  % as input.
  %
  % </TestDescription>
  function result = utp_02

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

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

    % <AlgoDescription>
    %
    % 1) Check that the shape of the output RATIONALs is the same as the
    %    input shape. 
    % 2) Check that each output RATIONAL is a copy of the input RATIONAL.
    % 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(rav), 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(rav(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(rav(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(rav(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 rational method works with a matrix of RATIONAL objects
  % as input.
  %
  % </TestDescription>
  function result = utp_03

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

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

    % <AlgoDescription>
    %
    % 1) Check that the shape of the output RATIONALs is the same as the
    %    input shape.
    % 2) Check that each output RATIONAL is a copy of the input RATIONAL.
    % 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(ram), 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(ram(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(ram(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(ram(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 rational method works with a list of RATIONAL objects as
  % input.
  %
  % </TestDescription>
  function result = utp_04

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

    try
      % <SyntaxCode>
      out = rational(ra1,ra2,ra3);
      % </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 RATIONAL is a copy of the input RATIONAL.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pfin = [ra1,ra2,ra3];
    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 rational method works with a mix of different shaped
  % RATIONALs as input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      out   = rational(ra1,rav,ra2,ram,ra3);
      % </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 RATIONAL is a copy of the input RATIONAL.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pfin = [ra1, reshape(rav, 1, []), ra2, reshape(ram, 1, []), ra3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 3+numel(rav)+numel(ram), 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 rational method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

    try
      % <SyntaxCode>
      out  = rational(ra3);
      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
    %    'rational'.
    % 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, '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_06

  %% UTP_07

  % <TestDescription>
  %
  % Tests that the rational 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 = rational(ra1);
      out2 = rational(ra1, ra2);
      out3 = rational(ra1, ra2, ra3);
      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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check the originals
      if strcmp(ra1, 'my name'), atest = false; end
      if strcmp(ra2, 'my name'), atest = false; end
      if strcmp(ra3, '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 rational 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 = 'ra.mat';
      ra = rational(ra3);
      save(ra, filename);

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

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

    atest = true;
    if stest
      % <AlgoCode>
      if ~eq(out, ra, ple1), 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 rational 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 = 'ram.xml';
      amat = rational(ram);
      save(amat, filename);

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

    % <AlgoDescription>
    %
    % 1) 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)
        if ~eq(out(kk), amat(kk), ple1), 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 rational 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>
      sra3         = struct(ra3);
      sra3.iunits  = struct(ra3.iunits);
      sra3.ounits  = struct(ra3.ounits);
      sra3.hist    = struct(ra3.hist);

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a RATIONAL object
      if ~isa(out1,'rational'), atest = false; end
      if ~isa(out2,'rational'), 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 rational method properly applies history to the parfrac
  % constructor.
  %
  % </TestDescription>
  function result = utp_11

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

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a RATIONAL object
      if ~isa(out,'rational'), atest = false; end
      % Check the values of the rationalt object
      [a,b] = residue(pf.res,pf.poles,pf.dir);
      if ~isequal(out.num, a), atest = false; end
      if ~isequal(out.den, b), atest = false; end
      if ~isequal(out.name, sprintf('rational(%s)', 'my parfrac')), 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, 'rational'), atest = false; end
      if ~strcmp(out.hist.inhists.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_11

  %% UTP_12

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a RATIONAL object
      if ~isa(out,'rational'), atest = false; end
      % Check the values of the different proerpties      
      pg = 2*pi*ps(1).f * 2*pi*ps(2).f * 4*pi*pi*ps(3).f*ps(3).f;
      zg = 2*pi*zs(1).f * 4*pi*pi*zs(2).f*zs(2).f;      
      den = poly(vertcat(pzm.poles(:).ri))./pg;
      num = pzm.gain.*poly(vertcat(pzm.zeros(:).ri))./zg;

      if ~isequal(out.den, den),      atest = false; end
      if ~isequal(out.num, num),      atest = false; end
      if ~eq(out.iunits, pzm.iunits), atest = false; end
      if ~eq(out.ounits, pzm.ounits), atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'rational'), 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 rational 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 = 'ra.xml';
      filename2 = 'ra.mat';

      f1 = rational(ra3);
      f2 = rational(ra2);

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

      out1 = rational(plist('filename', filename1));
      out2 = rational(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 last two entries in the history of 'out'
    %    doesn't corresponds to 'rational' and 'save'
    % 3) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % The load object is the same object as the stored object.
      if ~eq(out1, f1, ple1), atest = false; end
      if ~eq(out2, f2, ple1), 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 RATIONAL 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_rational_rational_14: submit rational vector';
      sinfo.experiment_description = 'utp_rational_rational_14: &description';
      sinfo.analysis_description   = '<utp_rational_rational_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(rav, sinfo, plForAutoTest);

      out  = rational(plist('hostname', utpGetHostname, 'database', utpGetDatabase, 'conn', conn, 'id', ids));
      % </SyntaxCode>
      % Close connection
      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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      end
      % Check data values
      if ~eq(out, rav, 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 RATIONAL method properly applies history to the
  % plist(den || num) constructor.
  %
  % </TestDescription>
  function result = utp_15

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

    try
      % <SyntaxCode>
      den    = [1 2 3];
      num    = [4 5 6 7];
      name   = 'my rational';
      iunits = unit('V');
      ounits = unit('Hz');
      pl = plist('den', den, 'num', num, 'name', name, 'iunits', iunits, 'ounits', ounits);
      out = rational(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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check the properties
      if ~isequal(out.den, den),   atest = false; end
      if ~isequal(out.num, num),   atest = false; end
      if ~isequal(out.name, name), atest = false; end
      if ~eq(out.iunits, iunits),  atest = false; end
      if ~eq(out.ounits, ounits),  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 RATIONAL 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   = rational(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
    %    'rational'.
    % 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, 'rational'), 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
      pg = 2*pi*ps(1).f * 2*pi*ps(2).f;
      zg = 2*pi*zs(1).f;
      den = poly(vertcat(pzm.poles(:).ri))./pg;
      num = pzm.gain.*poly(vertcat(pzm.zeros(:).ri))./zg;

      if ~isequal(out.den, den),     atest = false; end
      if ~isequal(out.num, num), 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 RATIONAL method properly applies history to the
  % plist(rational) constructor.
  %
  % </TestDescription>
  function result = utp_17

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

    try
      % <SyntaxCode>
      pf  = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2 3 4], 'my parfrac', unit('V'), unit('Hz'));
      pl  = plist('parfrac', pf);
      out = rational(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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check the next to the last step in the history.
      % It must be the history of the partial fraction model (here 'rational')
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'parfrac'), atest = false; end
      % Check values
      [a,b] = residue(pf.res,pf.poles,pf.dir);
      if ~isequal(out.num, a), atest = false; end
      if ~isequal(out.den, b), atest = false; end
      if ~isequal(out.name, 'rational(my parfrac)'), 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 RATIONAL 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>
      pf  = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2 3 4], 'my parfrac', unit('V'), unit('Hz'));
      pl  = plist('parfrac', pf);
      out1  = rational(plist('plist', pl));
      out2  = rational(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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check the next to the last step in the history.
      % It must be the history of the partial fraction model (here 'rational')
      if ~strcmp(out1.hist.inhists.methodInfo.mname, 'parfrac'), 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, 'rational'), 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 RATIONAL 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>

    try
      % <SyntaxCode>
      conn     = utpGetConnection

      sinfo.conn                   = conn;
      sinfo.experiment_title       = 'utp_rational_rational_19: submit rational';
      sinfo.experiment_description = 'utp_rational_rational_19: description';
      sinfo.analysis_description   = 'utp_rational_rational_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(ra3, sinfo, plForAutoTest);

      out = rational(conn, ids);
      % </SyntaxCode>
      % Close connection
      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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check data values
      if ~eq(out,ra3, ple3), atest = false; end
      % Check the history except the additional 'submit' + 'retrieve' steps
      if ~eq(out.hist.inhists, ra3.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 RATIONAL method properly applies history to the
  % num + den object constructor.
  %
  % </TestDescription>
  function result = utp_20

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

    try
      % <SyntaxCode>
      num = [2 -4 7 -2.5];
      den = [1 9 -12];
      out = rational(num ,den);

      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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check properties
      if ~isequal(out.num, num),  atest = false; end
      if ~isequal(out.den, den), 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 RATIONAL method properly applies history to the
  % num + den + name object constructor.
  %
  % </TestDescription>
  function result = utp_21

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

    try
      % <SyntaxCode>
      num = [2 -4 7 -2.5];
      den = [1 9 -12];
      name = 'my new crazy name';
      out = rational(num, den, 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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check properties
      if ~isequal(out.num, num),   atest = false; end
      if ~isequal(out.den, den),  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 RATIONAL method properly applies history to the
  % num + den + 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>
      num = [2 -4 7 -2.5];
      den = [1 9 -12];
      name = 'I''m crazy';
      iu   = unit('V^2');
      ou   = 's^2/3';
      out  = rational(num, den, 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
    %    'rational'.
    % 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, 'rational'), atest = false; end
      % Check properties
      if ~isequal(out.num, num),   atest = false; end
      if ~isequal(out.den, den),  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
