Beispiel #1
0
 def _get_list(self, i):
     from prolog.interpreter.term import Number
     if i == 0:
         return Number(self.vals_fixed_0)
     else:
         assert i == 1
         return Number(self.vals_fixed_1)
Beispiel #2
0
    def test_shr(self):
        assert Number(8).arith_shr(Number(2)).num == 2
        assert BigInt(rbigint.fromint(256)).arith_shr(Number(5)).num == 8
        assert BigInt(rbigint.fromint(256)).arith_shr(BigInt(rbigint.fromint(5))).num == 8
        assert Number(256).arith_shr(BigInt(rbigint.fromint(5))).num == 8

        py.test.raises(ValueError, 'BigInt(rbigint.fromint(2)).arith_shr(BigInt(rbigint.fromdecimalstr(\'100000000000000000000000000000000000000000000000\')))')
Beispiel #3
0
    def test_mod(self):
        assert Number(8).arith_mod(Number(2)).num == 0
        assert BigInt(rbigint.fromint(46546)).arith_mod(Number(33)).num == 16
        assert Number(46546).arith_mod(BigInt(rbigint.fromint(33))).num == 16

        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr("12342424234")).arith_mod(BigInt(rbigint.fromint(0)))')
        py.test.raises(error.CatchableError, 'Number(34535).arith_mod(BigInt(rbigint.fromint(0)))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr("12342424234")).arith_mod(Number(0))')
Beispiel #4
0
 def test_min(self):
     assert Number(5).arith_min(Number(1)).num == 1
     assert Float(-1.32).arith_min(Float(4.5)).floatval == -1.32
     assert BigInt(rbigint.fromdecimalstr('111111111111111111111111111')).arith_min(BigInt(rbigint.fromdecimalstr('222222222222222222222222222222'))).value.str() == '111111111111111111111111111'
     assert Number(-1000).arith_min(BigInt(rbigint.fromint(-1001))).num == -1001
     assert BigInt(rbigint.fromint(-1001)).arith_min(Number(-1000)).num == -1001
     assert BigInt(rbigint.fromdecimalstr('10000')).arith_min(Float(20000)).floatval == 10000.0
     assert Float(20000).arith_min(BigInt(rbigint.fromdecimalstr('10000'))).floatval == 10000.0
Beispiel #5
0
    def test_power(self):
        assert Number(5).arith_pow(Number(2)).num == 25
        assert Float(2.3).arith_pow(Float(3.1)).floatval == 13.223800591254721
        assert BigInt(rbigint.fromdecimalstr('1000000')).arith_pow(Number(4)).value.str() == '1000000000000000000000000'
        assert Float(10.0).arith_pow(BigInt(rbigint.fromdecimalstr('10'))).floatval == 10000000000.0
        assert BigInt(rbigint.fromdecimalstr('10')).arith_pow(Float(10.0)).floatval == 10000000000.0

        assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_pow(Number(0)).num == 1
        assert Float(10000000000).arith_pow(BigInt(rbigint.fromdecimalstr('0'))).floatval == 1.0
Beispiel #6
0
def test_dont_clone_ground_arg():
    m = Engine().modulewrapper
    n = Number(1)
    n.unify_and_standardize_apart = None
    head = Callable.build("f", [n])
    r = Rule(head, C(2), m.user_module)
    assert r.groundargs == [True]

    b = BindingVar()
    callhead = Callable.build("f", [b])
    h = Heap()
    # should not fail, because ground args don't need cloning
    r.clone_and_unify_head(h, callhead)
def test_copy_standardize_apart():
    heap = Heap()
    Z = NumberedVar(0)
    env = [None]
    t1, f1 = Z.copy_standardize_apart(heap, env)
    t2, f2 = Z.copy_standardize_apart(heap, env)
    assert not f1
    assert not f2
    assert isinstance(t1, Var)
    assert t1 is t2

    env = [Number(1)]
    t1, f1 = Z.copy_standardize_apart(heap, env)
    assert isinstance(t1, Number)
    t2, f2 = Z.copy_standardize_apart(heap, env)
    assert t1 is t2
    assert not f1
    assert not f2

    Z = NumberedVar(-1)
    t1, f1 = Z.copy_standardize_apart(heap, [None])
    t2, f2 = Z.copy_standardize_apart(heap, [None])
    assert isinstance(t1, Var)
    assert isinstance(t2, Var)
    assert t1 is not t2
    assert not f1
    assert not f2
