Go to the documentation of this file.00001 import unittest
00002 import numpy as np
00003 import ros_numpy
00004 from nav_msgs.msg import OccupancyGrid, MapMetaData
00005
00006 from io import BytesIO
00007
00008 class TestOccupancyGrids(unittest.TestCase):
00009 def test_masking(self):
00010 data = -np.ones((30, 30), np.int8)
00011 data[10:20, 10:20] = 100
00012
00013 msg = ros_numpy.msgify(OccupancyGrid, data)
00014
00015 data_out = ros_numpy.numpify(msg)
00016
00017 self.assertIs(data_out[5, 5], np.ma.masked)
00018 np.testing.assert_equal(data_out[10:20, 10:20], 100)
00019
00020 def test_serialization(self):
00021 msg = OccupancyGrid(
00022 info=MapMetaData(
00023 width=3,
00024 height=3
00025 ),
00026 data = [0, 0, 0, 0, -1, 0, 0, 0, 0]
00027 )
00028
00029 data = ros_numpy.numpify(msg)
00030 self.assertIs(data[1,1], np.ma.masked)
00031 msg2 = ros_numpy.msgify(OccupancyGrid, data)
00032
00033 self.assertEqual(msg.info, msg2.info)
00034
00035 io1 = BytesIO()
00036 io2 = BytesIO()
00037 msg.serialize(io1)
00038 msg2.serialize(io2)
00039
00040 self.assertEqual(io1.getvalue(), io2.getvalue(), "Message serialization survives round-trip")
00041
00042 if __name__ == '__main__':
00043 unittest.main()