def setUp(self): # obligatory and optional ports out_port_obl = OutPort( NamespacedName("port_name", None), DInt(), InPort(NamespacedName("port_name", None), DInt(), None, 0), None ) out_port_opt = OutPort( NamespacedName("port_name", None), DInt(), InPort(NamespacedName("port_name", None), DOptional(DInt()), None, 0), None ) # 4 types of queues self.delta_queue_obl = DeltaQueue(out_port_obl) self.delta_queue_opt = DeltaQueue(out_port_opt) self.const_queue_obl = ConstQueue(out_port_obl) self.const_queue_opt = ConstQueue(out_port_opt) # test messages self.msg1 = QueueMessage(1) self.msg2 = QueueMessage(2) self.msg_with_none = QueueMessage(None) self.msg_unpackable = QueueMessage("abcde") # these messages should be received self.msg1_answer = QueueMessage(1) self.msg2_answer = QueueMessage(2) self.msg_with_none_answer = QueueMessage(None)
def test_str(self): self.assertEqual(str(NamespacedName("a", "b")), "a.b") self.assertEqual(str(NamespacedName("a", None)), "a") with self.assertRaises(TypeError): NamespacedName("a") with self.assertRaises(TypeError): NamespacedName()
def test_out_port_capnp_wiring(self): """Generate wiring. Should just call same method in destination.""" in_port = Mock() out_port = OutPort(NamespacedName("node_name", None), as_delta_type(int), in_port, None) out_port.capnp_wiring([], None) in_port.capnp_wiring.assert_called_with([], None)
def test_in_port_capnp_optional(self): """Generate optional in port.""" in_port = InPort(NamespacedName("node_name", "index"), DOptional(int), None, 0) capnp_in_port = dotdf_capnp.InPort.new_message() in_port.capnp(capnp_in_port) self.assertEqual(capnp_in_port.name, "index") self.assertEqual(dill.loads(capnp_in_port.type), DInt()) self.assertEqual(capnp_in_port.optional, True)
def test_in_port_capnp(self): """Generate in port.""" in_port = InPort(NamespacedName("node_name", "index"), as_delta_type(int), None, 0) capnp_in_port = dotdf_capnp.InPort.new_message() in_port.capnp(capnp_in_port) self.assertEqual(capnp_in_port.name, "index") self.assertEqual(dill.loads(capnp_in_port.type), as_delta_type(int)) self.assertEqual(capnp_in_port.optional, False)
def add_out_port(self, port_destination: InPort, index=None): """Creates an out-port and adds it to my out-port store. Parameters ---------- index : Optional[str] If the out-port is one of several for this node, index specifies what part of the output is sent via this port. If the node has only one output, then this is `None`. port_destination : InPort The in-port that this out-port exports to. """ try: if issubclass(self.outputs, Void): raise DeltaIOError( f"Cannot make an out-port on node {self.name} " "with return type \'Void\'.\n" "Please either add the proper return type to the " "node definition or do not try to create an " "output data channel from this node.") except TypeError: pass if index is None: # out-port type is whole node return type # due to the strict typing the out type should match the in type if self.is_autogenerated: type_out = port_destination.port_type else: type_out = as_delta_type(self.outputs) self.out_ports.append( OutPort(NamespacedName(self.name, None), type_out, port_destination, self)) else: # out-port type is indexed node return type self.out_ports.append( OutPort(NamespacedName(self.name, index), as_delta_type(self.outputs.elem_dict[index]), port_destination, self)) # If this port is going into a port on a different graph, # flatten this graph into said graph into_graph = port_destination.node.graph if into_graph is not self.graph: into_graph.flatten(self.graph)
def test_out_port_capnp(self): """Generate out port.""" out_port = OutPort(NamespacedName("node_name", "index"), as_delta_type(int), None, None) capnp_out_port = dotdf_capnp.OutPort.new_message() out_port.capnp(capnp_out_port) self.assertEqual(capnp_out_port.name, "index") self.assertEqual(dill.loads(capnp_out_port.type), as_delta_type(int)) with self.assertRaises(AttributeError): dummy = capnp_out_port.optional
def test_out_port_capnp_no_index(self): """Generate out port when index not provided. In this case, name should be empty string. """ out_port = OutPort(NamespacedName("node_name", None), as_delta_type(int), None, None) capnp_out_port = dotdf_capnp.OutPort.new_message() out_port.capnp(capnp_out_port) self.assertEqual(capnp_out_port.name, "") self.assertEqual(dill.loads(capnp_out_port.type), as_delta_type(int))
def test_eq(self): self.assertEqual(NamespacedName("a", "b"), NamespacedName("a", "b")) self.assertEqual(NamespacedName("a", None), NamespacedName("a", None)) self.assertNotEqual(NamespacedName("a", "b"), NamespacedName("a", None))
def test_in_port_capnp_wiring_direct(self): """In port has limit on port size.""" in_port = InPort(NamespacedName("node_name", "index"), as_delta_type(int), None, 1) wire = dotdf_capnp.Wire.new_message() nodes = [dotdf_capnp.Node.new_message()] nodes[0].name = "node_name" nodes[0].init("inPorts", 1) nodes[0].inPorts[0].name = "index" in_port.capnp_wiring(nodes, wire) self.assertEqual(wire.destNode, 0) self.assertEqual(wire.destInPort, 0) self.assertEqual(wire.direct, True)
def test_in_port_capnp_wiring(self): """Generate wiring.""" in_port = InPort(NamespacedName("node_name", "index"), as_delta_type(int), None, 0) wire = dotdf_capnp.Wire.new_message() nodes = [dotdf_capnp.Node.new_message() for _ in range(3)] nodes[0].name = "fake_name" nodes[1].name = "fake_name" nodes[2].name = "node_name" nodes[2].init("inPorts", 3) nodes[2].inPorts[0].name = "fake_name" nodes[2].inPorts[1].name = "index" nodes[2].inPorts[2].name = "fake_name" in_port.capnp_wiring(nodes, wire) self.assertEqual(wire.destNode, 2) self.assertEqual(wire.destInPort, 1) self.assertEqual(wire.direct, False)
def test_str(self): """Test string representation of data types.""" # primitive self.assertEqual(str(DInt()), "DInt32") self.assertEqual(str(DInt(DSize(64))), "DInt64") self.assertEqual(str(DUInt()), "DUInt32") self.assertEqual(str(DUInt(DSize(64))), "DUInt64") self.assertEqual(str(DBool()), "DBool") self.assertEqual(str(DChar()), "DChar8") self.assertEqual(str(DFloat()), "DFloat32") self.assertEqual(str(DFloat(DSize(64))), "DFloat64") # compound self.assertEqual(str(DArray(int, DSize(8))), "[DInt32 x 8]") self.assertEqual(str(DStr()), "DStr8192") self.assertEqual(str(DStr(DSize(100))), "DStr800") self.assertEqual(str(DTuple([int, bool])), "(DInt32, DBool)") self.assertEqual(str(DRecord(RecBIS)), "{x: DBool, y: DInt32, z: DStr8192}") self.assertEqual(str(DUnion([int, bool])), "<DBool | DInt32>") # compound: DUnion self.assertEqual(str(DUnion([int])), "<DInt32>") self.assertEqual(str(DUnion([int, DUnion([int, bool])])), "<DBool | DInt32>") self.assertEqual(str(DUnion([int, DUnion([int, DUnion([int, bool])])])), "<DBool | DInt32>") # encapsulation of various types self.assertEqual(str(DUnion([int, DTuple([int, bool])])), "<(DInt32, DBool) | DInt32>") self.assertEqual(str(DArray(DTuple([int, bool]), DSize(8))), "[(DInt32, DBool) x 8]") # special self.assertEqual(str(Top()), "T") self.assertEqual(str(DSize(5)), "5") self.assertEqual(str(DSize(NamespacedName("a", "b"))), "(a.b)") self.assertEqual(str(ForkedReturn(dict(x=int, y=bool, z=str))), "ForkedReturn(x:DInt32, y:DBool, z:DStr8192)")
def add_in_port(self, arg_name: str, in_type: Type, in_port_size: int = 0): """Creates an in-port and adds it to my in-port store. Parameters ---------- arg_name : str Name of the argument this port supplies. in_type : Type The type that is expected to be supplied for this port. in_port_size: int Maximum size of the in ports. If 0 then size is unlimited. Returns ------- InPort The created port. """ my_port = InPort(NamespacedName(self.name, arg_name), as_delta_type(in_type), self, in_port_size) self.in_ports[my_port.port_name] = my_port return my_port
def test_DSize(self): """Test various use DSize.""" with self.assertRaises(DeltaTypeError): DInt(5) with self.assertRaises(ValueError): DSize(-1) with self.assertRaises(ValueError): dummy = DSize(4) + DSize(NamespacedName("a", "b")) # add d16_32 = DSize(16) d16_32 += DSize(32) self.assertEqual(d16_32, DSize(48)) self.assertEqual(DSize(4) + DSize(5), DSize(9)) # sub d32_16 = DSize(32) d32_16 -= DSize(16) self.assertEqual(d32_16, DSize(16)) self.assertEqual(DSize(5) - DSize(2), DSize(3)) with self.assertRaises(ValueError): dummy = DSize(5) - DSize(6) # mul self.assertEqual(DSize(4) * 5, DSize(20)) self.assertEqual(5 * DSize(4), DSize(20)) d16x4 = DSize(16) d16x4 *= 4 self.assertEqual(d16x4, DSize(64)) d16x4 = DSize(16) d16x4 *= DSize(4) self.assertEqual(d16x4, DSize(64)) # comparison self.assertTrue(DSize(8) == DSize(8)) self.assertTrue(DSize(8) > DSize(6)) self.assertTrue(DSize(4) < DSize(6))
def test_Size(self): """Test various use Size.""" with self.assertRaises(DeltaTypeError): Int(5) with self.assertRaises(ValueError): Size(-1) with self.assertRaises(ValueError): dummy = Size(4) + Size(NamespacedName("a", "b")) # add d16_32 = Size(16) d16_32 += Size(32) self.assertEqual(d16_32, Size(48)) self.assertEqual(Size(4) + Size(5), Size(9)) # sub d32_16 = Size(32) d32_16 -= Size(16) self.assertEqual(d32_16, Size(16)) self.assertEqual(Size(5) - Size(2), Size(3)) with self.assertRaises(ValueError): dummy = Size(5) - Size(6) # mul self.assertEqual(Size(4) * 5, Size(20)) self.assertEqual(5 * Size(4), Size(20)) d16x4 = Size(16) d16x4 *= 4 self.assertEqual(d16x4, Size(64)) d16x4 = Size(16) d16x4 *= Size(4) self.assertEqual(d16x4, Size(64)) # comparison self.assertTrue(Size(8) == Size(8)) self.assertTrue(Size(8) > Size(6)) self.assertTrue(Size(4) < Size(6))
def test_str(self): """Test string representation of data types.""" # primitive self.assertEqual(str(Int()), "Int32") self.assertEqual(str(Int(Size(64))), "Int64") self.assertEqual(str(UInt()), "UInt32") self.assertEqual(str(UInt(Size(64))), "UInt64") self.assertEqual(str(Bool()), "Bool") self.assertEqual(str(Char()), "Char8") self.assertEqual(str(Float()), "Float32") self.assertEqual(str(Float(Size(64))), "Float64") # compound self.assertEqual(str(Array(int, Size(8))), "[Int32 x 8]") self.assertEqual(str(Str()), "Str8192") self.assertEqual(str(Str(Size(100))), "Str800") self.assertEqual(str(Tuple([int, bool])), "(Int32, Bool)") self.assertEqual(str(Record(RecBIS)), "{x: Bool, y: Int32, z: Str8192}") self.assertEqual(str(Union([int, bool])), "<Bool | Int32>") # compound: Union self.assertEqual(str(Union([int])), "<Int32>") self.assertEqual(str(Union([int, Union([int, bool])])), "<Bool | Int32>") self.assertEqual(str(Union([int, Union([int, Union([int, bool])])])), "<Bool | Int32>") # encapsulation of various types self.assertEqual(str(Union([int, Tuple([int, bool])])), "<(Int32, Bool) | Int32>") self.assertEqual(str(Array(Tuple([int, bool]), Size(8))), "[(Int32, Bool) x 8]") # special self.assertEqual(str(Top()), "T") self.assertEqual(str(Size(5)), "5") self.assertEqual(str(Size(NamespacedName("a", "b"))), "(a.b)")
def test_DOptional(self): port = OutPort(NamespacedName("test_name", None), DOptional(DInt()), None, None) with self.assertRaises(TypeError): dummy = port.port_type
def test_non_DOptional(self): port = OutPort(NamespacedName("test_name", None), DInt(), None, None) self.assertEqual(port.port_type, DInt())
def test_DOptiona_of_DUnion(self): port = InPort(NamespacedName("test_name", None), DOptional(DUnion([DInt(), DFloat()])), None, 0) self.assertEqual(port.port_type, DUnion([DInt(), DFloat()])) self.assertEqual(port.is_optional, True)
def test_DUnion_of_single(self): """DUnion of a single type is not converted to a single type.""" port = InPort(NamespacedName("test_name", None), DUnion([DInt()]), None, 0) self.assertEqual(port.port_type, DUnion([DInt()])) self.assertEqual(port.is_optional, False)
def test_DOptional(self): port = InPort(NamespacedName("test_name", None), DOptional(DInt()), None, 0) self.assertEqual(port.port_type, DInt()) self.assertEqual(port.is_optional, True)