view testing/utp_1.1/utps/mfir/utp_mfir_save.m @ 52:daf4eab1a51e database-connection-manager tip

Fix. Default password should be [] not an empty string
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:29:47 +0100
parents 409a22968d5e
children
line wrap: on
line source

% UTP_MFIR_SAVE a set of UTPs for the mfir/save method
%
% M Hewitson 06-08-08
%
% $Id: utp_mfir_save.m,v 1.7 2010/08/31 09:43:48 hewitson Exp $
%

% <MethodDescription>
%
% The save method of the mfir class saves a mfir object to disk. Save stores
% the variables in a MATLAB formatted file (MAT-file) named filename.mat or in a
% XML fromat named filename.xml
%
% </MethodDescription>

function results = utp_mfir_save(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'mfir';
    mthd    = 'save';

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

    % Test MFIR objects
    [firhp,firlp,firbp,firbr,firpzm,firao,firv,firm] = get_test_objects_mfir;

    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Test plist contains the filename
    results = [results utp_09];    % Test with standard MFIR objects
    results = [results utp_10];    % Test MFIR which is build from a pzmodel
    results = [results utp_11];    % Test MFIR which is build from a AO

    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end

  %% UTP_01

  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01


    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(3).plists.isparam('filename'), atest = false; end
        if ~io(3).plists.isparam('prefix'), atest = false; end
        if ~io(3).plists.isparam('postfix'), atest = false; end
        if ~io(3).plists.isparam('individual files'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('filename'), ''), atest = false; end
        if ~isequal(io(3).plists.find('prefix'), ''), atest = false; end
        if ~isequal(io(3).plists.find('postfix'), ''), atest = false; end
        if ~isequal(io(3).plists.find('individual files'), false), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('filename'), {[]}), 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 save method works with a vector of MFIR objects as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the save method works for a vector of MFIR objects as input.
    % Test both formats 'xml' and 'mat'.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      save(firv, 'test.xml');
      save(firv, 'test.mat');
      out1 = mfir('test.xml');
      out2 = mfir('test.mat');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out1' and 'out2' are the same
    %    as in 'firv'
    % 2) Check that the loaded objects are the same as the saved objects.
    % 3) The outputs 'out1' and 'out2' must be the same.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out1), size(firv)), atest = false; end
      if ~isequal(size(out2), size(firv)), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(firv(kk), out1(kk), ple1), atest = false; end
        if ~eq(firv(kk), out2(kk), ple1), atest = false; end
      end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    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 save method works with a matrix of MFIR objects as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the save method works for a matrix of MFIR objects as input.
    % Test both formats 'xml' and 'mat'.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      save(firm, 'test.xml');
      save(firm, 'test.mat');
      out1 = mfir('test.xml');
      out2 = mfir('test.mat');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out1' and 'out2' are the same
    %    as in 'firm'
    % 2) Check that the loaded objects are the same as the saved objects.
    % 3) The outputs 'out1' and 'out2' must be the same.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out1), size(firm)), atest = false; end
      if ~isequal(size(out2), size(firm)), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(firm(kk), out1(kk), ple1), atest = false; end
        if ~eq(firm(kk), out2(kk), ple1), atest = false; end
      end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    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 save method works with a list of MFIR objects as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the save method works for a list of MFIR objects as input.
    % Test both formats 'xml' and 'mat'.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      save(firhp, firpzm, firbr, 'test.xml');
      save(firhp, firpzm, firbr, 'test.mat');
      out1 = mfir('test.xml');
      out2 = mfir('test.mat');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out1' and 'out2' are the same
    %    as in the list
    % 2) Check that the loaded objects are the same as the saved objects.
    % 3) The outputs 'out1' and 'out2' must be the same.
    %
    % </AlgoDescription>

    atest = true;
    firin  = [firhp, firpzm, firbr];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= 3, atest = false; end
      if numel(out2) ~= 3, atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(firin(kk), out1(kk), ple1), atest = false; end
        if ~eq(firin(kk), out2(kk), ple1), atest = false; end
      end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    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 save method works with a mix of different shaped MFIR objects
  % as input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the save method works with an input of matrices and vectors
    % and single MFIR objects. Test both formats 'xml' and 'mat'.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      save(firhp,firv,firpzm, 'test.xml');
      save(firhp,firv,firpzm, 'test.mat');
      out1 = mfir('test.xml');
      out2 = mfir('test.mat');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that each output MFIR object contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    firin  = [firhp, reshape(firv, 1, []), firpzm];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= 2+numel(firv), atest = false; end
      if numel(out2) ~= 2+numel(firv), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(firin(kk), out1(kk), ple1), atest = false; end
        if ~eq(firin(kk), out2(kk), ple1), atest = false; end
      end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    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 save method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the save method can be processed back
    % to an m-file. Do this for both extensions 'mat' and 'xml'
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out1 = save(firao,  'test.xml');
      out2 = save(firpzm, 'test.mat');
      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the history applies to the output object. Check that
    %    save doesn't add a history step to the input object.
    % 2) Check that the read object doesn't contain the save + load history steps.
    % 3) Check that the method rebuild produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % The last history step is the save method
      if strcmp(out1.hist.methodInfo.mname, 'save'), atest = false; end
      if strcmp(out2.hist.methodInfo.mname, 'save'), atest = false; end
      if ~eq(out1.hist, firao.hist),  atest = false; end
      if ~eq(out2.hist, firpzm.hist), atest = false; end
      % check the history steps of the read object (load + save)
      outr1 = mfir('test.xml');
      outr2 = mfir('test.mat');
      if strcmp(outr1.hist.methodInfo.mname, 'mfir'), atest = false; end
      if strcmp(outr1.hist.inhists.methodInfo.mname, 'save'), atest = false; end
      if strcmp(outr2.hist.methodInfo.mname, 'mfir'), atest = false; end
      if strcmp(outr2.hist.inhists.methodInfo.mname, 'save'), atest = false; end
      % Check the rebuilt object
      if ~eq(mout1, out1, ple2), atest = false; end
      if ~eq(mout2, out2, ple2), atest = false; end
      % </AlgoCode>
      % delete test file
      delete('test.xml')
      delete('test.mat')
    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 save method works with the modify command.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Use the save method with the modifier command.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy firhp to work with
      fir_mat = mfir(firhp);
      fir_mat.save('test.mat');
      fir_xml = mfir(firhp);
      fir_xml.save('test.xml');
      out1 = mfir('test.mat');
      out2 = mfir('test.xml');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the save method applies the history.
    % 2) Check the output against the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that the modified object have the 'save' method as the last
      % history step
      if strcmp(fir_xml.hist.methodInfo.mname, 'save'), atest = false; end
      if strcmp(fir_mat.hist.methodInfo.mname, 'save'), atest = false; end
      % Check the output without the history
      if ~eq(fir_mat, out1, ple1), atest = false; end
      if ~eq(fir_xml, out2, ple1), atest = false; end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    else
      atest = false;
    end

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

  %% UTP_08

  % <TestDescription>
  %
  % Control the method with a plist.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the save method uses the filename which is stored in a plist.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pl1 = plist('filename', 'test.mat');
      pl2 = plist('filename', 'test.xml');
      save(firbp, pl1);
      save(firbp, pl2);
      out1 = mfir('test.mat');
      out2 = mfir('test.xml');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output
      if ~eq(firbp, out1, ple1), atest = false; end
      if ~eq(firbp, out2, ple1), atest = false; end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    else
      atest = false;
    end

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

  %% UTP_09

  % <TestDescription>
  %
  % Test the save method with standard MFIR objects.
  %
  % </TestDescription>
  function result = utp_09

    % <SyntaxDescription>
    %
    % Save all standard MFIR objects with both extensions.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      save(firhp, 'test_hp.mat'); % highpass
      save(firhp, 'test_hp.xml'); % highpass
      save(firlp, 'test_lp.mat'); % lowpass
      save(firlp, 'test_lp.xml'); % lowpass
      save(firbp, 'test_bp.mat'); % bandpass
      save(firbp, 'test_bp.xml'); % bandpass
      save(firbr, 'test_br.mat'); % bandreject
      save(firbr, 'test_br.xml'); % bandreject
      out1 = mfir('test_hp.mat');
      out2 = mfir('test_hp.xml');
      out3 = mfir('test_lp.mat');
      out4 = mfir('test_lp.xml');
      out5 = mfir('test_bp.mat');
      out6 = mfir('test_bp.xml');
      out7 = mfir('test_br.mat');
      out8 = mfir('test_br.xml');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check highpass
      if ~eq(firhp, out1, ple1), atest = false; end
      if ~eq(firhp, out2, ple1), atest = false; end
      if ~eq(firlp, out3, ple1), atest = false; end
      if ~eq(firlp, out4, ple1), atest = false; end
      if ~eq(firbp, out5, ple1), atest = false; end
      if ~eq(firbp, out6, ple1), atest = false; end
      if ~eq(firbr, out7, ple1), atest = false; end
      if ~eq(firbr, out8, ple1), atest = false; end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      if ~eq(out3, out4, ple1), atest = false; end
      if ~eq(out5, out6, ple1), atest = false; end
      if ~eq(out7, out8, ple1), atest = false; end
      % </AlgoCode>
      delete('test_hp.mat');
      delete('test_hp.xml');
      delete('test_lp.mat');
      delete('test_lp.xml');
      delete('test_bp.mat');
      delete('test_bp.xml');
      delete('test_br.mat');
      delete('test_br.xml');
    else
      atest = false;
    end

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

  %% UTP_10

  % <TestDescription>
  %
  % Test the save method with MFIR object which is created from a pole/zero model
  %
  % </TestDescription>
  function result = utp_10

    % <SyntaxDescription>
    %
    % Save MFIR object which is created from a pzmodel.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Create a MFIR object from a pole/zero model
      fir = mfir(plist('pzmodel', pzmodel(1,{[1 4], 2}, 3), 'Nsecs', 10, 'fs', 37));
      save(fir, 'test.mat');
      save(fir, 'test.xml');
      out1 = mfir('test.mat');
      out2 = mfir('test.xml');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output
      if ~eq(fir, out1, ple1), atest = false; end
      if ~eq(fir, out2, ple1), atest = false; end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    else
      atest = false;
    end

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

  %% UTP_11

  % <TestDescription>
  %
  % Test the save method with MFIR object which is created from an analysis
  % model.
  %
  % </TestDescription>
  function result = utp_11

    % <SyntaxDescription>
    %
    % Save MFIR object which is created from an analysis model.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Create a MFIR object from an analysis model
      aa = ao(plist('fsfcn', '1./(50+f)', 'f', linspace(0, 500, 1000)));
      aa.setFs(1000);
      fir = mfir(aa);
      save(fir, 'test.mat');
      save(fir, 'test.xml');
      out1 = mfir('test.mat');
      out2 = mfir('test.xml');
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output
      if ~eq(fir, out1, ple1), atest = false; end
      if ~eq(fir, out2, ple1), atest = false; end
      % Compare the outputs
      if ~eq(out1, out2, ple1), atest = false; end
      % </AlgoCode>
      delete('test.xml');
      delete('test.mat');
    else
      atest = false;
    end

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

end