% <TestDescription>
%
% Tests that the model <MODEL> responds to 'DIM' configuration key.
%
% </TestDescription>
% 
% $Id: utp_903.m,v 1.4 2010/07/27 07:37:28 hewitson Exp $
%

function result = utp_903(cl, model_name, pl)
  
  % <SyntaxDescription>
  %
  % Test that the model <MODEL> responds to 'DIM' configuration key.
  %
  % </SyntaxDescription>
  
  try
    
    % <SyntaxCode>
    pl  = combine(pl, plist('built-in', model_name, 'DIM', 1));
    mdl1 = feval(cl, pl);
    pl.pset('DIM', 2)
    mdl2 = feval(cl, pl);
    pl.pset('DIM', 3)
    mdl3 = feval(cl, pl);
    % </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) Check that the model builds with DIM==1
  % 2) Check that the model builds with DIM==2
  % 3) Check that the model builds with DIM==3
  % 4) Check that the 3 models are different
  %
  % </AlgoDescription>
  
  atest = true;
  if stest
    msg = '';
    % <AlgoCode>
    % check we have a model of the correct class
    if ~isa(mdl1, cl)
      atest = false; 
      msg = sprintf('The model produced by %s is not a %s model', model_name, cl);
    end
    if ~isa(mdl2, cl)
      atest = false; 
      msg = sprintf('The model produced by %s is not a %s model', model_name, cl);
    end
    if ~isa(mdl3, cl)
      atest = false; 
      msg = sprintf('The model produced by %s is not a %s model', model_name, cl);
    end
    
    % check models are different by looking at the states
    if ~isempty(mdl1.states) && ~isempty(mdl2.states)
      if isequal(size(mdl1.states.ports), size(mdl2.states.ports))
        atest = false; 
        msg = sprintf('The model %s with DIM=1 and DIM=2 has the same number of states.', model_name);
      end    
    end
    if ~isempty(mdl1.states) && ~isempty(mdl3.states)
      if isequal(size(mdl1.states.ports), size(mdl3.states.ports))
        atest = false; 
        msg = sprintf('The model %s with DIM=1 and DIM=3 has the same number of states.', model_name);
      end
    end
    if ~isempty(mdl2.states) && ~isempty(mdl3.states)
      if isequal(size(mdl2.states.ports), size(mdl3.states.ports))
        atest = false; 
        msg = sprintf('The model %s with DIM=2 and DIM=3 has the same number of states.', model_name);
      end
    end
    
    % check models are different by looking at the input sizes
    if ~isempty(mdl1.inputsizes) && ~isempty(mdl2.inputsizes)
      if isequal(mdl1.inputsizes, mdl2.inputsizes)
        atest = false; 
        msg = sprintf('The model %s with DIM=1 and DIM=2 has the same input sizes.', model_name);
      end    
    end
    if ~isempty(mdl1.inputsizes) && ~isempty(mdl3.inputsizes)
      if isequal(mdl1.inputsizes, mdl3.inputsizes)
        atest = false; 
        msg = sprintf('The model %s with DIM=1 and DIM=3 has the same input sizes.', model_name);
      end    
    end
    if ~isempty(mdl2.inputsizes) && ~isempty(mdl3.inputsizes)
      if isequal(mdl2.inputsizes, mdl3.inputsizes)
        atest = false; 
        msg = sprintf('The model %s with DIM=2 and DIM=3 has the same input sizes.', model_name);
      end    
    end
    % check the history plist contains the correct value for 'DIM'
    if mdl1.hist.plistUsed.find('DIM') ~= 1
      atest = false; 
      msg = sprintf('The model %s with DIM=1 has the wrong DIM value in the history.', model_name);
    end
    if mdl2.hist.plistUsed.find('DIM') ~= 2
      atest = false; 
      msg = sprintf('The model %s with DIM=2 has the wrong DIM value in the history.', model_name);
    end
    if mdl3.hist.plistUsed.find('DIM') ~= 3
      atest = false; 
      msg = sprintf('The model %s with DIM=3 has the wrong DIM value in the history.', model_name);
    end
    % </AlgoCode>
  else
    atest = false;
  end
  
  % Return a result structure
  dd = dbstack;
  mfilename = dd(2).file(1:end-2);  
  result = utp_prepare_result(atest, stest, dbstack, mfilename, msg);
end % END UTP_901