% UTP_SMODEL_SMODEL a set of UTPs for the smodel/smodel method
%
% M Hewitson 06-08-08
%
% $Id: utp_ssm_ssm.m,v 1.4 2009/08/07 07:31:46 hewitson Exp $
%

% <MethodDescription>
%
% The smodel method of the smodel class constructs smodel objects.
%
% </MethodDescription>

function results = utp_smodel_smodel(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'smodel';
    mthd    = 'smodel';

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test objects
    sm1 = smodel('a');
    sm1.setName;
    sm2 = smodel('a+b');
    sm2.setName;
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
 
    % Run the general tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02(mthd, [sm1 sm2 sm1], [], [], ple3)];    % Vector input
    results = [results utp_03(mthd, [sm1 sm2 sm1; sm2 sm2 sm1], [], [], ple3)];    % Matrix input
    results = [results utp_04(mthd, sm1, sm2, sm1, [], [], ple3)];    % List input
    results = [results utp_05(mthd, sm1, [sm1 sm2], [sm1 sm2 sm1; sm2 sm2 sm1], [], [], ple3)];    % Test with mixed input
    results = [results utp_06(mthd, sm1, [], ple2)]; % Test history is working        
    results = [results utp_07];                      % Read older MAT-files
    % constructor specific tests
    results = [results utp_60(class, sm1, ple2)];    % Test history is properly handled with MAT file constructor
    results = [results utp_61(class, sm1, ple2)];    % Test history is properly handled with XML file constructor
    results = [results utp_62(class, sm1, sm2, ple2)];    % Test history is working with struct constructor
    results = [results utp_64(class, sm1, ple2, ple2)]; % Test history is working with plist(filename) constructor
    results = [results utp_65(class, sm2, ple3)]; % Test history is working with plist(hostname) constructor
    results = [results utp_68(class, sm1, ple3)];  % Test history is working with conn+Id constructor
    
    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    elseif strcmpi(varargin{1}, 'needs repository')
      results = 2;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end

  %% UTP_01

  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01


    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Expression')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From XML File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From MAT File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From ASCII File')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Repository')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'From Built-in Model')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        pn = 3;
        if io(pn).plists.nparams ~= 3, atest = false; end
        % Check key
        % Check default value
        % Check options
        %%%%%%%%%%   SET 'From MAT File'
        pn = 4;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('filename'), {''}), atest = false; end
        %%%%%%%%%%   SET 'From XML File'
        pn = 5;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('filename'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('filename')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('filename'), {''}), atest = false; end
        %%%%%%%%%%   SET 'From Repository'        
        pn = 6;
        if io(pn).plists.nparams ~= 10, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('hostname'), atest = false; end
        if ~io(pn).plists.isparam('id'), atest = false; end
        if ~io(pn).plists.isparam('cid'), atest = false; end
        if ~io(pn).plists.isparam('database'), atest = false; end
        if ~io(pn).plists.isparam('binary'), atest = false; end
        if ~io(pn).plists.isparam('username'), atest = false; end
        if ~io(pn).plists.isparam('password'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(pn).plists.find('id')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('cid')), atest = false; end
        if ~isequal(io(pn).plists.find('binary'), 'yes'), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('id'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('cid'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('binary'), {'yes', 'no'}), atest = false; end
        %%%%%%%%%%   SET 'From Built-in Model'
        pn = 7;
        if io(pn).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('BUILT-IN'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(pn).plists.find('built-in')), atest = false; end
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('built-in'), {''}), atest = false; end
        %%%%%%%%%%   SET 'From Expression'
        pn = 8;
        if io(pn).plists.nparams ~= 10, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('EXPRESSION'), atest = false; end
        if ~io(pn).plists.isparam('params'), atest = false; end
        if ~io(pn).plists.isparam('values'), atest = false; end
        if ~io(pn).plists.isparam('xvar'), atest = false; end
        if ~io(pn).plists.isparam('xvals'), atest = false; end
        if ~io(pn).plists.isparam('xunits'), atest = false; end
        if ~io(pn).plists.isparam('yunits'), atest = false; end
        % Check default value
        % Check options
        %%%%%%%%%%   SET 'From ASCII File'
        pn = 9;
        if io(pn).plists.nparams ~= 7, atest = false; end
        % Check key
        if ~io(pn).plists.isparam('Name'), atest = false; end
        if ~io(pn).plists.isparam('description'), atest = false; end
        if ~io(pn).plists.isparam('filename'), atest = false; end
        if ~io(pn).plists.isparam('xvals'), atest = false; end
        if ~io(pn).plists.isparam('yunits'), atest = false; end
        if ~io(pn).plists.isparam('xunits'), atest = false; end
        % Check default value
        % Check options
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
  %% UTP_07
  
  % <TestDescription>
  %
  % Tests that the smodel can read older MAT files which have different
  % values in 'xvals'
  %
  % </TestDescription>
  function result = utp_07
    
    % <SyntaxDescription>
    %
    % Tests that the smodel can read older MAT files which have different
    % values in 'xvals'
    % The smodel object was created with the commands:
    %     s = smodel('A.*(t >= toff)');
    %     s.setXvar('t');
    %     s.setParams({'A','toff'}, {5,300});
    %     s.setName('Step at toff');
    %     s.setDescription('Step function of amplitude A at time toff');
    %     s.setXunits('Hz^1/2 km^2');
    %     s.setYunits('s^2 m^-1');
    %     s.setXvals(1:1e3);
    %     s.setAliasNames('a');
    %     s.setAliasValues(8);
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      p = mfilename('fullpath');
      % p = regexp(p, '.*(?=/\w+$)', 'match');
      p = p(1:end-(numel(mfilename)+1));
      p = fullfile(p, 'reference_files');
      
      fn1 = 'smodel_231_xvals_cdata.mat';
      fn2 = 'smodel_231_xvals_fsdata.mat';
      fn3 = 'smodel_231_xvals_double.mat';
      fn4 = 'smodel_231_xvals_tsdata.mat';
      fn5 = 'smodel_231_xvals_double.xml';
      
      s1 = smodel(fullfile(p, fn1));
      s2 = smodel(fullfile(p, fn2));
      s3 = smodel(fullfile(p, fn3));
      s4 = smodel(fullfile(p, fn4));
      s5 = smodel(fullfile(p, fn5));
      
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that the objects are quite the same
    %
    % 2) Check that 'xvals' is a cell-array with the numbers from 1 to 1000
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      try
        assert(eq(s1, s2, 'UUID', 'proctime', 'mversion', 'plistUsed'));
        assert(eq(s1, s3, 'UUID', 'proctime', 'mversion', 'plistUsed'));
        assert(eq(s1, s4, 'UUID', 'proctime', 'mversion', 'plistUsed'));
        assert(eq(s1, s5, 'UUID', 'proctime', 'mversion', 'plistUsed', 'mpackage', 'creator'));
        
        assert(isequal(s1.xvals{1}, 1:1e3));
        assert(isequal(s2.xvals{1}, 1:1e3));
        assert(isequal(s3.xvals{1}, 1:1e3));
        assert(isequal(s4.xvals{1}, 1:1e3));
        assert(isequal(s5.xvals{1}, 1:1e3));
      catch
        atest = false;
      end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07
  

end
