| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn test_integer_division_dispatch_preserves_signs_promotion_and_rebinding() { |
| 6 | mut vm := new() |
| 7 | for source, expected in { |
| 8 | '(list (quotient 10 3) (quotient -10 3) (quotient 10 -3) (quotient -10 -3))': '(3 -3 -3 3)' |
| 9 | '(list (remainder 10 3) (remainder -10 3) (remainder 10 -3) (remainder -10 -3))': '(1 -1 1 -1)' |
| 10 | '(list (modulo 10 3) (modulo -10 3) (modulo 10 -3) (modulo -10 -3))': '(1 2 -2 -1)' |
| 11 | '(list (quotient -9223372036854775808 -1) (remainder -9223372036854775808 -1) (modulo -9223372036854775808 -1))': '(9223372036854775808 0 0)' |
| 12 | '(list (modulo -9223372036854775808 3) (modulo 9223372036854775807 -3) (modulo -1 -9223372036854775808))': '(1 -2 -1)' |
| 13 | } { |
| 14 | assert write(vm.eval(source)!)! == expected |
| 15 | } |
| 16 | vm.eval('(define saved modulo) (define(run x y)(modulo x y)) (set! modulo (lambda(x y) 42))')! |
| 17 | assert vm.eval('(run 10 3)')!.whole() == 42 |
| 18 | assert vm.eval('(saved -10 3)')!.whole() == 2 |
| 19 | assert vm.eval_alt('saved(-10, 3)')!.whole() == 2 |
| 20 | } |
| 21 | |
| 22 | fn test_vector_write_dispatch_preserves_aliases_callable_values_and_live_bindings() { |
| 23 | mut vm := new() |
| 24 | vm.eval('(define target (vector 0 0)) (define alias target) (define original vector-set!) |
| 25 | (define (put i x) (vector-set! target i x))')! |
| 26 | assert vm.eval('(put 0 (lambda(x)(+ x 1)))')!.tag == .unspecified |
| 27 | assert vm.eval('((vector-ref alias 0) 41)')!.whole() == 42 |
| 28 | vm.eval_alt('putAlt := fn(i, x) { original(target, i, x) }\nputAlt(1, #f)')! |
| 29 | assert write(vm.eval('(vector-ref alias 1)')!)! == '#f' |
| 30 | vm.eval('(set! vector-set! (lambda(v i x) 99))')! |
| 31 | assert vm.eval('(put 1 22)')!.whole() == 99 |
| 32 | assert write(vm.eval('(vector-ref target 1)')!)! == '#f' |
| 33 | vm.eval('(original target 1 (if #f #t))')! |
| 34 | assert vm.eval('(vector-ref alias 1)')!.tag == .unspecified |
| 35 | } |
| 36 | |
| 37 | // The bounded VM runs the full dispatcher. Compare errors, values and the live |
| 38 | // destination after each budget, including denied writes in ordinary/tail calls. |
| 39 | fn test_primitive_dispatch_matches_complete_dispatcher_budgets_and_write_effects() { |
| 40 | mut plain := new() |
| 41 | mut bounded := new_bounded(Options{}, arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! |
| 42 | defer { bounded.close() or { panic(err) } } |
| 43 | for source in ['(vector-set! target 0 42)', '(begin (vector-set! target 0 42) (vector-ref target 0))', |
| 44 | '((lambda(v)(vector-set! v 0 42)) target)', '(vector-set! target -1 42)', |
| 45 | '(vector-set! target 1 42)', '(vector-set! target 0.0 42)', '(vector-set! 7 0 42)', |
| 46 | '(vector-set! #(0) 0 42)', '(vector-set! target 0)', '(vector-set! target 0 42 43)', |
| 47 | '(modulo -10 3)', '(quotient -9223372036854775808 -1)', |
| 48 | '(remainder -9223372036854775808 -1)', '(modulo -9223372036854775808 -1)', |
| 49 | '(quotient 3 0)', '(remainder 3 0)', '(modulo 3 0)', '(modulo 3 2.0)', '(quotient 3)', |
| 50 | '(remainder 3 2 1)'] { |
| 51 | for budget in 0 .. 40 { |
| 52 | plain.options = Options{...plain.options, max_steps: 0} |
| 53 | plain.eval('(define target (vector 0))')! |
| 54 | plain.options = Options{...plain.options, max_steps: budget} |
| 55 | mut expected_error := ?IError(none) |
| 56 | expected := plain.eval(source) or { expected_error = err; unspecified() } |
| 57 | plain.options = Options{...plain.options, max_steps: 0} |
| 58 | expected_target := write(plain.lookup('target')!)! |
| 59 | mut actual_error := ?IError(none) |
| 60 | actual := bounded.run(fn [source, budget] (mut vm VM) !Value { |
| 61 | vm.options = Options{...vm.options, max_steps: 0} |
| 62 | vm.eval('(define target (vector 0))')! |
| 63 | vm.options = Options{...vm.options, max_steps: budget} |
| 64 | return vm.eval(source) |
| 65 | }) or { actual_error = err; unspecified() } |
| 66 | if wanted := expected_error { |
| 67 | got := actual_error or { panic('bounded dispatcher accepted a denied primitive call') } |
| 68 | assert wanted.msg() == got.msg() |
| 69 | } else { |
| 70 | assert actual_error == none |
| 71 | assert write(actual)! == write(expected)! |
| 72 | } |
| 73 | actual_target := bounded.run(fn (mut vm VM) !Value { |
| 74 | vm.options = Options{...vm.options, max_steps: 0} |
| 75 | return vm.lookup('target') |
| 76 | })! |
| 77 | assert write(actual_target)! == expected_target |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |