예제 #1
0
def test_find_coherent_result_sync():
    def fn(x):
        if x == 0:
            raise ValueError('Oh no! Zero!')
        else:
            return x > 0

    loop = asyncio.new_event_loop()
    p1 = PendingFromList([1, 2, 3], None, None, loop=loop)
    p2 = PendingFromList([-1, -2, -3], None, None, loop=loop)
    p3 = PendingFromList([1, 2, -3], None, None, loop=loop)
    p4 = PendingFromList([0], None, None, loop=loop)
    assert find_coherent_result_sync(p1, fn) is True
    assert find_coherent_result_sync(p2, fn) is False
    with pytest.raises(InferenceError):
        find_coherent_result_sync(p3, fn)
    with pytest.raises(ValueError):
        find_coherent_result_sync(p4, fn)

    p = Pending(None, None, loop=loop)
    with pytest.raises(InferenceError):
        find_coherent_result_sync(p, fn)

    assert find_coherent_result_sync(10, fn) is True
    assert find_coherent_result_sync(-10, fn) is False
예제 #2
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, loop=None, forced=False) is a
    assert amerge(a, c, loop=None, forced=False) == c
    assert amerge(c, a, loop=None, forced=False) is c

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

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

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

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

    loop = asyncio.new_event_loop()
    p = PendingFromList([ty.Int[64], ty.Float[64]], None, None, loop=loop)
    assert amerge(ty.Number, p, loop=None, forced=False, bind_pending=False) \
        is ty.Number
    assert amerge(p, ty.Number, loop=None, forced=False, bind_pending=False) \
        is ty.Number
    with pytest.raises(MyiaTypeError):
        print(amerge(p, ty.Number, loop=None, forced=True, bind_pending=False))
예제 #3
0
파일: test_abstract.py 프로젝트: TQCAI/myia
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
예제 #4
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)

    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))

    assert amerge(AbstractError(DEAD),
                  AbstractError(ANYTHING),
                  forced=False) 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))