def testMapByteStreamWithSequenceWithExpression2(self): """Tests the MapByteStream function with a sequence with expression.""" definitions_file = self._GetTestFilePath([u'structure2.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName( u'extension_block') data_type_map = runtime.StructureMap(data_type_definition) byte_values = [4, 1, 0, 0] for byte_value in range(0, 256): byte_values.extend([byte_value]) byte_stream = bytes(bytearray(byte_values)) extension_block = data_type_map.MapByteStream(byte_stream) self.assertEqual(extension_block.size, 260) self.assertEqual(extension_block.data[0], 0) self.assertEqual(extension_block.data[-1], 255) byte_values = [3, 0, 0, 0] for byte_value in range(0, 256): byte_values.extend([byte_value]) byte_stream = bytes(bytearray(byte_values)) with self.assertRaises(errors.MappingError): data_type_map.MapByteStream(byte_stream)
def testMapByteStreamWithSequenceWithExpression(self): """Tests the MapByteStream function with a sequence with expression.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'sphere3d') data_type_map = runtime.StructureMap(data_type_definition) byte_values = [3, 0, 0, 0] for value in range(1, 113): byte_value_upper, byte_value_lower = divmod(value, 256) byte_values.extend([byte_value_lower, byte_value_upper, 0, 0]) byte_stream = bytes(bytearray(byte_values)) sphere = data_type_map.MapByteStream(byte_stream) self.assertEqual(sphere.number_of_triangles, 3) self.assertEqual(sphere.triangles[0].a.x, 1) self.assertEqual(sphere.triangles[0].a.y, 2) self.assertEqual(sphere.triangles[0].a.z, 3) self.assertEqual(sphere.triangles[0].b.x, 4) self.assertEqual(sphere.triangles[0].b.y, 5) self.assertEqual(sphere.triangles[0].b.z, 6) self.assertEqual(sphere.triangles[0].c.x, 7) self.assertEqual(sphere.triangles[0].c.y, 8) self.assertEqual(sphere.triangles[0].c.z, 9)
def testInitialize(self): """Tests the __init__ function.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'point3d') data_type_map = runtime.StructureMap(data_type_definition) self.assertIsNotNone(data_type_map)
def testCheckCompositeMap(self): """Tests the _CheckCompositeMap function.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'point3d') data_type_map = runtime.StructureMap(data_type_definition) result = data_type_map._CheckCompositeMap(data_type_definition) self.assertFalse(result) with self.assertRaises(errors.FormatError): data_type_map._CheckCompositeMap(None) with self.assertRaises(errors.FormatError): data_type_definition = EmptyDataTypeDefinition(u'empty') data_type_map._CheckCompositeMap(data_type_definition) data_type_definition = definitions_registry.GetDefinitionByName( u'triangle3d') data_type_map = runtime.StructureMap(data_type_definition) result = data_type_map._CheckCompositeMap(data_type_definition) self.assertTrue(result) data_type_definition = definitions_registry.GetDefinitionByName(u'box3d') data_type_map = runtime.StructureMap(data_type_definition) result = data_type_map._CheckCompositeMap(data_type_definition) self.assertTrue(result) data_type_definition = definitions_registry.GetDefinitionByName( u'sphere3d') data_type_map = runtime.StructureMap(data_type_definition) result = data_type_map._CheckCompositeMap(data_type_definition) self.assertTrue(result)
def testGetByteStreamOperation(self): """Tests the _GetByteStreamOperation function.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'point3d') data_type_map = runtime.StructureMap(data_type_definition) operation = data_type_map._GetByteStreamOperation(data_type_definition) self.assertIsInstance(operation, runtime.StructOperation) with self.assertRaises(errors.FormatError): data_type_map._GetByteStreamOperation(None) with self.assertRaises(errors.FormatError): data_type_definition = EmptyDataTypeDefinition(u'empty') data_type_map._GetByteStreamOperation(data_type_definition)
def testGetMemberDataTypeMaps(self): """Tests the _GetMemberDataTypeMaps function.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'point3d') data_type_map = runtime.StructureMap(data_type_definition) members_data_type_maps = data_type_map._GetMemberDataTypeMaps( data_type_definition, {}) self.assertIsNotNone(members_data_type_maps) with self.assertRaises(errors.FormatError): data_type_map._GetMemberDataTypeMaps(None, {}) with self.assertRaises(errors.FormatError): data_type_definition = EmptyDataTypeDefinition(u'empty') data_type_map._GetMemberDataTypeMaps(data_type_definition, {})
def testMapByteStreamWithSequence(self): """Tests the MapByteStream function with a sequence.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'box3d') data_type_map = runtime.StructureMap(data_type_definition) byte_values = [] for value in range(1, 433): byte_value_upper, byte_value_lower = divmod(value, 256) byte_values.extend([byte_value_lower, byte_value_upper, 0, 0]) byte_stream = bytes(bytearray(byte_values)) box = data_type_map.MapByteStream(byte_stream) self.assertEqual(box.triangles[0].a.x, 1) self.assertEqual(box.triangles[0].a.y, 2) self.assertEqual(box.triangles[0].a.z, 3)
def testMapByteStream(self): """Tests the MapByteStream function.""" definitions_file = self._GetTestFilePath([u'structure.yaml']) definitions_registry = self._CreateDefinitionRegistryFromFile( definitions_file) data_type_definition = definitions_registry.GetDefinitionByName(u'point3d') data_type_map = runtime.StructureMap(data_type_definition) byte_values = [] for value in range(1, 4): byte_value_upper, byte_value_lower = divmod(value, 256) byte_values.extend([byte_value_lower, byte_value_upper, 0, 0]) byte_stream = bytes(bytearray(byte_values)) named_tuple = data_type_map.MapByteStream(byte_stream) self.assertEqual(named_tuple.x, 1) self.assertEqual(named_tuple.y, 2) self.assertEqual(named_tuple.z, 3)