view testing/utp_1.1/utps/plist/utp_plist_save.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
line wrap: on
line source

% UTP_PLIST_SAVE a set of UTPs for the plist/save method
%
% M Hewitson 06-08-08
%
% $Id: utp_plist_save.m,v 1.4 2010/08/31 09:43:48 hewitson Exp $
%

% <MethodDescription>
%
% The save method of the plist class saves a plist 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_plist_save(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test PLIST objects
    [pl1, pl2, pl3, pl4, plv, plm] = get_test_objects_plist;

    % 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 the modify call works
    results = [results utp_07];    % Test plist contains the filename

    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 PLIST objects as input.
  %
  % </TestDescription>
  function result = utp_02

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

    try
      % <SyntaxCode>
      save(plv, 'test.xml');
      save(plv, 'test.mat');
      out1 = plist('test.xml');
      out2 = plist('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 'plv'
    % 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(plv)), atest = false; end
      if ~isequal(size(out2), size(plv)), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(plv(kk), out1(kk), ple1), atest = false; end
        if ~eq(plv(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 PLIST objects as input.
  %
  % </TestDescription>
  function result = utp_03

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

    try
      % <SyntaxCode>
      save(plm, 'test.xml');
      save(plm, 'test.mat');
      out1 = plist('test.xml');
      out2 = plist('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 'plm'
    % 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(plm)), atest = false; end
      if ~isequal(size(out2), size(plm)), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(plm(kk), out1(kk), ple1), atest = false; end
        if ~eq(plm(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 PLIST objects as input.
  %
  % </TestDescription>
  function result = utp_04

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

    try
      % <SyntaxCode>
      save(pl2, pl3, pl4, 'test.xml');
      save(pl2, pl3, pl4, 'test.mat');
      out1 = plist('test.xml');
      out2 = plist('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;
    plin  = [pl2, pl3, pl4];
    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(plin(kk), out1(kk), ple1), atest = false; end
        if ~eq(plin(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 PLIST objects
  % as input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      save(pl2,plv,pl3, 'test.xml');
      save(pl2,plv,pl3, 'test.mat');
      out1 = plist('test.xml');
      out2 = plist('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 PLIST object contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    plin  = [pl2, reshape(plv, 1, []), pl3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= 2+numel(plv), atest = false; end
      if numel(out2) ~= 2+numel(plv), atest = false; end
      % Check each output against the input
      for kk=1:numel(out1)
        if ~eq(plin(kk), out1(kk), ple1), atest = false; end
        if ~eq(plin(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 works with the modify command.
  %
  % </TestDescription>
  function result = utp_06

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

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

    % <AlgoDescription>
    %
    % 1) Check that the save method doesn't change the input PLIST object.
    % 2) Check the output against the input except.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that save doesn't modified the input
      if ~eq(plist(pl2), pl, ple1), atest = false; end
      % Check the output
      if ~eq(pl, out1, ple1), atest = false; end
      if ~eq(pl, 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_06

  %% UTP_07

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

    % <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(pl3, pl1);
      save(pl3, pl2);
      out1 = plist('test.mat');
      out2 = plist('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(pl3, out1, ple1), atest = false; end
      if ~eq(pl3, 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

end