
% <TestDescription>
%
% Tests the arithmetic operators rule 7.
%
% </TestDescription>
function result = utp_generic_aop_rule7(fcn)
  
  % <SyntaxDescription>
  %
  % Tests the arithmetic operators rule 7 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_P_ts  = [copy(a_ts, 1), copy(b_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:n, '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_P_fs  = [copy(a_fs, 1), copy(b_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_P_xy  = [copy(a_xy, 1), copy(b_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_P_c  = [copy(a_c, 1), copy(b_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 = {...
    @checkDataObjectRule7, ...
    @checkValuesRule7,     ...
    @checkErrorsRule7,     ...
    @checkUnitsRule7,      ...
    @check_aop_history};
  
  result = [];
  
  % <AlgoDescription>
  %
  % Here we test  element-wise operator rule7 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, 'rule7', 'NxP tsdata and vector tsdata', M_NP_ts, V_P_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector tsdata and NxP tsdata', V_P_ts, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP tsdata and vector xydata', M_NP_ts, V_P_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector xydata and NxP tsdata', V_P_xy, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP tsdata and vector cdata', M_NP_ts, V_P_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector cdata and NxP tsdata', V_P_c, M_NP_ts, checkFcns)];
  
  % fsdata
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP fsdata and vector fsdata', M_NP_fs, V_P_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector fsdata and NxP fsdata', V_P_fs, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP fsdata and vector xydata', M_NP_fs, V_P_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector xydata and NxP fsdata', V_P_xy, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP fsdata and vector cdata', M_NP_fs, V_P_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector cdata and NxP fsdata', V_P_c, M_NP_fs, checkFcns)];
  
  % xydata
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP xydata and vector xydata', M_NP_xy, V_P_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector xydata and NxP xydata', V_P_xy, M_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP xydata and vector cdata', M_NP_xy, V_P_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector cdata and NxP xydata', V_P_c, M_NP_xy, checkFcns)];
  
  % cdata
  result = [result utp_generic_aop_core(fcn, 'rule7', 'NxP cdata and vector cdata', M_NP_c, V_P_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule7', 'vector cdata and NxP cdata', V_P_c, M_NP_c, checkFcns)];
  % </AlgoCode>
  
end % END UTP_rule7

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

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

%%%%%%%%%%   Value test   %%%%%%%%%%
function atest = checkValuesRule7(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(pp).data.getY), out(nn,pp).data.getY), atest = false; end
      else
        if ~isequal(fcn(in1(pp).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 = checkErrorsRule7(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(pp), out(nn,pp));
      else
        atest = check_aop_errors(fcn, in1(pp), in2(nn,pp), out(nn,pp));
      end
      if atest == 0
        break;
      end
    end
  end
end

%%%%%%%%%%   Units test   %%%%%%%%%%
function atest = checkUnitsRule7(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(pp), out(nn,pp));
      else
        atest = check_aop_units(fcn, in1(pp), in2(nn,pp), out(nn,pp));
      end
      if atest == 0
        break;
      end
    end
  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



