Exemplo n.º 1
0
    def test_types_comparison(self):
        """Various tests of types comparison."""
        # primitive
        self.assertEqual(DInt(DSize(32)), DInt())
        self.assertNotEqual(DInt(), DUInt())
        self.assertNotEqual(DInt(), DInt(DSize(64)))

        # compound
        self.assertEqual(DTuple([int, bool]), DTuple([int, bool]))
        self.assertNotEqual(DTuple([int, bool]), DTuple([bool, int]))
        self.assertEqual(DArray(int, DSize(4)), DArray(int, DSize(4)))
        self.assertEqual(DArray(int, DSize(4)), DArray(DInt(), DSize(4)))
        self.assertNotEqual(DArray(int, DSize(4)), DArray(int, DSize(5)))
        self.assertNotEqual(DStr(), DStr(DSize(100)))
        self.assertEqual(DRecord(RecBI), DRecord(RecBI))

        # compound: DUnion
        self.assertEqual(DUnion([int, bool]), DUnion([bool, int]))
        self.assertEqual(DUnion([int, DUnion([int, bool])]),
                         DUnion([int, bool]))
        self.assertEqual(DUnion([int, DUnion([int, DUnion([int, bool])])]),
                         DUnion([int, bool]))
        self.assertEqual(DUnion([int, int]), DUnion([int]))
        self.assertNotEqual(DUnion([DInt()]), DInt())

        # special
        self.assertEqual(ForkedReturn(dict(x=int, y=bool, z=str)),
                         ForkedReturn(dict(x=int, y=bool, z=str)))
Exemplo n.º 2
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]))
Exemplo n.º 3
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)))
Exemplo n.º 4
0
    def test_DUnion(self):
        # primitive
        self.check(5, DUnion([int, bool]))
        self.check(True, DUnion([int, bool]))

        # compound
        self.check(5, DUnion([int, DTuple([int, float])]))
        self.check((4, 5), DUnion([int, DTuple([int, int])]))
        self.check((4, 5),
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))
        self.check([4, 5],
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))
        self.assertTrue(DeltaGraph.check_wire(DRaw(DUnion([DStr(), int])),
                                              DRaw(DUnion([DStr(), int]))))
Exemplo n.º 5
0
    def test_DTuple(self):
        # primitive elements are poperly handled
        self.check((-5, True, 3.25), DTuple([int, bool, float]))

        # incapsulation
        self.check((-5, (1, 2)), DTuple([int, DTuple([int, int])]))

        # mixed types
        self.check(([1, 2, 3], [4.0, 5.0]),
                   DTuple([DArray(int, DSize(3)), DArray(float, DSize(2))]))

        self.check(("hello", "world"), DTuple([DStr(), DStr(DSize(6))]))
        self.assertTrue(DeltaGraph.check_wire(DRaw(DTuple([DStr(), int])),
                                              DRaw(DTuple([DStr(), int]))))
Exemplo n.º 6
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)))
Exemplo n.º 7
0
    def test_DStr(self):
        self.check('hello world', DStr())
        self.check('A' * 1024, DStr())
        self.check('check digits 14213', DStr())
        self.check('check spaces in the end ', DStr())

        self.check((-5, 'text'), DTuple([int, DStr()]))
        self.check(['hello', 'world!'], DArray(DStr(), DSize(2)))
        self.assertTrue(DeltaGraph.check_wire(DRaw(DStr()), DRaw(DStr())))
