diff testing/utp_1.1/utps/time/utp_time_parse.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 diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testing/utp_1.1/utps/time/utp_time_parse.m	Tue Dec 06 18:42:11 2011 +0100
@@ -0,0 +1,328 @@
+% UTP_TIME_PARSE
+%
+% <MethodDescription>
+%
+% Test the 'parse' static 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_parse.m,v 1.3 2011/04/28 07:06:27 hewitson Exp $
+
+function results = utp_time_parse(varargin)
+  
+  % check inputs
+  if nargin == 0
+    
+    % some keywords
+    class   = 'time';
+    mthd    = 'parse';
+    
+    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_902];
+      results = [results utp_903];
+      results = [results utp_904];
+      results = [results utp_905];
+    catch ex
+      % restore preferences
+      prefs.getTimePrefs.setTimeTimezone(oldTimezone);
+      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 'parse' static method with time string only.
+  %
+  % </TestDescription>
+  function result = utp_901
+    
+    % <SyntaxDescription>
+    % Use parse() to parse different time strings with different time-zones
+    % set.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      % set timezone to CET
+      prefs.getTimePrefs.setTimeTimezone('CET');
+      msec1 = time.parse('1970-01-01 01:00:01');
+      msec2 = time.parse('1970-01-02');
+      msec3 = time.parse('01:00:01');
+      % set timezone to UTC
+      prefs.getTimePrefs.setTimeTimezone('UTC');
+      msec4 = time.parse('1970-01-01 00:00:01');
+      msec5 = time.parse('1970-01-02');
+      msec6 = time.parse('01:00:01');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if syntax tests failed
+      assert(stest);
+      
+      % time string is interpreted in the given timezone
+      % <AlgoDescription>
+      % Check the resulting millsecond values are the expected ones.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(msec1 == 1000);
+      assert(msec2 == 24*60*60*1000 - 60*60*1000)
+      assert(msec3 == 1000);
+      assert(msec4 == 1000);
+      assert(msec5 == 24*60*60*1000);
+      assert(msec6 == 1000 + 60*60*1000);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_902
+  
+  % <TestDescription>
+  %
+  % Tests 'parse' static method with time and format strings.
+  %
+  % </TestDescription>
+  function result = utp_902
+    
+    % <SyntaxDescription>
+    % Use parse() to parse different time strings with different time-zones
+    % set. The time-string format is specified as a second input argument.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      prefs.getTimePrefs.setTimeTimezone('UTC');
+      msec1 = time.parse('1970-01-01 00:00:01', 'yyyy-mm-dd HH:MM:SS');
+      msec2 = time.parse('1970,01,01 00,00,01', 'yyyy,mm,dd HH,MM,SS');
+      msec3 = time.parse('00/00/01', 'HH/MM/SS');
+      msec4 = time.parse('02/01/1970', 'dd/mm/yyyy');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if syntax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the resulting millsecond values are the expected ones.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(msec1 == 1000);
+      assert(msec2 == 1000);
+      assert(msec3 == 1000);
+      assert(msec4 == 24*60*60*1000);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_903
+  
+  % <TestDescription>
+  %
+  % Tests 'parse' static method with time string and numeric format.
+  %
+  % </TestDescription>
+  function result = utp_903
+    
+    % <SyntaxDescription>
+    % Use parse() to parse different time strings using the supported
+    % MATLAB numeric time formats.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      prefs.getTimePrefs.setTimeTimezone('UTC');
+      % of the 31 formats supported by matlab it makes sense to support just a few
+      % the ones that contain date and time
+      fullfrmts = [ 0, 21, 30, 31 ];
+      % the ones that contain date
+      datefrmts = [ 1, 2, 22, 23, 24, 25, 26, 29 ];
+      % the ones that contain time
+      timefrmts = [ 13, 14 ];
+      % the ones that contain time without seconds
+      hourfrmts = [ 15, 16 ];
+      t = datenum(2010,07,28,01,02,03);
+      for kk = [ fullfrmts, datefrmts, timefrmts, hourfrmts ]
+        msec(kk+1) = time.parse(datestr(t, kk), kk);
+      end
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if syntax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the resulting millsecond values against the result of parsing
+      % a time string. (See time/parse utp_902.)
+      % </AlgoDescription>
+      % <AlgoCode>
+      % we checked this before and we can trust this value
+      tmsec = time.parse('2010-07-28 01:02:03');
+      assert(all(msec(fullfrmts+1) == tmsec));
+      % we checked this before and we can trust this value
+      tmsec = time.parse('2010-07-28');
+      assert(all(msec(datefrmts+1) == tmsec));
+      % we checked this before and we can trust this value
+      tmsec = time.parse('01:02:03');
+      assert(all(msec(timefrmts+1) == tmsec));
+      % we checked this before and we can trust this value
+      tmsec = time.parse('01:02:00');
+      assert(all(msec(hourfrmts+1) == tmsec));
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_904
+  
+  % <TestDescription>
+  %
+  % Tests 'parse' static method with time string containing timezone information.
+  %
+  % </TestDescription>
+  function result = utp_904
+    
+    % <SyntaxDescription>
+    % Use parse() to parse different time strings which contain the
+    % time-zone.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      % set timezone to UTC
+      prefs.getTimePrefs.setTimeTimezone('UTC');
+      msec(1) = time.parse('1970-01-01 00:00:01 UTC');
+      msec(2) = time.parse('1970-01-01 01:00:01 CET');
+      msec(3) = time.parse('1970-01-01 00:00:01 +0000');
+      msec(4) = time.parse('1970-01-01 01:00:01 +0100');
+      % set timezone to CET
+      prefs.getTimePrefs.setTimeTimezone('CET');
+      msec(5) = time.parse('1970-01-01 00:00:01 UTC');
+      msec(6) = time.parse('1970-01-01 01:00:01 CET');
+      msec(7) = time.parse('1970-01-01 00:00:01 +0000');
+      msec(8) = time.parse('1970-01-01 01:00:01 +0100');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if syntax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the resulting millisecond value is the expected one.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(all(msec == 1000));
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  
+  %% UTP_905
+  
+  % <TestDescription>
+  %
+  % Tests 'parse' static method with time string and timezone specification.
+  %
+  % </TestDescription>
+  function result = utp_905
+    
+    % <SyntaxDescription>
+    % Use parse() to parse different time strings when passing the
+    % time-zone as the third argument. The second argument (the format) is
+    % left empty.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      % set timezone to UTC
+      prefs.getTimePrefs.setTimeTimezone('UTC');
+      msec(1) = time.parse('1970-01-01 00:00:01', '', 'UTC');
+      msec(2) = time.parse('1970-01-01 01:00:01', '', 'CET');
+      % set timezone to CET
+      prefs.getTimePrefs.setTimeTimezone('CET');
+      msec(3) = time.parse('1970-01-01 00:00:01', '', 'UTC');
+      msec(4) = time.parse('1970-01-01 01:00:01', '', 'CET');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if syntax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the resulting millisecond value is the expected one.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(all(msec == 1000));
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+end