view testing/utp_1.1/generic_utps/utp_generic_aop_rule6.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


% <TestDescription>
%
% Tests the arithmetic operators rule 6.
%
% </TestDescription>
function result = utp_generic_aop_rule6(fcn)
  
  % <SyntaxDescription>
  %
  % Tests the arithmetic operators rule 6 for each data type: xydata,
  % fsdata, tsdata, cdata and useful combinations.
  %
  % </SyntaxDescription>
  
  % Test AOs
  % tsdata
  n = 12;
  fs = 12.1;
  x = 0:(1/fs):(n/fs)-1/fs;
  a_ts = ao(x,randn(n,1), fs);
  a_ts.setDx(ones(n,1)*1.1);
  a_ts.setDy((1:n)*2.2);
  a_ts.setYunits('Hz^(-1/2) V^2');
  a_ts.setName();
  
  b_ts = ao(x,randn(n,1), fs);
  b_ts.setDx(7);
  b_ts.setDy(9);
  b_ts.setYunits('Hz^(-1/2) V^2');
  b_ts.setName();
  
  V_N_ts  = [copy(a_ts, 1); copy(b_ts, 1)];
  M_NP_ts = [copy(a_ts, 1), copy(a_ts, 1), copy(b_ts, 1); copy(b_ts, 1), copy(a_ts, 1), copy(a_ts, 1)];
  
  % fsdata
  a_fs = ao(plist('xvals', 1:12, 'yvals', abs(randn(n,1)), 'type', 'fsdata'));
  a_fs.setDx(ones(n,1)*1.1);
  a_fs.setDy((1:n)*2.2);
  a_fs.setName();
  
  b_fs = ao(plist('xvals', 1:n, 'yvals', abs(randn(n,1)), 'type', 'fsdata'));
  b_fs.setDx(7);
  b_fs.setDy(9);
  b_fs.setName();
  
  V_N_fs  = [copy(a_fs, 1); copy(b_fs, 1)];
  M_NP_fs = [copy(a_fs, 1), copy(a_fs, 1), copy(b_fs, 1); copy(b_fs, 1), copy(a_fs, 1), copy(a_fs, 1)];
  
  % xydata
  a_xy = ao(plist('xvals', 1:n, 'yvals', abs(randn(n,1)), 'type', 'xydata'));
  a_xy.setDx(ones(n,1)*1.1);
  a_xy.setDy((1:n)*2.2);
  a_xy.setYunits('Hz^(-1/2) V^2');
  a_xy.setName();
  
  b_xy = ao(plist('xvals', 1:n, 'yvals', abs(randn(n,1)), 'type', 'xydata'));
  b_xy.setDx(7);
  b_xy.setDy(9);
  b_xy.setName();
  
  V_N_xy  = [copy(a_xy, 1); copy(b_xy, 1)];
  M_NP_xy = [copy(a_xy, 1), copy(a_xy, 1), copy(b_xy, 1); copy(b_xy, 1), copy(a_xy, 1), copy(a_xy, 1)];
  
  % cdata
  a_c = ao(8);
  a_c.setDy(2);
  a_c.setName();
  
  b_c = ao(111:122);
  b_c.setDy((111:122)/100);
  b_c.setName();
  
  V_N_c  = [copy(a_c, 1); copy(b_c, 1)];
  M_NP_c = [copy(a_c, 1), copy(a_c, 1), copy(b_c, 1); copy(b_c, 1), copy(a_c, 1), copy(a_c, 1)];
  
  % Define check functions
  checkFcns = {...
    @checkDataObjectRule6, ...
    @checkValuesRule6,     ...
    @checkErrorsRule6,     ...
    @checkUnitsRule6,      ...
    @check_aop_history};
  
  result = [];
  
  % <AlgoDescription>
  %
  % Here we test  element-wise operator rule6 as in S2-AEI-TN-3059.
  %
  % 1) Check the data type of the resulting object.
  % 2) Check the resulting object contains the correct values.
  % 3) Check the error propagation.
  % 4) Check the units of the output object.
  % 5) Check the resulting object can be rebuilt.
  %
  % </AlgoDescription>
  
  
  % <AlgoCode>
  % tsdata
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP tsdata and vector tsdata', M_NP_ts, V_N_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector tsdata and NxP tsdata', V_N_ts, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP tsdata and vector xydata', M_NP_ts, V_N_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector xydata and NxP tsdata', V_N_xy, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP tsdata and vector cdata', M_NP_ts, V_N_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector cdata and NxP tsdata', V_N_c, M_NP_ts, checkFcns)];
  
  % fsdata
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP fsdata and vector fsdata', M_NP_fs, V_N_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector fsdata and NxP fsdata', V_N_fs, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP fsdata and vector xydata', M_NP_fs, V_N_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector xydata and NxP fsdata', V_N_xy, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP fsdata and vector cdata', M_NP_fs, V_N_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector cdata and NxP fsdata', V_N_c, M_NP_fs, checkFcns)];
  
  % xydata
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP xydata and vector xydata', M_NP_xy, V_N_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector xydata and NxP xydata', V_N_xy, M_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP xydata and vector cdata', M_NP_xy, V_N_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector cdata and NxP xydata', V_N_c, M_NP_xy, checkFcns)];
  
  % cdata
  result = [result utp_generic_aop_core(fcn, 'rule6', 'NxP cdata and vector cdata', M_NP_c, V_N_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule6', 'vector cdata and NxP cdata', V_N_c, M_NP_c, checkFcns)];
  % </AlgoCode>
  
end % END UTP_rule6

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      Define here the checking functions                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%   Data object test   %%%%%%%%%%
function atest = checkDataObjectRule6(fcn, in1, in2, out)
  
  for nn = 1:size(in1,1)
    for pp = 1:size(in1,2)
      if numel(in1) > numel(in2)
        atest = check_aop_data_object(fcn, in1(nn,pp), in2(nn), out(nn,pp));
      else
        atest = check_aop_data_object(fcn, in1(nn), in2(nn,pp), out(nn,pp));
      end
      if atest == 0
        break;
      end
    end
  end
end

%%%%%%%%%%   Value test   %%%%%%%%%%
function atest = checkValuesRule6(fcn, in1, in2, out)
  
  atest = true;
  for nn = 1:size(in1,1)
    for pp = 1:size(in1,2)
      if isMatrix(in1) && isVector(in2)
        if ~isequal(fcn(in1(nn,pp).data.getY, in2(nn).data.getY), out(nn,pp).data.getY), atest = false; end
      else
        if ~isequal(fcn(in1(nn).data.getY, in2(nn,pp).data.getY), out(nn,pp).data.getY), atest = false; end
      end
      if atest == 0
        break;
      end
    end
  end
end

%%%%%%%%%%   Errors test   %%%%%%%%%%
function atest = checkErrorsRule6(fcn, in1, in2, out)
  
  atest = true;
  for nn = 1:size(in1,1)
    for pp = 1:size(in1,2)
      if isMatrix(in1) && isVector(in2)
        atest = check_aop_errors(fcn, in1(nn,pp), in2(nn), out(nn,pp));
      else
        atest = check_aop_errors(fcn, in1(nn), in2(nn,pp), out(nn,pp));
      end
      if atest == 0
        break;
      end
    end
  end
end

%%%%%%%%%%   Units test   %%%%%%%%%%
function atest = checkUnitsRule6(fcn, in1, in2, out)
  
  atest = true;
  for nn = 1:size(in1,1)
    for pp = 1:size(in1,2)
      if isMatrix(in1) && isVector(in2)
        atest = check_aop_units(fcn, in1(nn,pp), in2(nn), out(nn,pp));
      else
        atest = check_aop_units(fcn, in1(nn), in2(nn,pp), out(nn,pp));
      end
      if atest == 0
        break;
      end
    end
  end
end

function [a1, a2] = findMatrix(in1, in2)
  if numel(in1) > numel(in2)
    a1 = in1;
    a2 = in2;
  else
    a1 = in2;
    a2 = in1;
  end
end

function res = isMatrix(a)
  res = all(size(a) > 1);
end

function res = isVector(a)
  res = any(size(a) > 1) && any(size(a) == 1);
end