Пример #1
0
def test_flvector_set_wrong_type():
    with pytest.raises(SchemeException):
        run_mod("""
            #lang pycket
            (require '#%flfxnum '#%unsafe)
            (let [(a (flvector 1.2 1.3))] (flvector-set! a 1 'a))
        """)
Пример #2
0
def test_use_before_definition():
    with pytest.raises(SchemeException):
        m = run_mod("""
        #lang pycket
        x
        (define x 1)
    """)

    with pytest.raises(SchemeException):
        m = run_mod("""
        #lang pycket
        x
        (define x 1)
        (set! x 2)
    """)
Пример #3
0
def test_use_before_definition():
    with pytest.raises(SchemeException):
        m = run_mod("""
        #lang pycket
        x
        (define x 1)
    """)

    with pytest.raises(SchemeException):
        m = run_mod("""
        #lang pycket
        x
        (define x 1)
        (set! x 2)
    """)
Пример #4
0
def test_module_star():
    m = run_mod("""
    #lang pycket
    (module outer pycket
      (define a 1)
      (module* inner #f
        (provide b)
        (define b (+ a 2))))
    (module snd pycket
      (require (submod ".." outer inner))
      (provide c)
      (define c (+ b 1)))
    (require (submod "." snd))
    (define d c)
    """)
    assert len(m.submodules) == 2
    outer = m.find_submodule("outer")
    snd = m.find_submodule("snd")

    assert outer.parent is m
    assert snd.parent is m
    assert len(outer.submodules) == 1
    assert len(snd.submodules) == 0

    d = m.defs[W_Symbol.make("d")]
    assert isinstance(d, W_Integer) and d.value == 4
Пример #5
0
def test_dynamic_wind4():
    m = run_mod("""
    #lang pycket
    (require racket/control)
    (define val
        (let* ([x (make-parameter 0)]
                 [l null]
                 [add (lambda (a b)
                        (set! l (append l (list (cons a b)))))])
            (let ([k (parameterize ([x 5])
                       (dynamic-wind
                           (lambda () (add 1 (x)))
                           (lambda () (parameterize ([x 6])
                                        (let ([k+e (let/cc k (cons k void))])
                                          (add 2 (x))
                                          ((cdr k+e))
                                          (car k+e))))
                           (lambda () (add 3 (x)))))])
              (parameterize ([x 7])
                (let/cc esc
                  (k (cons void esc)))))
            l))
    (define equal (equal? val '((1 . 5) (2 . 6) (3 . 5) (1 . 5) (2 . 6) (3 . 5))))
    """)
    val = m.defs[values.W_Symbol.make("val")]
    equal = m.defs[values.W_Symbol.make("equal")]
    assert equal is values.w_true
Пример #6
0
def test_with_continuation_mark3():
    m = run_mod("""
    #lang racket/base
    (define result
        (let* ([extract
	        (lambda (k) (continuation-mark-set->list
	           (continuation-marks k)
	           'x))]
           [go
	        (lambda (in?)
              (lambda ()
	        	      (let ([k (with-continuation-mark 'x 10
	        			 (begin0
	        			  (with-continuation-mark 'x 11
	        			    (call/cc
	        			     (lambda (k )
	        			       (with-continuation-mark 'x 12
	        				 (if in?
	        				     (extract k)
	        				     k)))))
	        			  (+ 2 3)))])
	        		(if in?
	        		    k
	        		    (extract k)))))])
        ((go #t))))
    (define valid (equal? result '(11 10)))
    """)
    sym = W_Symbol.make("valid")
    assert m.defs[sym] is w_true
Пример #7
0
def test_set_mod_other():
    m = run_mod("""
#lang pycket
    (require pycket/set-export)
(define y (not x))
""")
    assert m.defs[W_Symbol.make("y")]
Пример #8
0
def test_set_mod_other():
    m = run_mod("""
#lang pycket
    (require pycket/set-export)
(define y (not x))
""")
    assert m.defs[W_Symbol.make("y")]
