def test_as_delta_type(self): """Test conversion from python to Deltaflow data types.""" # special self.assertEqual(as_delta_type(object), Top()) self.assertEqual(as_delta_type(type(object)), Top()) self.assertEqual(as_delta_type(type), Top()) self.assertEqual(as_delta_type(Void), Void) with self.assertRaises(DeltaTypeError): as_delta_type(None) with self.assertRaises(DeltaTypeError): as_delta_type(type(None)) # primitive self.assertNotEqual(as_delta_type(bool), DUInt(DSize(1))) self.assertEqual(as_delta_type(bool), DBool()) self.assertEqual(as_delta_type(np.bool_), DBool()) self.assertEqual(as_delta_type(int), DInt(DSize(32))) self.assertEqual(as_delta_type(np.int8), DChar()) self.assertEqual(as_delta_type(np.int16), DInt(DSize(16))) self.assertEqual(as_delta_type(np.int32), DInt(DSize(32))) self.assertEqual(as_delta_type(np.int64), DInt(DSize(64))) self.assertEqual(as_delta_type(np.uint8), DChar()) self.assertEqual(as_delta_type(np.uint16), DUInt(DSize(16))) self.assertEqual(as_delta_type(np.uint32), DUInt(DSize(32))) self.assertEqual(as_delta_type(np.uint64), DUInt(DSize(64))) self.assertEqual(as_delta_type(float), DFloat()) self.assertEqual(as_delta_type(np.float32), DFloat(DSize(32))) self.assertEqual(as_delta_type(np.float64), DFloat(DSize(64))) self.assertEqual(as_delta_type(complex), DComplex()) self.assertEqual(as_delta_type(np.complex64), DComplex(DSize(64))) self.assertEqual(as_delta_type(np.complex128), DComplex(DSize(128))) # compound with self.assertRaises(DeltaTypeError): as_delta_type(Tuple[int, bool]) with self.assertRaises(DeltaTypeError): as_delta_type(List[int]) self.assertNotEqual(as_delta_type(str), DArray(DChar(), DSize(1024))) self.assertEqual(as_delta_type(str), DStr()) self.assertEqual(as_delta_type(RecBI), DRecord(RecBI)) # numpy compound self.assertEqual(as_delta_type(DArray(int, DSize(5)).as_numpy_type()), DArray(int, DSize(5))) self.assertEqual(as_delta_type(DStr().as_numpy_type()), DStr()) self.assertEqual( as_delta_type(DTuple([int, bool, float]).as_numpy_type()), DTuple([int, bool, float]) ) self.assertEqual(as_delta_type(DRecord(RecBI).as_numpy_type()), DRecord(RecBI)) self.assertEqual( as_delta_type(DUnion([bool, float, int]).as_numpy_type()), DUnion([bool, float, int]))
def test_delta_type(self): """Test mapping python objects to Deltaflow data types.""" # special with self.assertRaises(DeltaTypeError): delta_type(None) # primitive self.assertEqual(delta_type(False), DBool()) self.assertEqual(delta_type(np.bool_(False)), DBool()) self.assertEqual(delta_type(5), DInt(DSize(32))) self.assertEqual(delta_type(np.int16(5)), DInt(DSize(16))) self.assertEqual(delta_type(np.int32(5)), DInt(DSize(32))) self.assertEqual(delta_type(np.int64(5)), DInt(DSize(64))) self.assertEqual(delta_type(np.uint16(5)), DUInt(DSize(16))) self.assertEqual(delta_type(np.uint32(5)), DUInt(DSize(32))) self.assertEqual(delta_type(np.uint64(5)), DUInt(DSize(64))) self.assertEqual(delta_type(4.2), DFloat(DSize(32))) self.assertEqual(delta_type(np.float32(4.2)), DFloat(DSize(32))) self.assertEqual(delta_type(np.float64(4.2)), DFloat(DSize(64))) self.assertEqual(delta_type(3+1j), DComplex(DSize(64))) self.assertEqual(delta_type(np.complex64(3+1j)), DComplex(DSize(64))) self.assertEqual(delta_type(np.complex128(3+1j)), DComplex(DSize(128))) self.assertEqual(delta_type('c'), DChar()) # compound self.assertEqual(delta_type((1, True, 3.7)), DTuple([int, bool, float])) self.assertEqual(delta_type([1, 2, 4]), DArray(int, DSize(3))) self.assertEqual(delta_type(RecBI(True, 5)), DRecord(RecBI)) # numpy compound self.assertEqual(delta_type(np.array([1, 2, 3, 4, 5])), DArray(DInt(DSize(64)), DSize(5))) self.assertEqual(delta_type(np.array([1, 2.0, 3, 4, 5])), DArray(DFloat(DSize(64)), DSize(5))) self.assertEqual(delta_type( DStr(DSize(5)).as_numpy_object("abcde")), DStr(DSize(5))) self.assertEqual( delta_type(DTuple([int, float, bool] ).as_numpy_object((1, 2.0, True))), DTuple([int, float, bool]) ) self.assertEqual( delta_type(DRecord(RecBI).as_numpy_object(RecBI(True, 2))), DRecord(RecBI) ) self.assertEqual( delta_type(DUnion([bool, float, int]).as_numpy_object(5.0)), DUnion([bool, float, int]) ) # different combinations self.assertEqual(delta_type([(4, 4.3), (2, 3.3)]), DArray(DTuple([int, float]), DSize(2)))
def test_DComplex(self): """Only 64 and 128 bits are supported.""" for bits in (64, 128): for _ in range(1000): self.check_complex(random.uniform(-1, 1) + random.uniform(-1, 1) * 1j, DComplex(DSize(bits))) self.assertTrue(DeltaGraph.check_wire(DRaw(complex), DRaw(complex))) with self.assertRaises(DeltaTypeError): DeltaGraph.check_wire(DRaw(DComplex(DSize(64))), DRaw(DComplex(DSize(128))))
def test_as_python_type(self): """Test conversion of Deltaflow data types to python.""" # special self.assertEqual(Top().as_python_type(), Any) # primitive self.assertEqual(DInt(DSize(32)).as_python_type(), int) self.assertEqual(DInt(DSize(64)).as_python_type(), int) self.assertEqual(DUInt(DSize(32)).as_python_type(), int) self.assertEqual(DUInt(DSize(64)).as_python_type(), int) self.assertEqual(DBool().as_python_type(), bool) with self.assertRaises(NotImplementedError): DChar().as_python_type() self.assertEqual(DFloat(DSize(32)).as_python_type(), float) self.assertEqual(DFloat(DSize(64)).as_python_type(), float) self.assertEqual(DComplex(DSize(64)).as_python_type(), complex) self.assertEqual(DComplex(DSize(128)).as_python_type(), complex) # compound self.assertEqual(DTuple([int, bool]).as_python_type(), Tuple[int, bool]) self.assertEqual(DTuple([int, DTuple([int, bool])]).as_python_type(), Tuple[int, Tuple[int, bool]]) self.assertEqual(DArray(int, DSize(3)).as_python_type(), List[int]) self.assertEqual(DStr().as_python_type(), str) self.assertEqual(DStr(DSize(10)).as_python_type(), str) self.assertEqual(DRecord(RecBI).as_python_type(), RecBI) self.assertEqual(DRecord(RecBDi).as_python_type(), RecBDi) self.assertNotEqual(DRecord(RecBI).as_python_type(), RecBI_copy) # compound: DUnion self.assertEqual(DUnion([bool, int]).as_python_type(), Union[bool, int]) self.assertEqual(DUnion([bool, DTuple([int, bool])]).as_python_type(), Union[bool, Tuple[int, bool]])
def test_DComplex(self): """Only 64 and 128 bits are supported.""" for bits in (64, 128): for _ in range(1000): self.check_complex(random.uniform(-1, 1) + random.uniform(-1, 1) * 1j, DComplex(DSize(bits))) # Booleans, ints and floats can be packed self.check(True, DComplex(DSize(bits))) self.check(False, DComplex(DSize(bits))) self.check(3, DComplex(DSize(bits))) self.check(3.5, DComplex(DSize(bits))) # Strings are not packable self.assertFalse(DComplex(DSize(bits)).is_packable("abc"))
def test_DComplex_object(self): self.check_complex(1 + 0.5j, DComplex(DSize(64))) self.check_complex(5 + 0.2j, DComplex(DSize(128)))
def test_DComplex_type(self): self.assertEqual(DComplex(DSize(64)).as_numpy_type(), np.complex64) self.assertEqual(DComplex(DSize(128)).as_numpy_type(), np.complex128)