
% <TestDescription>
%
% Tests the arithmetic operators rule 4.
%
% </TestDescription>
function result = utp_generic_aop_rule4(fcn)
  
  % <SyntaxDescription>
  %
  % Tests the arithmetic operators rule 4 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(a_ts, 1), copy(b_ts, 1)];
  U_N_ts = [copy(b_ts, 1); copy(b_ts, 1); copy(a_ts, 1)];
  
  V_M_ts = [copy(a_ts, 1); copy(b_ts, 1); copy(a_ts, 1); copy(b_ts, 1)];
  U_M_ts = [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_N_fs = [copy(a_fs, 1), copy(a_fs, 1), copy(b_fs, 1)];
  U_N_fs = [copy(b_fs, 1); copy(b_fs, 1); copy(a_fs, 1)];
  
  V_M_fs = [copy(a_fs, 1); copy(b_fs, 1); copy(a_fs, 1); copy(b_fs, 1)];
  U_M_fs = [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(a_xy, 1), copy(b_xy, 1)];
  U_N_xy = [copy(b_xy, 1); copy(b_xy, 1); copy(a_xy, 1)];
  
  V_M_xy = [copy(a_xy, 1); copy(b_xy, 1); copy(a_xy, 1); copy(b_xy, 1)];
  U_M_xy = [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(a_c, 1), copy(b_c, 1)];
  U_N_c = [copy(b_c, 1); copy(b_c, 1); copy(a_c, 1)];
  
  V_M_c = [copy(a_c, 1); copy(b_c, 1); copy(a_c, 1); copy(b_c, 1)];
  U_M_c = [copy(b_c, 1), copy(b_c, 1), copy(a_c, 1), copy(a_c, 1)];
  
  % Define check functions
  checkFcns = {...
    @checkDataObjectRule4, ...
    @checkValuesRule4,     ...
    @checkErrorsRule4,     ...
    @checkUnitsRule4,      ...
    @check_aop_history};
  
  result = [];
  
  % <AlgoDescription>
  %
  % Here we test  element-wise operator rule4 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, 'rule4', 'vector tsdata and vector tsdata', U_N_ts, V_N_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector tsdata and vector tsdata', V_M_ts, U_M_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector tsdata and vector xydata', V_M_ts, U_M_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector cdata and vector tsdata', U_M_c, V_M_ts, checkFcns)];
  
  % fsdata
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector fsdata and vector fsdata', U_N_fs, V_N_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector fsdata and vector fsdata', V_M_fs, U_M_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector fsdata and vector xydata', V_M_fs, U_M_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector cdata and vector fsdata', U_M_c, V_M_fs, checkFcns)];
  
  % xydata
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector xydata and vector xydata', U_N_xy, V_N_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector xydata and vector xydata', V_M_xy, U_M_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector cdata and vector xydata', V_M_c, U_M_xy, checkFcns)];
  
  % cdata
  result = [result utp_generic_aop_core(fcn, 'rule4', 'vector cdata and vector cdata', V_N_c, U_N_c, checkFcns)];
  % </AlgoCode>
  
end % END UTP_rule4

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

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

%%%%%%%%%%   Value test   %%%%%%%%%%
function atest = checkValuesRule4(fcn, in1, in2, out)
  
  atest = true;
  for ii = 1:numel(in1)
    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 = checkErrorsRule4(fcn, in1, in2, out)
  
  atest = true;
  for ii = 1:numel(in1)
    atest = check_aop_errors(fcn, in1(ii), in2(ii), out(ii));
    if atest == 0
      break;
    end
  end
end

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




