
% <TestDescription>
%
% Tests the arithmetic operators rule 10.
%
% </TestDescription>
function result = utp_generic_aop_rule10(fcn)
  
  % <SyntaxDescription>
  %
  % Tests the arithmetic operators rule 10 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();
  
  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)];
  H_NP_ts = [copy(b_ts, 1), copy(a_ts, 1), copy(a_ts, 1); copy(b_ts, 1), copy(b_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();
  
  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)];
  H_NP_fs = [copy(b_fs, 1), copy(a_fs, 1), copy(a_fs, 1); copy(b_fs, 1), copy(b_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();
  
  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)];
  H_NP_xy = [copy(b_xy, 1), copy(a_xy, 1), copy(a_xy, 1); copy(b_xy, 1), copy(b_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();
  
  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)];
  H_NP_c = [copy(b_c, 1), copy(a_c, 1), copy(a_c, 1); copy(b_c, 1), copy(b_c, 1), copy(a_c, 1)];
  
  % Define check functions
  checkFcns = {...
    @checkDataObjectRule10, ...
    @checkValuesRule10,     ...
    @checkErrorsRule10,     ...
    @checkUnitsRule10,      ...
    @check_aop_history};
  
  result = [];
  
  % <AlgoDescription>
  %
  % Here we test  element-wise operator rule10 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, 'rule10', 'NxP tsdata and NxP tsdata', M_NP_ts, H_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP tsdata and NxP tsdata', H_NP_ts, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP tsdata and NxP xydata', M_NP_ts, H_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP xydata and NxP tsdata', H_NP_xy, M_NP_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP tsdata and NxP cdata', M_NP_ts, H_NP_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP cdata and NxP tsdata', H_NP_c, M_NP_ts, checkFcns)];
  
  % fsdata
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP fsdata and NxP fsdata', M_NP_fs, H_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP fsdata and NxP fsdata', H_NP_fs, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP fsdata and NxP xydata', M_NP_fs, H_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP xydata and NxP fsdata', H_NP_xy, M_NP_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP fsdata and NxP cdata', M_NP_fs, H_NP_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP cdata and NxP fsdata', H_NP_c, M_NP_fs, checkFcns)];
  
  % xydata
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP xydata and NxP xydata', M_NP_xy, H_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP xydata and NxP xydata', H_NP_xy, M_NP_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP xydata and NxP cdata', M_NP_xy, H_NP_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP cdata and NxP xydata', H_NP_c, M_NP_xy, checkFcns)];
  
  % cdata
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP cdata and NxP cdata', M_NP_c, H_NP_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule10', 'NxP cdata and NxP cdata', H_NP_c, M_NP_c, checkFcns)];
  % </AlgoCode>
  
end % END UTP_rule10

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

%%%%%%%%%%   Data object test   %%%%%%%%%%
function atest = checkDataObjectRule10(fcn, in1, in2, out)
  
  for ii = 1:size(in1,1)
    atest = check_aop_data_object(fcn, in1(ii), in2(ii), out(ii));
    if atest == 0
      break;
    end
  end
end

%%%%%%%%%%   Value test   %%%%%%%%%%
function atest = checkValuesRule10(fcn, in1, in2, out)
  
  atest = true;
  for ii = 1:size(in1,1)
    if ~isequal(fcn(in1(ii).data.getY, in2(ii).data.getY), out(ii).data.getY)
      atest = false;
      break;
    end
  end
end

%%%%%%%%%%   Errors test   %%%%%%%%%%
function atest = checkErrorsRule10(fcn, in1, in2, out)
  
  atest = true;
  for ii = 1:size(in1,1)
    atest = check_aop_errors(fcn, in1(ii), in2(ii), out(ii));
    if atest == 0
      break;
    end
  end
end

%%%%%%%%%%   Units test   %%%%%%%%%%
function atest = checkUnitsRule10(fcn, in1, in2, out)
  
  atest = true;
  for ii = 1:size(in1,1)
    atest = check_aop_units(fcn, in1(ii), in2(ii), out(ii));
    if atest == 0
      break;
    end
  end
end


