vile / scheme / record_protocol_test.v
34 lines · 29 sloc · 2.04 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4
5fn protocol_text(mut vm VM, source string) string {
6 return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) }
7}
8
9fn test_record_protocol_three_levels_laziness_final_descendant_and_reused_partial() {
10 mut vm := new()
11 // The probe writes events; capture it using the ordinary output parameter.
12 program := os.read_file(@VMODROOT + '/scheme/testdata/records/segments-probe.scm') or { panic(err) }
13 assert protocol_text(mut vm, '(define out (open-output-string)) (parameterize ((current-output-port out)) ${program}) (get-output-string out)') == r'"(prepare-c)\n(prepare-c call-c (after-n-c #t) prepare-b call-b (after-n-b #t) prepare-a call-a (a-sees-final #t 20 30))\n(110 31 30)\n(#f 101 13 3 4)\n"'
14}
15
16fn test_record_protocol_multishot_before_and_after_allocation_have_correct_identity() {
17 mut vm := new()
18 program := os.read_file(@VMODROOT + '/scheme/testdata/records/continuation-probe.scm') or { panic(err) }
19 assert protocol_text(mut vm, '(define out (open-output-string)) (parameterize ((current-output-port out)) ${program}) (get-output-string out)') == r'"(#f #f #f (10 11 12) (20 20 20))\n(#t #t #t (13 13 13) (20 20 20))\n"'
20}
21
22fn test_record_protocol_variadic_recursive_and_implicit_default_parent() {
23 mut vm := new()
24 assert protocol_text(mut vm, "(define a (make-record-type-descriptor 'a #f #f #f #f '#((mutable x))))
25 (define b (make-record-type-descriptor 'b a #f #f #f '#((immutable y))))
26 (define ax (record-accessor a 0)) (define by (record-accessor b 0))
27 (define ac (make-record-constructor-descriptor a #f (lambda(p) (lambda xs
28 (let ((r (p (car xs)))) (if (> (car xs) 0) (make-b (- (car xs) 1) 99)) r)))))
29 (define bc (make-record-constructor-descriptor b ac (lambda(n) (lambda(x y) ((n x 'ignored) y)))))
30 (define make-b (record-constructor bc)) (define first (make-b 3 42))
31 (define direct (record-constructor (make-record-constructor-descriptor b #f (lambda(n)(lambda(x y)((n x)y))))))
32 (define second (direct 20 22))
33 (list (ax first) (by first) (ax second) (by second))") == '(3 42 20 22)'
34}
35