Beispiel #1
0
    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]))
Beispiel #2
0
    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)))
Beispiel #3
0
    def test_DFloat(self):
        """Only 32 and 64 bits are supported."""
        precision_dict = {32: -23, 64: -52}

        for bits, precision in precision_dict.items():
            for _ in range(1000):
                self.check_float(random.uniform(-1, 1), DFloat(DSize(bits)))

            self.check(1 + 2**precision, DFloat(DSize(bits)))

        self.assertTrue(DeltaGraph.check_wire(DRaw(float), DRaw(float)))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DRaw(DFloat(DSize(32))),
                                  DRaw(DFloat(DSize(64))))
Beispiel #4
0
    def test_DArray(self):
        # primitive elements are properly handled
        # int are passed as DInt, not DUInt
        self.check([1, 2, 3], DArray(DInt(), DSize(3)))

        # for floats use a dot
        # might be a potential problem, due to python silent type downcasting
        self.check([1.0, 2.0, 3.0], DArray(DFloat(), DSize(3)))

        # bool are passed as DBool, not DInt
        self.check([True, False, False], DArray(DBool(), DSize(3)))

        # encapsulation of compound types
        self.check([[1, 2, 3], [4, 5, 6]],
                   DArray(DArray(DInt(), DSize(3)), DSize(2)))

        with self.assertRaises(DeltaTypeError):
            self.check([1, 2, 3, 4, 5, 6],
                       DArray(DArray(DInt(), DSize(3)), DSize(2)))

        with self.assertRaises(AssertionError):
            self.check([1, 2, 3, 4, 5, 6],
                       DArray(DInt(), DSize(6)),
                       DArray(DArray(DInt(), DSize(3)), DSize(2)))

        # mixed types
        self.check([(1, 2, 3), (4, 5, 6)],
                   DArray(DTuple([int, int, int]), DSize(2)))

        self.check(["hello", "world"], DArray(DStr(DSize(5)), DSize(2)))

        # numpy
        self.check_numpy([1, 2, 3, 4, 5], DArray(int, DSize(5)))
Beispiel #5
0
    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)")
Beispiel #6
0
    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]])
Beispiel #7
0
    def test_size(self):
        """Test how many bits each data type takes."""
        # primitive
        self.assertEqual(DInt().size, DSize(32))
        self.assertEqual(DUInt().size, DSize(32))
        self.assertEqual(DBool().size, DSize(1))
        self.assertEqual(DChar().size, DSize(8))
        self.assertEqual(DFloat().size, DSize(32))

        # compound
        self.assertEqual(DTuple([int, bool]).size, DSize(33))
        self.assertEqual(DArray(int, DSize(10)).size, DSize(320))
        self.assertEqual(DStr().size, DSize(8192))
        self.assertEqual(DRecord(RecBI).size, DSize(33))

        # compound: DUnion
        self.assertEqual(DUnion([bool]).size, DSize(9))
        self.assertEqual(DUnion([int, bool]).size, DSize(40))
        self.assertEqual(DUnion([int, DTuple([int, int])]).size, DSize(2*32+8))
Beispiel #8
0
    def test_DFloat(self):
        """Only 32 and 64 bits are supported."""
        precision_dict = {32: -23, 64: -52}

        for bits, precision in precision_dict.items():
            for _ in range(1000):
                self.check_float(random.uniform(-1, 1), DFloat(DSize(bits)))

            self.check(1 + 2**precision, DFloat(DSize(bits)))

            # Booleans and ints can be packed
            self.check(True, DFloat(DSize(bits)))
            self.check(False, DFloat(DSize(bits)))
            self.check(3, DFloat(DSize(bits)))

            # Strings and complex numbers are not packable
            self.assertFalse(DFloat(DSize(bits)).is_packable("abc"))
            self.assertFalse(DFloat(DSize(bits)).is_packable(3+5j))
Beispiel #9
0
 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)
Beispiel #10
0
 def test_DFloat_object(self):
     self.check_float(3.14, DFloat(DSize(32)))
     self.check_float(6.28, DFloat(DSize(64)))
Beispiel #11
0
 def test_DFloat_type(self):
     self.assertEqual(DFloat(DSize(32)).as_numpy_type(), np.float32)
     self.assertEqual(DFloat(DSize(64)).as_numpy_type(), np.float64)
     with self.assertRaises(NotImplementedError):
         DUInt(DSize(128)).as_numpy_type()