Пример #1
0
 def test_execute(self):
     f = Function('a', ['b', 'c'], "a.v = b.v + c.v", name="add")
     a = Itom('a', 0)
     b = Itom('b', 1)
     c = Itom('c', 2)
     itoms = Itoms([a, b, c])
     v = f.execute(itoms)
     self.assertAlmostEqual(v['a'].v, 3)
     b.v = 4
     c.v = 2
     v = f.execute(itoms)
     self.assertAlmostEqual(v['a'].v, 6)
     a.v = 0
     b.v = 5
     c.v = 5
     v = f.execute(itoms)
     self.assertAlmostEqual(v['a'].v, 10)
     with self.assertRaises(Exception):
         f.execute({a.name: a})
     with self.assertRaises(Exception):
         f.execute({b.name: b})
     # test execution without wrapping the code in a function
     f = Function('a', ['b', 'c'],
                  "a.v = b.v + c.v",
                  name="add",
                  wrap=False)
     b.v = 1
     c.v = 2
     v = f.execute(itoms)
     self.assertAlmostEqual(v['a'].v, 3)
     # function setting timestamp
     f = Function('a', ['b', 'c'],
                  "a.v = b.v + c.v; a.t = max(b.t, c.t)",
                  name="add")
     b.t = 0
     c.t = 1
     v = f.execute(itoms)
     self.assertEqual(v['a'].t, 1)
     # test execution with non-Python-identifiers as variables
     b = Itom('0b', 1)
     c = Itom('1c', 2)
     f = Function('a', [b.name, c.name],
                  "a.v = {}.v + {}.v".format(b.codename, c.codename))
     v = f.execute(Itoms([b, c]))
     self.assertAlmostEqual(v['a'].v, 3)
     # function with lists (e.g., ROS itoms representing point clouds)
     sonar = Itom('/p2os/sonar', [1, 3, 4])
     f = Function('dmin', [sonar.name],
                  "dmin.v = min({}.v)".format(sonar.codename))
     v = f.execute(Itoms([sonar]))
     self.assertAlmostEqual(v['dmin'].v, 1)
Пример #2
0
 def test_init_itom(self):
     a = Itom('a', 0)
     self.assertEqual(a.v, 0)
     self.assertEqual(a.t, None)
     a.v = 1
     a.t = 0
     self.assertEqual(a.v, 1)
     self.assertEqual(a.t, 0)
     a.v = 0
     self.assertEqual(a.v, 0)
     self.assertEqual(a.t, None)
     b = Itom('b1', 1, variable='b')
     self.assertEqual(b.variable, 'b')
     c = Itom('c', 2.1)
     self.assertAlmostEqual(c.v, 2.1)