vile / scheme / conditional_test.v
116 lines · 109 sloc · 5.2 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4import time
5
6fn conditional_eval(source string) Value {
7 mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) }
8 return vm.eval(source) or { panic('${source}: ${err}') }
9}
10
11fn test_features_are_truthful_immutable_cached_and_available_in_standard_libraries() {
12 mut vm := new()
13 features := vm.eval('(features)') or { panic(err) }
14 mut current := features
15 mut seen := map[string]bool{}
16 for current.tag == .pair {
17 pair := current.pair_pointer()
18 assert pair.car.tag == .symbol
19 assert pair.car.text() !in seen
20 seen[pair.car.text()] = true
21 current = pair.cdr
22 }
23 assert current.tag == .nil && 'vile' in seen && 'ieee-float' in seen && 'exact-complex' in seen
24 for name in ['r7rs', 'r6rs', 'exact-closed', 'ratios'] {
25 assert name !in seen
26 }
27 assert vm.eval('(eq? (features) (features))') or { panic(err) }.is_true()
28 for source in ["(set-car! (features) 'forged)", "(set-cdr! (features) '())", '(features 1)'] {
29 if _ := vm.eval(source) {
30 assert false, source
31 } else {
32 assert err is EvalError
33 }
34 }
35 assert vm.eval('(import (only (scheme base) features)) (pair? (features))') or { panic(err) }.is_true()
36 vm.eval('(define features (lambda () (list (quote fake))))') or {
37 // The source import protects its local name; use an independent VM below.
38 assert err.msg().contains('imported binding')
39 }
40 mut other := new()
41 assert other.eval('(define features (lambda () #f)) (let ((vile #f)) (cond-expand (vile 42) (else 0)))') or { panic(err) }.whole() == 42
42}
43
44fn test_cond_expand_boolean_requirements_are_static_and_short_circuit() {
45 for source in [
46 '(cond-expand (vile 42) (else missing))',
47 '(cond-expand ((and vile ieee-float (not absent)) 42) (else 0))',
48 '(cond-expand ((or absent (and)) 42) (else 0))',
49 '(cond-expand ((or) missing) ((not (and)) missing) (else 42))',
50 '(let ((and #f) (not #f) (vile #f)) (cond-expand ((and vile (not absent)) 42)))',
51 '(cond-expand ((or vile (unknown bad)) 42) (else 0))',
52 '(cond-expand ((and absent (unknown bad)) missing) (else 42))',
53 '(call-with-values (lambda () (cond-expand (vile (values 20 22)))) +)',
54 '(cond-expand (absent (include "must-not-open.scm")) (vile 42))',
55 '(let () (cond-expand (vile (define answer 42))) answer)',
56 '(let ((cond-expand +)) (cond-expand 20 22))',
57 '(define-syntax select (syntax-rules () ((_) (cond-expand (vile 42))))) (select)',
58 ] {
59 assert conditional_eval(source).whole() == 42, source
60 }
61 assert conditional_eval('(cond-expand (absent missing))').tag == .unspecified
62 assert conditional_eval('(cond-expand (vile))').tag == .unspecified
63 assert conditional_eval('(cond-expand (vile (define answer 42))) answer').whole() == 42
64 assert conditional_eval('(cond-expand ((library (scheme base)) 42) (else 0))').whole() == 42
65 assert conditional_eval('(cond-expand ((library (scheme nonexistent)) 0) (else 42))').whole() == 42
66}
67
68fn test_cond_expand_rejects_malformed_requirements_and_recovers_the_vm() {
69 mut vm := new()
70 for source in ['(cond-expand)', '(cond-expand ())', '(cond-expand (else 1) (vile 2))',
71 '(cond-expand ((not) 1))', '(cond-expand ((not vile absent) 1))', '(cond-expand ((library) 1))',
72 '(cond-expand ((library ()) 1))', '(cond-expand ((unknown vile) 1))', '(cond-expand (#t 1))'] {
73 if _ := vm.eval(source) {
74 assert false, source
75 } else {
76 assert err is EvalError, '${source}: ${err}'
77 }
78 assert vm.eval('42') or { panic(err) }.whole() == 42
79 }
80 assert vm.eval_alt('cond-expand((vile,42),(else,0))') or { panic(err) }.whole() == 42
81}
82
83fn test_library_availability_does_not_register_or_initialize_discovered_user_libraries() {
84 root := os.join_path(os.temp_dir(), 'vile-feature-${os.getpid()}-${time.sys_mono_now()}')
85 os.mkdir_all(root) or { panic(err) }
86 defer { os.rmdir_all(root) or {} }
87 os.write_file(os.join_path(root, 'present.sld'), '(define-library (present) (export value)
88 (import (scheme base)) (begin (define value (/ 1 0))))') or { panic(err) }
89 path := os.join_path(root, 'main.scm')
90 os.write_file(path, '(cond-expand ((library (present)) 42) (else 0))') or { panic(err) }
91 mut vm := new()
92 assert vm.eval_file(path) or { panic(err) }.whole() == 42
93 if !isnil(vm.libraries) {
94 for _, library in vm.libraries.entries {
95 assert library.name.display != '(present)'
96 }
97 }
98 os.write_file(os.join_path(root, 'present.sld'), '(define-library (other))') or { panic(err) }
99 assert vm.eval_file(path) or { panic(err) }.whole() == 0
100 os.write_file(os.join_path(root, 'present.sld'), '(define-library (present)) (display "bad")') or { panic(err) }
101 assert vm.eval_file(path) or { panic(err) }.whole() == 0
102 assert vm.eval('(define-library (inline)) (cond-expand ((library (inline)) 42) (else 0))') or { panic(err) }.whole() == 42
103 for _, library in vm.libraries.entries {
104 if library.name.display == '(inline)' {
105 assert library.status == .registered
106 }
107 }
108}
109
110fn test_static_selection_handles_deep_requirements_and_preserves_tail_space() {
111 mut predicate := 'vile'
112 for _ in 0 .. 200 { predicate = '(not ${predicate})' }
113 assert conditional_eval('(cond-expand (${predicate} 42) (else 0))').whole() == 42
114 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 10 }) or { panic(err) }
115 assert vm.eval('(define (loop n) (cond-expand (vile (if (= n 0) 42 (loop (- n 1)))))) (loop 100000)') or { panic(err) }.whole() == 42
116}
117