% UTP_COLLECTION_LOADOBJ a set of UTPs for the collection/loadobj method
%
% M Hewitson 06-08-08
%
% $Id: utp_collection_loadobj.m,v 1.1 2010/05/12 15:58:16 ingo Exp $
%

% <MethodDescription>
%
% The loadobj method of the collection class will be automatically called by
% MATLAB if an older object is not compatible with the current object structure.
% This happens only for objects which are stored as a MATLAB formatted
% (MAT-file). The load mechanism for XML formated files calls always the
% 'update_struct' method to keep the stored objects up to date. The
% 'update_struct' method convert the read object structure to the object
% structure which is used in the currend LTPDA version.
% This UTP will load old objects which are stored with an older LTPDA version.
% 
% REMARK: Since LTPDA version 1.9.2 will be an object which should be saved as a
%         MAT file stored as a struct and not as the object.
%
% </MethodDescription>

function results = utp_collection_loadobj(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'collection';
    mthd    = 'loadobj';

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

    % Run the tests
    results = [results utp_01];    % getInfo call

    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    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>
      % Get all .MAT and .XML files
      path = fullfile(fileparts(which(mfilename)), '..', '..', 'utp_test_files');

      matfiles = utils.prog.filescan(path, '.mat');
      xmlfiles = utils.prog.filescan(path, '.xml');

      % Load .MAT files
      obj_no = 1;
      for ii=1:numel(matfiles)
        fn = matfiles{ii};
        [path, name] = fileparts(fn);
        cl = strtok(name, '_');

        if strcmp(cl, class)
          objs(obj_no).fname = name;
          objs(obj_no).obj  = collection(fn);
          obj_no = obj_no+1;
        end
      end

      % Load .XML files
      for ii=1:numel(xmlfiles)
        fn = xmlfiles{ii};
        [path, name] = fileparts(fn);
        cl = strtok(name, '_');

        if strcmp(cl, class)
          objs(obj_no).fname = name;
          objs(obj_no).obj  = collection(fn);
          obj_no = obj_no+1;
        end
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(objs(obj_no).fname)
      disp(fn)
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the shape of the loaded objects.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      for ii = 1:numel(objs)
        obj   = objs(ii).obj;
        fname = objs(ii).fname;

        if ~isempty(strfind(fname, '_vec'))
          % Loaded object must be a vector
          if ~isvector(obj),  atest = false; end
        elseif ~isempty(strfind(fname, '_mat'))
          % Loaded object must be a matrix
          % REMARK: Known error in LTPDA version 1.9.2
          %         A saved matrix doesn't keep the shape of the matrix.
          if isempty(strfind(fname, '_192'))
            if ~(size(obj,1) > 1 && size(obj,2) > 1), atest = false; end
          end
        else
          % Loaded object must be a single object
          if ~(size(obj,1) == 1 && size(obj,2) == 1), 
            atest = false; end
        end

      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01

end