Пример #9
0
def test_module_star():
    m = run_mod(
    """
    #lang pycket
    (module outer pycket
      (define a 1)
      (module* inner #f
        (provide b)
        (define b (+ a 2))))
    (module snd pycket
      (require (submod ".." outer inner))
      (provide c)
      (define c (+ b 1)))
    (require (submod "." snd))
    (define d c)
    """)
    assert len(m.submodules) == 2
    outer = m.find_submodule("outer")
    snd   = m.find_submodule("snd")

    assert outer.parent is m
    assert snd.parent is m
    assert len(outer.submodules) == 1
    assert len(snd.submodules) == 0

    d = m.defs[W_Symbol.make("d")]
    assert isinstance(d, W_Integer) and d.value == 4
Пример #10
0
def test_dynamic_wind4():
    m = run_mod(
    """
    #lang pycket
    (require racket/control)
    (define val
        (let* ([x (make-parameter 0)]
                 [l null]
                 [add (lambda (a b)
                        (set! l (append l (list (cons a b)))))])
            (let ([k (parameterize ([x 5])
                       (dynamic-wind
                           (lambda () (add 1 (x)))
                           (lambda () (parameterize ([x 6])
                                        (let ([k+e (let/cc k (cons k void))])
                                          (add 2 (x))
                                          ((cdr k+e))
                                          (car k+e))))
                           (lambda () (add 3 (x)))))])
              (parameterize ([x 7])
                (let/cc esc
                  (k (cons void esc)))))
            l))
    (define equal (equal? val '((1 . 5) (2 . 6) (3 . 5) (1 . 5) (2 . 6) (3 . 5))))
    """)
    val   = m.defs[values.W_Symbol.make("val")]
    equal = m.defs[values.W_Symbol.make("equal")]
    assert equal is values.w_true
Пример #11
0
def test_with_continuation_mark3():
    m = run_mod(
        """
    #lang racket/base
    (define a 1)
    (set! a 2)
    (define result
        (let* ([extract
	        (lambda (k) (continuation-mark-set->list
	           (continuation-marks k)
	           'x))]
           [go
	        (lambda (in?)
              (lambda ()
	        	      (let ([k (with-continuation-mark 'x 10
	        			 (begin0
	        			  (with-continuation-mark 'x 11
	        			    (call/cc
	        			     (lambda (k )
	        			       (with-continuation-mark 'x 12
	        				 (if in?
	        				     (extract k)
	        				     k)))))
	        			  (+ a 3)))])
	        		(if in?
	        		    k
	        		    (extract k)))))])
        ((go #t))))
    (define valid (equal? result '(11 10)))
    """
    )
    sym = W_Symbol.make("valid")
    assert m.defs[sym] is w_true
Пример #12
0
def test_kw_submodule():
    # Just test to see if this runs.
    # It caused quite a few problems
    # when implementing submodules.
    m = run_mod("""
    #lang pycket
    (require racket/private/kw)
    procedure-arity-includes?
    """)
Пример #13
0
def test_set_mod2():
    m = run_mod("""
#lang pycket
(provide table)
(define table #f)
(set! table #f)
""")
    ov = m.defs[W_Symbol.make("table")]
    assert isinstance(ov, W_Cell)
Пример #14
0
def test_set_mod2():
    m = run_mod("""
#lang pycket
(provide table)
(define table #f)
(set! table #f)
""")
    ov = m.defs[W_Symbol.make("table")]
    assert isinstance(ov, W_Cell)
Пример #15
0
def test_path_to_complete_path():
    m = run_mod("""
    #lang pycket
    (define p (path->complete-path "test.rkt"))
    """)
    p = m.defs[values.W_Symbol.make("p")]
    cwd = os.getcwd()
    assert isinstance(p, values.W_Path)
    full = cwd + "/" + "test.rkt"
    assert full == p.path
Пример #16
0
def test_kw_submodule():
    # Just test to see if this runs.
    # It caused quite a few problems
    # when implementing submodules.
    m = run_mod(
    """
    #lang pycket
    (require racket/private/kw)
    procedure-arity-includes?
    """)
Пример #17
0
def test_path_to_complete_path():
    m = run_mod(
    """
    #lang pycket
    (define p (path->complete-path "test.rkt"))
    """)
    p = m.defs[values.W_Symbol.make("p")]
    cwd = os.getcwd()
    assert isinstance(p, values.W_Path)
    full = cwd + "/" + "test.rkt"
    assert full == p.path