Exemplo n.º 8
0
    def test_compound_objects(self):
        t = DArray(DTuple([bool, int]), DSize(3))
        val = [(True, 1), (False, 2), (True, 3), (False, 4), (True, 5)]
        self.check(val, t)

        t = DTuple([int, DTuple([bool, int])])
        val = (12, (True, 8))
        self.check(val, t)

        t = DTuple([int, DArray(int, DSize(2))])
        val = (12, [14, 18])
        self.check(val, t)

        t = DTuple([int, DStr()])
        val = (12, "hello")
        self.check(val, t)

        t = DTuple([int, DRecord(RecBI)])
        val = (12, RecBI(True, 8))
        self.check(val, t)

        t = DTuple([int, DUnion([bool, int])])
        val = (12, True)
        np_val = t.as_numpy_object(val)
        self.assertEqual(DInt().from_numpy_object(np_val[0][0]), 12)
        self.assertEqual(DBool().from_numpy_object(np_val[0][1][1]), True)

        t = DRecord(RecATI)
        val = RecATI([1, 2], (3.0, 4), 5)
        self.check(val, t)

        t = DUnion([DArray(int, DSize(2)), int])
        val = [1, 2]
        np_val = t.as_numpy_object(val)
        new_val = DArray(int, DSize(2)).from_numpy_object(np_val[0][1])
        self.assertEqual(val, new_val)

        t = DUnion([str, int])
        val = "abcde"
        np_val = t.as_numpy_object(val)
        new_val = DStr().from_numpy_object(np_val[0][1])
        self.assertEqual(val, new_val)
Exemplo n.º 9
0
    def test_DStr(self):
        self.check('hello world', DStr())
        self.check('A' * 1024, DStr())
        self.check('check digits 14213', DStr())
        self.check('check spaces in the end ', DStr())

        with self.assertRaises(DeltaTypeError):
            self.check('123456', DStr(DSize(4)))

        self.check((-5, 'text'), DTuple([int, DStr()]))
        self.check(['hello', 'world!'], DArray(DStr(), DSize(2)))

        self.check_numpy('hello world', DStr())
Exemplo n.º 10
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)")
Exemplo n.º 11
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]])
Exemplo n.º 12
0
    def test_DTuple(self):
        # primitive elements are properly handled
        self.check((-5, True, 3.25), DTuple([int, bool, float]))

        with self.assertRaises(DeltaTypeError):
            self.check((-5, True, 3.25), DTuple([int, bool, int]))

        # incapsulation
        self.check((-5, (1, 2)), DTuple([int, DTuple([int, int])]))

        with self.assertRaises(AssertionError):
            self.check((-5, (1, 2)),
                       DTuple([int, DTuple([int, int])]),
                       DTuple([int, int, int]))

        # mixed types
        self.check(([1, 2, 3], [4.0, 5.0]),
                   DTuple([DArray(int, DSize(3)), DArray(float, DSize(2))]))

        self.check(("hello", "world"), DTuple([DStr(), DStr(DSize(6))]))

        # numpy
        self.check_numpy((1, 2.0, True), DTuple([int, float, bool]))
Exemplo n.º 13
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))
Exemplo n.º 14
0
    def test_Top(self):
        """Everything can be accepted as Top()."""
        self.assertTrue(DeltaGraph.check_wire(DInt(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DUInt(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DBool(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DTuple([int, bool]), Top()))
        self.assertTrue(DeltaGraph.check_wire(DUnion([int, bool]), Top()))
        self.assertTrue(DeltaGraph.check_wire(DArray(int, DSize(8)), Top()))
        self.assertTrue(DeltaGraph.check_wire(DStr(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DRecord(RecBI), Top()))
        self.assertTrue(DeltaGraph.check_wire(Top(), Top()))

        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(Top(), DInt())

        # however it's not true if Top is used within a non-primitive type
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DTuple([int, int]), DTuple([int, Top()]))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DArray(int, DSize(8)),
                                  DArray(Top(), DSize(8)))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DRecord(RecBI), DRecord(RecBT))
Exemplo n.º 15
0
 def test_DStr_object(self):
     self.check("abcde", DStr(DSize(32)))
Exemplo n.º 16
0
 def test_DStr_type(self):
     str_type = DStr(DSize(32)).as_numpy_type()
     self.assertEqual(str_type, (np.string_, 32))