Beispiel #1
0
def test_choice():
    p = choice(partial(int, 15.5), (15, 4), (3, 2))
    assert evaluate(p) == 4
    p = choice(variable('x', value_type=['a', 'b', 'c']), ('a', 'b'),
               ('b', 'c'))
    assert evaluate(p, x='a') == 'b'
    assert evaluate(p, x='b') == 'c'
Beispiel #2
0
def test_pyll_deeply_nested_func():
    # N.B. uses stuff that isn't in the SymbolTable yet, must remove.
    try:
        def my_add(x, y):
            return x + y

        x = as_partialplus(
            (partial(float, partial(my_add, 0, partial(int, 3.3))) / 2,
             partial(float, 3))
        )
        y = as_pyll(x)
        evaluate(x) == rec_eval(y)
    finally:
        scope.undefine(my_add)
Beispiel #3
0
def test_lazy_index_range():
    """
    Test that lazy evaluation of indexing works with range lookups.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    plist = as_pp([-1, 0, 1, partial(dont_eval)])
    assert [-1, 0, 1] == evaluate(plist[:3])

    plist = as_pp((-1, 0, 1, partial(dont_eval)))
    assert (-1, 0, 1) == evaluate(plist[:3])
Beispiel #4
0
def test_pyll_list_tuple_nested():
    x = as_partialplus([[5, 3, (5, 3)], (4, 5)])
    y = as_pyll(x)
    # rec_eval always uses tuple
    val_y = rec_eval(y)
    # Correct for tuple-only in rec_eval.
    assert evaluate(x) == [list(val_y[0]), val_y[1]]
Beispiel #5
0
def test_dict():
    """Test that dicts are correctly traversed/converted."""
    def mod(x, y):
        return x % y

    x = as_pp({5: partial(mod, 5, 3), 3: (7, 9), 4: [partial(mod, 9, 4)]})
    y = evaluate(x)
    assert y == {5: 2, 3: (7, 9), 4: [1]}
Beispiel #6
0
def test_dict():
        x = as_partialplus({'x': partial(float,
                                         partial(float,
                                                 partial(int, 3.3))) / 2,
                            'y': partial(float, 3)
                            })
        y = as_pyll(x)
        assert evaluate(x) == rec_eval(y)
Beispiel #7
0
def test_switch_ordered_dict():
    """Test that lazy evaluation works with OrderedDicts."""
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    r = evaluate(as_pp(OrderedDict({'a': partial(dont_eval), 'b': 3}))['b'])
    assert r == 3
Beispiel #8
0
def test_list():
    """Test that lists are correctly traversed/converted."""
    def sub(x, y):
        return x - y

    x = as_pp([[3, partial(sub, 2, 3)], partial(sub, 5, 7), partial(float, 9)])
    y = evaluate(x)
    assert y == [[3, -1], -2, 9.0]
    assert isinstance(y[2], float)
Beispiel #9
0
def test_tuple():
    """Test that tuples are correctly traversed/converted."""
    def add(x, y):
        return x + y

    x = as_pp(((3, partial(add, 2, 3)), partial(add, 5, 7), partial(float, 9)))
    y = evaluate(x)
    assert y == ((3, 5), 12, 9.0)
    assert isinstance(y[2], float)
Beispiel #10
0
def test_variable_substitution():
    """Test that variable substitution works correctly."""
    x = variable(name='x', value_type=int)
    y = variable(name='y', value_type=float)
    p = as_pp({3: x, x: [y, [y]], y: 4})
    # Currently no type-checking. This will fail when we add it and need to be
    # updated.
    e = evaluate(p, x='hey', y=5)
    assert e[3] == 'hey'
    assert e['hey'] == [5, [5]]
    assert e[5] == 4
Beispiel #11
0
def test_lazy_index_dict():
    """
    Test that lazy evaluation of indexing works with dict lookups.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    r = evaluate(as_pp({'a': partial(dont_eval), 'b': 3})['b'])
    assert r == 3
Beispiel #12
0
def test_two_objects():
    """
    Test that identical expression in different parts of graph evaluates
    to an identical object.
    """
    class Foo(object):
        pass

    p = partial(Foo)
    q = as_pp([p, [0, 1, p], [(p, )]])
    r = evaluate(q)
    assert r[0] is r[1][-1]
    assert r[0] is r[2][0][0]
Beispiel #13
0
def test_lazy_index_tuple():
    """
    Test that lazy evaluation of indexing works with element lookups on
    lists.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    # TODO: James: I opted for this behaviour rather than list(f, el1, el2...)
    # is there a compelling reason to do that? It kind of breaks with the
    # model.
    plist = as_pp((-1, partial(dont_eval)))
    assert -1 == evaluate(plist[0])
Beispiel #14
0
 def check(a, b):
     assert evaluate(as_pp(partial(int, a)) +
                     as_pp(partial(int, b))) == a + b
     assert evaluate(as_pp(partial(int, a)) -
                     as_pp(partial(int, b))) == a - b
     assert evaluate(as_pp(partial(int, a)) *
                     as_pp(partial(int, b))) == a * b
     assert evaluate(as_pp(partial(int, a)) /
                     as_pp(partial(int, b))) == a / b
     assert evaluate(as_pp(partial(int, a)) %
                     as_pp(partial(int, b))) == a % b
     assert evaluate(as_pp(partial(int, a))
                     | as_pp(partial(int, b))) == a | b
     assert evaluate(as_pp(partial(int, a))
                     ^ as_pp(partial(int, b))) == a ^ b
     assert evaluate(as_pp(partial(int, a))
                     & as_pp(partial(int, b))) == a & b
Beispiel #15
0
def test_pyll_tuple():
    x = as_partialplus((6, 9, 4))
    y = as_pyll(x)
    assert evaluate(x) == rec_eval(y)
Beispiel #16
0
def test_pyll_func():
    # N.B. Only uses stuff that's already in the SymbolTable.
    x = partial(float, 5)
    y = as_pyll(x)
    assert evaluate(x) == rec_eval(y)
Beispiel #17
0
def test_pyll_nested_func():
    x = partial(float, partial(int, 5.5))
    y = as_pyll(x)
    assert evaluate(x) == rec_eval(y)
Beispiel #18
0
def test_pyll_list():
    x = as_partialplus([5, 3, 9])
    y = as_pyll(x)
    # rec_eval always uses tuple
    assert evaluate(x) == list(rec_eval(y))
Beispiel #19
0
def test_getitem_dict():
    """Test that indexing works on a PartialPlus'd dict."""
    v = evaluate(as_pp({'a': partial(float, '3'), 'b': 3})['a'])
    assert v == 3.0