Пример #18
0
def test_bytes_conversions():
    m = run_mod("""
    #lang pycket
    (define a (real->floating-point-bytes 1 8 #f))
    (define b (integer-bytes->integer a #f))
    """)
    a = values.W_Symbol.make("a")
    b = values.W_Symbol.make("b")

    vb = m.defs[b]
    assert isinstance(vb, values.W_Fixnum) and vb.value == 4607182418800017408
Пример #19
0
def test_string_set_bang():
    m = run_mod(
    """
    #lang pycket
    (define str (substring "hello world" 0 5))
    (string-set! str 0 #\\x)
    """)
    sym = W_Symbol.make("str")
    res = m.defs[sym]
    assert isinstance(res, W_String)
    assert not res.immutable()
    assert res.value == "xello"
Пример #20
0
def test_let_remove_num_envs_edge_case():
    m = run_mod("""
    #lang pycket
    (define d
      (let-values (((es) values))
        (let-values (((adj) '0))
          (let-values ((() (es)))
            adj))))
    """)
    d = W_Symbol.make("d")
    assert type(m.defs[d]) is W_Fixnum and m.defs[d].value == 0

    m = run_mod("""
    #lang pycket
    (define d
      (let-values (((es) '0))
        (let-values (((adj) '1))
          (let-values (((a b c) (begin es (values '2 '3 '4))))
            (+ adj a)))))
    """)
    assert type(m.defs[d]) is W_Fixnum and m.defs[d].value == 3
Пример #21
0
def test_bytes_conversions():
    m = run_mod(
    """
    #lang pycket
    (define a (real->floating-point-bytes 1 8 #f))
    (define b (integer-bytes->integer a #f))
    """)
    a = values.W_Symbol.make("a")
    b = values.W_Symbol.make("b")

    vb = m.defs[b]
    assert isinstance(vb, values.W_Fixnum) and vb.value == 4607182418800017408
Пример #22
0
def test_shadowing_macro():
    m = run_mod("""
#lang pycket

(define-syntax bind+
  (syntax-rules ()
    [(bind+ v e) (let ([x v]) (+ x e))]
    [(bind+ v0 v ... e) (let ([x v0]) (bind+ v ... (+ x e)))]))

(define x (bind+ 1 2 3))
""")
    ov = m.defs[W_Symbol.make("x")]
    assert ov.value == 6
Пример #23
0
def test_shadowing_macro():
    m = run_mod("""
#lang pycket

(define-syntax bind+
  (syntax-rules ()
    [(bind+ v e) (let ([x v]) (+ x e))]
    [(bind+ v0 v ... e) (let ([x v0]) (bind+ v ... (+ x e)))]))

(define x (bind+ 1 2 3))
""")
    ov = m.defs[W_Symbol.make("x")]
    assert ov.value == 6
Пример #24
0
def test_let_remove_num_envs_edge_case():
    m = run_mod(
    """
    #lang pycket
    (define d
      (let-values (((es) values))
        (let-values (((adj) '0))
          (let-values ((() (es)))
            adj))))
    """)
    d = W_Symbol.make("d")
    assert type(m.defs[d]) is W_Fixnum and m.defs[d].value == 0

    m = run_mod(
    """
    #lang pycket
    (define d
      (let-values (((es) '0))
        (let-values (((adj) '1))
          (let-values (((a b c) (begin es (values '2 '3 '4))))
            (+ adj a)))))
    """)
    assert type(m.defs[d]) is W_Fixnum and m.defs[d].value == 3
Пример #25
0
def test_dynamic_wind3():
    m = run_mod("""
    #lang pycket
    (require racket/control)
    (define val
      (let/ec k0
          (let/ec k1
            (dynamic-wind
             void
             (lambda () (k0 'cancel))
             (lambda () (k1 'cancel-canceled))))))
    """)
    val = m.defs[values.W_Symbol.make("val")]
    assert val is values.W_Symbol.make("cancel-canceled")
