def expand(s, wrap=False, stdlib=False):
    data = expand_string(s)
    return pycket_json.loads(data) def _is_ascii(s):
def _is_ascii_elidable(s):
    def fromstr_utf8(s, immutable=False):
    def fromascii(s, immutable=False):
    def fromunicode(u, immutable=False):
    def make(val):
    def make_immutable(self):
    def get_strategy(self):
    def get_storage(self):
    def as_str_ascii(self):
    def as_str_utf8(self):
    def as_unicode(self):
    def as_charlist_ascii(self):
    def as_charlist_utf8(self):
    def as_unicharlist(self):
    def as_escaped_utf8(self):
    def length(self):
    def getitem(self, index):
    def getslice(self, start, stop):
    def hash_equal(self, info=None):
    def equal(self, other):
    def cmp(self, other):
    def cmp_case_insensitive(self, other):
    def upper(self):
    def lower(self):
    def __repr__(self):
    def tostring(self):
    def setitem(self, index, unichar):
    def setslice(self, index, w_from, fromstart, fromend):
    def __init__(self, strategy, storage):
    def get_strategy(self):
    def get_storage(self):
    def change_strategy(self, strategy, storage):
    def make_immutable(self):
    def immutable(self):
    def setitem(self, index, unichar):
    def setslice(self, index, w_from, fromstart, fromend):
    def __init__(self, strategy, storage):
    def make_immutable(self):
    def immutable(self):
    def get_storage(self):
    def get_strategy(self):
    def get_strategy(self):
    def make_mutable(self, w_str):
    def as_str_ascii(self, w_str):
    def as_str_utf8(self, w_str):
    def as_unicode(self, w_str):
    def as_charlist_ascii(self, w_str):
    def as_charlist_utf8(self, w_str):
    def as_unicharlist(self, w_str):
    def length(self, w_str):
    def getitem(self, w_str, index):
    def getslice(self, w_str, start, stop):
    def eq(self, w_str, w_other):
    def cmp(self, w_str, w_other):
    def cmp_case_insensitive(self, w_str, w_other):
    def hash(self, w_str):
    def upper(self, w_str):
    def lower(self, w_str):
    def setitem(self, w_str, index, unichar):
    def setslice(self, w_str, index, w_from, fromstart, fromend):
    def as_charlist_ascii(self, w_str):
    def as_charlist_utf8(self, w_str):
    def as_unicharlist(self, w_str):
    def setitem(self, w_str, index, unichar):
    def setslice(self, w_str, index, w_from, fromstart, fromend):
    def as_str_ascii(self, w_str):
    def as_str_utf8(self, w_str):
    def as_unicode(self, w_str):
    def make_mutable(self, w_str):
    def setitem(self, w_str, index, unichar):
    def setslice(self, w_str, index, w_from, fromstart, fromend):
    def make_mutable(self, w_str):
    def as_str_utf8(self, w_str):
    def as_unicode(self, w_str):
    def length(self, w_str):
    def getitem(self, w_str, index):
    def getslice(self, w_str, start, stop):
    def eq(self, w_str, w_other):
    def hash(self, w_str):
    def upper(self, w_str):
    def lower(self, w_str):
    def make_unicode(self, w_str):
    def as_charlist_utf8(self, w_str):
    def as_unicharlist(self, w_str):
    def as_str_utf8(self, w_str):
    def length(self, w_str):
    def getitem(self, w_str, index):
    def getslice(self, w_str, start, stop):
    def eq(self, w_str, w_other):
    def setitem(self, w_str, index, unichar):
    def setslice(self, w_str, index, w_from, fromstart, fromend):
    def upper(self, w_str):
    def lower(self, w_str):
    def make_mutable(self, w_str):
    def as_str_ascii(self, w_str):
    def as_str_utf8(self, w_str):
    def as_unicode(self, w_str):
    def length(self, w_str):
    def getitem(self, w_str, index):
    def getslice(self, w_str, start, stop):
    def eq(self, w_str, w_other):
    def upper(self, w_str):
    def lower(self, w_str):
    def as_charlist_ascii(self, w_str):
    def as_charlist_utf8(self, w_str):
    def as_str_utf8(self, w_str):
    def as_unicharlist(self, w_str):
    def as_unicode(self, w_str):
    def length(self, w_str):
    def getitem(self, w_str, index):
    def getslice(self, w_str, start, stop):
    def eq(self, w_str, w_other):
    def setitem(self, w_str, index, unichar):
    def setslice(self, w_str, index, w_from, fromstart, fromend):
    def upper(self, w_str):
    def lower(self, w_str):
