vile / scheme / io_catalog_test.v
89 lines · 85 sloc · 4.15 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn io_catalog_expect(value Value, expected string) {
4 actual := write(value) or { panic(err.msg()) }
5 if actual != expected { panic('expected ${expected}, got ${actual}') }
6}
7
8fn test_selected_io_custom_constructors_and_halfclose_exports_keep_pristine_identity() {
9 names := ['make-custom-binary-input-port', 'make-custom-binary-output-port',
10 'make-custom-binary-input/output-port', 'make-custom-textual-input-port',
11 'make-custom-textual-output-port', 'make-custom-textual-input/output-port', 'input-port-open?',
12 'output-port-open?', 'close-input-port', 'close-output-port']
13 mut vm := new()
14 mut originals := []Value{cap: names.len}
15 for name in names {
16 originals << vm.lookup(name) or { panic(err.msg()) }
17 vm.define(name, integer(0)) or { panic(err.msg()) }
18 }
19 vm.eval('(import (prefix (rnrs io ports) r:) (prefix (vile io ports) v:))') or { panic(err.msg()) }
20 for index, name in names {
21 for prefix in ['r:', 'v:'] {
22 imported := vm.lookup(prefix + name) or { panic(err.msg()) }
23 if !eqv(imported, originals[index]) {
24 panic('${prefix}${name}: lost pristine identity')
25 }
26 }
27 }
28 if _ := vm.eval('(set! r:make-custom-binary-output-port 0)') {
29 panic('imported constructor location must remain immutable')
30 }
31 io_catalog_expect(vm.eval('(let ((p (r:make-custom-binary-output-port "renamed"
32 (lambda (b s n) n) #f #f #f)))
33 (v:close-output-port p) (list (r:output-port? p) (r:output-port-open? p)))') or { panic(err.msg()) }, '(#t #f)')
34}
35
36fn test_selected_io_all_six_custom_constructors_execute_in_both_frontends() {
37 scheme_source := '(import (prefix (rnrs io ports) io:))
38 (define closes 0) (define (done) (set! closes (+ closes 1)))
39 (define (rx b s n) (bytevector-u8-set! b s 65) 1)
40 (define (rt b s n) (string-set! b s #\\A) 1)
41 (define (tx b s n) n)
42 (define bi (io:make-custom-binary-input-port "bi" rx #f #f done))
43 (define bo (io:make-custom-binary-output-port "bo" tx #f #f done))
44 (define bc (io:make-custom-binary-input/output-port "bc" rx tx #f #f done))
45 (define ti (io:make-custom-textual-input-port "ti" rt #f #f done))
46 (define to (io:make-custom-textual-output-port "to" tx #f #f done))
47 (define tc (io:make-custom-textual-input/output-port "tc" rt tx #f #f done))
48 (define result (list (io:get-u8 bi) (io:get-u8 bc)
49 (char->integer (io:get-char ti)) (char->integer (io:get-char tc))))
50 (io:put-u8 bo 65) (io:put-u8 bc 65) (io:put-string to "λ") (io:put-char tc #\\A)
51 (for-each io:close-port (list bi bo bc ti to tc)) (append result (list closes))'
52 vile_source := 'import(prefix((vile,io,ports),io:))
53 closes := 0
54 done() := { closes = closes + 1 }
55 rx(b,s,n) := { bytevector-u8-set!(b,s,65), 1 }
56 rt(b,s,n) := { string-set!(b,s,#\\A), 1 }
57 tx(b,s,n) := n
58 bi := io:make-custom-binary-input-port("bi",rx,#f,#f,done)
59 bo := io:make-custom-binary-output-port("bo",tx,#f,#f,done)
60 bc := |io:make-custom-binary-input/output-port|("bc",rx,tx,#f,#f,done)
61 ti := io:make-custom-textual-input-port("ti",rt,#f,#f,done)
62 to := io:make-custom-textual-output-port("to",tx,#f,#f,done)
63 tc := |io:make-custom-textual-input/output-port|("tc",rt,tx,#f,#f,done)
64 result := list(io:get-u8(bi),io:get-u8(bc),char->integer(io:get-char(ti)),char->integer(io:get-char(tc)))
65 io:put-u8(bo,65)
66 io:put-u8(bc,65)
67 io:put-string(to,"λ")
68 io:put-char(tc,#\\A)
69 for-each(io:close-port,list(bi,bo,bc,ti,to,tc))
70 append(result,list(closes))'
71 mut scheme_vm := new()
72 io_catalog_expect(scheme_vm.eval(scheme_source) or { panic(err.msg()) }, '(65 65 65 65 6)')
73 mut vile_vm := new()
74 io_catalog_expect(vile_vm.eval_alt(vile_source) or { panic(err.msg()) }, '(65 65 65 65 6)')
75}
76
77fn test_selected_io_renamed_constructor_reexports_use_r7rs_library_namespaces() {
78 mut vm := new()
79 value := vm.eval('(define-library (example custom-output)
80 (export run)
81 (import (scheme base) (rename (only (rnrs io ports) make-custom-binary-output-port put-u8)
82 (make-custom-binary-output-port create) (put-u8 send)))
83 (begin (define (run)
84 (let ((count 0))
85 (let ((p (create "private" (lambda (b s n) (set! count (+ count n)) n) #f #f #f)))
86 (send p 65) (send p 66) (close-port p) count)))))
87 (import (example custom-output)) (run)') or { panic(err.msg()) }
88 io_catalog_expect(value, '2')
89}
90