% UTP_PZMODEL_PZMODEL a set of UTPs for the pzmodel/pzmodel method
%
% M Hewitson 06-08-08
%
% $Id: utp_pzmodel_pzmodel.m,v 1.31 2011/08/22 05:37:13 hewitson Exp $
%

% <MethodDescription>
%
% The pzmodel method of the pzmodel class constructs PZMODEL objects.
%
% </MethodDescription>

function results = utp_pzmodel_pzmodel(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test PZMODEL objects
    [pz1, pz2, pz3, pz4, pz5, pzvec, pzmat] = get_test_objects_pzmodel;

    % 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 FIL file constructor
    results = [results utp_11];    % Test history is working with struct constructor
    results = [results utp_12];    % Test history is working with constant constructor
    results = [results utp_13];    % Test history is working with rational-object constructor
    results = [results utp_14];    % Test history is working with plist(filename) constructor
    results = [results utp_15];    % Test history is working with plist(hostname) constructor
    results = [results utp_16];    % Test history is working with plist(rational) constructor
    results = [results utp_17];    % Test history is working with plist(gain|poles|zeros) 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 gain + poles + zeros constructor
    results = [results utp_21];    % Test history is working with gain + poles + zeros + name constructor
    results = [results utp_22];    % Test history is working with gain + poles + zeros + 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 XML File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From MAT File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From LISO File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Repository')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Rational')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Poles/Zeros')), 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 LISO File'
        pn = 8;
        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 Rational'
        pn = 9;
        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 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
        %%%%%%%%%%   SET 'From Poles/Zeros'
        pn = 11;
        if io(pn).plists.nparams ~= 9, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('gain'), atest = false; end
        if ~io(pn).plists.isparam('poles'), atest = false; end
        if ~io(pn).plists.isparam('zeros'), atest = false; end
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('iunits'), atest = false; end
        if ~io(pn).plists.isparam('ounits'), atest = false; end
        if ~io(pn).plists.isparam('delay'), atest = false; end
        % Check default value
        if ~isequal(io(pn).plists.find('gain'), 1), atest = false; end
        if ~isempty(io(pn).plists.find('poles')), atest = false; end
        if ~isempty(io(pn).plists.find('zeros')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('name')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('iunits')), atest = false; end
        if ~isEmptyChar(io(pn).plists.find('ounits')), atest = false; end
        if ~isequal(io(pn).plists.find('delay'), 0), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('gain'), {1}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('poles'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('zeros'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('name'), {''}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('iunits'), {''}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('ounits'), {''}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('delay'), {0}), 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 pzmodel method works with a vector of PZMODEL objects as input.
  %
  % </TestDescription>
  function result = utp_02

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

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

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

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

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

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

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

    try
      % <SyntaxCode>
      out = pzmodel(pz5, pz4, pz3);
      % </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 PZMODEL is a copy of the input PZMODEL.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pzin = [pz5, pz4, pz3];
    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(pzin(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pzin(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(pzin(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 pzmodel method works with a mix of different shaped PZMODELs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      out   = pzmodel(pz5,pzvec,pz2,pzmat,pz4);
      % </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 PZMODEL is a copy of the input PZMODEL.
    % 3) Check that the copy have an additional history step.
    %
    % </AlgoDescription>

    atest = true;
    pzin = [pz5, reshape(pzvec, 1, []), pz2, reshape(pzmat, 1, []), pz4];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 3+numel(pzvec)+numel(pzmat), 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(pzin(ii), out(ii), ple3), atest = false; end
        % Check the history
        if ~eq(pzin(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(pzin(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 pzmodel method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

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

  %% UTP_07

  % <TestDescription>
  %
  % Tests that the pzmodel 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 = pzmodel(pz5);
      out2 = pzmodel(pz5, pz4);
      out3 = pzmodel(pz5, pz4, pz3);
      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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check the originals
      if strcmp(pz5, 'my name'), atest = false; end
      if strcmp(pz4, 'my name'), atest = false; end
      if strcmp(pz3, '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 pzmodel 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 = 'test_pzm.mat';
      pzm = pzmodel(pz5);
      save(pzm, filename);

      out  = pzmodel(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 objects
      if ~eq(out, pzm), atest = false; end
      % Check the rebuilt object
      if ~eq(mout, out, ple2), 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 pzmodel 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 = 'test_pzm.xml';
      amat = pzmodel(pzmat);
      save(amat, filename);

      out  = pzmodel(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 objects
        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 pzmodel method properly applies history to the read
  % FIL-file constructor.
  %
  % </TestDescription>
  function result = utp_10

    % <SyntaxDescription>
    %
    % Read the FIL file which is created from LISO.
    % Test that the output can be processed back with the 'rebuild' method.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      filename = 'test_pzm.fil';
      out  = pzmodel(filename);
      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 'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Reading a file adds additionally history steps
      if ~isa(out, '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_10

  %% UTP_11

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

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

    try
      % <SyntaxCode>
      spzm         = struct(pz5);
      spzm.poles(1)= struct(pz5.poles(1)); % pz5 have two poles
      spzm.poles(2)= struct(pz5.poles(2));
      spzm.zeros   = struct(pz5.zeros);
      spzm.iunits  = struct(pz5.iunits);
      spzm.ounits  = struct(pz5.ounits);
      spzm.hist    = struct(pz5.hist);

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PZMODEL object
      if ~isa(out1,'pzmodel'), atest = false; end
      if ~isa(out2,'pzmodel'), 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 algorithm
      if ~eq(pz3, out1), atest = false; end
      if ~eq(pz5, out2), 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_11

  %% UTP_12

  % <TestDescription>
  %
  % Tests that the pzmodel from a constant properly applies history.
  %
  % </TestDescription>
  function result = utp_12

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

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PZMODEL object
      if ~isa(out,'pzmodel'), atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      % Check the algorithm
      if ~eq(mout, out, ple1), atest = false; end
      % Check the rebuilt object
      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_12

  %% UTP_13

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

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

    try
      % <SyntaxCode>
      rat  = rational([1 2 -3], [4 5 -6]); % two real zeros and poles
      out  = pzmodel(rat);
      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 'pzmodel'.
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the output is a PZMODEL object
      if ~isa(out,'pzmodel'), atest = false; end
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      if ~strcmp(out.hist.inhists.methodInfo.mname, 'rational'), atest = false; end
      % Check the rebuilt object
      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_13

  %% UTP_14

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

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

    try
      % <SyntaxCode>
      filename1 = 'test_pzm.xml';
      filename2 = 'test_pzm.mat';
      filename3 = 'test_pzm.fil';

      f1 = pzmodel(pz5);
      f2 = pzmodel(pz4);

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

      out1 = pzmodel(plist('filename', filename1));
      out2 = pzmodel(plist('filename', filename2));
      out3 = pzmodel(plist('filename', filename3));

      rout1 = out1.rebuild;
      rout2 = out2.rebuild;
      rout3 = out3.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, pz5, ple3), atest = false; end
      if ~eq(f1.hist.inhists, pz5.hist) , atest = false; end
      if ~eq(f2, pz4, ple3) , atest = false; end
      if ~eq(f2.hist.inhists, pz4.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
      if ~eq(rout3, out3, 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_14

  %% UTP_15

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

    % <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_pzmodel_pzmodel_15: submit pzmodel vector';
      sinfo.experiment_description = 'utp_pzmodel_pzmodel_15: &description';
      sinfo.analysis_description   = '<utp_pzmodel_pzmodel_15>';
      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(pzmat, sinfo, plForAutoTest);

      out  = pzmodel(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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      end
      % Check data values
      if ~eq(reshape(out, size(pzmat)), pzmat, ple3), atest = false; end
      % Rebuild object and check the result
      rout = rebuild(out);
      if ~eq(rout, 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_15

  %% UTP_16

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

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

    try
      % <SyntaxCode>
      iu   = unit('Hz^2');
      ou   = unit('mm');
      rat  = rational([1 2 -3], [4 5 -6], 'my rat', iu, ou);
      pl   = plist('rational', rat);
      out  = pzmodel(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
    %    'pzmodel'.
    % 2) Check the algorithm
    % 3) 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, 'pzmodel'), atest = false; end
      % Check algorithm
      if numel(out.poles) ~= 2, atest = false; end
      if numel(out.zeros) ~= 2, atest = false; end
      if ~strcmp(out.name, sprintf('pzmodel(%s)',rat.name)), atest = false; end
      if ~eq(out.iunits, iu), atest = false; end
      if ~eq(out.ounits, ou), atest = false; end
      % Rebuild object and check the result
      if ~eq(out, 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_16

  %% UTP_17

  % <TestDescription>
  %
  % Tests that the PZMODEL method properly applies history to the
  % plist(gain|poles|zeros) constructor.
  %
  % </TestDescription>
  function result = utp_17

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

    try
      % <SyntaxCode>
      iu   = unit('Hz^2');
      ou   = unit('mm');
      z    = [pz(1,2), pz(5)];
      p    = pz(1);
      name = 'new name';

      % Create test plists
      pl1 = plist('gain', 12.2, 'zeros', z, 'poles', p, 'iunits', iu, 'ounits', ou, 'name', name);
      pl2 = plist('gain', 12.2);
      pl3 = plist('zeros', z);
      pl4 = plist('poles', p);

      out1 = pzmodel(pl1);
      out2 = pzmodel(pl2);
      out3 = pzmodel(pl3);
      out4 = pzmodel(pl4);

      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      mout3 = rebuild(out3);
      mout4 = rebuild(out4);
      % </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
    %    'pzmodel'.
    % 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(out1.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      if ~strcmp(out2.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      if ~strcmp(out3.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      if ~strcmp(out4.hist.methodInfo.mname, 'pzmodel'), atest = false; end
      % Check values
      if ~isequal(out1.gain, 12.2), atest = false; end
      if ~strcmp(out1.name, name), atest = false; end
      if ~eq(out1.zeros, z), atest = false; end
      if ~eq(out1.poles, p), atest = false; end
      if ~eq(out1.iunits, iu), atest = false; end
      if ~eq(out1.ounits, ou), atest = false; end
      if ~isequal(out2.gain, 12.2), atest = false; end
      if ~strcmp(out2.name, ''), atest = false; end
      if ~isequal(out2.zeros, []), atest = false; end
      if ~isequal(out2.poles, []), atest = false; end
      if ~eq(out2.iunits, unit()), atest = false; end
      if ~eq(out2.ounits, unit()), atest = false; end
      if ~isequal(out3.gain, 1), atest = false; end
      if ~strcmp(out3.name, ''), atest = false; end
      if ~eq(out3.zeros, z), atest = false; end
      if ~isequal(out3.poles, []), atest = false; end
      if ~eq(out3.iunits, unit()), atest = false; end
      if ~eq(out3.ounits, unit()), atest = false; end
      if ~isequal(out4.gain, 1), atest = false; end
      if ~strcmp(out4.name, ''), atest = false; end
      if ~isequal(out4.zeros, []), atest = false; end
      if ~eq(out4.poles, p), atest = false; end
      if ~eq(out4.iunits, unit()), atest = false; end
      if ~eq(out4.ounits, unit()), atest = false; end
      % Rebuild object and check the result
      if ~eq(mout1, out1, ple2), atest = false; end
      if ~eq(mout2, out2, ple2), atest = false; end
      if ~eq(mout3, out3, ple2), atest = false; end
      if ~eq(mout4, out4, ple2), 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 PZMODEL 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>
      name  = 'new name';
      p     = pz(4);
      pl    = plist('poles', p, 'name', name);
      out1  = pzmodel(plist('plist', pl));
      out2  = pzmodel(plist('plist', plist())); % empty plist

      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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check values
      if ~strcmp(out1.name, name), atest = false; end
      if ~eq(out1.poles, p), atest = false; end
      % Check the last step in the history of 'out2'
      if ~strcmp(out2.hist.methodInfo.mname, 'pzmodel'), 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(out1, mout1, ple1), atest = false; end
      if ~eq(out2, mout2, 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 PZMODEL 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_pzmodel_pzmodel_19: submit pzmodel';
      sinfo.experiment_description = 'utp_pzmodel_pzmodel_19: description';
      sinfo.analysis_description   = 'utp_pzmodel_pzmodel_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(pz5, sinfo, plForAutoTest);

      out = pzmodel(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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check data values
      if ~eq(out,pz5, ple3), atest = false; end
      % Check the history except the additional 'submit' + 'retrieve' steps
      if ~eq(out.hist.inhists, pz5.hist), atest = false; end
      % Check the rebuild method
      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 PZMODEL method properly applies history to the
  % gain + poles + zeros constructor.
  %
  % </TestDescription>
  function result = utp_20

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

    try
      % <SyntaxCode>
      gain = 12.2;
      z    = pz(2.2);
      p    = [pz(1,2), pz(7.7)];
      out  = pzmodel(gain, p, z);

      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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check values
      if ~isequal(out.gain, gain), atest = false; end
      if ~eq(out.zeros, z), atest = false; end
      if ~eq(out.poles, p), atest = false; end
      if ~strcmp(out.name, ''), atest = false; end
      if ~eq(out.iunits, unit()), atest = false; end
      if ~eq(out.ounits, unit()), 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_20

  %% UTP_21

  % <TestDescription>
  %
  % Tests that the PZMODEL method properly applies history to the
  % gain + poles + zeros + name constructor.
  %
  % </TestDescription>
  function result = utp_21

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

    try
      % <SyntaxCode>
      name = 'new name';
      gain = 12.2;
      z    = pz(2.2);
      p    = [pz(1,2), pz(7.7)];
      out  = pzmodel(gain, p, z, name);

      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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check values
      if ~isequal(out.gain, gain), atest = false; end
      if ~eq(out.zeros, z), atest = false; end
      if ~eq(out.poles, p), atest = false; end
      if ~strcmp(out.name, name), atest = false; end
      if ~eq(out.iunits, unit()), atest = false; end
      if ~eq(out.ounits, unit()), 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_21

  %% UTP_22

  % <TestDescription>
  %
  % Tests that the PZMODEL method properly applies history to the
  % gain + poles + zeros + iunits + ounits constructor.
  %
  % </TestDescription>
  function result = utp_22

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

    try
      % <SyntaxCode>
      gain = 12.2;
      z    = pz(2.2);
      p    = [pz(1,2), pz(7.7)];
      iu   = unit('Hz^2');
      ou   = unit('V');
      out  = pzmodel(gain, p, z, iu, ou);

      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
    %    'pzmodel'.
    % 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, 'pzmodel'), atest = false; end
      % Check values
      if ~isequal(out.gain, gain), atest = false; end
      if ~eq(out.zeros, z), atest = false; end
      if ~eq(out.poles, p), atest = false; end
      if ~strcmp(out.name, ''), atest = false; end
      if ~eq(out.iunits, iu), atest = false; end
      if ~eq(out.ounits, ou), 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_22

end
