| 1 |
;; |
| 2 |
;; test for procedures |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(test-start "procedures") |
| 7 |
|
| 8 |
;;------------------------------------------------------------------- |
| 9 |
(test-section "C-stack boundary crossing") |
| 10 |
|
| 11 |
(test* "apply-rec0" 'a |
| 12 |
((with-module gauche.internal %apply-rec0) (lambda () 'a))) |
| 13 |
(test* "apply-rec1" '(a . b) |
| 14 |
((with-module gauche.internal %apply-rec1) (lambda (x) (cons 'a x)) 'b)) |
| 15 |
(test* "apply-rec2" '(x y c) |
| 16 |
((with-module gauche.internal %apply-rec2) |
| 17 |
(lambda (a b) (list a b 'c)) 'x 'y)) |
| 18 |
(test* "apply-rec3" '(x y z c) |
| 19 |
((with-module gauche.internal %apply-rec3) |
| 20 |
(lambda (a b c) (list a b c 'c)) 'x 'y 'z)) |
| 21 |
(test* "apply-rec4" '(x y z w c) |
| 22 |
((with-module gauche.internal %apply-rec4) |
| 23 |
(lambda (a b c d) (list a b c d 'c)) 'x 'y 'z 'w)) |
| 24 |
(test* "apply-rec" '(x y z w u v c) |
| 25 |
((with-module gauche.internal %apply-rec) |
| 26 |
(lambda (a b c d e f) (list a b c d e f 'c)) 'x 'y 'z 'w 'u 'v)) |
| 27 |
|
| 28 |
;;------------------------------------------------------------------- |
| 29 |
(test-section "combinatorial programming utilities") |
| 30 |
|
| 31 |
(test* "pa$" 10 ((pa$ + 3) 7)) |
| 32 |
(test* "pa$" '(a b c) |
| 33 |
((pa$ list 'a) 'b 'c)) |
| 34 |
(test* "pa$" '(a b c) |
| 35 |
((pa$ list 'a 'b) 'c)) |
| 36 |
(test* "pa$" '(a b c) |
| 37 |
((pa$ (pa$ list 'a) 'b) 'c)) |
| 38 |
|
| 39 |
(test "map$" '(2 4 6) |
| 40 |
(lambda () |
| 41 |
(define map2* (map$ (pa$ * 2))) |
| 42 |
(map2* '(1 2 3)))) |
| 43 |
|
| 44 |
(test "compose" '(#t #f #t) |
| 45 |
(lambda () |
| 46 |
(define not-zero? (compose not zero?)) |
| 47 |
(list (not-zero? 3) |
| 48 |
(not-zero? 0) |
| 49 |
(not-zero? -100)))) |
| 50 |
|
| 51 |
(test "compose" 'a (lambda () ((compose car) '(a b c)))) |
| 52 |
(test "compose" '(a b c) (lambda () ((compose) '(a b c)))) |
| 53 |
|
| 54 |
(test "constantly" '(3 3 3) |
| 55 |
(lambda () |
| 56 |
(list ((constantly 3)) |
| 57 |
((constantly 3) 1) |
| 58 |
((constantly 3) 1 2)))) |
| 59 |
(test "constantly" '((1 2) (1 2) (1 2)) |
| 60 |
(lambda () |
| 61 |
(list (values->list ((constantly 1 2))) |
| 62 |
(values->list ((constantly 1 2) 3)) |
| 63 |
(values->list ((constantly 1 2) 3 4))))) |
| 64 |
|
| 65 |
(test "complement" '(#t #f #t) |
| 66 |
(lambda () (map (complement even?) '(1 2 3)))) |
| 67 |
(test "complement" '(#t #f #t) |
| 68 |
(lambda () (map (complement zero?) '(-1 0 1)))) |
| 69 |
(test "complement" '(#f #t #f) |
| 70 |
(lambda () (map (complement =) '(1 2 3) '(1 1 3)))) |
| 71 |
(test "complement" '(#f #t #f) |
| 72 |
(lambda () (map (complement (lambda (x y) (= x y))) '(1 2 3) '(1 1 3)))) |
| 73 |
(test "complement" #t |
| 74 |
(lambda () ((complement (lambda () #f))))) |
| 75 |
|
| 76 |
(test "flip" '(() (1) (2 1) (3 2 1) (4 3 2 1)) |
| 77 |
(lambda () |
| 78 |
(list ((flip list)) |
| 79 |
((flip list) 1) |
| 80 |
((flip list) 1 2) |
| 81 |
((flip list) 1 2 3) |
| 82 |
((flip list) 1 2 3 4)))) |
| 83 |
(test "swap" '((2 1) (2 1 3) (2 1 3 4)) |
| 84 |
(lambda () |
| 85 |
(list ((swap list) 1 2) |
| 86 |
((swap list) 1 2 3) |
| 87 |
((swap list) 1 2 3 4)))) |
| 88 |
|
| 89 |
(test "compose, apply$, map$" 32 |
| 90 |
(lambda () |
| 91 |
(define dot-product (compose (apply$ +) (map$ *))) |
| 92 |
(dot-product '(1 2 3) '(4 5 6)))) |
| 93 |
|
| 94 |
(test "any-pred" '(#t #t #f) |
| 95 |
(lambda () |
| 96 |
(define string-or-symbol? (any-pred string? symbol?)) |
| 97 |
(list (string-or-symbol? "abc") |
| 98 |
(string-or-symbol? 'abc) |
| 99 |
(string-or-symbol? 3)))) |
| 100 |
|
| 101 |
(test "any-pred" '(b c) |
| 102 |
(lambda () |
| 103 |
((any-pred (cut memq <> '(a b c)) |
| 104 |
(cut memq <> '(1 2 3))) |
| 105 |
'b))) |
| 106 |
|
| 107 |
(test "any-pred" '(#t #f) |
| 108 |
(lambda () |
| 109 |
(define <> (any-pred < >)) |
| 110 |
(list (<> 3 4) |
| 111 |
(<> 3 3)))) |
| 112 |
|
| 113 |
(test "every-pred" '(#t #f #f) |
| 114 |
(lambda () |
| 115 |
(list ((every-pred odd? positive?) 3) |
| 116 |
((every-pred odd? positive?) 4) |
| 117 |
((every-pred odd? positive?) -3)))) |
| 118 |
|
| 119 |
(test "every-pred" '(3 #f) |
| 120 |
(lambda () |
| 121 |
(define safe-length (every-pred list? length)) |
| 122 |
(list (safe-length '(a b c)) |
| 123 |
(safe-length "aaa")))) |
| 124 |
|
| 125 |
;;------------------------------------------------------------------- |
| 126 |
#| disabled for now. |
| 127 |
|
| 128 |
(test-section "currying") |
| 129 |
|
| 130 |
(define-curry (curried2 a b) (list a b)) |
| 131 |
(define-curry (curried2+ a b . c) (list a b c)) |
| 132 |
(define-curry (curried3 a b c) (list a b c)) |
| 133 |
(define-curry (curried3+ a b c . d) (list a b c d)) |
| 134 |
(define-curry (curried4 a b c d) (list a b c d)) |
| 135 |
(define-curry (curried4+ a b c d . e) (list a b c d e)) |
| 136 |
(define-curry (curried5 a b c d e) (list a b c d e)) |
| 137 |
(define-curry (curried5+ a b c d e . f) (list a b c d e f)) |
| 138 |
|
| 139 |
(define-macro (ctest expected . tests) |
| 140 |
`(begin |
| 141 |
,@(map (lambda (test) |
| 142 |
`(test* ,(write-to-string test) ,expected ,test)) |
| 143 |
tests))) |
| 144 |
|
| 145 |
(ctest '(1 2) (curried2 1 2) ((curried2 1) 2)) |
| 146 |
(ctest (test-error) ((curried2 1) 2 3) (curried2)) |
| 147 |
|
| 148 |
(ctest '(1 2) |
| 149 |
(apply curried2 '(1 2)) |
| 150 |
(apply curried2 1 '(2)) |
| 151 |
(apply curried2 1 2 '()) |
| 152 |
((apply curried2 '(1)) 2) |
| 153 |
((apply curried2 1 '()) 2) |
| 154 |
(apply (apply curried2 '(1)) '(2))) |
| 155 |
(ctest (test-error) (apply curried2 '())) |
| 156 |
|
| 157 |
(ctest '(1 2 ()) (curried2+ 1 2) ((curried2+ 1) 2)) |
| 158 |
(ctest '(1 2 (3 4)) (curried2+ 1 2 3 4) ((curried2+ 1) 2 3 4)) |
| 159 |
(ctest (test-error) ((curried2+ 1 2) 3 4)) |
| 160 |
|
| 161 |
(ctest '(1 2 ()) (apply curried2+ '(1 2)) ((apply curried2+ '(1)) 2)) |
| 162 |
(ctest '(1 2 (3 4)) |
| 163 |
(apply curried2+ '(1 2 3 4)) |
| 164 |
(apply curried2+ 1 '(2 3 4)) |
| 165 |
(apply curried2+ 1 2 '(3 4)) |
| 166 |
(apply curried2+ 1 2 3 '(4)) |
| 167 |
(apply curried2+ 1 2 3 4 '()) |
| 168 |
((apply curried2+ '(1)) 2 3 4) |
| 169 |
((apply curried2+ 1 '()) 2 3 4) |
| 170 |
(apply (apply curried2+ '(1)) '(2 3 4))) |
| 171 |
(ctest (test-error) (apply curried2+ '())) |
| 172 |
|
| 173 |
(ctest '(1 2 3) (curried3 1 2 3) ((curried3 1) 2 3) ((curried3 1 2) 3)) |
| 174 |
(ctest '(1 2 3) |
| 175 |
(apply curried3 '(1 2 3)) |
| 176 |
(apply curried3 1 '(2 3)) |
| 177 |
(apply curried3 1 2 '(3)) |
| 178 |
(apply curried3 1 2 3 '()) |
| 179 |
((apply curried3 '(1 2)) 3) |
| 180 |
((apply curried3 1 '(2)) 3) |
| 181 |
((apply curried3 1 2 '()) 3) |
| 182 |
((apply curried3 '(1)) 2 3) |
| 183 |
((apply curried3 1 '()) 2 3) |
| 184 |
(apply (apply curried3 '(1)) '(2 3)) |
| 185 |
((apply (apply curried3 '(1)) '(2)) 3) |
| 186 |
((apply (apply curried3 '(1)) 2 '()) 3)) |
| 187 |
|
| 188 |
(ctest '(1 2 3 (4 5)) |
| 189 |
(apply curried3+ '(1 2 3 4 5)) |
| 190 |
(apply curried3+ 1 '(2 3 4 5)) |
| 191 |
(apply curried3+ 1 2 '(3 4 5)) |
| 192 |
(apply curried3+ 1 2 3 '(4 5)) |
| 193 |
(apply curried3+ 1 2 3 4 '(5)) |
| 194 |
(apply curried3+ 1 2 3 4 5 '()) |
| 195 |
((apply curried3+ '(1)) 2 3 4 5) |
| 196 |
((apply curried3+ 1 '()) 2 3 4 5) |
| 197 |
(apply (apply curried3+ '(1)) '(2 3 4 5)) |
| 198 |
(apply (apply curried3+ '(1)) 2 '(3 4 5)) |
| 199 |
(apply (apply curried3+ '(1)) 2 3 '(4 5)) |
| 200 |
(apply (apply curried3+ '(1)) 2 3 4 '(5)) |
| 201 |
(apply (apply curried3+ 1 '()) '(2 3 4 5)) |
| 202 |
(apply (apply curried3+ 1 '()) 2 '(3 4 5)) |
| 203 |
(apply (apply curried3+ 1 '()) 2 3 '(4 5)) |
| 204 |
(apply (apply curried3+ 1 '()) 2 3 4 '(5)) |
| 205 |
(apply (apply curried3+ '(1 2)) '(3 4 5)) |
| 206 |
(apply (apply curried3+ '(1 2)) 3 '(4 5)) |
| 207 |
(apply (apply curried3+ '(1 2)) 3 4 '(5)) |
| 208 |
(apply (apply curried3+ 1 '(2)) '(3 4 5)) |
| 209 |
(apply (apply curried3+ 1 '(2)) 3 '(4 5)) |
| 210 |
(apply (apply curried3+ 1 '(2)) 3 4 '(5)) |
| 211 |
(apply (apply curried3+ 1 2 '()) '(3 4 5)) |
| 212 |
(apply (apply curried3+ 1 2 '()) 3 '(4 5)) |
| 213 |
(apply (apply curried3+ 1 2 '()) 3 4 '(5)) |
| 214 |
(apply (apply (apply curried3+ '(1)) '(2)) '(3 4 5)) |
| 215 |
(apply (apply (apply curried3+ '(1)) '(2)) 3 '(4 5)) |
| 216 |
(apply (apply (apply curried3+ 1 '()) 2 '()) '(3 4 5)) |
| 217 |
(apply (apply (apply curried3+ 1 '()) 2 '()) 3 '(4 5)) |
| 218 |
) |
| 219 |
|# |
| 220 |
|
| 221 |
;;----------------------------------------------------------------------- |
| 222 |
;; procedures in src/scmlib.scm |
| 223 |
|
| 224 |
(test-section "scmlib builtins") |
| 225 |
|
| 226 |
(test* "map* 1 arg" '(-1 -2 -3 . 1/4) |
| 227 |
(map* - / '(1 2 3 . 4))) |
| 228 |
|
| 229 |
(test* "map* 1 arg" '(-1 -2 -3 . #t) |
| 230 |
(map* - null? '(1 2 3))) |
| 231 |
|
| 232 |
(test* "map* 1 arg" #f |
| 233 |
(map* - null? 1)) |
| 234 |
|
| 235 |
(test* "map* n arg same elements" '(6 9 12 . 120) |
| 236 |
(map* + * '(1 2 3 . 4) '(2 3 4 . 5) '(3 4 5 . 6))) |
| 237 |
|
| 238 |
(test* "map* n arg uneven elements" '(6 9 12 4 () (6)) |
| 239 |
(map* + list '(1 2 3 . 4) '(2 3 4) '(3 4 5 6))) |
| 240 |
|
| 241 |
(test* "map* n arg uneven elements" '((1 2 3 . 4) () (3 4 5 6)) |
| 242 |
(map* + list '(1 2 3 . 4) '() '(3 4 5 6))) |
| 243 |
|
| 244 |
(test* "map* n arg uneven elements" '((1 2 3 . 4) 0 (3 4 5 6)) |
| 245 |
(map* + list '(1 2 3 . 4) 0 '(3 4 5 6))) |
| 246 |
|
| 247 |
;;------------------------------------------------------------------- |
| 248 |
(test-section "optional arguments") |
| 249 |
|
| 250 |
(define (oof x . args) |
| 251 |
(let-optionals* args ((a 'a) |
| 252 |
(b 'b) |
| 253 |
(c 'c)) |
| 254 |
(list x a b c))) |
| 255 |
|
| 256 |
(test* "let-optionals*" '(0 a b c) (oof 0)) |
| 257 |
(test* "let-optionals*" '(0 1 b c) (oof 0 1)) |
| 258 |
(test* "let-optionals*" '(0 1 2 c) (oof 0 1 2)) |
| 259 |
(test* "let-optionals*" '(0 1 2 3) (oof 0 1 2 3)) |
| 260 |
|
| 261 |
(define (oof* x . args) |
| 262 |
(let-optionals* args ((a 'a) |
| 263 |
(b 'b) |
| 264 |
. c) |
| 265 |
(list x a b c))) |
| 266 |
|
| 267 |
(test* "let-optionals*" '(0 a b ()) (oof* 0)) |
| 268 |
(test* "let-optionals*" '(0 1 b ()) (oof* 0 1)) |
| 269 |
(test* "let-optionals*" '(0 1 2 ()) (oof* 0 1 2)) |
| 270 |
(test* "let-optionals*" '(0 1 2 (3)) (oof* 0 1 2 3)) |
| 271 |
|
| 272 |
(define (oof+ x . args) |
| 273 |
(let ((i 0)) |
| 274 |
(let-optionals* (begin (inc! i) args) |
| 275 |
((a 'a) |
| 276 |
(b 'b) |
| 277 |
(c 'c)) |
| 278 |
i))) |
| 279 |
|
| 280 |
(test* "let-optionals*" 1 (oof+ 0)) |
| 281 |
(test* "let-optionals*" 1 (oof+ 0 1)) |
| 282 |
(test* "let-optionals*" 1 (oof+ 0 1 2)) |
| 283 |
(test* "let-optionals*" 1 (oof+ 0 1 2 3)) |
| 284 |
|
| 285 |
(define (oaf x . args) |
| 286 |
(let ((y (get-optional args 'foof))) |
| 287 |
(list x y))) |
| 288 |
|
| 289 |
(test* "get-optional" '(0 foof) (oaf 0)) |
| 290 |
(test* "get-optional" '(0 1) (oaf 0 1)) |
| 291 |
|
| 292 |
(define (oaf+ x . args) |
| 293 |
(let ((i 0)) |
| 294 |
(let ((y (get-optional (begin (inc! i) args) 'foof))) |
| 295 |
i))) |
| 296 |
|
| 297 |
(test* "get-optional" 1 (oaf+ 0)) |
| 298 |
(test* "get-optional" 1 (oaf+ 0 1)) |
| 299 |
|
| 300 |
(define (oef x . args) |
| 301 |
(let-keywords* args ((a 'a) |
| 302 |
(b :bb 'b) |
| 303 |
(c 'c)) |
| 304 |
(list x a b c))) |
| 305 |
|
| 306 |
(test* "let-keywords*" '(0 a b c) (oef 0)) |
| 307 |
(test* "let-keywords*" '(0 1 b c) (oef 0 :a 1)) |
| 308 |
(test* "let-keywords*" '(0 a 1 c) (oef 0 :bb 1)) |
| 309 |
(test* "let-keywords*" '(0 a b 1) (oef 0 :c 1)) |
| 310 |
(test* "let-keywords*" '(0 1 2 3) (oef 0 :c 3 :bb 2 :a 1)) |
| 311 |
;;(test* "let-keywords*" (test-error) (oef 0 :c 3 :bb 2 :a 1 :unknown 1)) |
| 312 |
|
| 313 |
(define (oef+ x . args) |
| 314 |
(let ((i 0)) |
| 315 |
(let-keywords* (begin (inc! i) args) |
| 316 |
((a 'a) |
| 317 |
(b :bb 'b) |
| 318 |
(c 'c)) |
| 319 |
i))) |
| 320 |
|
| 321 |
(test* "let-keywords*" 1 (oef+ 0)) |
| 322 |
(test* "let-keywords*" 1 (oef+ 0 :a 1)) |
| 323 |
(test* "let-keywords*" 1 (oef+ 0 :bb 1)) |
| 324 |
(test* "let-keywords*" 1 (oef+ 0 :c 1)) |
| 325 |
(test* "let-keywords*" 1 (oef+ 0 :c 3 :bb 2 :a 1)) |
| 326 |
;;(test* "let-keywords*" (test-error) (oef+ 0 :c 3 :bb 2 :a 1 :unknown 1)) |
| 327 |
|
| 328 |
(define (orf x . args) |
| 329 |
(let-keywords args ((a 'a) |
| 330 |
(b :bb 'b) |
| 331 |
(c 'c)) |
| 332 |
(list x a b c))) |
| 333 |
|
| 334 |
(test* "let-keywords" '(0 a b c) (orf 0)) |
| 335 |
(test* "let-keywords" '(0 1 b c) (orf 0 :a 1)) |
| 336 |
(test* "let-keywords" '(0 a 1 c) (orf 0 :bb 1)) |
| 337 |
(test* "let-keywords" '(0 a b 1) (orf 0 :c 1)) |
| 338 |
(test* "let-keywords" '(0 1 2 3) (orf 0 :c 3 :bb 2 :a 1)) |
| 339 |
(test* "let-keywords" (test-error) (orf 0 :c 3 :bb 2 :a 1 :unknown 1)) |
| 340 |
|
| 341 |
(define (orf+ x . args) |
| 342 |
(let ((i 0)) |
| 343 |
(let-keywords (begin (inc! i) args) |
| 344 |
((a 'a) |
| 345 |
(b :bb 'b) |
| 346 |
(c 'c)) |
| 347 |
i))) |
| 348 |
|
| 349 |
(test* "let-keywords" 1 (orf+ 0)) |
| 350 |
(test* "let-keywords" 1 (orf+ 0 :a 1)) |
| 351 |
(test* "let-keywords" 1 (orf+ 0 :bb 1)) |
| 352 |
(test* "let-keywords" 1 (orf+ 0 :c 1)) |
| 353 |
(test* "let-keywords" 1 (orf+ 0 :c 3 :bb 2 :a 1)) |
| 354 |
(test* "let-keywords" (test-error) (orf 0 :c 3 :bb 2 :a 1 :unknown 1)) |
| 355 |
|
| 356 |
;; let-keywords* combined with syntax rules |
| 357 |
(define-syntax lambda++ |
| 358 |
(syntax-rules (:key) |
| 359 |
((lambda++ "sub" () (margs ...) kargs . body) |
| 360 |
(lambda (margs ... . rest) |
| 361 |
(let-keywords* rest kargs |
| 362 |
. body))) |
| 363 |
((lambda++ "sub" (:key) margs kargs . body) |
| 364 |
(lambda++ "sub" () margs kargs . body)) |
| 365 |
((lambda++ "sub" (:key (arg1 def1) args ...) margs (kargs ...) . body) |
| 366 |
(lambda++ "sub" (:key args ...) margs (kargs ... (arg1 def1)) . body)) |
| 367 |
((lambda++ "sub" (:key arg1 args ...) margs (kargs ...) . body) |
| 368 |
(lambda++ "sub" (:key args ...) margs (kargs ... (arg1 #f)) . body)) |
| 369 |
((lambda++ "sub" (arg1 args ...) (margs ...) kargs . body) |
| 370 |
(lambda++ "sub" (args ...) (margs ... arg1) kargs . body)) |
| 371 |
((lambda++ args . body) |
| 372 |
(lambda++ "sub" args () () . body)) |
| 373 |
)) |
| 374 |
|
| 375 |
(test* "macro + let-keywords*" '(1 2 3 #f 5) |
| 376 |
((lambda++ (a b c :key d e) (list a b c d e)) |
| 377 |
1 2 3 :e 5)) |
| 378 |
|
| 379 |
(test* "macro + let-keywords*" (test-error) |
| 380 |
((lambda++ (a b c :key d e) (list a b c d e)) |
| 381 |
1 2 :d 3)) |
| 382 |
|
| 383 |
(test* "macro + let-keywords*" '(1 2 3 4 #f) |
| 384 |
((lambda++ (a b c :key d e) (list a b c d e)) |
| 385 |
1 2 3 :d 4)) |
| 386 |
|
| 387 |
(test* "macro + let-keywords*" '(1 2 3 0 1) |
| 388 |
((lambda++ (a b c :key (d 0) (e 1)) (list a b c d e)) |
| 389 |
1 2 3)) |
| 390 |
|
| 391 |
;; extended lambda formals are supported within compile.scm, but |
| 392 |
;; it expands to let-optionals*/let-keywords*, so we test it here. |
| 393 |
(test-section "extended lambda formals") |
| 394 |
|
| 395 |
(define (test-optkey sig proc arg res) |
| 396 |
(let loop ((res res) |
| 397 |
(rarg (reverse arg))) |
| 398 |
(test* sig (car res) (apply proc (reverse rarg))) |
| 399 |
(unless (null? rarg) |
| 400 |
(loop (cdr res) (cdr rarg))))) |
| 401 |
|
| 402 |
(test-optkey ":optional c d" |
| 403 |
(lambda (a b :optional c d) (list a b c d)) |
| 404 |
'(1 2 3 4) |
| 405 |
`((1 2 3 4) (1 2 3 ,(undefined)) |
| 406 |
(1 2 ,(undefined) ,(undefined)) |
| 407 |
,(test-error) ,(test-error))) |
| 408 |
|
| 409 |
(test-optkey ":optional (c 99) (d 100)" |
| 410 |
(lambda (a b :optional (c 99) (d 100)) (list a b c d)) |
| 411 |
'(1 2 3 4) |
| 412 |
`((1 2 3 4) (1 2 3 100) (1 2 99 100) |
| 413 |
,(test-error) ,(test-error))) |
| 414 |
|
| 415 |
(test-optkey ":optional a (b 99) (c 100) d" |
| 416 |
(lambda (:optional a (b 99) (c 100) d) (list a b c d)) |
| 417 |
'(1 2 3 4) |
| 418 |
`((1 2 3 4) (1 2 3 ,(undefined)) (1 2 100 ,(undefined)) |
| 419 |
(1 99 100 ,(undefined)) (,(undefined) 99 100 ,(undefined)))) |
| 420 |
|
| 421 |
(test-optkey ":key (c 99) (d 100)" |
| 422 |
(lambda (a b :key (c 99) (d 100)) (list a b c d)) |
| 423 |
'(1 2 :c 3 :d 4) |
| 424 |
`((1 2 3 4) ,(test-error) (1 2 3 100) ,(test-error) |
| 425 |
(1 2 99 100) |
| 426 |
,(test-error) ,(test-error))) |
| 427 |
|
| 428 |
(test-optkey ":optional (c 0) (d 1) :rest z" |
| 429 |
(lambda (a b :optional (c 0) (d 1) :rest z) (list a b c d z)) |
| 430 |
'(1 2 3 4 5 6) |
| 431 |
`((1 2 3 4 (5 6)) |
| 432 |
(1 2 3 4 (5)) |
| 433 |
(1 2 3 4 ()) |
| 434 |
(1 2 3 1 ()) |
| 435 |
(1 2 0 1 ()) |
| 436 |
,(test-error) |
| 437 |
,(test-error))) |
| 438 |
|
| 439 |
(test-optkey ":rest z :optional (c 0) (d 1)" |
| 440 |
(lambda (a b :rest z :optional (c 0) (d 1)) (list a b c d z)) |
| 441 |
'(1 2 3 4 5 6) |
| 442 |
`((1 2 3 4 (5 6)) |
| 443 |
(1 2 3 4 (5)) |
| 444 |
(1 2 3 4 ()) |
| 445 |
(1 2 3 1 ()) |
| 446 |
(1 2 0 1 ()) |
| 447 |
,(test-error) |
| 448 |
,(test-error))) |
| 449 |
|
| 450 |
(test-optkey ":key (b 0) (c 1) :rest z" |
| 451 |
(lambda (a :key (b 0) (c 1) :rest z) (list a b c z)) |
| 452 |
'(1 :c 99 :b 88) |
| 453 |
`((1 88 99 (:c 99 :b 88)) ,(test-error) |
| 454 |
(1 0 99 (:c 99)) ,(test-error) |
| 455 |
(1 0 1 ()) ,(test-error))) |
| 456 |
|
| 457 |
(test-optkey ":key (b 0) (c 1) :allow-other-keys :rest z" |
| 458 |
(lambda (a :key (b 0) (c 1) :allow-other-keys :rest z) |
| 459 |
(list a b c z)) |
| 460 |
'(1 :c 99 :a 77) |
| 461 |
`((1 0 99 (:c 99 :a 77)) ,(test-error) |
| 462 |
(1 0 99 (:c 99)) ,(test-error) |
| 463 |
(1 0 1 ()) ,(test-error))) |
| 464 |
|
| 465 |
(test-optkey ":key (b 0) (c 1) :allow-other-keys y :rest z" |
| 466 |
(lambda (a :key (b 0) (c 1) :allow-other-keys y :rest z) |
| 467 |
(list a b c y z)) |
| 468 |
'(1 :d 66 :c 99 :a 77) |
| 469 |
`((1 0 99 (:a 77 :d 66) (:d 66 :c 99 :a 77)) ,(test-error) |
| 470 |
(1 0 99 (:d 66) (:d 66 :c 99)) ,(test-error) |
| 471 |
(1 0 1 (:d 66) (:d 66)) ,(test-error) |
| 472 |
(1 0 1 () ()) ,(test-error))) |
| 473 |
|
| 474 |
(test-optkey ":optional b c :key (d 0) (e 1) :rest z" |
| 475 |
(lambda (a :optional b c :key (d 0) (e 1) :rest z) |
| 476 |
(list a b c d e z)) |
| 477 |
'(1 :e 99 :d 66) |
| 478 |
`((1 :e 99 66 1 (:d 66)) ,(test-error) |
| 479 |
(1 :e 99 0 1 ()) |
| 480 |
(1 :e ,(undefined) 0 1 ()) |
| 481 |
(1 ,(undefined) ,(undefined) 0 1 ()) |
| 482 |
,(test-error))) |
| 483 |
|
| 484 |
;; :allow-other-keys without :key |
| 485 |
;; http://chaton.practical-scheme.net/gauche/a/2020/03/12#entry-5e6a02c5-9b99a |
| 486 |
(test-optkey ":allow-other-keys z" |
| 487 |
(lambda (x :allow-other-keys z) |
| 488 |
(cons x z)) |
| 489 |
'(1 :a 2 :b 3) |
| 490 |
`((1 :b 3 :a 2) |
| 491 |
,(test-error) |
| 492 |
(1 :a 2) |
| 493 |
,(test-error) |
| 494 |
(1) |
| 495 |
,(test-error))) |
| 496 |
|
| 497 |
(test-optkey ":allow-other-keys" |
| 498 |
(lambda (:allow-other-keys) 'ok) |
| 499 |
'(:a 2 :b 3) |
| 500 |
`(ok |
| 501 |
,(test-error) |
| 502 |
ok |
| 503 |
,(test-error) |
| 504 |
ok)) |
| 505 |
|
| 506 |
;; If keyword is symbol, we should allow them as formals. |
| 507 |
(test-section "rnrs lambda formals") |
| 508 |
|
| 509 |
(define-syntax rnrs-lambda (with-module null lambda)) |
| 510 |
(when (symbol? :a) |
| 511 |
(test* "rnrs lambda formals" '(x y) |
| 512 |
((rnrs-lambda (:a :b) (list :a :b)) 'x 'y)) |
| 513 |
(test* "rnrs lambda formals" '(x z) |
| 514 |
((rnrs-lambda (a :key b) (list a b)) 'x 'y 'z)) |
| 515 |
(test* "rnrs lambda formals" '(x (z)) |
| 516 |
((rnrs-lambda (a :optional . b) (list a b)) 'x 'y 'z)) |
| 517 |
(test* "rnrs lambda formals" '(x y z) |
| 518 |
((rnrs-lambda :rest :rest) 'x 'y 'z)) |
| 519 |
) |
| 520 |
|
| 521 |
;;----------------------------------------------------------------------- |
| 522 |
;; case-lambda |
| 523 |
(test-section "case-lambda") |
| 524 |
|
| 525 |
(define (case-lambda-test sig fn . arg&exp) |
| 526 |
(for-each (lambda (arg&exp) |
| 527 |
(let [(args (car arg&exp)) |
| 528 |
(exp (cdr arg&exp))] |
| 529 |
(test* #"case-lambda ~sig ~(length args)" exp |
| 530 |
(apply fn args)))) |
| 531 |
arg&exp)) |
| 532 |
|
| 533 |
(case-lambda-test "[0..]" |
| 534 |
(case-lambda |
| 535 |
(() 0) |
| 536 |
((x) x) |
| 537 |
((x y) (+ x y)) |
| 538 |
((x y z) (+ (+ x y) z)) |
| 539 |
(args (apply + args))) |
| 540 |
'[() . 0] |
| 541 |
'[(1) . 1] |
| 542 |
'[(1 2) . 3] |
| 543 |
'[(1 2 3) . 6] |
| 544 |
'[(1 2 3 4) . 10]) |
| 545 |
|
| 546 |
(case-lambda-test "[2..4]" |
| 547 |
(case-lambda |
| 548 |
[(a b) `(two ,a ,b)] |
| 549 |
[(a b c) `(three ,a ,b ,c)] |
| 550 |
[(a b c d) `(four ,a ,b ,c ,d)]) |
| 551 |
`[() . ,(test-error)] |
| 552 |
`[(1) . ,(test-error)] |
| 553 |
`[(1 2) . (two 1 2)] |
| 554 |
`[(1 2 3) . (three 1 2 3)] |
| 555 |
`[(1 2 3 4) . (four 1 2 3 4)] |
| 556 |
`[(1 2 3 4 5) . ,(test-error)]) |
| 557 |
|
| 558 |
(case-lambda-test "[1..] matching order" |
| 559 |
(case-lambda |
| 560 |
[(x y) `(foo ,x ,y)] |
| 561 |
[(x . y) `(bar ,x ,y)]) |
| 562 |
`[() . ,(test-error)] |
| 563 |
`[(1) . (bar 1 ())] |
| 564 |
`[(1 2) . (foo 1 2)] |
| 565 |
`[(1 2 3) . (bar 1 (2 3))]) |
| 566 |
|
| 567 |
(case-lambda-test "[0..] matching order" |
| 568 |
(case-lambda |
| 569 |
[a `(foo ,a)] |
| 570 |
[(x) `(bar ,x)]) |
| 571 |
`[(1) . (foo (1))]) |
| 572 |
|
| 573 |
(case-lambda-test "[0,2]" |
| 574 |
(case-lambda |
| 575 |
[() 'yot] |
| 576 |
[(x y) `(bot ,x, y)]) |
| 577 |
`[() . yot] |
| 578 |
`[(1) . ,(test-error)] |
| 579 |
`[(1 2) . (bot 1 2)]) |
| 580 |
|
| 581 |
(case-lambda-test "one lambda" |
| 582 |
(case-lambda |
| 583 |
[(x . y) `(foo ,x ,y)]) |
| 584 |
`[() . ,(test-error)] |
| 585 |
`[(1) . (foo 1 ())] |
| 586 |
`[(1 2) . (foo 1 (2))]) |
| 587 |
|
| 588 |
;; Test if inlining case-lambda works correctly |
| 589 |
(define-inline (case-lambda-inlining-test x) |
| 590 |
(case-lambda |
| 591 |
[() x] |
| 592 |
[(a) (+ x a)] |
| 593 |
[(a b c) (* x a b c)] |
| 594 |
[(a . b) 'oof])) |
| 595 |
|
| 596 |
(define case-lambda-inlining-test-2 (case-lambda-inlining-test 2)) |
| 597 |
|
| 598 |
(let ([data '((() . 2) |
| 599 |
((1) . 3) |
| 600 |
((3 4) . oof) |
| 601 |
((3 4 5) . 120) |
| 602 |
((3 4 5 6) . oof))]) |
| 603 |
(define f (case-lambda-inlining-test 2)) |
| 604 |
|
| 605 |
(for-each (^d |
| 606 |
(test* `("case-lambda inlining" ,(car d)) (cdr d) |
| 607 |
(apply case-lambda-inlining-test-2 (car d)))) |
| 608 |
data) |
| 609 |
|
| 610 |
;; The followings expect case-lambda optimizations |
| 611 |
(test* "case-lambda inlining & optimization () " 2 |
| 612 |
(f)) |
| 613 |
(test* "case-lambda inlining & optimization (1) " 3 |
| 614 |
(f 1)) |
| 615 |
(test* "case-lambda inlining & optimization (3 4) " 'oof |
| 616 |
(f 3 4)) |
| 617 |
(test* "case-lambda inlining & optimization (3 4 5) " 120 |
| 618 |
(f 3 4 5)) |
| 619 |
(test* "case-lambda inlining & optimization (3 4 5 6) " 'oof |
| 620 |
(f 3 4 5 6)) |
| 621 |
) |
| 622 |
|
| 623 |
;; Test for locally bound case-lambda |
| 624 |
(define (case-lambda-local-test-1 a) |
| 625 |
(let ((c (case-lambda ([] a) ([x] (cons '1 x)) ([x y] (list '2 x y))))) |
| 626 |
(list (c 'X) (c) (c 'X 'Y)))) |
| 627 |
(test* "case-lambda-local-test-1" '((1 . X) foo (2 X Y)) |
| 628 |
(case-lambda-local-test-1 'foo)) |
| 629 |
|
| 630 |
(define (case-lambda-local-test-2) |
| 631 |
(define f (case-lambda ([x] x) ([x y] y))) |
| 632 |
(define g (case-lambda ([x] 1) ([x y] 2))) |
| 633 |
(list (f (g 'a)) (f 3 (g 'b 'c)))) |
| 634 |
(test* "case-lambda-local-test-2" '(1 2) |
| 635 |
(case-lambda-local-test-2)) |
| 636 |
|
| 637 |
|
| 638 |
;;----------------------------------------------------------------------- |
| 639 |
;; generator-* |
| 640 |
(test-section "generator-*") |
| 641 |
|
| 642 |
(define (gen-1arg-test name input expect thunk :optional (wrap identity)) |
| 643 |
(test* (format "~a, 1 arg" name) expect |
| 644 |
(wrap (with-input-from-string input thunk)))) |
| 645 |
|
| 646 |
(gen-1arg-test "generator-fold" "a b c d e" '(e d c b a . z) |
| 647 |
(cut generator-fold cons 'z read)) |
| 648 |
(gen-1arg-test "generator-fold-right" "a b c d e" '(a b c d e . z) |
| 649 |
(cut generator-fold-right cons 'z read)) |
| 650 |
(gen-1arg-test "generator-map" "a b c d e" '("a" "b" "c" "d" "e") |
| 651 |
(cut generator-map symbol->string read)) |
| 652 |
(let1 r '() |
| 653 |
(gen-1arg-test "generator-for-each" "a b c d e" '(e d c b a) |
| 654 |
(cut generator-for-each (^v (push! r v)) read) |
| 655 |
(^_ r))) |
| 656 |
(gen-1arg-test "generator-find" "2 4 0 8 3 6 9" 3 |
| 657 |
(cut generator-find odd? read)) |
| 658 |
|
| 659 |
(define (gen-2arg-test name input1 input2 expect proc :optional (wrap identity)) |
| 660 |
(test* (format "~a, 2 args" name) expect |
| 661 |
(wrap (call-with-input-string input1 |
| 662 |
(^x (call-with-input-string input2 |
| 663 |
(^y (proc x y)))))))) |
| 664 |
|
| 665 |
(gen-2arg-test "generator-fold" "a b c d e f" "1 2 3 4 5" |
| 666 |
'(e 5 (d 4 (c 3 (b 2 (a 1 z))))) |
| 667 |
(^[x y] (generator-fold list 'z (cut read x) (cut read y)))) |
| 668 |
(gen-2arg-test "generator-fold-right" "a b c d e f" "1 2 3 4 5" |
| 669 |
'(a 1 (b 2 (c 3 (d 4 (e 5 z))))) |
| 670 |
(^[x y] (generator-fold-right list 'z (cut read x) (cut read y)))) |
| 671 |
(gen-2arg-test "generator-map" "1 2 3 4 5" "6 7 8 9" |
| 672 |
'(7 9 11 13) |
| 673 |
(^[x y] (generator-map + (cut read x) (cut read y)))) |
| 674 |
(let1 r '() |
| 675 |
(gen-2arg-test "generator-for-each" "1 2 3 4 5" "6 7 8 9" |
| 676 |
'(13 11 9 7) |
| 677 |
(^[x y] (generator-for-each (^[a b] (push! r (+ a b))) |
| 678 |
(cut read x) (cut read y))) |
| 679 |
(^_ r))) |
| 680 |
|
| 681 |
;;----------------------------------------------------------------------- |
| 682 |
;; large env frame |
| 683 |
;; https://github.com/shirok/Gauche/issues/487 |
| 684 |
(test-section "large env frame") |
| 685 |
|
| 686 |
(define-macro (biglet n) |
| 687 |
(let1 syms (map (^_ (gensym)) (iota n)) |
| 688 |
`(let (,@(map (^s `(,s #f)) syms)) |
| 689 |
,@(map (^[s i] `(set! ,s ,i)) syms (iota n)) |
| 690 |
(list ,@syms)))) |
| 691 |
|
| 692 |
(test* "bigenv 1030" (iota 1030) |
| 693 |
(biglet 1030)) |
| 694 |
|
| 695 |
;; Test LREF-VAL0-NUMADD2 path |
| 696 |
(define-macro (bigsum n) |
| 697 |
(let1 syms (map (^_ (gensym)) (iota n)) |
| 698 |
`(lambda ,syms (+ ,@syms)))) |
| 699 |
|
| 700 |
(define bigsum-proc (bigsum 1029)) |
| 701 |
|
| 702 |
(test* "bigsum 1029" (apply + (iota 1029)) |
| 703 |
(apply bigsum-proc (iota 1029))) |
| 704 |
|
| 705 |
;; Test LREF-VAL0-BNGT etc. path |
| 706 |
(define-macro (bigcond op n) |
| 707 |
(let1 syms (map (^_ (gensym)) (iota n)) |
| 708 |
`(lambda (k ,@syms) |
| 709 |
(cond ,@(map (^s `((,op k ,s) ,s)) syms))))) |
| 710 |
|
| 711 |
(define bigcond<-proc (bigcond < 1028)) |
| 712 |
|
| 713 |
(test* "bigcond< 1028" 1025 |
| 714 |
(apply bigcond<-proc 1024 (iota 1028))) |
| 715 |
|
| 716 |
(define bigcond=-proc (bigcond = 1028)) |
| 717 |
|
| 718 |
(test* "bigcond= 1028" 1027 |
| 719 |
(apply bigcond=-proc 1027 (iota 1028))) |
| 720 |
|
| 721 |
(test-end) |