| 1 |
;;; |
| 2 |
;;; primitive syntax test |
| 3 |
;;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
|
| 7 |
(test-start "primitive syntax") |
| 8 |
|
| 9 |
;; We use prim-test instead of test, for error-handler is not tested yet. |
| 10 |
;; NB: errsyn.scm has tests for invalid syntax. It is run after |
| 11 |
;; error-handler and macro tests. |
| 12 |
|
| 13 |
;;---------------------------------------------------------------- |
| 14 |
(test-section "conditionals") |
| 15 |
|
| 16 |
(prim-test "if" 5 (lambda () (if #f 2 5))) |
| 17 |
(prim-test "if" 2 (lambda () (if (not #f) 2 5))) |
| 18 |
|
| 19 |
(prim-test "and" #t (lambda () (and))) |
| 20 |
(prim-test "and" 5 (lambda () (and 5))) |
| 21 |
(prim-test "and" #f (lambda () (and 5 #f 2))) |
| 22 |
(prim-test "and" #f (lambda () (and 5 #f unbound-var))) |
| 23 |
(prim-test "and" 'a (lambda () (and 3 4 'a))) |
| 24 |
|
| 25 |
(prim-test "or" #f (lambda () (or))) |
| 26 |
(prim-test "or" 3 (lambda () (or 3 9))) |
| 27 |
(prim-test "or" 3 (lambda () (or #f 3 unbound-var))) |
| 28 |
|
| 29 |
(prim-test "when" 4 (lambda () (when 3 5 4))) |
| 30 |
(prim-test "when" (undefined) (lambda () (when #f 5 4))) |
| 31 |
(prim-test "when" (undefined) (lambda () (when #f unbound-var))) |
| 32 |
(prim-test "unless" (undefined) (lambda () (unless 3 5 4))) |
| 33 |
(prim-test "unless" (undefined) (lambda () (unless #t unbound-var))) |
| 34 |
(prim-test "unless" 4 (lambda () (unless #f 5 4))) |
| 35 |
|
| 36 |
(prim-test "cond" (undefined) (lambda () (cond (#f 2)))) |
| 37 |
(prim-test "cond" 5 (lambda () (cond (#f 2) (else 5)))) |
| 38 |
(prim-test "cond" 2 (lambda () (cond (1 2) (else 5)))) |
| 39 |
(prim-test "cond" 8 (lambda () (cond (#f 2) (1 8) (else 5)))) |
| 40 |
(prim-test "cond" 3 (lambda () (cond (1 => (lambda (x) (+ x 2))) (else 8)))) |
| 41 |
(prim-test "cond (SRFI-61)" 1 (lambda () (cond (1 number? => values) (else 8)))) |
| 42 |
(prim-test "cond (SRFI-61)" 8 (lambda () (cond (1 string? => values) (else 8)))) |
| 43 |
(prim-test "cond (SRFI-61)" '(1 2) |
| 44 |
(lambda () (cond ((values 1 2) |
| 45 |
(lambda (x y) (and (= x 1) (= y 2))) |
| 46 |
=> list)))) |
| 47 |
|
| 48 |
(prim-test "case" #t (lambda () (case (+ 2 3) ((1 3 5 7 9) #t) ((0 2 4 6 8) #f)))) |
| 49 |
(prim-test "case" #t (lambda () (undefined? (case 1 ((2 3) #t))))) |
| 50 |
(prim-test "case" #t (lambda () (case 1 (() #f) ((1) #t)))) |
| 51 |
(prim-test "case" #t (lambda () (case 1 (() #f) (else #t)))) |
| 52 |
(prim-test "case" #t (lambda () (undefined? (case 1 (() #t))))) |
| 53 |
(prim-test "case (SRFI-87)" 0 (lambda () (case (+ 2 3) ((1 3 5) 0) (else => values)))) |
| 54 |
(prim-test "case (SRFI-87)" 6 (lambda () (case (+ 2 3) ((1 3 5) => (cut + 1 <>)) (else => values)))) |
| 55 |
(prim-test "case (SRFI-87)" 5 (lambda () (case (+ 2 3) ((2 4 6) 0) (else => values)))) |
| 56 |
|
| 57 |
;;---------------------------------------------------------------- |
| 58 |
(test-section "binding") |
| 59 |
|
| 60 |
(prim-test "let" 35 |
| 61 |
(lambda () |
| 62 |
(let ((x 2) (y 3)) |
| 63 |
(let ((x 7) (z (+ x y))) |
| 64 |
(* z x))))) |
| 65 |
(prim-test "let*" 70 |
| 66 |
(lambda () |
| 67 |
(let ((x 2) (y 3)) |
| 68 |
(let* ((x 7) (z (+ x y))) |
| 69 |
(* z x))))) |
| 70 |
(prim-test "let*" 2 |
| 71 |
(lambda () |
| 72 |
(let* ((x 1) (x (+ x 1))) x))) |
| 73 |
|
| 74 |
(prim-test "named let" -3 |
| 75 |
(lambda () |
| 76 |
(let ((f -)) |
| 77 |
(let f ((a (f 3))) |
| 78 |
a)))) |
| 79 |
|
| 80 |
;;---------------------------------------------------------------- |
| 81 |
(test-section "closure and saved env") |
| 82 |
|
| 83 |
(prim-test "lambda" 5 (lambda () ((lambda (x) (car x)) '(5 6 7)))) |
| 84 |
(prim-test "lambda" 12 |
| 85 |
(lambda () |
| 86 |
((lambda (x y) |
| 87 |
((lambda (z) (* (car z) (cdr z))) (cons x y))) 3 4))) |
| 88 |
|
| 89 |
(define (addN n) (lambda (a) (+ a n))) |
| 90 |
(prim-test "lambda" 5 (lambda () ((addN 2) 3))) |
| 91 |
(define add3 (addN 3)) |
| 92 |
(prim-test "lambda" 9 (lambda () (add3 6))) |
| 93 |
|
| 94 |
(define count (let ((c 0)) (lambda () (set! c (+ c 1)) c))) |
| 95 |
(prim-test "lambda" 1 (lambda () (count))) |
| 96 |
(prim-test "lambda" 2 (lambda () (count))) |
| 97 |
|
| 98 |
;;---------------------------------------------------------------- |
| 99 |
(test-section "application") |
| 100 |
|
| 101 |
(define Apply apply) ; avoid inline expansion |
| 102 |
|
| 103 |
(prim-test "apply" '(1 2 3) (lambda () (Apply list '(1 2 3)))) |
| 104 |
(prim-test "apply" '(2 3 4) (lambda () (Apply list 2 '(3 4)))) |
| 105 |
(prim-test "apply" '(3 4 5) (lambda () (Apply list 3 4 '(5)))) |
| 106 |
(prim-test "apply" '(4 5 6) (lambda () (Apply list 4 5 6 '()))) |
| 107 |
|
| 108 |
(prim-test "apply^2" '() (lambda () (Apply Apply list '() '()))) |
| 109 |
(prim-test "Apply^2" '() (lambda () (Apply Apply list '(())))) |
| 110 |
(prim-test "apply^2" '(1 . 2) (lambda () (Apply Apply cons '((1 2))))) |
| 111 |
(prim-test "apply^2" '(3 . 4) (lambda () (Apply Apply cons 3 '((4))))) |
| 112 |
(prim-test "apply^2" '(5 . 6) (lambda () (Apply Apply (list cons 5 '(6))))) |
| 113 |
|
| 114 |
|
| 115 |
(prim-test "apply" '(6 7 8) (lambda () (Apply Apply (list list 6 7 '(8))))) |
| 116 |
|
| 117 |
|
| 118 |
;; This tests 'unfolding' path in ADJUST_ARGUMENT_FRAME. |
| 119 |
(prim-test "apply, copying args" '(1 2 3) |
| 120 |
(lambda () |
| 121 |
(let ((orig (list 1 2 3))) |
| 122 |
(let ((new (Apply list orig))) |
| 123 |
(set-car! (cdr new) '100) |
| 124 |
orig)))) |
| 125 |
|
| 126 |
;; This tests 'folding' path in ADJUST_ARGUMENT_FRAME |
| 127 |
(prim-test "apply, copying args" '(2 3) |
| 128 |
(lambda () |
| 129 |
(let ((orig (list 2 3))) |
| 130 |
(let ((new (Apply list 1 orig))) |
| 131 |
(set-car! (cdr new) '100) |
| 132 |
orig)))) |
| 133 |
|
| 134 |
;; Detect circular list in the argument |
| 135 |
;; https://github.com/shirok/Gauche/issues/684 |
| 136 |
;; NB: At this point, we haven't tested #0= reader notation, |
| 137 |
;; and to avoid optimization, these data should be in global space. |
| 138 |
(define *apply-circular-data-1* |
| 139 |
(let ((x (list 'a))) |
| 140 |
(set-cdr! x x) |
| 141 |
x)) |
| 142 |
(define *apply-circular-data-2* |
| 143 |
(let ((x (list 'a 'a))) |
| 144 |
(set-cdr! (cdr x) x) |
| 145 |
x)) |
| 146 |
|
| 147 |
(prim-test "apply, circular list 1" |
| 148 |
"improper list not allowed: #0=(a . #0#)" |
| 149 |
(lambda () |
| 150 |
(with-error-handler |
| 151 |
(lambda (e) (slot-ref e 'message)) |
| 152 |
(lambda () |
| 153 |
(apply list *apply-circular-data-1*))))) |
| 154 |
(prim-test "apply, circular list 2" |
| 155 |
"improper list not allowed: #0=(a . #0#)" |
| 156 |
(lambda () |
| 157 |
(with-error-handler |
| 158 |
(lambda (e) (slot-ref e 'message)) |
| 159 |
(lambda () |
| 160 |
(apply list 'a *apply-circular-data-1*))))) |
| 161 |
(prim-test "apply, circular list 3" |
| 162 |
"improper list not allowed: #0=(a a . #0#)" |
| 163 |
(lambda () |
| 164 |
(with-error-handler |
| 165 |
(lambda (e) (slot-ref e 'message)) |
| 166 |
(lambda () |
| 167 |
(apply list 'a *apply-circular-data-2*))))) |
| 168 |
(prim-test "apply, circular list 4" |
| 169 |
"improper list not allowed: #0=(a a . #0#)" |
| 170 |
(lambda () |
| 171 |
(with-error-handler |
| 172 |
(lambda (e) (slot-ref e 'message)) |
| 173 |
(lambda () |
| 174 |
;; This is caught in different place (#<subr apply>), |
| 175 |
;; rather than VM APPLY instruction. |
| 176 |
(Apply list 'a *apply-circular-data-2*))))) |
| 177 |
|
| 178 |
;; This test exhibits the optimizer bug reported by Michael Campbell. |
| 179 |
(define bug-optimizer-local-inliner |
| 180 |
(lambda (flag) |
| 181 |
(define (a . args) |
| 182 |
(receive x args |
| 183 |
(cons x x) |
| 184 |
(Apply values x)) |
| 185 |
(Apply format args)) |
| 186 |
(define (b bar) |
| 187 |
(a "~a" bar)) |
| 188 |
(b 1) |
| 189 |
(cond |
| 190 |
(flag (b 1)) |
| 191 |
(else (a "~a" 1))))) |
| 192 |
(prim-test "apply local inliner optimizer" "1" |
| 193 |
(lambda () (bug-optimizer-local-inliner #f)) equal?) |
| 194 |
(prim-test "apply local inliner optimizer" "1" |
| 195 |
(lambda () (bug-optimizer-local-inliner #t)) equal?) |
| 196 |
|
| 197 |
(prim-test "map" '() (lambda () (map car '()))) |
| 198 |
(prim-test "map" '(1 2 3) (lambda () (map car '((1) (2) (3))))) |
| 199 |
(prim-test "map" '(() () ()) (lambda () (map cdr '((1) (2) (3))))) |
| 200 |
(prim-test "map" '((1 . 4) (2 . 5) (3 . 6)) (lambda () (map cons '(1 2 3) '(4 5 6)))) |
| 201 |
|
| 202 |
;;---------------------------------------------------------------- |
| 203 |
(test-section "loop") |
| 204 |
|
| 205 |
(define (fact-non-tail-rec n) |
| 206 |
(if (<= n 1) n (* n (fact-non-tail-rec (- n 1))))) |
| 207 |
(prim-test "loop non-tail-rec" 120 (lambda () (fact-non-tail-rec 5))) |
| 208 |
|
| 209 |
(define (fact-tail-rec n r) |
| 210 |
(if (<= n 1) r (fact-tail-rec (- n 1) (* n r)))) |
| 211 |
(prim-test "loop tail-rec" 120 (lambda () (fact-tail-rec 5 1))) |
| 212 |
|
| 213 |
(define (fact-named-let n) |
| 214 |
(let loop ((n n) (r 1)) (if (<= n 1) r (loop (- n 1) (* n r))))) |
| 215 |
(prim-test "loop named-let" 120 (lambda () (fact-named-let 5))) |
| 216 |
|
| 217 |
(define (fact-int-define n) |
| 218 |
(define (rec n r) (if (<= n 1) r (rec (- n 1) (* n r)))) |
| 219 |
(rec n 1)) |
| 220 |
(prim-test "loop int-define" 120 (lambda () (fact-int-define 5))) |
| 221 |
|
| 222 |
(define (fact-do n) |
| 223 |
(do ((n n (- n 1)) (r 1 (* n r))) ((<= n 1) r))) |
| 224 |
(prim-test "loop do" 120 (lambda () (fact-do 5))) |
| 225 |
|
| 226 |
;; tricky case |
| 227 |
(prim-test "do" #f (lambda () (do () (#t #f) #t))) |
| 228 |
|
| 229 |
;;---------------------------------------------------------------- |
| 230 |
(test-section "quasiquote") |
| 231 |
|
| 232 |
;; The new compiler generates constant list for much wider |
| 233 |
;; range of quasiquoted forms (e.g. constant numerical expressions |
| 234 |
;; and constant variable definitions are folded at the compile time). |
| 235 |
|
| 236 |
(define-constant quasi0 99) |
| 237 |
(define quasi1 101) |
| 238 |
(define-constant quasi2 '(a b)) |
| 239 |
(define quasi3 '(c d)) |
| 240 |
|
| 241 |
(prim-test "qq" '(1 2 3) (lambda () `(1 2 3))) |
| 242 |
(prim-test "qq" '() (lambda () `())) |
| 243 |
(prim-test "qq" 99 (lambda () `,quasi0)) |
| 244 |
(prim-test "qq" 101 (lambda () `,quasi1)) |
| 245 |
(prim-test "qq," '((1 . 2)) (lambda () `(,(cons 1 2)))) |
| 246 |
(prim-test "qq," '((1 . 2) 3) (lambda () `(,(cons 1 2) 3))) |
| 247 |
(prim-test "qq," '(0 (1 . 2)) (lambda () `(0 ,(cons 1 2)))) |
| 248 |
(prim-test "qq," '(0 (1 . 2) 3) (lambda () `(0 ,(cons 1 2) 3))) |
| 249 |
(prim-test "qq," '(((1 . 2))) (lambda () `((,(cons 1 2))))) |
| 250 |
(prim-test "qq," '(((1 . 2)) 3) (lambda () `((,(cons 1 2)) 3))) |
| 251 |
(prim-test "qq," '(99 3) (lambda () `(,quasi0 3))) |
| 252 |
(prim-test "qq," '(3 99) (lambda () `(3 ,quasi0))) |
| 253 |
(prim-test "qq," '(3 99 3) (lambda () `(3 ,quasi0 3))) |
| 254 |
(prim-test "qq," '(100 3) (lambda () `(,(+ quasi0 1) 3))) |
| 255 |
(prim-test "qq," '(3 100) (lambda () `(3 ,(+ quasi0 1)))) |
| 256 |
(prim-test "qq," '(101 3) (lambda () `(,quasi1 3))) |
| 257 |
(prim-test "qq," '(3 101) (lambda () `(3 ,quasi1))) |
| 258 |
(prim-test "qq," '(102 3) (lambda () `(,(+ quasi1 1) 3))) |
| 259 |
(prim-test "qq," '(3 102) (lambda () `(3 ,(+ quasi1 1)))) |
| 260 |
(prim-test "qq,(r6rs)" '(98 99 (a b) 100) |
| 261 |
(lambda () `(98 (unquote quasi0 quasi2) 100))) |
| 262 |
(prim-test "qq,(r6rs)" '(98 99 101 100) |
| 263 |
(lambda () `(98 (unquote quasi0 quasi1) 100))) |
| 264 |
(prim-test "qq,(r6rs)" '(98 99 (a b) 100) |
| 265 |
(lambda () `(98 (unquote quasi0 quasi2) 100))) |
| 266 |
(prim-test "qq,(r6rs)" '(98 99 (a b) (1 2) (3 4)) |
| 267 |
(lambda () `(98 (unquote quasi0 quasi2) (unquote (list 1 2) (list 3 4))))) |
| 268 |
(prim-test "qq@" '(1 2 3 4) (lambda () `(1 ,@(list 2 3) 4))) |
| 269 |
(prim-test "qq@" '(1 2 3 4) (lambda () `(1 2 ,@(list 3 4)))) |
| 270 |
(prim-test "qq@" '(a b c d) (lambda () `(,@quasi2 ,@quasi3))) |
| 271 |
(prim-test "qq@(r6rs)" '(1 a b a b 2) |
| 272 |
(lambda () `(1 (unquote-splicing quasi2 quasi2) 2))) |
| 273 |
(prim-test "qq@(r6rs)" '(1 a b c d 2) |
| 274 |
(lambda () `(1 (unquote-splicing quasi2 quasi3) 2))) |
| 275 |
(prim-test "qq@(r6rs)" '(1 a b c d 2) |
| 276 |
(lambda () `(1 (unquote-splicing (list 'a 'b) '(c d)) ,@(list 2)))) |
| 277 |
(prim-test "qq." '(1 2 3 4) (lambda () `(1 2 . ,(list 3 4)))) |
| 278 |
(prim-test "qq." '(a b c d) (lambda () `(,@quasi2 . ,quasi3))) |
| 279 |
(prim-test "qq#," '#((1 . 2) 3) (lambda () `#(,(cons 1 2) 3))) |
| 280 |
(prim-test "qq#," '#(99 3) (lambda () `#(,quasi0 3))) |
| 281 |
(prim-test "qq#," '#(100 3) (lambda () `#(,(+ quasi0 1) 3))) |
| 282 |
(prim-test "qq#," '#(3 101) (lambda () `#(3 ,quasi1))) |
| 283 |
(prim-test "qq#," '#(3 102) (lambda () `#(3 ,(+ quasi1 1)))) |
| 284 |
(prim-test "qq#@" '#(1 2 3 4) (lambda () `#(1 ,@(list 2 3) 4))) |
| 285 |
(prim-test "qq#@" '#(1 2 3 4) (lambda () `#(1 2 ,@(list 3 4)))) |
| 286 |
(prim-test "qq#@" '#(a b c d) (lambda () `#(,@quasi2 ,@quasi3))) |
| 287 |
(prim-test "qq#@" '#(a b (c d)) (lambda () `#(,@quasi2 ,quasi3))) |
| 288 |
(prim-test "qq#@" '#((a b) c d) (lambda () `#(,quasi2 ,@quasi3))) |
| 289 |
(prim-test "qq#" '#() (lambda () `#())) |
| 290 |
(prim-test "qq#@" '#() (lambda () `#(,@(list)))) |
| 291 |
|
| 292 |
(prim-test "qq@@" '(1 2 1 2) (lambda () `(,@(list 1 2) ,@(list 1 2)))) |
| 293 |
(prim-test "qq@@" '(1 2 a 1 2) (lambda () `(,@(list 1 2) a ,@(list 1 2)))) |
| 294 |
(prim-test "qq@@" '(a 1 2 1 2) (lambda () `(a ,@(list 1 2) ,@(list 1 2)))) |
| 295 |
(prim-test "qq@@" '(1 2 1 2 a) (lambda () `(,@(list 1 2) ,@(list 1 2) a))) |
| 296 |
(prim-test "qq@@" '(1 2 1 2 a b) (lambda () `(,@(list 1 2) ,@(list 1 2) a b))) |
| 297 |
(prim-test "qq@." '(1 2 1 2 . a) |
| 298 |
(lambda () `(,@(list 1 2) ,@(list 1 2) . a))) |
| 299 |
(prim-test "qq@." '(1 2 1 2 1 . 2) |
| 300 |
(lambda () `(,@(list 1 2) ,@(list 1 2) . ,(cons 1 2)))) |
| 301 |
(prim-test "qq@." '(1 2 1 2 a b) |
| 302 |
(lambda () `(,@(list 1 2) ,@(list 1 2) . ,quasi2))) |
| 303 |
(prim-test "qq@." '(1 2 1 2 a 1 . 2) |
| 304 |
(lambda () `(,@(list 1 2) ,@(list 1 2) a . ,(cons 1 2)))) |
| 305 |
(prim-test "qq@." '(1 2 1 2 a c d) |
| 306 |
(lambda () `(,@(list 1 2) ,@(list 1 2) a . ,quasi3))) |
| 307 |
|
| 308 |
(prim-test "qq#@@" '#(1 2 1 2) (lambda () `#(,@(list 1 2) ,@(list 1 2)))) |
| 309 |
(prim-test "qq#@@" '#(1 2 a 1 2) (lambda () `#(,@(list 1 2) a ,@(list 1 2)))) |
| 310 |
(prim-test "qq#@@" '#(a 1 2 1 2) (lambda () `#(a ,@(list 1 2) ,@(list 1 2)))) |
| 311 |
(prim-test "qq#@@" '#(1 2 1 2 a) (lambda () `#(,@(list 1 2) ,@(list 1 2) a))) |
| 312 |
(prim-test "qq#@@" '#(1 2 1 2 a b) (lambda () `#(,@(list 1 2) ,@(list 1 2) a b))) |
| 313 |
|
| 314 |
(prim-test "qqq" '(1 `(1 ,2 ,3) 1) |
| 315 |
(lambda () `(1 `(1 ,2 ,,(+ 1 2)) 1))) |
| 316 |
(prim-test "qqq" '(1 `(1 ,99 ,101) 1) |
| 317 |
(lambda () `(1 `(1 ,,quasi0 ,,quasi1) 1))) |
| 318 |
(prim-test "qqq" '(1 `(1 ,@2 ,@(1 2))) |
| 319 |
(lambda () `(1 `(1 ,@2 ,@,(list 1 2))))) |
| 320 |
(prim-test "qqq" '(1 `(1 ,@2 (unquote 1 2))) |
| 321 |
(lambda () `(1 `(1 ,@2 ,,@(list 1 2))))) |
| 322 |
(prim-test "qqq" '(1 `(1 ,@2 (unquote-splicing 1 2))) |
| 323 |
(lambda () `(1 `(1 ,@2 ,@,@(list 1 2))))) |
| 324 |
(prim-test "qqq" '(1 `(1 ,@(a b) ,@(c d))) |
| 325 |
(lambda () `(1 `(1 ,@,quasi2 ,@,quasi3)))) |
| 326 |
(prim-test "qqq" '(1 `(1 ,(a b x) ,(y c d))) |
| 327 |
(lambda () `(1 `(1 ,(,@quasi2 x) ,(y ,@quasi3))))) |
| 328 |
(prim-test "qqq#" '#(1 `(1 ,2 ,3) 1) |
| 329 |
(lambda () `#(1 `(1 ,2 ,,(+ 1 2)) 1))) |
| 330 |
(prim-test "qqq#" '#(1 `(1 ,99 ,101) 1) |
| 331 |
(lambda () `#(1 `(1 ,,quasi0 ,,quasi1) 1))) |
| 332 |
(prim-test "qqq#" '#(1 `(1 ,@2 ,@(1 2))) |
| 333 |
(lambda () `#(1 `(1 ,@2 ,@,(list 1 2))))) |
| 334 |
(prim-test "qqq#" '#(1 `(1 ,@(a b) ,@(c d))) |
| 335 |
(lambda () `#(1 `(1 ,@,quasi2 ,@,quasi3)))) |
| 336 |
(prim-test "qqq#" '#(1 `(1 ,(a b x) ,(y c d))) |
| 337 |
(lambda () `#(1 `(1 ,(,@quasi2 x) ,(y ,@quasi3))))) |
| 338 |
(prim-test "qqq#" '(1 `#(1 ,(a b x) ,(y c d))) |
| 339 |
(lambda () `(1 `#(1 ,(,@quasi2 x) ,(y ,@quasi3))))) |
| 340 |
|
| 341 |
(prim-test "qq-hygiene 0" '(2 1) |
| 342 |
(lambda () (let ((quasiquote reverse)) `(list 1 2)))) |
| 343 |
(prim-test "qq-hygiene 1" '(,(+ 1 2)) |
| 344 |
(lambda () (let ((unquote 3)) `(,(+ 1 2))))) |
| 345 |
(prim-test "qq-hygiene 2" '(,@(+ 1 2)) |
| 346 |
(lambda () (let ((unquote-splicing 3)) `(,@(+ 1 2))))) |
| 347 |
|
| 348 |
;;---------------------------------------------------------------- |
| 349 |
(test-section "multiple values") |
| 350 |
|
| 351 |
(prim-test "receive" '(1 2 3) |
| 352 |
(lambda () (receive (a b c) (values 1 2 3) (list a b c)))) |
| 353 |
(prim-test "receive" '(1 2 3) |
| 354 |
(lambda () (receive (a . r) (values 1 2 3) (cons a r)))) |
| 355 |
(prim-test "receive" '(1 2 3) |
| 356 |
(lambda () (receive x (values 1 2 3) x))) |
| 357 |
(prim-test "receive" 1 |
| 358 |
(lambda () (receive (a) 1 a))) |
| 359 |
(prim-test "call-with-values" '(1 2 3) |
| 360 |
(lambda () (call-with-values (lambda () (values 1 2 3)) list))) |
| 361 |
(prim-test "call-with-values" '() |
| 362 |
(lambda () (call-with-values (lambda () (values)) list))) |
| 363 |
|
| 364 |
;; This is not 'right' in R5RS sense---for now, I just tolerate it |
| 365 |
;; by CommonLisp way, i.e. if more than one value is passed to an |
| 366 |
;; implicit continuation that expects one value, the second and after |
| 367 |
;; values are just discarded. This behavior may be changed later, |
| 368 |
;; so do not count on it. The test just make sure it doesn't screw |
| 369 |
;; up anything. |
| 370 |
(prim-test "receive" '((0 0)) |
| 371 |
(lambda () (receive l (list 0 (values 0 1 2)) l))) |
| 372 |
|
| 373 |
;;---------------------------------------------------------------- |
| 374 |
(test-section "eval") |
| 375 |
|
| 376 |
(prim-test "eval" '(1 . 2) |
| 377 |
(lambda () (eval '(cons 1 2) (interaction-environment)))) |
| 378 |
|
| 379 |
(define (vector-ref x y) 'foo) |
| 380 |
|
| 381 |
(prim-test "eval" '(foo foo 3) |
| 382 |
(lambda () |
| 383 |
(list (vector-ref '#(3) 0) |
| 384 |
(eval '(vector-ref '#(3) 0) (interaction-environment)) |
| 385 |
(eval '(vector-ref '#(3) 0) (scheme-report-environment 5))))) |
| 386 |
|
| 387 |
(define vector-ref (with-module scheme vector-ref)) |
| 388 |
|
| 389 |
(prim-test "eval" #t |
| 390 |
(lambda () |
| 391 |
(with-error-handler |
| 392 |
(lambda (e) #t) |
| 393 |
(lambda () (eval '(car '(3 2)) (null-environment 5)))))) |
| 394 |
|
| 395 |
;; check interaction w/ modules |
| 396 |
(define-module primsyn.test (define foo 'a)) |
| 397 |
(define foo '(x y)) |
| 398 |
|
| 399 |
(prim-test "eval (module)" '(a b (x y)) |
| 400 |
(lambda () |
| 401 |
(let* ((m (find-module 'primsyn.test)) |
| 402 |
(a (eval 'foo m)) |
| 403 |
(b (eval '(begin (set! foo 'b) foo) m))) |
| 404 |
(list a b foo)))) |
| 405 |
|
| 406 |
(prim-test "eval (module)" '(x y) |
| 407 |
(lambda () |
| 408 |
(with-error-handler |
| 409 |
(lambda (e) foo) |
| 410 |
(lambda () |
| 411 |
(eval '(Apply car foo '()) (find-module 'primsyn.test)))))) |
| 412 |
|
| 413 |
;;---------------------------------------------------------------- |
| 414 |
(test-section "max literal arguments") |
| 415 |
|
| 416 |
;; Fix this after we have separate compile-error condition. |
| 417 |
(define (test-max-literal-args msg expr) |
| 418 |
(prim-test (string-append "max literal arguments for " msg) |
| 419 |
'caught |
| 420 |
(lambda () |
| 421 |
(with-error-handler (lambda (e) 'caught) |
| 422 |
(lambda () (eval expr (interaction-environment))))))) |
| 423 |
|
| 424 |
(test-max-literal-args "inliner" `(list ,@(make-list 10000 #f))) |
| 425 |
(test-max-literal-args "global proc" `(make ,@(make-list 10000 #f))) |
| 426 |
(test-max-literal-args "local proc" |
| 427 |
`(let ((foo (lambda x x))) |
| 428 |
(foo ,@(make-list 10000 #f)))) |
| 429 |
|
| 430 |
;;---------------------------------------------------------------- |
| 431 |
(test-section "local procedure optimization") |
| 432 |
|
| 433 |
;; this caused an internal compiler error in 0.8.6. |
| 434 |
;; (found and fixed by Jun Inoue) |
| 435 |
(prim-test "internal-define inilining" '(1) |
| 436 |
(lambda () |
| 437 |
(with-error-handler |
| 438 |
(lambda (e) 'ouch!) |
| 439 |
(lambda () |
| 440 |
(eval '(let () |
| 441 |
(define (a x) x) |
| 442 |
(define (b x) (a x)) |
| 443 |
(define (c x) (b x)) |
| 444 |
(list 1)) |
| 445 |
(interaction-environment)))))) |
| 446 |
|
| 447 |
;; this caused an internal compiler error in 0.8.6 |
| 448 |
;; (found and fixed by Kazuki Tsujimoto) |
| 449 |
(prim-test "multiple inlining" 0 |
| 450 |
(lambda () |
| 451 |
(let ((f (lambda (i) (set! i 0) i))) (f (f 1))))) |
| 452 |
|
| 453 |
;; this caused an internal compiler error in 0.9.1 |
| 454 |
(define (zero) 0) |
| 455 |
(prim-test "pass3 inlining with pass3/$call optimization" #t |
| 456 |
(lambda () |
| 457 |
(eval '((letrec ((f (lambda (a b) |
| 458 |
(do ((x a (+ x 1))) |
| 459 |
((>= x b)))))) |
| 460 |
f) |
| 461 |
(zero) (zero)) |
| 462 |
(interaction-environment)))) |
| 463 |
|
| 464 |
;; This caused internal error in 0.9.1, and infinite loop in dev version |
| 465 |
;; after it. |
| 466 |
(prim-test "pass3/$call inlining problem" #t |
| 467 |
(lambda () |
| 468 |
(procedure? |
| 469 |
(eval '(lambda (n p t) |
| 470 |
(define (y a r s f) |
| 471 |
(let loop ([e 0]) |
| 472 |
(cond [(a n) (unwind-protect (s) (r n))] |
| 473 |
[(< e 10) (loop (+ 1 e))] |
| 474 |
[else (f)]))) |
| 475 |
(define (l0 a r) |
| 476 |
(y a r (^() (r n)) (^() (error "oo")))) |
| 477 |
;; Main locker |
| 478 |
(define (l1 a r) |
| 479 |
(y a r p (^() (if (and-let* ([ t ] |
| 480 |
[m (file-mtime n)]) |
| 481 |
(< (+ m t) 10)) |
| 482 |
(begin (l0 a r) (l1 a r)))))) |
| 483 |
(error "zz")) |
| 484 |
(interaction-environment))))) |
| 485 |
|
| 486 |
;;---------------------------------------------------------------- |
| 487 |
(test-section "optimized frames") |
| 488 |
|
| 489 |
;; Empty environment frame is omitted by compiler optimization. |
| 490 |
;; The following tests makes sure if it works correctly. |
| 491 |
|
| 492 |
(prim-test "lambda (empty env)" 1 |
| 493 |
(lambda () |
| 494 |
(let* ((a 1) |
| 495 |
(b (lambda () |
| 496 |
((lambda () a))))) |
| 497 |
(b)))) |
| 498 |
|
| 499 |
(prim-test "let (empty env)" 1 |
| 500 |
(lambda () |
| 501 |
(let ((a 1)) |
| 502 |
(let () |
| 503 |
(let () |
| 504 |
a))))) |
| 505 |
|
| 506 |
(prim-test "let (empty env)" '(1 . 1) |
| 507 |
(lambda () |
| 508 |
(let ((a 1)) |
| 509 |
(cons (let () (let () a)) |
| 510 |
(let* () (letrec () a)))))) |
| 511 |
|
| 512 |
(prim-test "let (empty env)" '(3 . 1) |
| 513 |
(lambda () |
| 514 |
(let ((a 1) |
| 515 |
(b 0)) |
| 516 |
(cons (let () (let () (set! b 3)) b) |
| 517 |
(let () (let () a)))))) |
| 518 |
|
| 519 |
(prim-test "named let (empty env)" 1 |
| 520 |
(lambda () |
| 521 |
(let ((a -1)) |
| 522 |
(let loop () |
| 523 |
(unless (positive? a) |
| 524 |
(set! a (+ a 1)) |
| 525 |
(loop))) |
| 526 |
a))) |
| 527 |
|
| 528 |
(prim-test "do (empty env)" 1 |
| 529 |
(lambda () (let ((a 0)) (do () ((positive? a) a) (set! a (+ a 1)))))) |
| 530 |
|
| 531 |
;;---------------------------------------------------------------- |
| 532 |
(test-section "hygienity") |
| 533 |
|
| 534 |
(prim-test "hygienity (named let)" 4 |
| 535 |
(lambda () |
| 536 |
(let ((lambda list)) |
| 537 |
(let loop ((x 0)) |
| 538 |
(if (> x 3) x (loop (+ x 1))))))) |
| 539 |
|
| 540 |
(prim-test "hygienity (internal defines)" 4 |
| 541 |
(lambda () |
| 542 |
(let ((lambda list)) |
| 543 |
(define (x) 4) |
| 544 |
(x)))) |
| 545 |
|
| 546 |
(prim-test "hygienity (do)" 4 |
| 547 |
(lambda () |
| 548 |
(let ((lambda #f) |
| 549 |
(begin #f) |
| 550 |
(if #f) |
| 551 |
(letrec #f)) |
| 552 |
(do ((x 0 (+ x 1))) |
| 553 |
((> x 3) x) |
| 554 |
#f)))) |
| 555 |
|
| 556 |
;;---------------------------------------------------------------- |
| 557 |
(test-section "letrec and letrec*") |
| 558 |
|
| 559 |
(prim-test "letrec reordering" '((1 3) . (2 3 1)) |
| 560 |
(lambda () |
| 561 |
(let ((r '())) |
| 562 |
(cons (letrec ((a (begin (set! r (cons 1 r)) 1)) |
| 563 |
(b (begin (set! r (cons 2 r)) 2)) |
| 564 |
(c (begin (set! r (cons 3 r)) 3))) |
| 565 |
(list a c)) |
| 566 |
r)))) |
| 567 |
|
| 568 |
(prim-test "letrec* non-reordering" '((1 3) . (3 2 1)) |
| 569 |
(lambda () |
| 570 |
(let ((r '())) |
| 571 |
(cons (letrec* ((a (begin (set! r (cons 1 r)) 1)) |
| 572 |
(b (begin (set! r (cons 2 r)) 2)) |
| 573 |
(c (begin (set! r (cons 3 r)) 3))) |
| 574 |
(list a c)) |
| 575 |
r)))) |
| 576 |
|
| 577 |
|
| 578 |
(test-end) |