test_TimeMeasure.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 # @file test_TimeMeasure.py
5 # @brief test for TimeMeasure class
6 # @date $Date: 2009/02/18$
7 # @author Shinji Kurihara
8 #
9 # Copyright (C) 2009
10 # Task-intelligence Research Group,
11 # Intelligent Systems Research Institute,
12 # National Institute of
13 # Advanced Industrial Science and Technology (AIST), Japan
14 # All rights reserved.
15 #
16 
17 import time
18 import sys
19 import math
20 sys.path.insert(1,"../")
21 
22 import unittest
23 from TimeMeasure import *
24 
25 
26 
27 class TestTimeMeasure(unittest.TestCase):
28  def setUp(self):
29  self._tm = TimeMeasure(10)
30 
31  def test_tick_tack(self):
32  for i in range(10):
33  self._tm.tick()
34  time.sleep(0.01)
35  self._tm.tack()
36  _max = [0]
37  _min = [0]
38  _mean = [0]
39  _stddev = [0]
40  print "count: ", self._tm.count()
41  print "result: ", self._tm.getStatistics(_max, _min, _mean, _stddev)
42  print "max: ", _max[0]
43  print "min: ", _min[0]
44  print "mean: ", _mean[0]
45  print "stddev: ", _stddev[0]
46  return
47 
48  def test_coutn(self):
49  tm_ = TimeMeasure()
50  self.assertEqual(0, tm_.count())
51 
52  count0 = 10
53  for i in range(10):
54  tm_.tick()
55  tm_.tack()
56  self.assertEqual(10, tm_.count())
57 
58  tm_.reset()
59  self.assertEqual(0, tm_.count())
60 
61 
62  count0 = 102
63  for i in range(102):
64  tm_.tick()
65  tm_.tack()
66  self.assertNotEqual(102, tm_.count())
67  return
68 
69  def test_stat(self):
70  tm_ = TimeMeasure()
71  maxi = [0.0]
72  mini = [0.0]
73  mean = [0.0]
74  stdev = [0.0]
75 
76  self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
77  tm_.tick()
78  self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
79  tm_.tack()
80  self.assertEqual(True, tm_.getStatistics(maxi, mini, mean, stdev))
81  tm_.reset()
82  self.assertEqual(False, tm_.getStatistics(maxi, mini, mean, stdev))
83  return
84 
85  def test_buflen(self):
86  tm_0 = TimeMeasure(1)
87  self.assertEqual(0, tm_0.count())
88  tm_0.tick()
89  tm_0.tack()
90  self.assertEqual(1, tm_0.count())
91 
92  count_ = 1024
93  tm_1 = TimeMeasure(count_)
94 
95  for i in range(count_):
96  self.assertEqual(i, tm_1.count())
97  tm_1.tick()
98  tm_1.tack()
99 
100  for i in range(count_):
101  tm_1.tick()
102  tm_1.tack()
103  self.assertEqual(count_ + 1, tm_1.count())
104  return
105 
106  def test_30ms(self):
107  wait_ = 0.03 # [s]
108  tm_ = TimeMeasure()
109  for i in range(10):
110  tm_.tick()
111  time.sleep(wait_)
112  tm_.tack()
113 
114  maxi = [0.0]
115  mini = [0.0]
116  mean = [0.0]
117  stdev = [0.0]
118  tm_.getStatistics(maxi, mini, mean, stdev)
119  print "test_30ms"
120  print "max interval : ", maxi[0], " [sec]"
121  print "min interval : ", mini[0], " [sec]"
122  print "mean interval: ", mean[0], " [sec]"
123  print "stddev : ", stdev[0], " [sec]"
124  self.assert_(maxi[0] < (wait_ + 0.030))
125  self.assert_(mini[0] > (wait_ - 0.010))
126  self.assert_(math.fabs(mean[0] - wait_) < 0.03)
127  self.assert_(stdev[0] < (wait_ / 5.0))
128  return
129 
130  def test_1s(self):
131  wait_ = 1.0 # [s]
132  tm_ = TimeMeasure()
133  for i in range(1):
134  tm_.tick()
135  time.sleep(wait_)
136  tm_.tack()
137 
138  maxi = [0.0]
139  mini = [0.0]
140  mean = [0.0]
141  stdev = [0.0]
142  tm_.getStatistics(maxi, mini, mean, stdev)
143  print "test_1ms"
144  print "max interval : ", maxi[0], " [sec]"
145  print "min interval : ", mini[0], " [sec]"
146  print "mean interval: ", mean[0], " [sec]"
147  print "stddev : ", stdev[0], " [sec]"
148  self.assert_(maxi[0] < (wait_ + 0.030))
149  self.assert_(mini[0] > (wait_ - 0.010))
150  self.assert_(math.fabs(mean[0] - wait_) < 0.03)
151  self.assert_(stdev[0] < (wait_ / 5.0))
152  return
153 
154 ############### test #################
155 if __name__ == '__main__':
156  unittest.main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:35