| 1 | module scheme |
| 2 | |
| 3 | fn case_lambda_text(source string) string { |
| 4 | mut vm := new() |
| 5 | return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 6 | } |
| 7 | |
| 8 | fn test_case_lambda_selects_first_matching_fixed_or_rest_clause() { |
| 9 | for source, expected in { |
| 10 | '((case-lambda (() 42)))': '42' |
| 11 | '((case-lambda ((x) x) ((x y) (+ x y))) 42)': '42' |
| 12 | '((case-lambda ((x) x) ((x y) (+ x y))) 20 22)': '42' |
| 13 | '((case-lambda ((x . rest) (list x rest))) 1 2 3)': '(1 (2 3))' |
| 14 | '((case-lambda (args args)))': '()' |
| 15 | '((case-lambda (args args)) 1 2 3)': '(1 2 3)' |
| 16 | '((case-lambda (args 42) (() 0)))': '42' |
| 17 | '((case-lambda ((x) 42) ((y) 0)) 1)': '42' |
| 18 | '((case-lambda ((x . rest) 42) ((x y) 0)) 1 2)': '42' |
| 19 | '(apply (case-lambda ((x y) (+ x y))) (list 20 22))': '42' |
| 20 | '(procedure? (case-lambda))': '#t' |
| 21 | '(call-with-values (lambda () ((case-lambda (() (values 20 22))))) +)': '42' |
| 22 | '((case-lambda (() missing) ((x) x)) 42)': '42' |
| 23 | } { |
| 24 | assert case_lambda_text(source) == expected, source |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn test_case_lambda_clauses_capture_the_same_outer_locations_without_scope_shift() { |
| 29 | assert case_lambda_text('(define (counter start) |
| 30 | (case-lambda (() start) ((x) (set! start (+ start x)) start))) |
| 31 | (define a (counter 1)) (define b (counter 10)) |
| 32 | (list (a) (a 2) (a) (b 4) (b) (a))') == '(1 3 3 14 14 3)' |
| 33 | assert case_lambda_text('(let ((x 42)) |
| 34 | (((case-lambda (() (lambda () x))))))') == '42' |
| 35 | assert case_lambda_text('(let ((x 42)) |
| 36 | ((lambda () ((case-lambda (() x))))))') == '42' |
| 37 | assert case_lambda_text('(let ((case-lambda +)) (case-lambda 20 22))') == '42' |
| 38 | assert case_lambda_text('(define-syntax build (syntax-rules () ((_ x) (case-lambda (() x))))) |
| 39 | (let ((case-lambda #f) (x 42)) ((build x)))') == '42' |
| 40 | } |
| 41 | |
| 42 | fn test_case_lambda_errors_and_vile_frontend() { |
| 43 | for source in ['((case-lambda))', '((case-lambda ((x) x)))', '(case-lambda ())', |
| 44 | '(case-lambda ((x)))', '(case-lambda ((x x) x))', '(case-lambda ((1) 42))', |
| 45 | '(case-lambda ((x . x) x))', '(case-lambda (x . y))'] { |
| 46 | mut vm := new() |
| 47 | if _ := vm.eval('\n ${source}') { |
| 48 | assert false, source |
| 49 | } else { |
| 50 | assert err is EvalError, '${source}: ${err}' |
| 51 | if err is EvalError { |
| 52 | assert err.line == 2 |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | mut vm := new() |
| 57 | assert vm.eval_alt('f:=case-lambda(((),42),((x,),x),((x,y),x+y))\n[f(),f(42),f(20,22)]') or { panic(err) }.vector_items() or { panic(err) }.len == 3 |
| 58 | assert vm.eval_alt('f(20,22)') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 59 | } |
| 60 | |
| 61 | fn test_case_lambda_host_calls_gc_ownership_and_continuation_reentry() { |
| 62 | mut vm := new() |
| 63 | vm.eval('(define saved #f) (define f (let ((n 40)) |
| 64 | (case-lambda (() n) ((x) (+ n (call/cc (lambda (k) (set! saved k) x)))))))') or { panic(err) } |
| 65 | f := vm.lookup('f') or { panic(err) } |
| 66 | gc_collect() |
| 67 | assert vm.call(f, [integer(2)]) or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 68 | gc_collect() |
| 69 | assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(3)]) or { panic(err) }.as_integer() or { panic(err) } == 43 |
| 70 | assert vm.call(f, []) or { panic(err) }.as_integer() or { panic(err) } == 40 |
| 71 | mut other := new() |
| 72 | if _ := other.call(f, []) { |
| 73 | assert false |
| 74 | } else { |
| 75 | assert err.msg().contains('another VM') |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn test_case_lambda_mutual_tail_calls_do_not_add_wrapper_frames() { |
| 80 | mut vm := new_with_options(Options{ max_steps: 0, max_frames: 2 }) or { panic(err) } |
| 81 | assert vm.eval('(define f (case-lambda ((n) (f n 0)) |
| 82 | ((n acc) (if (= n 0) acc (f (- n 1) (+ n acc)))))) |
| 83 | (f 100000)') or { panic(err) }.as_integer() or { panic(err) } == 5000050000 |
| 84 | } |
| 85 | |