diff testing/utp_1.1/utps/time/utp_time_time.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_time.m	Tue Dec 06 18:42:11 2011 +0100
@@ -0,0 +1,464 @@
+% UTP_TIME_TIME
+%
+% <MethodDescription>
+%
+% Tests time object constructor.
+%
+% </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_time.m,v 1.5 2011/04/28 07:06:27 hewitson Exp $
+
+function results = utp_time_time(varargin)
+  
+  global DEBUG
+  DEBUG = false;
+  
+  % check inputs
+  if nargin == 0
+    
+    % some keywords
+    class   = 'time';
+    mthd    = 'time';
+    
+    results = [];
+    disp('******************************************************');
+    disp(['****  Running UTPs for ' class '/' mthd]);
+    disp('******************************************************');
+    
+    % get preferences
+    prefs = getappdata(0, 'LTPDApreferences');
+    oldTimezone   = char(prefs.getTimePrefs.getTimeTimezone);
+    oldTimeformat = char(prefs.getTimePrefs.getTimestringFormat);
+    
+    % set timezone to UTC to simplify testing
+    prefs.getTimePrefs.setTimeTimezone('UTC');
+    
+    % get preferences
+    try
+      results = [results utp_901];
+      results = [results utp_902];
+      results = [results utp_903];
+      results = [results utp_904];
+      results = [results utp_905];
+      results = [results utp_906];
+      results = [results utp_907];
+      results = [results utp_908];
+      results = [results utp_909];
+    catch ex
+      % restore preferences
+      prefs.getTimePrefs.setTimeTimezone(oldTimezone);
+      prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
+      rethrow(ex);
+    end
+    
+    % restore preferences
+    prefs.getTimePrefs.setTimeTimezone(oldTimezone);
+    prefs.getTimePrefs.setTimestringFormat(oldTimeformat);
+    
+    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 time object constructor without arguments. Should return the current time.
+  %
+  % </TestDescription>
+  function result = utp_901
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with no inputs.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj = time();
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % nothing to test here
+      % <AlgoDescription>
+      % No test can be done here since we don't know the time when the
+      % constructor is called. We could check that the resulting time is >
+      % 0, but that's not so useful.
+      % </AlgoDescription>
+      % <AlgoCode>
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_902
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from numeric value.
+  %
+  % </TestDescription>
+  function result = utp_902
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with a numeric input (number of seconds
+    % since epoch).
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj = time(1234.5);
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % nothing to test here
+      % <AlgoDescription>
+      % Check the time object has the expected millisecond value.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(obj.utc_epoch_milli == 1234.5*1000.0);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_903
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from string.
+  %
+  % </TestDescription>
+  function result = utp_903
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with different time-string inputs.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj1 = time('1970-01-01 00:00:00');
+      obj2 = time('1970-01-01 00:01:00');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the time objects have the expected millisecond values.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assertEq(obj1.utc_epoch_milli, 0);
+      assertEq(obj2.utc_epoch_milli, 60*1000.0);
+      % </AlgoCode>
+    catch ex
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  
+  %% UTP_904
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from a cell array of strings.
+  %
+  % </TestDescription>
+  function result = utp_904
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with cell-array of time-strings.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj = time({'1970-01-01 00:00:00', '1970-01-01 00:01:00', '1970-01-01 00:02:00'});
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the time objects have the expected millisecond values.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(all(size(obj) == [1, 3]));
+      assert(all([obj.utc_epoch_milli] == [0 60 120]*1000.0));
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_905
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from plist with 'time' parameter.
+  %
+  % </TestDescription>
+  function result = utp_905
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with plist input. Check a plist with a
+    % numeric value and a plist with a time-string.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj1 = time(plist('time', 1234.5));
+      obj2 = time(plist('time', '1970-01-01 00:01:00'));
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the time objects have the expected millisecond values.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(obj1.utc_epoch_milli == 1234.5*1000.0);
+      assert(obj2.utc_epoch_milli == 60*1000.0);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  %% UTP_906
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from plist with 'milliseconds' parameter
+  %
+  % </TestDescription>
+  function result = utp_906
+    
+    % <SyntaxDescription>
+    % Call the time() constructor with plist input using the 'millisecond'
+    % key.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj = time(plist('milliseconds', 1234.5));
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the time object has the expected millisecond value.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assertEq(obj.utc_epoch_milli, 1234.5);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  
+  %% UTP_907
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from plist with more parameters.
+  %
+  % </TestDescription>
+  function result = utp_907
+    % <SyntaxDescription>
+    % Call the time() constructor with plist input containing the time
+    % string and the time-zone.
+    % key.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj1 = time(plist('time', '1970-01-01 01:01:00', 'timezone', 'UTC'));
+      obj2 = time(plist('time', '1970-01-01 01:01:00', 'timezone', 'CET'));
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the time objects have the expected millisecond values.
+      % </AlgoDescription>
+      % <AlgoCode>
+      % time string is interpreted in the given timezone
+      assertEq(obj1.utc_epoch_milli, 61*60*1000.0);
+      assertEq(obj2.utc_epoch_milli, 60*1000.0);
+      % </AlgoCode>
+    catch ex
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  
+  %% UTP_908
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from structure.
+  %
+  % </TestDescription>
+  function result = utp_908
+    % <SyntaxDescription>
+    % Call the time() constructor with an input structure obtained from
+    % calling struct() on a time object.
+    % key.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      t = time(plist('time', '1970-01-01 00:01:00'));
+      w = warning('off', 'MATLAB:structOnObject');
+      s = struct(t);
+      warning(w);
+      obj = time(s);
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the original and reconstructed time objects have the same
+      % millisecond value.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assert(obj.utc_epoch_milli == t.utc_epoch_milli);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+  
+  %% UTP_909
+  
+  % <TestDescription>
+  %
+  % Tests time object constructor from string and time format.
+  %
+  % </TestDescription>
+  function result = utp_909
+    % <SyntaxDescription>
+    % Call the time() constructor with an input time string and time format
+    % string.
+    % </SyntaxDescription>
+    stest = false;
+    try
+      % <SyntaxCode>
+      obj = time('1970-01-01 00:01:00', 'yyyy-mm-dd HH:MM:SS');
+      % </SyntaxCode>
+      stest = true;
+    end
+    
+    atest = true;
+    try
+      % do not run algorithm tests if sintax tests failed
+      assert(stest);
+      
+      % <AlgoDescription>
+      % Check the resulting time object has the expected millisecond value.
+      % </AlgoDescription>
+      % <AlgoCode>
+      assertEq(obj.utc_epoch_milli, 60*1000.0);
+      % </AlgoCode>
+    catch
+      atest = false;
+    end
+    
+    % return a result structure
+    result = utp_prepare_result(atest, stest, dbstack, mfilename);
+  end
+  
+end
+
+function rv = assertEq(v1, v2)
+  global DEBUG
+  if v1 == v2
+    rv = true;
+  else
+    msg = sprintf('ERROR: assertEq: expected %e got %e\n', v2, v1);
+    if DEBUG
+      fprintf([msg '\n']);
+    end
+    error(msg);
+  end
+end