Пример #26
0
def test_hash_iteration_enables_jitting(source):
    """
    #lang pycket
    (define h #hash((1 . 2) (2 . 3) (3 . 4)))
    (define (fe c v) '())
    (define (fm c v) '())
    (hash-for-each h fe)
    (hash-map h fm)
    """
    mod = run_mod(source)
    f = mod.defs[values.W_Symbol.make('fe')]
    assert f.closure.caselam.lams[0].body[0].should_enter
    f = mod.defs[values.W_Symbol.make('fm')]
    assert f.closure.caselam.lams[0].body[0].should_enter
Пример #27
0
def test_hash_iteration_enables_jitting(source):
    """
    #lang pycket
    (define h #hash((1 . 2) (2 . 3) (3 . 4)))
    (define (fe c v) '())
    (define (fm c v) '())
    (hash-for-each h fe)
    (hash-map h fm)
    """
    mod = run_mod(source)
    f = mod.defs[values.W_Symbol.make('fe')]
    assert f.closure.caselam.lams[0].body[0].should_enter
    f = mod.defs[values.W_Symbol.make('fm')]
    assert f.closure.caselam.lams[0].body[0].should_enter
Пример #28
0
def test_with_continuation_mark():
    m = run_mod("""
    #lang pycket
    (define key (make-continuation-mark-key))
    (define result
      (with-continuation-mark key "quiche"
        (with-continuation-mark key "ham"
           (continuation-mark-set-first
            (current-continuation-marks)
            key))))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], values_string.W_String)
    assert m.defs[sym].as_str_utf8() == "ham"
Пример #29
0
def test_set_modvar():
    m = run_mod("""
#lang pycket

(define sum 0)

(define (tail-rec-aux i n)
  (if (< i n)
      (begin (set! sum (+ sum 1)) (tail-rec-aux (+ i 1) n))
      sum))

(tail-rec-aux 0 100)
""")
    ov = m.defs[W_Symbol.make("sum")].get_val()
    assert ov.value == 100
Пример #30
0
def test_dynamic_wind3():
    m = run_mod(
    """
    #lang pycket
    (require racket/control)
    (define val
      (let/ec k0
          (let/ec k1
            (dynamic-wind
             void
             (lambda () (k0 'cancel))
             (lambda () (k1 'cancel-canceled))))))
    """)
    val = m.defs[values.W_Symbol.make("val")]
    assert val is values.W_Symbol.make("cancel-canceled")
Пример #31
0
def test_with_continuation_mark():
    m = run_mod(
    """
    #lang pycket
    (define key (make-continuation-mark-key))
    (define result
      (with-continuation-mark key "quiche"
        (with-continuation-mark key "ham"
           (continuation-mark-set-first
            (current-continuation-marks)
            key))))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], values_string.W_String)
    assert m.defs[sym].as_str_utf8() == "ham"
Пример #32
0
def test_set_modvar():
    m = run_mod("""
#lang pycket

(define sum 0)

(define (tail-rec-aux i n)
  (if (< i n)
      (begin (set! sum (+ sum 1)) (tail-rec-aux (+ i 1) n))
      sum))

(tail-rec-aux 0 100)
""")
    ov = m.defs[W_Symbol.make("sum")].get_val()
    assert ov.value == 100
Пример #33
0
def test_with_continuation_mark_impersonator():
    m = run_mod("""
    #lang pycket
    (define mark-key
      (impersonate-continuation-mark-key
       (make-continuation-mark-key)
       (lambda (l) (car l))
       (lambda (s) (string->list s))))
    (define result
      (with-continuation-mark mark-key "quiche"
        (continuation-mark-set-first
         (current-continuation-marks)
         mark-key)))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], W_Character)
    assert m.defs[sym].value == 'q'
Пример #34
0
def test_with_continuation_mark_impersonator():
    m = run_mod(
    """
    #lang pycket
    (define mark-key
      (impersonate-continuation-mark-key
       (make-continuation-mark-key)
       (lambda (l) (car l))
       (lambda (s) (string->list s))))
    (define result
      (with-continuation-mark mark-key "quiche"
        (continuation-mark-set-first
         (current-continuation-marks)
         mark-key)))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], W_Character)
    assert m.defs[sym].value == 'q'
Пример #35
0
def test_impersonator_application_mark():
    m = run_mod("""
    #lang pycket
    (require racket/private/kw)
    (define key (make-continuation-mark-key))
    (define proc
      (lambda ()
        (continuation-mark-set-first
          (current-continuation-marks)
          key)))
    (define wrapped
      (impersonate-procedure proc (lambda () (values))
                             impersonator-prop:application-mark (cons key 42)))
    (define result (wrapped))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], W_Fixnum)
    assert m.defs[sym].value == 42
Пример #36
0
def test_impersonator_application_mark():
    m = run_mod(
    """
    #lang pycket
    (require racket/private/kw)
    (define key (make-continuation-mark-key))
    (define proc
      (lambda ()
        (continuation-mark-set-first
          (current-continuation-marks)
          key)))
    (define wrapped
      (impersonate-procedure proc (lambda () (values))
                             impersonator-prop:application-mark (cons key 42)))
    (define result (wrapped))
    """)
    sym = W_Symbol.make("result")
    assert isinstance(m.defs[sym], W_Fixnum)
    assert m.defs[sym].value == 42
Пример #37
0
def test_dynamic_wind2():
    m = run_mod("""
    #lang pycket
    (require racket/control)
    (define acc 0)
    (define v
      (let/cc k
        (dynamic-wind
          (lambda () (set! acc (+ acc 1)))
          (lambda () (set! acc (+ acc 1)) 42)
          (lambda () (set! acc (+ acc 1))))))
    """)
    acc = m.defs[values.W_Symbol.make("acc")]
    v = m.defs[values.W_Symbol.make("v")]
    assert isinstance(acc, values.W_Cell)
    acc = acc.get_val()

    assert isinstance(v, values.W_Fixnum) and v.value == 42
    assert isinstance(acc, values.W_Fixnum) and acc.value == 3
Пример #38
0
def test_dynamic_wind2():
    m = run_mod(
    """
    #lang pycket
    (require racket/control)
    (define acc 0)
    (define v
      (let/cc k
        (dynamic-wind
          (lambda () (set! acc (+ acc 1)))
          (lambda () (set! acc (+ acc 1)) 42)
          (lambda () (set! acc (+ acc 1))))))
    """)
    acc = m.defs[values.W_Symbol.make("acc")]
    v   = m.defs[values.W_Symbol.make("v")]
    assert isinstance(acc, values.W_Cell)
    acc = acc.get_val()

    assert isinstance(v, values.W_Fixnum) and v.value == 42
    assert isinstance(acc, values.W_Fixnum) and acc.value == 3
Пример #39
0
def test_submodule_operations():
    m = run_mod("""
    #lang pycket
    (module test-sub-module pycket
      (provide a)
      (define a 1))
    (require (submod "." test-sub-module))
    (define b (+ a a))
    """)
    assert len(m.submodules) == 1

    # properties concerning relationships between modules and submodules
    b = m.defs[W_Symbol.make("b")]
    sub = m.resolve_submodule_path(["test-sub-module"])
    assert sub.parent is m
    assert sub in m.submodules
    assert m.root_module() is m
    assert sub.root_module() is m

    a = sub.defs[W_Symbol.make("a")]
    assert isinstance(b, W_Integer) and b.value == 2
    assert isinstance(a, W_Integer) and a.value == 1
Пример #40
0
def test_submodule_operations():
    m = run_mod(
    """
    #lang pycket
    (module test-sub-module pycket
      (provide a)
      (define a 1))
    (require (submod "." test-sub-module))
    (define b (+ a a))
    """)
    assert len(m.submodules) == 1

    # properties concerning relationships between modules and submodules
    b   = m.defs[W_Symbol.make("b")]
    sub = m.resolve_submodule_path(["test-sub-module"])
    assert sub.parent is m
    assert sub in m.submodules
    assert m.root_module() is m
    assert sub.root_module() is m

    a   = sub.defs[W_Symbol.make("a")]
    assert isinstance(b, W_Integer) and b.value == 2
    assert isinstance(a, W_Integer) and a.value == 1
Пример #41
0
def test_racket_mod():
    m = run_mod("#lang racket/base\n (define x 1)")
    ov = m.defs[W_Symbol.make("x")]
    assert ov.value == 1
Пример #42
0
def test_racket_mod():
    m = run_mod("#lang racket/base\n (define x 1)")
    ov = m.defs[W_Symbol.make("x")]
    assert ov.value == 1