
% <TestDescription>
%
% Tests the arithmetic operators rule 1.
%
% </TestDescription>
function result = utp_generic_aop_rule1(fcn)
  
  % <SyntaxDescription>
  %
  % Tests the arithmetic operators rule 1 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();
  
  % 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();
  
  % 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();
  
  % 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();
  
  % Define check functions
  checkFcns = {...
    @checkDataObjectRule1, ...
    @checkValuesRule1,     ...
    @checkErrorsRule1,     ...
    @checkUnitsRule1,      ...
    @check_aop_history};
  
  % <AlgoDescription>
  %
  % Here we test element-wise operator rule1 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>
  result = [];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'tsdata and tsdata', a_ts, b_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'fsdata and fsdata', a_fs, b_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'xydata and xydata', a_xy, b_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'cdata and cdata', a_c, b_c, checkFcns)];
  
  % tsdata + (xydata or cdata)
  result = [result utp_generic_aop_core(fcn, 'rule1', 'tsdata and cdata', b_ts, b_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'tsdata and xydata', b_ts, a_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'cdata and tsdata', a_c, b_ts, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'xydata and tsdata', b_xy, a_ts, checkFcns)];
  
  % fsdata + (xydata or cdata)
  result = [result utp_generic_aop_core(fcn, 'rule1', 'fsdata and cdata', b_fs, b_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'fsdata and xydata', b_fs, a_xy, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'cdata and fsdata', a_c, b_fs, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'xydata and fsdata', b_xy, a_fs, checkFcns)];
  
  % xydata + cdata
  result = [result utp_generic_aop_core(fcn, 'rule1', 'xydata and cdata', b_xy, a_c, checkFcns)];
  result = [result utp_generic_aop_core(fcn, 'rule1', 'cdata and xydata', b_c, a_xy, checkFcns)];
  % </AlgoCode>
  
end % END UTP_rule1

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

%%%%%%%%%%   Data object test   %%%%%%%%%%
function atest = checkDataObjectRule1(fcn, in1, in2, out)
  atest = check_aop_data_object(fcn, in1, in2, out);
end

%%%%%%%%%%   Value test   %%%%%%%%%%
function atest = checkValuesRule1(fcn, in1, in2, out)
  atest = true;
  if ~isequal(fcn(in1.data.getY, in2.data.getY), out.data.getY)
    atest = false;
  end
end

%%%%%%%%%%   Errors test   %%%%%%%%%%
function atest = checkErrorsRule1(fcn, in1, in2, out)
  atest = check_aop_errors(fcn, in1, in2, out);
end

%%%%%%%%%%   Units test   %%%%%%%%%%
function atest = checkUnitsRule1(fcn, in1, in2, out)
  atest = check_aop_units(fcn, in1, in2, out);
end







