# Path: prolog/interpreter/parsing.py
# def parse_query_term(s):
#     return get_query_and_vars(s)[0]
# 
# def get_engine(source, create_files=False, load_system=False, **modules):
#     from prolog.interpreter.continuation import Engine
#     from prolog.interpreter.test.tool import create_file, delete_file
#     e = Engine(load_system)
#     for name, module in modules.iteritems():
#         if create_files:
#             create_file(name, module)
#         else:
#             e.runstring(module)
#     try:
#         e.modulewrapper.current_module = e.modulewrapper.user_module
#         e.runstring(source)
#     finally:
#         if create_files:
#             for name in modules.keys():
#                 delete_file(name)
#     return e
# 
# Path: prolog/interpreter/parsing.py
# def get_query_and_vars(s):
#     tokens = lexer.tokenize(s, eof=True)
#     s = parser_query.parse(tokens, lazy=False)
#     builder = TermBuilder()
#     query = builder.build_query(s)
#     return query, builder.varname_to_var
# 
# Path: prolog/interpreter/error.py
# class UnificationFailed(PrologError):
#     pass
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)
# 
# Path: prolog/interpreter/term.py
# class Number(Numeric):#, UnboxedValue):
#     TYPE_STANDARD_ORDER = 1
#     __slots__ = ("num", )
#     _immutable_fields_ = ["num"]
# 
#     def __init__(self, val):
#         self.num = val
# 
#     @specialize.arg(3)
#     def basic_unify(self, other, heap, occurs_check):
#         if isinstance(other, Number) and other.num == self.num:
#             return
#         raise UnificationFailed
# 
#     def __str__(self):
#         return repr(self.num)
#     
#     def __repr__(self):
#         return "Number(%r)" % (self.num, )
#     
#     def cmp_standard_order(self, other, heap):
#         # XXX looks a bit terrible
#         if isinstance(other, Number):
#             return rcmp(self.num, other.num)
#         elif isinstance(other, Float):
#             # int/float mixed are always compared as floats
#             return rcmp(float(self.num), other.floatval)
#         elif isinstance(other, BigInt):
#             return bigint_cmp(rbigint.fromint(self.num), other.value)
#         assert 0
# 
#     def quick_unify_check(self, other):
#         other = other.dereference(None)
#         if isinstance(other, Var):
#             return True
#         return isinstance(other, Number) and other.num == self.num


import py
from prolog.interpreter.continuation import *
from prolog.interpreter.parsing import parse_query_term, get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UnificationFailed
from prolog.interpreter.test.tool import collect_all, assert_true, assert_false
from prolog.interpreter.term import Number
            fcont = fcont.nextcont
        assert depth < self.seen
        depth = 0
        numvars = 0
        while heap:
            depth += 1
            numvars += heap.i
            heap = heap.prev
        assert depth < self.seen
        assert numvars < self.seen
        return DoneSuccessContinuation(self.engine), DoneFailureContinuation(self.engine), original_heap

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

@py.test.mark.xfail # bug introduced in aa127d31df0c
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_call():

# Path: prolog/interpreter/parsing.py
# def parse_query_term(s):
#     return get_query_and_vars(s)[0]
# 
# def get_engine(source, create_files=False, load_system=False, **modules):
#     from prolog.interpreter.continuation import Engine
#     from prolog.interpreter.test.tool import create_file, delete_file
#     e = Engine(load_system)
#     for name, module in modules.iteritems():
#         if create_files:
#             create_file(name, module)
#         else:
#             e.runstring(module)
#     try:
#         e.modulewrapper.current_module = e.modulewrapper.user_module
#         e.runstring(source)
#     finally:
#         if create_files:
#             for name in modules.keys():
#                 delete_file(name)
#     return e
# 
# Path: prolog/interpreter/parsing.py
# def get_query_and_vars(s):
#     tokens = lexer.tokenize(s, eof=True)
#     s = parser_query.parse(tokens, lazy=False)
#     builder = TermBuilder()
#     query = builder.build_query(s)
#     return query, builder.varname_to_var
# 
# Path: prolog/interpreter/error.py
# class UnificationFailed(PrologError):
#     pass
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)
# 
# Path: prolog/interpreter/term.py
# class Number(Numeric):#, UnboxedValue):
#     TYPE_STANDARD_ORDER = 1
#     __slots__ = ("num", )
#     _immutable_fields_ = ["num"]
# 
#     def __init__(self, val):
#         self.num = val
# 
#     @specialize.arg(3)
#     def basic_unify(self, other, heap, occurs_check):
#         if isinstance(other, Number) and other.num == self.num:
#             return
#         raise UnificationFailed
# 
#     def __str__(self):
#         return repr(self.num)
#     
#     def __repr__(self):
#         return "Number(%r)" % (self.num, )
#     
#     def cmp_standard_order(self, other, heap):
#         # XXX looks a bit terrible
#         if isinstance(other, Number):
#             return rcmp(self.num, other.num)
#         elif isinstance(other, Float):
#             # int/float mixed are always compared as floats
#             return rcmp(float(self.num), other.floatval)
#         elif isinstance(other, BigInt):
#             return bigint_cmp(rbigint.fromint(self.num), other.value)
#         assert 0
# 
#     def quick_unify_check(self, other):
#         other = other.dereference(None)
#         if isinstance(other, Var):
#             return True
#         return isinstance(other, Number) and other.num == self.num


# Path: prolog/interpreter/test/test_resource_consumption.py
import py
from prolog.interpreter.continuation import *
from prolog.interpreter.parsing import parse_query_term, get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UnificationFailed
from prolog.interpreter.test.tool import collect_all, assert_true, assert_false
from prolog.interpreter.term import Number

            fcont = fcont.nextcont
        assert depth < self.seen
        depth = 0
        numvars = 0
        while heap:
            depth += 1
            numvars += heap.i
            heap = heap.prev
        assert depth < self.seen
        assert numvars < self.seen
        return DoneSuccessContinuation(self.engine), DoneFailureContinuation(self.engine), original_heap

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

@py.test.mark.xfail # bug introduced in aa127d31df0c
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_call():

    e = get_engine("""