class W_String(W_Object):
class W_MutableString(W_String):
class W_ImmutableString(W_String):
class W_AsciiImmutableString(W_ImmutableString):
class W_UnicodeImmutableString(W_ImmutableString):
class StringStrategy(object):
class ImmutableStringStrategy(StringStrategy):
class MutableStringStrategy(StringStrategy):
class AsciiStringStrategy(ImmutableStringStrategy):
class AsciiMutableStringStrategy(MutableStringStrategy):
class UnicodeStringStrategy(ImmutableStringStrategy):
class UnicodeMutableStringStrategy(MutableStringStrategy): def run(p, v=None, stdlib=False, extra="", expect_to_fail=False):
    if pytest.config.new_pycket:
        return run_expr_result(p, expect_to_fail=expect_to_fail, v=v)
    return run_mod_expr(p,v=v,stdlib=stdlib, extra=extra) def run_fix(p, v=None, stdlib=False, extra="", equal_huh=False):
    new = pytest.config.new_pycket
    if new:
        return run_expr(p, v, equal_huh=equal_huh)
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Fixnum)
    if v is not None:
        assert ov.value == v
    return ov.value def run_flo(p, v=None, stdlib=False, extra=""):
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Flonum)
    if v is not None:
        assert ov.value == v
    return ov.value def run_top(p, v=None, stdlib=False, extra=""):
    return run_mod_expr(p,v=v,stdlib=stdlib, wrap=True, extra=extra) def execute(p, stdlib=False, extra=""):
    return run_mod_expr(p, stdlib=stdlib, extra=extra) def run_values(p, stdlib=False):
    e = "(call-with-values (lambda () %s) list)"%p
    v = run_mod_expr(e, stdlib=stdlib)
    return values.from_list(v) def check_equal(*pairs_of_equal_stuff, **kwargs):
    code = []
    tail = []
    assert len(pairs_of_equal_stuff) % 2 == 0
    ind = 0
    for i in range(len(pairs_of_equal_stuff) // 2):
        a = pairs_of_equal_stuff[i * 2]
        b = pairs_of_equal_stuff[i * 2 + 1]
        ind += 1
        if isinstance(a, list):
            code.append("  " * ind + "(let ()")
            ind += 1
            code.extend(["  " * ind + x for x in a[:-1]])
            code.append( "  " * ind + ("(if (equal? %s %s)" % (a[-1], b)))
            ind += 1
            tail.append("  " * ind + "%s))" % i)
        else:
            snippet = "(equal? %s %s)" % (a, b)
            code.append("  " * ind + "(if %s" % snippet)
            tail.append("  " * (ind + 1) + "%s)" % i)
    code.append("  " * (ind + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    try:
        print code
    except UnicodeEncodeError:
        print code.encode("ascii", 'replace')

    extra = kwargs.get("extra", "")
    res = execute(code, extra=extra)
    if res is not values.w_true:
        src = pairs_of_equal_stuff[res.value * 2]
        if isinstance(src, list):
            src = "(let ()\n" + "\n".join(src) + ")"
        wrong = execute(src, extra=extra)
        assert 0, u"%s is %s, which is different from %s" % (
            pairs_of_equal_stuff[res.value * 2], wrong.tostring(),
            pairs_of_equal_stuff[res.value * 2 + 1]) def run_mod(m, stdlib=False, srcloc=True):
    assert not stdlib
    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    else:
        ast = parse_module(expand_from_bytecode(m, srcloc), bytecode_expand=True)

    return run_ast(ast)
import pytest
from pycket.expand import expand
from pycket.interpreter import *
from pycket.values import *
from pycket import values_string
from pycket.prims import *
from pycket.test.testhelper import (run, run_fix, run_flo, run_top, execute,
        run_values, check_equal, run_mod)
    from pycket.expand    import expand_string, parse_module
    from pycket           import config
    from pycket.callgraph import LOOP_PARTICIPANT, LOOP_HEADER
    from pycket.env       import w_global_config as conf
    from pycket.expand import expand_string, parse_module
    from pycket        import config
    from pycket.env    import w_global_config as conf
    from pycket.expand import expand_string, parse_module
    from pycket        import config
    from pycket.env    import w_global_config as conf
"""
    run_fix("((%s %s) 2)"%(Y,fib), 1)
    run_fix("((%s %s) 2)"%(Y,fac), 2)

def test_vararg():
    run_fix ("((lambda x (car x)) 1)", 1)
    run_fix ("((lambda (a . x) a) 1)", 1)
    run ("((lambda (a . x) x) 1)", w_null)

def test_callwithcurrentcontinuation():
    run_fix ("(call-with-current-continuation (lambda (k) 1))", 1)
    run_fix ("(+ 1 (call-with-current-continuation (lambda (k) 1)))", 2)
    run_fix ("(+ 1 (call-with-current-continuation (lambda (k) (k 1))))", 2)
    run_fix ("(+ 1 (call-with-current-continuation (lambda (k) (+ 5 (k 1)))))", 2)


def test_values():
    run_fix("(values 1)", 1)
    run_fix("(let-values () (values 1 2) (values 3))", 3)
    prog = "(let-values () (call-with-current-continuation (lambda (k) (k 1 2))) 3)"
    run_fix(prog, 3)
    v = run_values("(values #t #f)")
    assert [w_true, w_false] == v
    run_fix("(call-with-values (lambda () (values 1 2)) (lambda (a b) (+ a b)))", 3)
    run_fix("(call-with-values (lambda () (values 1 2)) +)", 3)
    run_fix("(call-with-values (lambda () (values)) (lambda () 0))", 0)
    run_fix("(call-with-values (lambda () (values 1)) (lambda (x) x))", 1)
    run_fix("(call-with-values (lambda () (values 1)) values)", 1)
    run_fix("(call-with-values (lambda () 1) values)", 1)

@pytest.mark.skipif(pytest.config.new_pycket, reason="-and- is not in kernel")
def test_call_with_values():
    run_fix("""
    (call-with-values (lambda () (time-apply (lambda () (+ 1 2)) (quote ())))
                  (lambda (result t r gc) (and (fixnum? t) (fixnum? r) (fixnum? gc)
                                               (car result))))
""", 3)

def test_define():
    run_top("(define x 1) x", W_Fixnum(1))

@pytest.mark.skipif(pytest.config.new_pycket, reason="time is not in kernel")
def test_time():
    run_fix("(time 1)", 1)

def test_apply():
    run_fix("(apply + (list 1 2 3))", 6)
    run_fix("(apply + 1 2 (list 3))", 6)
    run_fix("(apply + 1 2 3 (list))", 6)

def test_setbang_recursive_lambda():
    run_fix("((letrec-values ([(f) (lambda (a) (begin (set! f (lambda (a) 1)) (f a)))]) f) 6)", 1)

def test_keyword():
    run("(quote #:foo)", W_Keyword.make("foo"))


#
# From http://people.csail.mit.edu/jaffer/r5rs_8.html
# And the racket docs
#
def test_eq():
    run("(eq? (quote yes) (quote yes))", w_true)
    run("(eq? (quote yes) (quote no))", w_false)
    run("(let-values ([(v) (mcons 1 2)]) (eq? v v))", w_true)
    run("(eq? (mcons 1 2) (mcons 1 2))", w_false)
    #run_top("(eq? (make-string 3 #\z) (make-string 3 #\z))", w_false, stdlib=True)

    run("(eq? (quote a) (quote a))", w_true)
    run("(eq? (quote (a)) (quote (a)))", w_false) #racket
    run("(eq? (list (quote a)) (list (quote a)))", w_false)
    # run('(eq? "a" "a")', w_true) #racket
    # run('(eq? "" "")', w_true) #racket
    run("(eq? (quote ()) (quote ()))", w_true)
    run("(eq? 2 2)",  w_true) #racket
    run("(eq? #\A #\A)", w_true) #racket
    run("(eq? car car)", w_true)
    run("(let-values (((n) (+ 2 3))) (eq? n n))", w_true) #racket
    run("(let-values (((x) (quote (a)))) (eq? x x))", w_true)
    #run("(let-values (((x) (quote #()))) (eq? x x))", w_true)
    run("(let-values (((p) (lambda (x) x))) (eq? p p))", w_true)

def test_equal():
    run("(equal? (quote a) (quote a))", w_true)
    run("(equal? (quote a) (quote a))", w_true)
    run("(equal? (quote (a (b) c)) (quote (a (b) c)))", w_true)
    run('(equal? "abc" "abc")', w_true)
    run("(equal? 2 2)", w_true)
    run("(equal? (make-vector 5 (quote a)) (make-vector 5 (quote a)))", w_true)
    run("(equal? (lambda (x) x) (lambda (y) y))", w_false) #racket

def test_eqv():
    check_equal(
        "(eqv? 'yes 'yes)", "#t",
        "(eqv? 'yes 'no)", "#f",
        "(eqv? (expt 2 100) (expt 2 100))", "#t",
        "(eqv? 2 2.0)", "#f",
        "(eqv? (integer->char 955) (integer->char 955))", "#t",
    #run_top("(eqv? (make-string 3 #\z) (make-string 3 #\z))", "#f", stdlib=True)
        "(eqv? +nan.0 +nan.0)", "#t",

        "(eqv? 'a 'a)", "#t",
        "(eqv? 'a 'b)", "#f",
        "(eqv? 2 2)", "#t",
        "(eqv? '() '())", "#t",
        "(eqv? 100000000 100000000)", "#t",
        "(eqv? 1.0+2i 1.0+2i)", "#t",
        "(eqv? (cons 1 2) (cons 1 2))", "#f",
        """(eqv? (lambda () 1)
                 (lambda () 2))""", "#f",
        "(eqv? #f 'nil)", "#f",
        """(let ((p (lambda (x) x)))
           (eqv? p p))""", "#t",
    # run('(eqv? "" "")', "#t") #racket
        "(eqv? '#() '#())", "#f", #racket
        """(eqv? (lambda (x) x)
                 (lambda (x) x))""", "#f", #racket
        """(eqv? (lambda (x) x)
                 (lambda (y) y))""", "#f", #racket
    )

    run_top("""(define gen-counter