view testing/utp_1.1/utps/ao/utp_ao_sumjoin.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_AO_SUMJOIN a set of UTPs for the ao/sumjoin method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_sumjoin.m,v 1.7 2011/04/17 15:45:04 ingo Exp $
%

% <MethodDescription>
%
% The sumjoin method of the ao class sums time-series signals togther.
%
% </MethodDescription>

function results = utp_ao_sumjoin(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'sumjoin';

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

    % 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 input data shape == output data shape

    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 ~= 0, atest = false; end
        % Check key
        % Check default value
        % 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 sumjoin method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the sumjoin method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = 3;
      pl = plist('fs', fs, 'nsecs', 30, 'waveform', 'sine wave', 'f', .1);
      a1 = ao(pl);
      a2 = ao(pl);
      a3 = ao(pl);
      a1.setT0('14:00:00');
      a2.setT0('14:00:02');
      a3.setT0('14:00:04');
      avec = [a1, a2, a3];
      out  = sumjoin(avec);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO
    % 2) Check that the output have the correct data.
    % 3) Check the rebuilt object
    %
    % </AlgoDescription>

    atest = true;
    TOL   = 1e-14;
    if stest
      % <AlgoCode>
      % Check that the output is exact one AO
      if numel(out) ~= 1, atest = false; end
      if ~isa(out, 'ao'), atest = false; end
      % Compute reference data.
      % Get min/max time
      ts = time('14:00:00');
      ts = ts.utc_epoch_milli/1000;
      te = time('14:00:04') + 30;
      te = te.utc_epoch_milli/1000;
      % Compute X and Y
      yall = zeros((te - ts)*fs,1);
      for kk = 1:numel(avec)
        x1   = avec(kk).x(1)   + avec(kk).t0.utc_epoch_milli/1000;
        xn   = avec(kk).x(end) + 1/fs;
        post = zeros((te - xn - x1)*fs,1);
        pre  = zeros((x1 - ts)*fs,1);
        y    = [pre; avec(kk).y; post];
        yall = yall + y;
      end
      xall = linspace(0, (te-ts)-1/fs, (te-ts)*fs)';
      % Check output data
      if any(abs(out.x - xall) > TOL), atest = false; end
      if ~isequal(out.y, yall), atest = false; end
      if ~eq(out.t0, time(ts), ple1), atest = false; end
      % Check nsecs. Here: 34 sec
      if ~isequal(out.data.nsecs, 34), atest = false; end
      % Check the re-built object
      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_02

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the sumjoin method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Tests that the sumjoin method works with a matrix of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = .3;
      pl = plist('fs', fs, 'nsecs', 300, 'waveform', 'sine wave', 'f', .1);
      a1 = ao(pl);
      a2 = ao(pl);
      a3 = ao(pl);
      a4 = ao(pl);
      a5 = ao(pl);
      a6 = ao(pl);
      a1.setT0('14:00:20');
      a2.setT0('14:01:40');
      a3.setT0('14:02:00');
      a4.setT0('14:00:00');
      a5.setT0('14:01:20');
      a6.setT0('14:00:40');
      amat = [a1, a2, a3; a4, a5, a6];
      out  = sumjoin(amat);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO
    % 2) Check that the output have the correct data.
    %
    % </AlgoDescription>

    atest = true;
    TOL   = 1e-14;
    if stest
      % <AlgoCode>
      % Check that the output is exact one AO
      if numel(out) ~= 1, atest = false; end
      if ~isa(out, 'ao'), atest = false; end
      % Compute reference data.
      % Get min/max time
      ts = time('14:00:00');
      ts = ts.utc_epoch_milli/1000;
      te = time('14:02:00') + 300;
      te = te.utc_epoch_milli/1000;
      % Compute X and Y
      yall = zeros((te - ts)*fs,1);
      for kk = 1:numel(amat)
        x1   = amat(kk).x(1)   + amat(kk).t0.utc_epoch_milli/1000;
        xn   = amat(kk).x(end) + 1/fs;
        post = zeros((te - xn - x1)*fs,1);
        pre  = zeros((x1 - ts)*fs,1);
        y    = [pre; amat(kk).y; post];
        yall = yall + y;
      end
      xall = linspace(0, (te-ts)-1/fs, (te-ts)*fs)';
      % Check output data
      if any(abs(out.x - xall) > TOL), atest = false; end
      if ~isequal(out.y, yall), atest = false; end
      if ~eq(out.t0, time(ts), ple1), atest = false; end
      % Check nsecs. Here: 420 sec
      if ~isequal(out.data.nsecs, 420), atest = false; 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 sumjoin method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Tests that the sumjoin method works with a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = 3;
      pl = plist('fs', fs, 'nsecs', 30, 'waveform', 'sine wave', 'f', 10);
      a1 = ao(pl);
      a2 = ao(pl);
      a3 = ao(pl);
      a1.setT0('14:00:00');
      a2.setT0('14:00:05');
      a3.setT0('14:00:10');
      out = sumjoin(a1, a2, a3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO
    % 2) Check that the output have the correct data.
    %
    % </AlgoDescription>

    atest = true;
    TOL   = 1e-14;
    aoin  = [a1, a2, a3];
    if stest
      % <AlgoCode>
      % Check that the output is exact one AO
      if numel(out) ~= 1, atest = false; end
      if ~isa(out, 'ao'), atest = false; end
      % Compute reference data.
      % Get min/max time
      ts = time('14:00:00');
      ts = ts.utc_epoch_milli/1000;
      te = time('14:00:10') + 30;
      te = te.utc_epoch_milli/1000;
      % Compute X and Y
      yall = zeros((te - ts)*fs,1);
      for kk = 1:numel(aoin)
        x1   = aoin(kk).x(1)   + aoin(kk).t0.utc_epoch_milli/1000;
        xn   = aoin(kk).x(end) + 1/fs;
        post = zeros((te - xn - x1)*fs,1);
        pre  = zeros((x1 - ts)*fs,1);
        y    = [pre; aoin(kk).y; post];
        yall = yall + y;
      end
      xall = linspace(0, (te-ts)-1/fs, (te-ts)*fs)';
      % Check output data
      if any(abs(out.x - xall) > TOL), atest = false; end
      if ~isequal(out.y, yall), atest = false; end
      if ~eq(out.t0, time(ts), ple1), atest = false; end
      % Check nsecs. Here: 40 sec
      if ~isequal(out.data.nsecs, 40), atest = false; 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 sumjoin method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Tests that the sumjoin method works with a mix of different shaped AOs as
    % input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = .3;
      pl = plist('fs', fs, 'nsecs', 300, 'waveform', 'sine wave', 'f', .1);
      a1 = ao(pl);
      a2 = ao(pl);
      a3 = ao(pl);
      a4 = ao(pl);
      a5 = ao(pl);
      a6 = ao(pl);
      a1.setT0('14:00:20');
      a2.setT0('14:01:40');
      a3.setT0('14:02:00');
      a4.setT0('14:00:00');
      a5.setT0('14:01:20');
      a6.setT0('14:00:40');
      out = sumjoin(a1, [a2, a3; a4, a5], [a6;a1]);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO
    % 2) Check that the output have the correct data.
    %
    % </AlgoDescription>

    atest = true;
    TOL   = 1e-14;
    aoin  = [a1, reshape([a2, a3; a4, a5], 1, []), a6, a1];
    if stest
      % <AlgoCode>
      % Check that the output is exact one AO
      if numel(out) ~= 1, atest = false; end
      if ~isa(out, 'ao'), atest = false; end
      % Compute reference data.
      % Get min/max time
      ts = time('14:00:00');
      ts = ts.utc_epoch_milli/1000;
      te = time('14:02:00') + 300;
      te = te.utc_epoch_milli/1000;
      % Compute X and Y
      yall = zeros((te - ts)*fs,1);
      for kk = 1:numel(aoin)
        x1   = aoin(kk).x(1)   + aoin(kk).t0.utc_epoch_milli/1000;
        xn   = aoin(kk).x(end) + 1/fs;
        post = zeros((te - xn - x1)*fs,1);
        pre  = zeros((x1 - ts)*fs,1);
        y    = [pre; aoin(kk).y; post];
        yall = yall + y;
      end
      xall = linspace(0, (te-ts)-1/fs, (te-ts)*fs)';
      % Check output data
      if any(abs(out.x - xall) > TOL), atest = false; end
      if ~isequal(out.y, yall), atest = false; end
      if ~eq(out.t0, time(ts), ple1), atest = false; end
      % Check nsecs. Here: 420 sec
      if ~isequal(out.data.nsecs, 420), atest = false; 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 sumjoin method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

    try
      % <SyntaxCode>
      fs = 3;
      pl = plist('fs', fs, 'nsecs', 30, 'waveform', 'sine wave', 'f', 10);
      a1 = ao(pl);
      a2 = ao(pl);
      a1.setT0('14:00:00');
      a2.setT0('14:00:05');
      out  = sumjoin(a1, a2);
      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
    %    'sumjoin'.
    % 2) Check that the re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'sumjoin'), atest = false; end
      % The rebuilt object must be the same as 'out'
      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 sumjoin method can modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the sumjoin method can modify the input AO by calling with
    % no output and that the method doesn't change the input of the
    % function notation (with a equal sign).
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = 3;
      pl = plist('fs', fs, 'nsecs', 30, 'waveform', 'sine wave', 'f', 10);
      a1 = ao(pl);
      a2 = ao(pl);
      a1.setT0('14:00:05');
      a2.setT0('14:00:00');
      amodi = ao(a1);
      aeq   = ao(a1);
      out = aeq.sumjoin(a2);
      amodi.sumjoin(a2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that 'out' and 'aeq' are now different.
    % 2) Check that 'aeq' is not changed
    % 3) Check that the modified input have the new data
    % 4) Check that out and amodi are the same
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that 'out' and 'aeq' are now different.
      if eq(out, aeq, ple2), atest = false; end
      % Check that 'aeq' is not changed
      if ~eq(aeq, ao(a1), ple2), atest = false; end
      % Check that the modified input have the new data
      yall = [zeros(5*3,1); a1.y] + [a2.y; zeros(5*3,1)];
      if ~isequal(amodi.y, yall), atest = false; end
      % Check that out and amodi are the same
      if ~eq(out, amodi, 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>
  %
  % Test the shape of the output.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the sumjoin method keeps the data shape of the input
    % object. The input AO must be an AO with row data and an AO with
    % column data.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      fs = 3;
      pl = plist('fs', fs, 'nsecs', 30, 'waveform', 'sine wave', 'f', 10);
      a1 = ao(pl);
      a2 = ao(pl);
      a2 = a2.';
      a1.setT0('14:00:05');
      a2.setT0('14:00:00');
      out1 = sumjoin(a1, a2);
      out2 = sumjoin(a2, a1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shape of the data doesn't change.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out1.data.y,1) == 1, atest = false; end
      if size(out2.data.y,2) == 1, atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

end