% <TestDescription>
%
% Test that bode works on this model.
%
% </TestDescription>
% 
% $Id: utp_920.m,v 1.2 2010/07/27 07:37:28 hewitson Exp $
%

function result = utp_920(cl, model_name, pl)
  
  % <SyntaxDescription>
  %
  % Test that bode works on this model.
  %
  % </SyntaxDescription>
  
  try
    % <SyntaxCode>
    pl   = combine(pl, plist('built-in', model_name));
    mdl  = feval(cl, pl);
    outs = bode(mdl);
    % </SyntaxCode>
    stest = true;
  catch err
    disp(err.message)
    msg = [err.message ' - ' err.stack(1).name ' - line ' num2str(err.stack(1).line)];
    stest = false;
  end
  
  % <AlgoDescription>
  %
  % 1) Test that bode produces the correct number of AOs.
  % 2) Compare each output AO against validated outputs.
  %
  % </AlgoDescription>
  
  atest = true;
  if stest
    msg = '';
    % <AlgoCode>
    % Check the correct number of outputs
    if numel(outs) ~= (sum(mdl.inputsizes) * sum(mdl.outputsizes))
      atest = false;
      msg = sprintf('bode produced the wrong number of outputs for model %s', model_name);
    end
    
    % Load references
    refs = ao(sprintf('utps/%s/reference_files/ref_bode_%s.mat', cl, model_name));
    
    for kk=1:numel(refs)
      ref = refs(kk);
      out = outs(kk);
      if ~isequal(ref.y, out.y)
        atest = false;
        msg = sprintf('bode resp for %s element %d [%s] doesn''t match the reference', model_name, kk, out.name);
      end
    end
        
    % </AlgoCode>
  else
    atest = false;
  end
  
  % Return a result structure
  result = utp_prepare_result(atest, stest, dbstack, mfilename, msg);
  
end % END UTP_920
