view testing/utp_1.1/utps/time/utp_time_datenum.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
line wrap: on
line source

% UTP_TIME_DOUBLE
%
% <MethodDescription>
%
% Test the 'datenum' method of the time class.
%
% </MethodDescription>
%
% NOTE: in this test we manipulate user preferences thus we should make
% sure we restore the original user preferences at the end of the testing
%
% $Id: utp_time_datenum.m,v 1.3 2011/04/28 07:06:27 hewitson Exp $

function results = utp_time_datenum(varargin)
  
  % check inputs
  if nargin == 0
    
    % some keywords
    class   = 'time';
    mthd    = 'datenum';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % get preferences
    prefs = getappdata(0, 'LTPDApreferences');
    oldTimezone = char(prefs.getTimePrefs.getTimeTimezone);
    
    try
      results = [results utp_901];
      results = [results utp_901];
      results = [results utp_902];
      results = [results utp_903];
    catch ex
      % restore preferences
      
      rethrow(ex);
    end
    
    % restore preferences
    prefs.getTimePrefs.setTimeTimezone(oldTimezone);
    
    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_901
  
  % <TestDescription>
  %
  % Tests the 'datenum' method.
  %
  % </TestDescription>
  function result = utp_901
    
    % <SyntaxDescription>
    % Create a time object and use datenum to produce a serial date number.
    % </SyntaxDescription>
    
    stest = false;
    try
      % <SyntaxCode>
      % construct a well known time object
      t1 = time('1982-08-31 01:02:03 UTC');
      n1 = datenum(t1);
      % </SyntaxCode>
      stest = true;
    end
    
    atest = true;
    try
      % do not run algorithm tests if sintax tests failed
      assert(stest);
      
      % <AlgoDescription>
      % Check that the returned value is the same as the one you get when
      % using time/format with the same format string when converted using
      % MATLAB's datestr() function.
      % </AlgoDescription>
      
      % <AlgoCode>
      assert(strcmp(datestr(n1), t1.format('dd-mmm-yyyy HH:MM:SS')))
      % </AlgoCode>
    catch
      atest = false;
    end
    
    % return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end
  
  %% UTP_902
  
  % <TestDescription>
  %
  % Tests the 'datenum' method.
  %
  % </TestDescription>
  function result = utp_902
    
    % <SyntaxDescription>
    % Set the time-zone to UTC and create a time object and use datenum to
    % produce a serial date number.
    % </SyntaxDescription>
    stest = false;
    try
      % <SyntaxCode>
      % set time zone to UTC
      prefs.getTimePrefs.setTimeTimezone('UTC');
      % construct a well known time object
      t1 = time('1982-08-31 01:02:03 UTC');
      n1 = datenum(t1);
      % </SyntaxCode>
      stest = true;
    end
    
    atest = true;
    try
      % do not run algorithm tests if sintax tests failed
      assert(stest);
      % <AlgoDescription>
      % Check that the returned value is the same as the one you get when
      % using time/format with the same format string when converted using
      % MATLAB's datestr() function.
      % </AlgoDescription>      
      % <AlgoCode>
      assert(strcmp(datestr(n1), t1.format('dd-mmm-yyyy HH:MM:SS')))
      % </AlgoCode>
      
    catch
      atest = false;
    end
    
    % return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end
  
  %% UTP_903
  
  % <TestDescription>
  %
  % Tests 'double' method.
  %
  % </TestDescription>
  function result = utp_903
    
    % <SyntaxDescription>
    % Set the time-zone to PST and create a time object and use datenum to
    % produce a serial date number.
    % </SyntaxDescription>
    stest = false;
    try
      % <SyntaxCode>
      % set time zone to PST
      prefs.getTimePrefs.setTimeTimezone('PST');
      % construct a well known time object
      t1 = time('1982-08-31 01:02:03 UTC');
      n1 = datenum(t1);
      % </SyntaxCode>
      stest = true;
    end
    
    atest = true;
    try
      % do not run algorithm tests if sintax tests failed
      assert(stest);
      
      % <AlgoDescription>
      % Check that the returned value is the same as the one you get when
      % using time/format with the same format string when converted using
      % MATLAB's datestr() function.
      % </AlgoDescription>      
      % <AlgoCode>
      assert(strcmp(datestr(n1), t1.format('dd-mmm-yyyy HH:MM:SS')))
      % </AlgoCode>
    catch
      atest = false;
    end
    
    % return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end
  
end