def test_exception():
    e = get_engine("""
        f(0).
        f(X) :- X>0, X0 is X - 1, throw(continue(X0)).
        g(X) :- catch(f(X), continue(X0), g(X0)).""")
    query = Callable.build("g", [Number(100)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
def test_cut():
    e = get_engine("""
        f(0).
        f(X) :- X>0, X0 is X - 1, !, f(X0).
        f(_).""")
    query = Callable.build("f", [Number(100)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
def test_recurse_with_if():
    e = get_engine("""
    equal(0, 0). equal(X, X).
    f(X) :- equal(X, 0) -> true ; Y is X - 1, f(Y).
    """)
    query = Callable.build("f", [Number(100)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
Beispiel #11
0
def test_ground_args():
    e = Engine()
    m = e.modulewrapper
    r = Rule(C(1), C(2), m.user_module)
    assert r.groundargs is None

    head = Callable.build("f", [Number(1)])
    r = Rule(head, C(2), m.user_module)
    assert r.groundargs == [True]

    head = Callable.build("f", [
        Callable.build("g", [Number(1)]),
        BindingVar(),
        Callable.build("h", [Number(2), BindingVar()])
    ])
    r = Rule(head, C(2), m.user_module)
    assert r.groundargs == [True, False, False]
def test_recurse_with_many_base_cases():
    e = get_engine("""
    f(X) :- X = 0.
    f(X) :- X = 0.
    f(X) :- X = 0.
    f(X) :- X = 0.
    f(X) :- X = 0.
    f(X) :- Y is X - 1, f(Y).
    """)
    query = Callable.build("f", [Number(100)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
def test_partition():
    e = get_engine("""
    partition([],_,[],[]).
    partition([X|L],Y,[X|L1],L2) :-
        X =< Y, !,
        partition(L,Y,L1,L2).
    partition([X|L],Y,L1,[X|L2]) :-
        partition(L,Y,L1,L2).
    i(X) :- partition([6, 6, 6, 6, 6, 6, 6, 1, 5, 1, 5, 7, 9,2,4, 3, 7, 9, 0, 10], 5, [X | _], _).
    """)
    query = Callable.build("i", [Number(1)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
def test_map():
    e = get_engine("""
        add1(X, X1) :- X1 is X + 1.
        map(_, [], []).
        map(Pred, [H1 | T1], [H2 | T2]) :-
            C =.. [Pred, H1, H2],
            call(C),
            map(Pred, T1, T2).
        map(X) :- !.
        h(X) :- map(add1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 11, 12, 13], [X | _]).
    """)
    query = Callable.build("h", [Number(2)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
Beispiel #15
0
def test_quick_unify_check():
    a = Callable.build("hallo", [NumberedVar(0), Number(10), Number(11)])
    b = Callable.build(
        "hallo", [Callable.build("a"),
                  Number(10), BindingVar()])
    assert a.quick_unify_check(b)

    a = Callable.build(
        "hallo", [NumberedVar(0),
                  Number(10), Callable.build("b")])
    b = Callable.build("hallo", [Callable.build("a"), Number(10), Number(11)])
    assert not a.quick_unify_check(b)

    a = Callable.build("hallo", [Callable.build("a"), Number(10), Number(11)])
    b = Callable.build(
        "hallo", [BindingVar(), Number(10),
                  Callable.build("b")])
    assert a.quick_unify_check(a)
    assert b.quick_unify_check(b)
    assert not a.quick_unify_check(b)
Beispiel #16
0
 def test_mul(self):
     assert Number(5).arith_mul(Number(100)).num == 500
     assert Number(5).arith_mul(BigInt(rbigint.fromdecimalstr('1000000000000000000000000000000'))).value.tofloat() == 5000000000000000000000000000000.0
     assert Number(-10).arith_mul(Float(-7.3)).floatval == 73
     assert BigInt(rbigint.fromdecimalstr('-1000000000000000000000000000')).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).value.str() == '-100000000000000000000000000000000000000000000000'
     assert Float(6.7).arith_mul(Float(-2.4)).floatval == -16.08
     assert Float(6.7).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000'))).floatval == 670000000000000000000000000000000000.0
     assert BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000')).arith_mul(Float(6.7)).floatval == 670000000000000000000000000000000000.0
     assert Number(2).arith_mul(Float(2.5)).floatval == 5
     assert Float(2.5).arith_mul(Number(2)).floatval == 5
Beispiel #17
0
    def test_div(self):
        assert Number(5).arith_div(Number(2)).num == 2
        assert Number(15).arith_div(Number(5)).num == 3
        assert Number(5).arith_div(Float(2.5)).floatval == 2.0
        assert Float(2.5).arith_div(Number(5)).floatval == 0.5
        assert Float(-10).arith_div(Float(2.5)).floatval == -4.0
        assert BigInt(rbigint.fromdecimalstr('50000000000000000')).arith_div(BigInt(rbigint.fromdecimalstr('25000000000000000'))).num == 2
        assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_div(Float(100000000000000000000.0)).floatval == 1.0
        assert Float(100000000000000000000).arith_div(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).floatval == 1.0
        assert Number(5).arith_div(BigInt(rbigint.fromdecimalstr('5'))).num == 1
        assert BigInt(rbigint.fromdecimalstr('5')).arith_div(Number(5)).num == 1

        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(BigInt(rbigint.fromdecimalstr(\'0\')))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Float(0))')
        py.test.raises(error.CatchableError, 'Float(1).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'Number(1).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'Number(1).arith_div(Float(0))')
Beispiel #18
0
    def test_sub(self):
        n1 = Number(5)
        n2 = Number(10)
        assert n1.arith_sub(n2).num == -5
        assert n2.arith_sub(n1).num == 5

        f1 = Float(10.5)
        f2 = Float(30.6)
        assert f1.arith_sub(f2).floatval == -20.1
        assert f2.arith_sub(f1).floatval == 20.1

        b1 = BigInt(rbigint.fromdecimalstr('10000000000000000000000000000000000000'))
        b2 = BigInt(rbigint.fromdecimalstr('20000000000000000000000000000000000000'))
        assert b1.arith_sub(b2).value.tofloat() == -10000000000000000000000000000000000000.0
        assert b2.arith_sub(b1).value.tofloat() == 10000000000000000000000000000000000000.0
        assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_sub(Number(1)).value.str() == '99999999999999999999'
        assert Number(5).arith_sub(BigInt(rbigint.fromdecimalstr('5'))).num == 0
        assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_sub(Float(500000000000000)).floatval == 500000000000000.0
        assert Float(2000000000000000).arith_sub(BigInt(rbigint.fromdecimalstr('1000000000000000'))).floatval == 1000000000000000.0
Beispiel #19
0
def test_copy_standardize_apart_term():
    heap = Heap()
    Z = NumberedVar(0)
    t = Callable.build("f", [Z, Z])
    t2 = t.copy_standardize_apart(heap, [None])
    assert isinstance(t2.argument_at(0), Var)
    assert t2.argument_at(0) is t2.argument_at(1)

    t2 = t.copy_standardize_apart(heap, [Number(1)])
    assert isinstance(t2.argument_at(0), Number)
    assert t2.argument_at(0) is t2.argument_at(1)

    Z = NumberedVar(-1)
    t = Callable.build("f", [Z, Z])
    t2 = t.copy_standardize_apart(heap, [None])
    assert isinstance(t2.argument_at(0), Var)
    assert isinstance(t2.argument_at(1), Var)
    assert t2.argument_at(0) is not t2.argument_at(1)
Beispiel #20
0
    def test_floordiv(self):
        assert Number(5).arith_floordiv(Number(2)).num == 2
        assert Number(15).arith_floordiv(Number(5)).num == 3
        py.test.raises(error.CatchableError, "Number(5).arith_floordiv(Float(2.5))")
        py.test.raises(error.CatchableError, "Float(2.5).arith_floordiv(Number(5))")
        py.test.raises(error.CatchableError, "Float(-10).arith_floordiv(Float(2.5))")
        assert BigInt(rbigint.fromdecimalstr('50000000000000000')).arith_floordiv(BigInt(rbigint.fromdecimalstr('25000000000000000'))).num == 2
        py.test.raises(error.CatchableError, "BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_floordiv(Float(100000000000000000000.0))")
        py.test.raises(error.CatchableError, "Float(100000000000000000000).arith_floordiv(BigInt(rbigint.fromdecimalstr('100000000000000000000')))")
        assert Number(5).arith_floordiv(BigInt(rbigint.fromdecimalstr('5'))).num == 1
        assert BigInt(rbigint.fromdecimalstr('5')).arith_floordiv(Number(5)).num == 1

        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_floordiv(BigInt(rbigint.fromdecimalstr(\'0\')))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_floordiv(Number(0))')
        py.test.raises(error.CatchableError, 'Number(1).arith_floordiv(Number(0))')
def test_tak():
    e = get_engine("""
    tak(X,Y,Z,A) :-
            write(tak(X, Y, Z, A)), nl,
            X =< Y, !,
            write('succeeded'), nl,
            Z = A.
    tak(X,Y,Z,A) :-
            % X > Y,
            X1 is X - 1,
            tak(X1,Y,Z,A1),
            Y1 is Y - 1,
            tak(Y1,Z,X,A2),
            Z1 is Z - 1,
            tak(Z1,X,Y,A3),
            tak(A1,A2,A3,A).

    j(1) :- tak(18, 5, 5, _).
    """)
    query = Callable.build("j", [Number(1)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))
Beispiel #22
0
 def test_invert(self):
     assert Number(2345).arith_not().num == -2346
     assert BigInt(rbigint.fromdecimalstr('37846578346543875674385')).arith_not().value.str() == '-37846578346543875674386'
Beispiel #23
0
 def test_xor(self):
     assert Number(8).arith_xor(Number(2)).num == 10
     assert BigInt(rbigint.fromint(46546)).arith_xor(Number(34)).num == 46576
     assert Number(46546).arith_xor(BigInt(rbigint.fromint(34))).num == 46576
Beispiel #24
0
 def test_or(self):
     assert Number(8).arith_or(Number(2)).num == 10
     assert BigInt(rbigint.fromint(256)).arith_or(Number(128)).num == 384
     assert BigInt(rbigint.fromdecimalstr('18446744073709551616')).arith_or(BigInt(rbigint.fromdecimalstr('9223372036854775808'))).value.str() == '27670116110564327424'
     assert Number(128).arith_or(BigInt(rbigint.fromint(256))).num == 384
Beispiel #25
0
 def test_shl(self):
     assert Number(2).arith_shl(Number(5)).num == 64
     assert BigInt(rbigint.fromint(1000)).arith_shl(Number(1)).num == 2000
     assert BigInt(rbigint.fromint(1000)).arith_shl(BigInt(rbigint.fromint(1))).num == 2000
     assert Number(1000).arith_shl(BigInt(rbigint.fromint(1))).num == 2000
     assert Number(1000).arith_shl(BigInt(rbigint.fromint(100))).value.str() == '1267650600228229401496703205376000'
Beispiel #26
0
 def _get_list(self, i):
     from prolog.interpreter.term import Number
     assert i == 0
     return Number(self.vals_fixed_0)
Beispiel #27
0
 def test_abs(self):
     assert Number(-345345345).arith_abs().num == 345345345
     assert Float(345345.435).arith_abs().floatval == 345345.435
     assert Float(-345345.435).arith_abs().floatval == 345345.435
     assert BigInt(rbigint.fromdecimalstr('-123123123123123123123123123')).arith_abs().value.str() == '123123123123123123123123123'
Beispiel #28
0
    def test_add(self):
        f1 = Float(5.1)
        f2 = Float(10.1)
        assert f1.arith_add(f2).floatval == 15.2

        n0 = Number(1)
        n1 = Number(2)
        assert n0.arith_add(n1).num == 3

        n2 = Number(2)
        f3 = Float(3.2)
        assert n2.arith_add(f3).floatval == 5.2
        assert f3.arith_add(n2).floatval == 5.2

        b1 = BigInt(rbigint.fromdecimalstr('50000000000000000000000'))
        b2 = BigInt(rbigint.fromdecimalstr('10000000000000000000001'))
        assert b1.arith_add(b2).value.str() == '60000000000000000000001'

        n3 = Number(sys.maxint)
        assert n3.arith_add(n0).value.str() == str(sys.maxint + 1)

        b = BigInt(rbigint.fromdecimalstr('100000000000000000000000000000'))
        f = Float(1.5)
        assert b.arith_add(f).floatval == 100000000000000000000000000001.5
        assert f.arith_add(b).floatval == 100000000000000000000000000001.5

        assert b.arith_add(n0).value.tofloat() == 100000000000000000000000000001.0
        assert n0.arith_add(b).value.tofloat() == 100000000000000000000000000001.0
Beispiel #29
0
 def test_and(self):
     assert Number(8).arith_and(Number(2)).num == 0
     assert BigInt(rbigint.fromint(46546)).arith_and(Number(34)).num == 2
     assert Number(46546).arith_and(BigInt(rbigint.fromint(34))).num == 2
Beispiel #30
0
def test_simple_hooks():
    hp = Heap()
    v = BindingVar()
    a = AttVar()
    v.unify(a, hp)
    assert hp.hook is None
    v.unify(Number(1), hp)
    assert hp.hook.attvar == a

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    assert hp.hook is None
    v2.unify(a2, hp)
    assert hp.hook is None
    v1.unify(v2, hp)
    assert hp.hook.attvar == a1

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    v3 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    a3 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    v3.unify(a3, hp)

    v1.unify(v2, hp)
    v2.unify(v3, hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    assert hp.hook is None
    v1.unify(v2, hp)
    assert hp.hook.attvar == a1
    v1.unify(Number(1), hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v1 = BindingVar()
    v2 = BindingVar()
    a1 = AttVar()
    a2 = AttVar()
    v1.unify(a1, hp)
    v2.unify(a2, hp)
    t1 = Callable.build("f", [v1, v2])
    t2 = Callable.build("f", [Atom("a"), Atom("b")])
    t1.unify(t2, hp)
    assert hp.hook.attvar == a2
    assert hp.hook.next.attvar == a1
    assert hp.hook.next.next is None

    hp = Heap()
    v = BindingVar()
    av = AttVar()
    v.unify(av, hp)
    assert hp.hook is None
    a = Callable.build("a")
    v.unify(a, hp)
    assert hp.hook.attvar == av
    v.unify(a, hp)
    assert hp.hook.attvar == av
    assert hp.hook.next is None