test_TimeMeasure.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 # @file test_TimeMeasure.py
00005 # @brief test for TimeMeasure class
00006 # @date $Date: 2009/02/18$
00007 # @author Shinji Kurihara
00008 #
00009 # Copyright (C) 2009
00010 #     Task-intelligence Research Group,
00011 #     Intelligent Systems Research Institute,
00012 #     National Institute of
00013 #         Advanced Industrial Science and Technology (AIST), Japan
00014 #     All rights reserved.
00015 #
00016 
00017 import time
00018 import sys
00019 import math
00020 sys.path.insert(1,"../")
00021 
00022 import unittest
00023 from TimeMeasure import *
00024 
00025 
00026 
00027 class TestTimeMeasure(unittest.TestCase):
00028   def setUp(self):
00029     self._tm = TimeMeasure(10)
00030   
00031   def test_tick_tack(self):
00032     for i in range(10):
00033       self._tm.tick()
00034       time.sleep(0.01)
00035       self._tm.tack()
00036     _max    = [0]
00037     _min    = [0]
00038     _mean   = [0]
00039     _stddev = [0]
00040     print "count: ", self._tm.count()
00041     print "result: ", self._tm.getStatistics(_max, _min, _mean, _stddev)
00042     print "max: ", _max[0]
00043     print "min: ", _min[0]
00044     print "mean: ", _mean[0]
00045     print "stddev: ", _stddev[0]
00046     return
00047 
00048   def test_coutn(self):
00049     tm_ = TimeMeasure()
00050     self.assertEqual(0, tm_.count())
00051 
00052     count0 = 10
00053     for i in range(10):
00054       tm_.tick()
00055       tm_.tack()
00056     self.assertEqual(10, tm_.count())
00057 
00058     tm_.reset()
00059     self.assertEqual(0, tm_.count())
00060 
00061 
00062     count0 = 102
00063     for i in range(102):
00064       tm_.tick()
00065       tm_.tack()
00066     self.assertNotEqual(102, tm_.count())
00067     return
00068 
00069   def test_stat(self):
00070     tm_ = TimeMeasure()
00071     maxi  = [0.0]
00072     mini  = [0.0]
00073     mean  = [0.0]
00074     stdev = [0.0]
00075 
00076     self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
00077     tm_.tick()
00078     self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
00079     tm_.tack()
00080     self.assertEqual(True, tm_.getStatistics(maxi, mini, mean, stdev))
00081     tm_.reset()
00082     self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
00083     return
00084 
00085   def test_buflen(self):
00086     tm_0 = TimeMeasure(1)
00087     self.assertEqual(0, tm_0.count())
00088     tm_0.tick()
00089     tm_0.tack()
00090     self.assertEqual(1, tm_0.count())
00091 
00092     count_ = 1024
00093     tm_1 = TimeMeasure(count_)
00094 
00095     for i in range(count_):
00096       self.assertEqual(i, tm_1.count())
00097       tm_1.tick()
00098       tm_1.tack()
00099 
00100     for i in range(count_):
00101       tm_1.tick()
00102       tm_1.tack()
00103       self.assertEqual(count_ + 1, tm_1.count())
00104     return
00105 
00106   def test_30ms(self):
00107     wait_ = 0.03 # [s]
00108     tm_ = TimeMeasure()
00109     for i in range(10):
00110       tm_.tick()
00111       time.sleep(wait_)
00112       tm_.tack()
00113 
00114     maxi  = [0.0]
00115     mini  = [0.0]
00116     mean  = [0.0]
00117     stdev = [0.0]
00118     tm_.getStatistics(maxi, mini, mean, stdev)
00119     print "test_30ms"
00120     print "max interval : ", maxi[0], " [sec]"
00121     print "min interval : ", mini[0], " [sec]"
00122     print "mean interval: ", mean[0], " [sec]"
00123     print "stddev       : ", stdev[0], " [sec]"
00124     self.assert_(maxi[0] < (wait_ + 0.030))
00125     self.assert_(mini[0] > (wait_ - 0.010))
00126     self.assert_(math.fabs(mean[0] - wait_) < 0.03)
00127     self.assert_(stdev[0] < (wait_ / 5.0))
00128     return
00129 
00130   def test_1s(self):
00131     wait_ = 1.0 # [s]
00132     tm_ = TimeMeasure()
00133     for i in range(1):
00134       tm_.tick()
00135       time.sleep(wait_)
00136       tm_.tack()
00137 
00138     maxi  = [0.0]
00139     mini  = [0.0]
00140     mean  = [0.0]
00141     stdev = [0.0]
00142     tm_.getStatistics(maxi, mini, mean, stdev)
00143     print "test_1ms"
00144     print "max interval : ", maxi[0], " [sec]"
00145     print "min interval : ", mini[0], " [sec]"
00146     print "mean interval: ", mean[0], " [sec]"
00147     print "stddev       : ", stdev[0], " [sec]"
00148     self.assert_(maxi[0] < (wait_ + 0.030))
00149     self.assert_(mini[0] > (wait_ - 0.010))
00150     self.assert_(math.fabs(mean[0] - wait_) < 0.03)
00151     self.assert_(stdev[0] < (wait_ / 5.0))
00152     return
00153   
00154 ############### test #################
00155 if __name__ == '__main__':
00156   unittest.main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28