% PRINT_UTP_RESULTS prints the results structure to a string.
%
% M Hewitson 07-08-08
%
% $Id: print_utp_results.m,v 1.5 2011/04/17 05:42:54 hewitson Exp $
%

function res = print_utp_results(results)
  
  DISP_ALL         = false;
  DISP_RUNTIME     = true;
  DISP_RUNTIME_NUM = 30;
  
  ntests  = numel(results);
  apassed = sum([results(:).a]);
  spassed = sum([results(:).s]);
  
  res = '';
  
  if DISP_ALL
    res = [res sprintf('\n\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('-------------           SUMMARY           -------------\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('UTP name                           | Syntax | Algorithm\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    for aa=1:ntests
      str = dispRes(results(aa).fcn, results(aa).s, results(aa).a, results(aa).msg);
      res = [res str];
    end
    res = [res sprintf('\n\n')];
  end
  
  if DISP_RUNTIME
    
    runtime = [0 results(:).runtime];
    d       = diff(runtime);
    [sorted, idx]  = sort(d);
    
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('-------------           RUNTIME           -------------\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    res = [res sprintf('UTP name                           | Runtime           \n')];
    res = [res sprintf('-------------------------------------------------------\n')];
    for ll = 0:min(DISP_RUNTIME_NUM, length(idx)-1);
      currentIdx = idx(end-ll);
      str = dispRes(results(currentIdx).fcn, sorted(end-ll), '', '');
      res = [res str];
    end
    res = [res sprintf('\n\n')];
  end
  
  res = [res sprintf('-------------------------------------------------------\n')];
  res = [res sprintf('-------------           FAILURES           ------------\n')];
  res = [res sprintf('-------------------------------------------------------\n')];
  res = [res sprintf('-------------------------------------------------------\n')];
  res = [res sprintf('UTP name                           | Syntax | Algorithm\n')];
  res = [res sprintf('-------------------------------------------------------\n')];  
  for jj=1:ntests
    if results(jj).a==0 || results(jj).s==0
      str = dispRes(results(jj).fcn, results(jj).s, results(jj).a, results(jj).msg);
      res = [res str];
    end
  end
  res = [res sprintf('-------------------------------------------------------\n')];
  res = [res sprintf('\n\n')];
  
  % Summary
  res = [res sprintf('------------------------------------\n')];
  res = [res sprintf('Total # tests:  %d\n', ntests)];
  res = [res sprintf('S tests failed: %d\n', ntests - spassed)];
  res = [res sprintf('A tests failed: %d\n', ntests - apassed)];
  res = [res sprintf('------------------------------------\n')];
end

function str = dispRes(fcn, s, a, msg)
  if numel(fcn) > 35
    fcn = [fcn(1:31), ' ...'];
  end
  str = sprintf('%-36s%s  %s %s\n', fcn, res2str(s), res2str(a), msg);
end

function str = res2str(a)
  if islogical(a)
    if (a)
      str = '  pass  ';
    else
      str = '--fail--';
    end
  elseif isnumeric(a)
    str = sprintf('%8.2f seconds', a);
  elseif ischar(a)
    str = a;
  else
    str = '';
  end
end
