Exemplo n.º 1
0
def test_type_to_abstract():
    assert type_to_abstract(int) is S(t=ty.Int[64])
    assert type_to_abstract(float) is S(t=ty.Float[64])
    assert type_to_abstract(bool) is S(t=ty.Bool)
    assert type_to_abstract(typing.List) is U(type_to_abstract(Empty),
                                              type_to_abstract(Cons))
    assert type_to_abstract(typing.Tuple) is T(ANYTHING)
Exemplo n.º 2
0
def test_broaden_recursive():
    s1 = S(1)
    t1 = T.empty()
    t1.__init__([s1, t1])
    t1 = t1.intern()

    sa = S(t=ty.Int[64])
    ta = T.empty()
    ta.__init__([sa, ta])
    ta = ta.intern()

    assert broaden(t1) is ta
    assert broaden(ta) is ta

    t2 = T.empty()
    u2 = AbstractUnion.empty()
    u2.__init__([s1, t2])
    t2.__init__([s1, u2])
    t2 = t2.intern()

    tb = T.empty()
    ub = AbstractUnion.empty()
    ub.__init__([sa, tb])
    tb.__init__([sa, ub])
    tb = tb.intern()

    assert broaden(t2) is tb
    assert broaden(tb) is tb
Exemplo n.º 3
0
def test_abstract_clone_pending():
    loop = asyncio.new_event_loop()
    s1 = S(t=ty.Int[32])
    p = Pending(loop=loop, resolve=None, priority=None)
    sp = S(t=p)
    assert abstract_clone(sp) is sp
    p.set_result(ty.Int[32])
    assert abstract_clone(sp) is s1
Exemplo n.º 4
0
def test_merge_from_types():
    a = T([S(1), S(t=ty.Int[64])])

    t1 = type_to_abstract(typing.Tuple)
    t2 = type_to_abstract(typing.Tuple[ty.Int[64], ty.Int[64]])
    t3 = type_to_abstract(typing.Tuple[ty.Int[64]])
    assert amerge(t1, a, forced=True) is t1
    assert amerge(t2, a, forced=True) is t2
    with pytest.raises(MyiaTypeError):
        amerge(t3, a, forced=True)
Exemplo n.º 5
0
def test_abstract_clone():
    s1 = S(t=ty.Int[32])
    s2 = S(t=ty.Int[64])
    assert upcast(s1, nbits=64) is s2

    a1 = T([s1, AbstractClass(object, {"field": s1})])
    a2 = T([s2, AbstractClass(object, {"field": s2})])
    assert upcast(a1, nbits=64) is a2

    jt = TransformedFunction(AbstractFunctionUnique((s1, ), s1), P.J)
    assert upcast(jt, nbits=64).fn.args == [s2]
    assert upcast(jt, nbits=64).fn.output is s2
Exemplo n.º 6
0
def test_amerge_pending():
    loop = asyncio.new_event_loop()

    p = PendingFromList([ty.Int[64], ty.Float[64]], None, None, loop=loop)
    assert amerge(ty.Number, p, forced=False, bind_pending=False) is ty.Number
    assert amerge(p, ty.Number, forced=False, bind_pending=False) is ty.Number
    with pytest.raises(MyiaTypeError):
        print(amerge(p, ty.Number, forced=True, bind_pending=False))

    s1 = S(t=ty.Int[32])
    p = Pending(loop=loop, resolve=None, priority=None)
    sp = S(t=p)
    assert amerge(sp, s1, forced=True) is sp
    p.set_result(ty.Int[32])
    assert amerge(sp, s1) is s1
    assert amerge(s1, sp) is s1
Exemplo n.º 7
0
def test_repr_recursive():
    sa = S(t=ty.Int[64])
    ta = T.empty()
    la = T.empty()
    la.__init__([ta])
    ta.__init__([sa, la])
    ta = ta.intern()
    repr(ta)
Exemplo n.º 8
0
def test_build_value():
    assert build_value(S(1)) == 1
    with pytest.raises(ValueError):
        build_value(S(t=ty.Int[64]))
    assert build_value(S(t=ty.Int[64]), default=ANYTHING) is ANYTHING

    assert build_value(T([S(1), S(2)])) == (1, 2)

    loop = InferenceLoop(errtype=Exception)
    p = loop.create_pending(resolve=(lambda: None), priority=(lambda: None))
    with pytest.raises(ValueError):
        assert build_value(S(p, t=ty.Int[64])) is p
    assert build_value(S(p, t=ty.Int[64]), default=ANYTHING) is ANYTHING
    p.set_result(1234)
    assert build_value(S(p, t=ty.Int[64])) == 1234

    pt = Point(1, 2)
    assert build_value(to_abstract_test(pt)) == pt
Exemplo n.º 9
0
def test_amerge():
    a = T([S(1), S(t=ty.Int[64])])
    b = T([S(1), S(t=ty.Int[64])])
    c = T([S(t=ty.Int[64]), S(t=ty.Int[64])])

    assert amerge(a, b, forced=False) is a
    assert amerge(a, c, forced=False) == c
    assert amerge(c, a, forced=False) is c

    with pytest.raises(MyiaTypeError):
        amerge(a, c, forced=True)

    assert amerge(1, 2, forced=False) is ANYTHING
    with pytest.raises(MyiaTypeError):
        assert amerge(1, 2, forced=True)

    with pytest.raises(MyiaTypeError):
        assert amerge("hello", "world", forced=False)

    assert amerge(ty.Int, ty.Int[64], forced=False) is ty.Int
    assert amerge(ty.Int[64], ty.Int, forced=False) is ty.Int
    with pytest.raises(MyiaTypeError):
        amerge(ty.Float, ty.Int, forced=False)
    with pytest.raises(MyiaTypeError):
        amerge(ty.Int[64], ty.Int, forced=True)

    assert amerge(AbstractError(DEAD), AbstractError(ANYTHING),
                  forced=False) is AbstractError(ANYTHING)

    assert amerge(AbstractError(ANYTHING), AbstractError(DEAD),
                  forced=True) is AbstractError(ANYTHING)

    d1 = {"x": 1}
    d2 = {"y": 2}
    with pytest.raises(MyiaTypeError):
        print(amerge(d1, d2))

    td1 = TrackDict({ALIASID: 1})
    td2 = TrackDict({})
    with pytest.raises(MyiaTypeError):
        print(amerge(td1, td2, forced=True))
Exemplo n.º 10
0
def test_to_abstract_xtype():
    assert to_abstract(ty.Int[64]) is AbstractType(S(t=ty.Int[64]))
Exemplo n.º 11
0
def test_to_abstract_list():
    assert to_abstract([]) is empty
    assert to_abstract([1, 2, 3]) is listof(S(t=ty.Int[64]))