misc-test.scm 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. (test-init "Miscellaneous" 223)
  2. ;;; DSSSL spec example 11
  3. (test '(3 4 5 6) (lambda x x) 3 4 5 6)
  4. (test '(5 6) (lambda (x y #!rest z) z) 3 4 5 6)
  5. (test '(3 4 5 i: 6 j: 1)
  6. (lambda ( x y #!optional z #!rest r #!key i (j 1))
  7. (list x y z 'i: i 'j: j))
  8. 3 4 5 i: 6 i: 7)
  9. ;; Test for optional argument handling.
  10. ;; (Savannah bug #10613 was a bug in name scoping of default arguments.)
  11. (define (test-opt-args a b c d)
  12. (define n 0)
  13. ;; We add a side-effect to check that default arguments are evaluated
  14. ;; at the correct time.
  15. (define (next-n) (set! n (+ 1 n)) n)
  16. (define (inner a
  17. #!optional (b (list a b c d (next-n)))
  18. (c (list a b c d (next-n)))
  19. #!key (d (list a b c d (next-n))))
  20. (vector 'arg-a: a 'arg-b: b 'arg-c: c 'argd: d))
  21. (list inner1: (inner 'a2) n: (next-n)
  22. inner2: (inner 'a3 'b3 'c3 d: 'd3) n: (next-n)))
  23. (test
  24. '(inner1:
  25. #(
  26. arg-a: a2
  27. arg-b: (a2 b1 c1 d1 1)
  28. arg-c: (a2 (a2 b1 c1 d1 1) c1 d1 2)
  29. argd: (a2 (a2 b1 c1 d1 1) (a2 (a2 b1 c1 d1 1) c1 d1 2) d1 3))
  30. n: 4
  31. inner2:
  32. #(arg-a: a3 arg-b: b3 arg-c: c3 argd: d3)
  33. n: 5)
  34. test-opt-args 'a1 'b1 'c1 'd1)
  35. (test '(200 . 100)
  36. (lambda (x #!optional (y (* 2 x)) (p (lambda () (cons y x))))
  37. (p))
  38. 100)
  39. (test '(100 . 200)
  40. (lambda (x #!optional (y (* 2 x)))
  41. (cons x y))
  42. 100)
  43. (test #t keyword? 'foo:)
  44. (test #f keyword? 'foo\:)
  45. (test #t keyword? 'foo:)
  46. (test #f keyword? 'foo)
  47. (test #f keyword? ':)
  48. (test #t keyword? '||:)
  49. (test #t keyword? (car '(a: b:)))
  50. (test #f keyword? "bar")
  51. ;; This is Savannah bug #39059: Method keywords problem
  52. (define (key-1 #!key (a "default a") (b "default b") (c "default c"))
  53. (list c a))
  54. (test '("c" "a") 'key-1 (key-1 a: "a" b: "b" c: "c"))
  55. (define key-2-counter 0)
  56. (define (incr-key-2-counter)
  57. (set! key-2-counter (+ key-2-counter 1))
  58. key-2-counter)
  59. (define (key-2 #!key (a "default a") (b (incr-key-2-counter)) (c "default c"))
  60. (list c a key-2-counter))
  61. (test '("c" "a" 0) 'key-2a (key-2 a: "a" b: "b" c: "c"))
  62. (test '("default c" "a" 1) 'key-2b (key-2 a: "a"))
  63. ;;; DSSSL spec example 44
  64. (test "Argentina" keyword->string 'Argentina:)
  65. (test "foo" keyword->string 'foo:)
  66. (test "" keyword->string '||:)
  67. (test "a b c" keyword->string (string->keyword "a b c"))
  68. (test 'foo: string->keyword "foo")
  69. (test '||: string->keyword "")
  70. ;; Test keyword parameter with primitive type.
  71. (define (fun-with-keys1 #!key (code ::int 400) (message ::string "brrp"))
  72. (format "code: ~a message: ~a." code message))
  73. (test "code: 400 message: brrp." 'test-fun-with-keys1 (fun-with-keys1))
  74. (test "code: 200 message: brrp." 'test-fun-with-keys1 (fun-with-keys1 code: 200))
  75. (test "Hello" symbol->string 'H\x65;llo)
  76. ;;; DSSSL spec example 45
  77. (test 'foobar: string->keyword "foobar")
  78. (define-unit ft 12in)
  79. (test 18in + 6in 1ft)
  80. (test 5 call-with-values (lambda () (values 4 5)) (lambda (a b) b))
  81. (test -1 call-with-values * -)
  82. ;; Test from: Joerg-Cyril.Hoehle@t-systems.com
  83. (test '(() #!eof) call-with-values
  84. (lambda () (values '() '#!eof))
  85. (lambda (x y) (list x y)))
  86. ;;; This caused a spurious warning in earlier releases.
  87. (test '(1 2 3) 'let (let ((x (lambda l l))) (x 1 2 3)))
  88. ;;; test old reader bugs
  89. (test '(b) cdr '(a .(b)))
  90. (test "foo" cdr '(a ."foo"))
  91. (test 'a car '(a #||#))
  92. (define (try-vector-ref vec index)
  93. (try-catch (vector-ref vec index)
  94. (ex <java.lang.IndexOutOfBoundsException>
  95. "Bad array index")))
  96. (test 3 try-vector-ref #(1 2 3) 2)
  97. (test "Bad array index" try-vector-ref #(1 2 3) 10)
  98. ;; Extracted from bug reported by Joerg-Cyril.Hoehle@t-systems.com
  99. (define (test-unary-minus)
  100. (- (char->integer #\0)))
  101. (test -48 test-unary-minus)
  102. (define (test-string->integer str start end)
  103. (and (< -1 start end (+ (string-length str) 1))
  104. (let loop ((pos start) (accum 0))
  105. (cond
  106. ((>= pos end) accum)
  107. ((char-numeric? (string-ref str pos))
  108. (loop (+ pos 1) (+ (char->integer (string-ref str pos))
  109. (- (char->integer #\0)) (* 10 accum))))
  110. (else #f)))))
  111. (test 123 test-string->integer "123" 0 3)
  112. (test 123 test-string->integer "123456" 0 3)
  113. (test 23 test-string->integer "123456" 1 3)
  114. (section "new-line handling")
  115. ;;; Test that #\return and #\newline are read robustly.
  116. (define cr-test-string (string-copy "a \"bRLc\" dRklLXY"))
  117. (do ((i 0 (+ i 1)))
  118. ((= i (string-length cr-test-string)) #t)
  119. (if (char=? #\R (string-ref cr-test-string i))
  120. (string-set! cr-test-string i #\Return))
  121. (if (char=? #\L (string-ref cr-test-string i))
  122. (string-set! cr-test-string i #\Linefeed)))
  123. (call-with-input-string
  124. cr-test-string
  125. (lambda (iport ::input-port)
  126. (iport:setConvertCR #t)
  127. (test 1 input-port-column-number iport)
  128. (test 1 input-port-line-number iport)
  129. (test 'a read iport)
  130. (test "b\nc" read iport)
  131. (test 'd read iport)
  132. (test 'kl read iport)
  133. (test 'XY read iport)
  134. (test #!eof read iport)))
  135. (call-with-input-string
  136. cr-test-string
  137. (lambda (iport)
  138. (test #\a read-char iport)
  139. (test #\Space read-char iport)
  140. (test #\" read-char iport)
  141. (test #\b read-char iport)
  142. (test #\Return peek-char iport)
  143. (test 5 input-port-column-number iport)
  144. (test 1 input-port-line-number iport)
  145. (test #\Return read-char iport)
  146. (test #\Linefeed read-char iport)
  147. (test #\c read-char iport)
  148. (test #\" read-char iport)
  149. (test #\Space read-char iport)
  150. (test #\d read-char iport)
  151. (test #\Return read-char iport)
  152. (test 3 input-port-line-number iport)
  153. (test 1 input-port-column-number iport)
  154. (test #\k read-char iport)
  155. (test #\l read-char iport)
  156. (test #\Linefeed read-char iport)
  157. (test #\X read-char iport)
  158. (test #\Y read-char iport)
  159. (test #!eof read-char iport)))
  160. (define str-inport (open-input-string "(a . (b c . ())) 34"))
  161. (test #t input-port? str-inport)
  162. (test '(a b c) read str-inport)
  163. (test 34 read str-inport)
  164. (test #t eof-object? (peek-char str-inport))
  165. (close-input-port str-inport)
  166. (test "a/b (c d)" 'open-output-string
  167. (let ((q (open-output-string))
  168. (x '(a b c d)))
  169. (write (car x) q)
  170. (display "/" q)
  171. (write (cadr x) q)
  172. (write (cddr x) q)
  173. (get-output-string q)))
  174. ;;; From: Hallvard Traetteberg <Hallvard.Traetteberg@idi.ntnu.no>
  175. ;;; Triggered bug with try-finally nested in an expression.
  176. (define (quote-keyword-values list)
  177. (if (null? list)
  178. list
  179. `(,(car list) ',(car (cdr list))
  180. . ,(quote-keyword-values (cdr (cdr list)))))
  181. )
  182. (defmacro with-content (object-form . content)
  183. (let ((var-symbol (string->symbol (string-append "context-"
  184. (symbol->string (car object-form)))))
  185. (object-form `(,(car object-form)
  186. . ,(quote-keyword-values (cdr object-form)))))
  187. `(fluid-let ((,var-symbol ,object-form))
  188. (let ((content (list . ,content)))
  189. (cons ,var-symbol content)))
  190. ))
  191. (define (document) (list 'document))
  192. (define (view #!key type)
  193. (list 'view type: type))
  194. (test '((view type: text)) 'with-content
  195. (with-content (view type: text)))
  196. (test '((document) ((view type: diagram)) ((view type: text))) 'with-content
  197. (with-content (document) (with-content (view type: diagram))
  198. (with-content (view type: text))))
  199. (test '("X" . "X:abc") 'synchronized
  200. (let* ((x "X")
  201. (y "abc")
  202. (z (synchronized y
  203. (set! y (string-append x ":" y))
  204. (cons x y))))
  205. z))
  206. (define *xx* 3)
  207. (define (fluid-test *xx*)
  208. (fluid-let ((*xx* *xx*))
  209. (set! *xx* (+ 100 (twice-*xx*)))
  210. (set! *xx* (let ((*xx* *xx*))
  211. (+ 100 *xx*)))
  212. *xx*))
  213. (define (twice-*xx*) (* 2 *xx*))
  214. (test '(206 . 3) 'fluid-let-1 (let ((res (fluid-test 10))) (cons res *xx*)))
  215. (test 'bar 'fluid-let-2 (fluid-let ((flt (lambda () 'bar))) (flt)))
  216. (section "closures")
  217. (define (f1 a)
  218. (define (f2 b)
  219. (cons a b))
  220. (cons a f2))
  221. (define f1-100 (f1 100))
  222. (define f2-20 ((cdr f1-100) 20))
  223. (test 100 'closure-f2-car (car f2-20))
  224. (test 20 'closure-f2-cdr (cdr f2-20))
  225. ;; Here f4 should be optimized away.
  226. (define (f3 a)
  227. (define (f4 b)
  228. (cons a b))
  229. (define (f5 c)
  230. (cons a c))
  231. (cons a f5))
  232. (define f3-10 (f3 10))
  233. (define f4-20 ((cdr f3-10) 20))
  234. (test '(10 . 20) 'closure-f4-20 f4-20)
  235. (define (f30 a)
  236. (define (f31 b)
  237. (cons a b))
  238. (define (f32 c)
  239. (cons a c))
  240. (list a f31 f32))
  241. (define f30-10 (f30 10))
  242. (define f31-20 ((cadr f30-10) 20))
  243. (define f32-33 ((caddr f30-10) 33))
  244. (test '(10 . 20) 'closure-f31-20 f31-20)
  245. (test '(10 . 33) 'closure-f32-33 f32-33)
  246. (define (f6 a)
  247. (define (f7 b)
  248. (define (f8 c)
  249. (define (f9 d)
  250. (list a b c d))
  251. (list a b c f9))
  252. (list a b f8))
  253. (list a f7))
  254. (define f6-100 (f6 100))
  255. (define f7-20 ((cadr f6-100) 20))
  256. (define f8-10 ((caddr f7-20) 10))
  257. (test '(100 20 10 2) 'closure-test3 ((cadddr f8-10) 2))
  258. (define (f60 a)
  259. (define (x6 b) a)
  260. (define (f70 b)
  261. (define (x7 c) b)
  262. (define (f8 c)
  263. (define (x8 d) c)
  264. (define (f9 d)
  265. (list a b c d))
  266. (list a b c f9))
  267. (list a b f8))
  268. (list a f70))
  269. (define f60-100 (f60 100))
  270. (define f70-20 ((cadr f60-100) 20))
  271. (define f80-10 ((caddr f70-20) 10))
  272. (test '(100 20 10 2) 'closure-test4 ((cadddr f80-10) 2))
  273. ;; A bug reported by Edward Mandac <ed@texar.com>.
  274. (test "Done" 'do-future (do ((test 'empty))
  275. (#t "Done")
  276. (future (begin(set! test 'goodbye)))))
  277. (define p1 (cons 9 45))
  278. (define-alias p2 p1)
  279. (define-alias p2car (car p2))
  280. (set! p2car 40)
  281. (test '(40 . 45) 'test-alias-1 p1)
  282. (define p1-cdr-loc (location (cdr p1)))
  283. (set! (p1-cdr-loc) 50)
  284. (set! (car p2) 49)
  285. (test '(49 . 50) 'test-alias-2 p2)
  286. (test '(49 . 50) 'test-alias-3 ((location p1)))
  287. (define (test-alias-4 x y)
  288. (define-alias xcar (car x))
  289. (define-alias yy y)
  290. (set! yy (+ yy xcar))
  291. (set! xcar yy)
  292. (list yy xcar x y))
  293. (test '(59 59 (59 . 50) 59) test-alias-4 p1 10)
  294. (define (test-alias-5 x)
  295. (define y (list x))
  296. (define-alias z y)
  297. (list x y z))
  298. (test '(8 (8) (8)) test-alias-5 8)
  299. (define test-nesting-1
  300. (lambda ()
  301. ((lambda (bar)
  302. (letrec
  303. ((foo
  304. (lambda (bar1) (foo bar))))
  305. 33))
  306. 100)))
  307. (test 33 test-nesting-1)
  308. (define (test-nesting-2)
  309. ((lambda (bar1)
  310. (lambda ()
  311. (lambda ()
  312. bar1)))
  313. #t)
  314. (let ((bar2 34))
  315. (lambda () (lambda () bar2))))
  316. (test 34 ((test-nesting-2)))
  317. (define (test-nesting-3 k l m n o)
  318. (define (foo a b c d e f)
  319. (list a b c d e f k l m n o))
  320. (foo foo (+ k k) (+ k l) (+ k m) (+ k n) (+ k o)))
  321. (test '(20 21 22 23 24 10 11 12 13 14) 'test-nesting-3
  322. (cdr (test-nesting-3 10 11 12 13 14)))
  323. ;;; Testcase from "Walter C. Pelissero" <wcp@lpds.sublink.org>:
  324. (test #t procedure?
  325. (let* ((is-equal eqv?)
  326. (false
  327. (lambda ()
  328. (is-equal 'bar 'foo)))
  329. (foo (lambda () 'foo)))
  330. (lambda ()
  331. (foo))))
  332. (test #t pair?
  333. (let* ((is-equal eqv?)
  334. (false
  335. (lambda ()
  336. (is-equal 'bar 'foo)))
  337. (foo (lambda () (false))))
  338. (list
  339. false
  340. (lambda () (foo)))))
  341. (test #t pair?
  342. (let* ((is-equal eqv?)
  343. (false
  344. (lambda ()
  345. (is-equal 'bar 'foo)))
  346. (foo (lambda () (false))))
  347. (list
  348. false
  349. (lambda ()
  350. (define (bar) (foo))
  351. (list bar (bar))))))
  352. (test #t not
  353. (let* ((foo (lambda ()
  354. 'foo))
  355. (bar (lambda ()
  356. (let loop ((arg 'bar))
  357. (foo)
  358. (not (loop (foo)))))))
  359. #f))
  360. (define (test-duplicate-names)
  361. (let ((bar #t)) (lambda () (lambda () bar)))
  362. (let ((bar #t)) (lambda () (lambda () bar)))
  363. (let ((bar #t)) (lambda () (lambda () bar)))
  364. 97)
  365. (test 97 test-duplicate-names)
  366. (test #f 'mutual-recursion-1
  367. (letrec ((a (lambda () (b)))
  368. (b (lambda () (a))))
  369. #f))
  370. (test #f 'mutual-recursion-2
  371. (letrec ((a (lambda () 10))
  372. (b (lambda () (a)))
  373. (c (lambda () (e) (b)))
  374. (d (lambda () (c)))
  375. (e (lambda () (d))))
  376. #f))
  377. ;; Used to cause a verification error.
  378. (define (sql-rsmd-all op rsmd . iter)
  379. (if (null? iter)
  380. (sql-rsmd-all op rsmd (sql-rsmd-columncount rsmd) '())
  381. (if (zero? (car iter))
  382. (cadr iter)
  383. (sql-rsmd-all op rsmd (- (car iter) 1)
  384. (cons (op rsmd (car iter))
  385. (cadr iter))))))
  386. (define (test-location-local x)
  387. (let* ((xl (location x)) ;; test location of formal parameter x
  388. (z (xl))
  389. (zl (location z))) ;; test location of local variable z
  390. (set! (xl) (+ (zl) 100))
  391. x))
  392. (test 110 test-location-local 10)
  393. (test 15 'tail-call (let loop ((a 1) (b 2) (c 3) (d 4) (e 5) (f 6))
  394. (if (> a 10) b (loop b c d e f (+ a b c)))))
  395. ;;; read-line should handle CR, LF and CRLF equally
  396. (section "read-line")
  397. (define (test-read-split port)
  398. (call-with-values (lambda () (read-line port 'split))
  399. (lambda x (car x))))
  400. (define (test-read-line proc)
  401. (call-with-input-string
  402. "line\rline\nline\r\nline"
  403. (lambda (strport)
  404. (list (proc strport) (proc strport) (proc strport) (proc strport)))))
  405. (test '("line" "line" "line" "line")
  406. test-read-line
  407. read-line)
  408. (test '("line" "line" "line" "line")
  409. test-read-line
  410. test-read-split)
  411. (define plus10 (make-procedure foo: 33 name: 'Plus10
  412. method: (lambda (x y)
  413. (+ x (if (number? y) y 0) 10))
  414. method: (lambda () 10)))
  415. (test 50 plus10 30 10)
  416. (test 10 plus10)
  417. (test 12 plus10 2 #!null)
  418. ;;(test 10 'plus10-error
  419. ;; (try-catch (plus10 3) (ex <java.lang.Exception> "error")))
  420. (test 33 procedure-property plus10 'foo)
  421. (set-procedure-property! plus10 'foo 44)
  422. (test 44 procedure-property plus10 'foo)
  423. (test "#<procedure Plus10>" 'plus10-name1 (format "~s" plus10))
  424. (set-procedure-property! plus10 'name 'PlusTen)
  425. (test "#<procedure PlusTen>" 'plus10-name2 (format "~s" plus10))
  426. (define-procedure Plus
  427. (lambda ((x :: <number>) (y :: <number>)) (+ x y))
  428. (lambda ((x :: <string>) (y :: <string>)) (string-append x y)))
  429. (test 12 Plus 5 7)
  430. (test "57" Plus "5" "7")
  431. (define (return-null) #!null)
  432. (test #!null return-null)
  433. ;;; Based on bug report 2002-3-1 from Petter &Ouml;sterlund <petos@fyrplus.se>
  434. (define (fie-1) (fie-2) (fie-3))
  435. (define (fie-4) (fie-3) (fie-3))
  436. (test #t 'names (and (procedure? fie-1) (procedure? fie-4)))
  437. ;; Test from Jim White <jim@pagesmiths.com> - fails if --full-tailscalls.
  438. (define (systime) (invoke-static <java.lang.System> 'currentTimeMillis))
  439. (define systime-1 (systime))
  440. (define systime-2 (systime))
  441. (test #t >= systime-2 systime-1)
  442. ;; Bug reported by Wen-Chun Ni <wcn@tbcommerce.com>.
  443. (define (fl-f y) (+ 10 y))
  444. (fluid-let ((fl-x 2)) (fl-f 1))
  445. ;;; Based on bug report 2002-12-3 from Petter &Ouml;sterlund <petos@fyrplus.se>
  446. (define (fie-6)
  447. 6
  448. (define (runn)
  449. foo)
  450. (define (foo)
  451. 'done)
  452. (apply runn '()))
  453. (test 'done 'call-fie-6 ((fie-6)))
  454. ;; Test instance?
  455. (test #t instance? 1 <number>)
  456. (test #t instance? "x" <string>)
  457. (test #f instance? "x" <number>)
  458. (test #f instance? #!null <string>)
  459. ;; Tests based on Savannah bug #31353 "boolean type"
  460. (test #t instance? #f boolean)
  461. (test #f instance? 123 boolean)
  462. (test #t 'test-instance (instance? #f boolean))
  463. (test #f 'test-instance (instance? 123 boolean))
  464. (define (is-prim-boolean x)
  465. (instance? x boolean))
  466. (test #f is-prim-boolean 123)
  467. (test #t is-prim-boolean #t)
  468. ;; Based on a bug reported 05-26 Sven.Hartrumpf@FernUni-Hagen.de
  469. (define (list-cond compare a b)
  470. (cons (compare a (list b)) b))
  471. (define (make-mf forms results)
  472. (let ((r
  473. (map
  474. (lambda (result)
  475. (map
  476. (lambda (form)
  477. (list-cond
  478. (lambda (a b)
  479. (string<? (cadr a) (caar b)))
  480. forms
  481. (list form)))
  482. forms))
  483. results)))
  484. (call-with-output-string
  485. (lambda (output-stream)
  486. (for-each
  487. (lambda (form)
  488. (format output-stream "[f:~a]" form))
  489. r)))))
  490. (test "[f:((#f a1) (#f a2))][f:((#f a1) (#f a2))]"
  491. make-mf '("a1" "a2") '("b1" "b2"))
  492. (require 'printf)
  493. (define (test-printf format value)
  494. (call-with-output-string
  495. (lambda (out)
  496. (fprintf out format value))))
  497. (test "[ 23]" test-printf "[%3d]" 23)
  498. (test "[3.50 ]" test-printf "[%-5.2f]" 3.5)
  499. (define fluid-stack '())
  500. (define fluid-let-test-level 'main)
  501. (define (push-fluid-let-test-level!)
  502. (set! fluid-stack (cons fluid-let-test-level fluid-stack)))
  503. (define (test-fluid-let-levels)
  504. (push-fluid-let-test-level!)
  505. (force
  506. (future
  507. (fluid-let ((fluid-let-test-level 'thread))
  508. (push-fluid-let-test-level!)
  509. (force (future (push-fluid-let-test-level!))))))
  510. fluid-stack)
  511. (test '(thread thread main) test-fluid-let-levels)
  512. (define (not-a) ((lambda (x) (not x)) 'a))
  513. (test #f not-a)
  514. (test "Test." 'from-psyntax
  515. ((lambda ()
  516. (letrec ((topfun
  517. (lambda (marks)
  518. ((lambda ()
  519. ((lambda ()
  520. (lambda () marks)))))))
  521. (chifun
  522. (lambda () (list topfun))))
  523. "Test."))))
  524. (require 'list-lib)
  525. (test '(1 3) 'filter!-test (filter! odd? (iota 5)))
  526. ;; Test fluid-let in the presence of threads
  527. (define-variable *X* #f)
  528. (define (get-*X*)
  529. *X*)
  530. ;; Should return '(1 2)
  531. (define (fluid-let-and-threads)
  532. (let* ((t1 (future
  533. (begin
  534. (fluid-let ((*X* 1))
  535. (sleep 0.5)
  536. (get-*X*)))))
  537. (t2 (future
  538. (begin
  539. (sleep 0.25)
  540. (fluid-let ((*X* 2))
  541. (sleep 0.5)
  542. (get-*X*))))))
  543. (list (force t1) (force t2))))
  544. (test '(1 2) fluid-let-and-threads)
  545. (define param1 (make-parameter 10 number->string))
  546. (test "10" 'param-test1 (param1))
  547. (define-alias param1v (param1))
  548. (set! (param1) 11)
  549. (test "11" 'param-test2 param1v)
  550. (param1 12)
  551. (test "12" 'param-test3 (param1))
  552. (set! param1v 13)
  553. (test "13" 'param-test4 (param1))
  554. (test '("15" "15" "16" "16" "13" "13") 'param-test5
  555. (let ((r0
  556. (fluid-let ((param1v (+ (string->number param1v) 2)))
  557. (let ((r1 (list (param1) param1v)))
  558. (set! param1v 16)
  559. (append r1 (list (param1) param1v))))))
  560. (append r0 (list (param1) param1v))))
  561. (param1 20)
  562. (test '("22" "22" "17" "17" "20" "20") 'param-test5
  563. (let ((r0
  564. (parameterize ((param1 (+ (string->number (param1)) 2)))
  565. (let ((r1 (list (param1) param1v)))
  566. (set! param1v 17)
  567. (append r1 (list (param1) param1v))))))
  568. (append r0 (list (param1) param1v))))
  569. (define param2 (make-parameter 7 vector))
  570. (test #(7) 'param-test7 (param2))
  571. (begin
  572. (define var1 1)
  573. (test 2 'test-fluid-future-1a
  574. (force
  575. (fluid-let ((var1 2))
  576. (future (begin (sleep 0.1s) var1)))))
  577. (test 1 'test-fluid-future-1b var1))
  578. (define-variable var2 1)
  579. (test 2 'test-fluid-future-2a
  580. (force
  581. (fluid-let ((var2 2))
  582. (future (begin (sleep 0.1s) var2)))))
  583. (test 1 'test-fluid-future-2b var2)
  584. ;; Bug reported 2005-05-08 by dominique.boucher@nuecho.com.
  585. (require <moduleFT>)
  586. (define (test-neg-abs)
  587. (let ((x (neg-abs 4)))
  588. (format #f "x = ~S." x)))
  589. (test "x = -4." test-neg-abs)
  590. (test '((prefix-test 11)
  591. (prefix-test:var2 12)
  592. (prefix-test:var2:var3 13)
  593. (prefix-test:filler:var4 14))
  594. 'prefix-test
  595. prefix-test-list)
  596. (test '(12) 'prefix-test:var2 prefix-test:var2)
  597. (test '(13) 'prefix-test:var2:var3 prefix-test:var2:var3)
  598. (test '(14) 'prefix-test:filler:var4 prefix-test:filler:var4)
  599. ;; Common Lisp hyperspec
  600. (test "[#24rn]" 'print-base-1 ;; Common Lisp returns upper-case #24rN
  601. (fluid-let ((*print-base* 24) (*print-radix* #t))
  602. (format #f "[~s]" 23)))
  603. (test '("101000" "1111" "220" "130" "104" "55" "50" "44" "40" "37" "34"
  604. "31" "2c" "2a" "28" "26" "24" "22" "20" "1j" "1i" "1h" "1g" "1f"
  605. "1e" "1d" "1c" "1b" "1a" "19" "18" "17" "16" "15" "14") 'print-base-2
  606. ;print the decimal number 40 in each base from 2 to 36
  607. (let loop ((i 36) (r '()))
  608. (if (= i 1) r
  609. (loop (- i 1)
  610. (cons (fluid-let ((*print-base* i)) (format #f "~s" 40))
  611. r)))))
  612. (test '("#b1010 #b1/1010" "#3r101 #3r1/101" "#o12 #o1/12" "10. #10r1/10" "#xa #x1/a") 'print-base-3
  613. ;;print the integer 10 and the ratio 1/10 in bases 2, 3, 8, 10, 16
  614. (map (lambda (pb)
  615. (fluid-let ((*print-radix* #t) (*print-base* pb))
  616. (format #f "~S ~S" 10 1/10)))
  617. '(2 3 8 10 16)))
  618. ;; Savannah bug #14697 Error using :: <int>
  619. ;; Submitted by: Gerardo Horvilleur <mago>
  620. (define bug14697-result "")
  621. (let ((GS.261 :: <int> 10)
  622. (GS.262 :: <int> 1))
  623. (do ((i :: <int> 1 (+ i GS.262)))
  624. ((> i GS.261))
  625. (set! bug14697-result (string-append bug14697-result " "
  626. (number->string i)))))
  627. (test " 1 2 3 4 5 6 7 8 9 10" 'bug14697 bug14697-result)
  628. (require 'xml)
  629. (test "<code xmlns=\"http://www.w3.org/1999/xhtml\">Foo</code>" 'html-contructor-1
  630. (as-xml (html:code "Foo")))
  631. (test "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"foo.html\">Foo</a>" 'html-contructor-2
  632. (as-xml (html:a href:"foo.html" "Foo")))
  633. (define-xml-namespace h "HTML")
  634. (test "<h:code xmlns:h=\"HTML\">Foo</h:code>" 'html-contructor-3
  635. (as-xml (h:code "Foo")))
  636. (test "<b xmlns=\"http://www.w3.org/1999/xhtml\"><code>Foo</code></b>" 'html-contructor-4
  637. (as-xml (html:b (html:code "Foo"))))
  638. (test "<code xmlns=\"http://www.w3.org/1999/xhtml\">Foo</code>" 'html-contructor-1lit
  639. (as-xml #<html:code>Foo</html:code>))
  640. (test "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"foo.html\">Foo</a>" 'html-contructor-2lit
  641. (as-xml #<html:a ['href]="&["foo"].&(string-append "ht" "ml")">Foo</>))
  642. ;; old syntax
  643. (test "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"foo.html\">Foo</a>" 'html-contructor-2lit
  644. (as-xml #<html:a ['href]="&["foo"].&(string-append "ht" "ml")">Foo</>))
  645. (define-xml-namespace h "HTML")
  646. (test "<h:code xmlns:h=\"HTML\">Foo</h:code>" 'html-contructor-3lit
  647. (as-xml #<h:code>Foo</>))
  648. (test "<b xmlns=\"http://www.w3.org/1999/xhtml\"><code>Foo</code></b>" 'html-contructor-4lit
  649. (as-xml #<html:b><html:code>Foo</></>))
  650. (test "<b xmlns=\"http://www.w3.org/1999/xhtml\"><code>FooBar</code></b>" 'html-contructor-4enc
  651. (let ((body1 "Foo")
  652. (body2 "Bar")
  653. (code 'html:code))
  654. (as-xml #<[(quote html:b)]><[code]>&[body1]&(car (list body2))</></>)))
  655. (test "<list><b xmlns=\"http://www.w3.org/1999/xhtml\">bold 1</b> <b xmlns=\"http://www.w3.org/1999/xhtml\">bold2</b></list>" 'html-contructor-5 (as-xml (map html:b '("bold 1" "bold2"))))
  656. ;; Test for Savannah bug #18909 "Recursive call to function in closure causes
  657. ;; NullPointerException". Chris Wegrzyn <chris.wegrzyn@gmail.com>
  658. (define (savannah-18909-outerproc foo)
  659. (define (innerproc)
  660. (if foo
  661. (lambda () (innerproc))
  662. '()))
  663. (innerproc))
  664. (define savannah-18909-destroy ((savannah-18909-outerproc #t)))
  665. (test savannah-18909-destroy 'savannah-18909 (savannah-18909-destroy))
  666. ;; Bug reported by Yaroslav Kavenchuk <kavenchuk@jenty.by> 2008-02-26:
  667. (define primes (<integer[]> 2 3 5 7 11 13))
  668. (test 11 'primes-integer-indexing (primes 4))
  669. (define sum 0)
  670. (define (test-exit-with-finally-1 x)
  671. (call-with-current-continuation
  672. (lambda (exit)
  673. (try-finally
  674. (if (< x 0)
  675. (exit (list x))
  676. (* 2 x))
  677. (set! sum (+ sum 1))))))
  678. (test '(8 10 (-9) (-1) 24) 'test-exit-with-finally-1
  679. (map test-exit-with-finally-1 '(4 5 -9 -1 12)))
  680. (set! sum 0)
  681. (define list-inner '())
  682. (define (test-exit-with-finally-2 x)
  683. (call-with-current-continuation
  684. (lambda (exit1)
  685. (try-finally
  686. (call-with-current-continuation
  687. (lambda (exit2)
  688. (try-finally
  689. (begin
  690. (if (< x 0)
  691. (exit2 (list 2 x)))
  692. (if (odd? x)
  693. (exit1 (list 1 x)))
  694. (set! list-inner (cons x list-inner))
  695. (list 0 x))
  696. (set! sum (+ sum 1)) #| Inner finally |#)))
  697. (set! sum (+ sum 10)) #| Outer finally |#))))
  698. (test '((0 4) (1 5) (2 -9) (0 14) (2 -1) (0 12) (1 7))
  699. 'test-exit-with-finally-2
  700. (map test-exit-with-finally-2 '(4 5 -9 14 -1 12 7)))
  701. (test "Sum: 77 Inner: (12 14 4)"
  702. 'test-exit-with-finally-2-results
  703. (format #f "Sum: ~s Inner: ~s" sum list-inner))
  704. ;; R6RS and SRFI-62 S-expression comments
  705. (test 5 'srfi-62-test-1 (+ 1 #;(* 2 3) 4))
  706. (test '(x z) 'srfi-62-test-2 (list 'x #;'y 'z))
  707. (test 12 'srfi-62-test-3 (* 3 4 #;(+ 1 2)))
  708. (test 16 'srfi-62-test-4 (#;sqrt abs -16))
  709. (test '(a d) 'srfi-62-test-5 (list 'a #; #;'b 'c 'd))
  710. (test '(a e) 'srfi-62-test-6 (list 'a #;(list 'b #;c 'd) 'e))
  711. (test '(a . c) 'srfi-62-test-7 '(a . #;b c))
  712. (test '(a . b) 'srfi-62-test-8 '(a . b #;c))
  713. ;; Savannah bug #26940 "Compiler stuck in endless loop"
  714. ;; Reported by Helmut Eller
  715. (define (mutual-tailcalls x)
  716. ;; Note that the order of the functions is reversed relative to the
  717. ;; Savannah bug report, because I recently fixed the implementation of
  718. ;; the letrec macro, which used to create declarations in reverse order.
  719. (letrec ((f0 (lambda () (if (= x 0) 1 (f1))))
  720. (f1 (lambda () (if (= x 0) (f0) (f2))))
  721. (f2 (lambda () (if (= x 0) (f1) 0))))
  722. (f2)))
  723. (test 0 mutual-tailcalls 4)
  724. ;; Savannah bug #24249 "Local define miscompiled"
  725. (let ()
  726. ;; Added 'list' to suppress tail-call-optimization.
  727. (define (baz) (list (bar)))
  728. (define (bar)
  729. (let ((k (lambda () #f)))
  730. (cond ((not (procedure? k))
  731. (error 'bad-k k)))
  732. k))
  733. (define (foo) (bar))
  734. (test "#<procedure k>" 'test-savannah-24249 ((foo):toString)))
  735. (require <InliningTest>)
  736. (test 16 inline-two-calls 5)
  737. (test 7 inline-two-calls -5)
  738. (test #f check-even 200001)
  739. (test #t check-even 18)
  740. (test #f check-even-unspec-return 23)
  741. ;; Savannah bug #27011: ArrayIndexOutOfBoundsException after 20 local variables
  742. (define (big-let) ; no argument!
  743. (define x0 "a")
  744. (define x1 "a")
  745. (define x2 "a")
  746. (define x3 "a")
  747. (define x4 "a")
  748. (define x5 "a")
  749. (define x6 "a")
  750. (define x7 "a")
  751. (define x8 "a")
  752. (define x9 "a")
  753. (define x10 "a")
  754. (define x11 "a")
  755. (define x12 "a")
  756. (define x13 "a")
  757. (define x14 "a")
  758. (define x15 "a")
  759. (define x16 "a")
  760. (define x17 "a")
  761. (define x18 "a")
  762. (define x19 "a")
  763. (define x20 "a")
  764. (set! x0 x1 )
  765. (set! x1 x2 )
  766. (set! x2 x3 )
  767. (set! x3 x4 )
  768. (set! x4 x5 )
  769. (set! x5 x6 )
  770. (set! x6 x7 )
  771. (set! x7 x8 )
  772. (set! x8 x9 )
  773. (set! x9 x10)
  774. (set! x10 x11)
  775. (set! x11 x12)
  776. (set! x12 x13)
  777. (set! x13 x14)
  778. (set! x14 x15)
  779. (set! x15 x16)
  780. (set! x16 x17)
  781. (set! x17 x18)
  782. (set! x18 x19)
  783. (set! x19 x20)
  784. (set! x20 x0 )
  785. x0)
  786. (test "a" big-let)
  787. ;; Savannah bug #27019 "setLength method of StringBuilder not found"
  788. (define sb (java.lang.StringBuilder "abcdef"))
  789. (define (set-length (builder :: java.lang.StringBuilder) (len :: int))
  790. (with-compile-options warn-invoke-unknown-method: #t
  791. warn-as-error: #t
  792. (builder:setLength len)))
  793. (set-length sb 4)
  794. (test "abcd" 'test-savannah-27019 (sb:toString))
  795. ;; Savannah bug #27188 "Sequence printing"
  796. (define sublist-27188 (invoke #(10 11 12 13 14 15) 'subList 1 3))
  797. (test "#(11 12)" 'test-savannah-27188 (format #f "~s" sublist-27188))
  798. (require "test-cycle12.scm")
  799. (test '(8 12) c1x-c2x)
  800. ;; Savannah bug #27257 "non-int dim. spec. in emitNewArray"
  801. (define (alloc-array count val)
  802. (object[] length: (+ 1 count) 1: val 2: (+ 1 val)))
  803. (test "[#!null 10 11 #!null #!null]" 'test-savannah-27257
  804. (format #f "~s" (alloc-array 4 10)))
  805. (define shared-1 '(#2=(3 4) 9 #2# #2#))
  806. (test '((3 4) 9 (3 4) (3 4)) 'shared-1 shared-1)
  807. (test 25 'multiple-do-with-type-specs
  808. (let ((ll '(1 3 5 7 9)))
  809. (do ((x :: list ll (cdr x))
  810. (sum :: int 0 (+ sum (car x))))
  811. ((null? x) sum))))
  812. ;; Savannah bug #28957: exception in inliner, from Helmut Eller
  813. (test "Type java.lang.CharSequence[]" 'test-savannah-28957
  814. (((string[]):getClass):toString))
  815. ;; Savannah bug #28926: EOL conversion in READ
  816. (test "(#\\X #\\return #\\Y #\\return #\\newline #\\Z)" 'test-savannah-28926
  817. (format "~w" (let ((in (string #\" #\X #\return #\Y #\return #\newline #\Z #\")))
  818. (string->list (call-with-input-string in read)))))
  819. ;; Savannah bug #31250: try/catch & endless loop
  820. (define (test-savannah-31250 f)
  821. (try-catch
  822. (let loop ()
  823. (f)
  824. (loop))
  825. (e java.lang.Exception
  826. (e:printStackTrace))))
  827. ;; Savannah bug #32656: ArrayIndexOutOfBoundsException in mergeLocalType
  828. (test 2 'savannah-32656
  829. (letrec ((f (lambda (x)
  830. (case x
  831. ((0) (f x))
  832. ((1) (g x))
  833. ((2) (h x)))))
  834. (g (lambda (x)
  835. (case x
  836. ((0) (f x))
  837. ((1) (g x))
  838. ((2) (h x)))))
  839. (h (lambda (x)
  840. (case x
  841. ((0) (f x))
  842. ((1) (g x))
  843. ((2) ;(h x)
  844. x)))))
  845. (f 2)))
  846. ;; Savannah bug #32657: Verification error with JDK7
  847. (begin
  848. (define (foo-savannah-32657) ()
  849. (let ((x (bar-savannah-32657))
  850. (fail (lambda () (error "fail"))))
  851. (if (instance? x <pair>)
  852. (let ((y :: <pair> x))
  853. (let ((z (y:getCar)))
  854. (if (eq? (y:getCdr) '())
  855. z
  856. (fail))))
  857. (fail))))
  858. (define (bar-savannah-32657) ::<list>
  859. (list 1))
  860. (test 1 'savannah-32657 (foo-savannah-32657)))
  861. ;; Testcase simplified from slime/config/swank-kawa.scm
  862. (define-syntax mif
  863. (syntax-rules ()
  864. ((mif ((p . ps) value) then)
  865. (let ((fail (lambda () (error "mlet failed")))
  866. (tmp value))
  867. (if (instance? tmp <pair>)
  868. (let* ((tmp :: <pair> tmp))
  869. then)
  870. (fail))))))
  871. (define (dispatch-events)
  872. (let ((tmp0 '(a b c)))
  873. (mif ((c . event) tmp0)
  874. 1234)))
  875. (test 1234 dispatch-events)
  876. ;; Savavvah bug #36592 "nested map causes compiler inliner NPE"
  877. (test '(1 2 3) 'savannah-36592
  878. (map (lambda (x) x) (map (lambda (x) x) '(1 2 3))))
  879. (define falseBool1 (java.lang.Boolean #f))
  880. (define falseBool2 (java.lang.Boolean #f))
  881. (test #f 'eq1-falseBool (eq? falseBool1 falseBool2))
  882. (test #f 'eq2-falseBool (apply eq? falseBool1 falseBool2 '()))
  883. (test #t 'eqv1-falseBool (eqv? falseBool1 falseBool2))
  884. (test #t 'eqv2-falseBool (apply eqv? falseBool1 falseBool2 '()))
  885. (test #t 'eqv3-falseBool (eqv? falseBool1 #f))
  886. (test #t 'equal1-falseBool (equal? falseBool1 falseBool2))
  887. (test #t 'equal2-falseBool (apply equal? falseBool1 falseBool2 '()))
  888. ;; # is a terminating macro character in Scheme.
  889. (test '(a b) 'adjacent-sharp-comment '(a#|com|#b))
  890. ;; Savannah bug report #39944 "Possible bug with omitted keyword arguments"
  891. (define (f-39944 #!key (y -1) (z -2)) z)
  892. (test 42 'savannah-39944 (f-39944 z: 42))
  893. (let ()
  894. (! [[a b] [c d] e] '((3 4) (5 6) (7 9)))
  895. (test "a:3 b:4 c:5 d:6 e:(7 9)"
  896. format #f "a:~w b:~w c:~w d:~w e:~w" a b c d e)
  897. (! [xs ...] [6 5 4])
  898. (test 15 'sum-each (+ xs ...))
  899. ;;(! [[as bs] ...] [[11 12] [21 22] [31 32]])
  900. ;;(test "xx" 'list-each (list bs ... as ... (+ as ...)))
  901. )
  902. (! iarr1 (int[] 3 4 5 6))
  903. (! [a b c d] iarr1)
  904. (test '(11 7) list (+ c d) (+ a b))
  905. (test #(4 5 7 x 9 8 3) 'scan-1
  906. (let (([a r ... b c] (list 3 4 5 7 8 9))) (vector r ... 'x c b a)))
  907. (let ()
  908. (define (f1 a b @rst) (format #f "a:~w b:~w r:~w" a b rst))
  909. (test "a:1 b:2 r:[7 8]" f1 1 2 7 8)
  910. (define (f2 a #!key k1 k2 @rst)
  911. (format #f "a:~w k1:~w k2:~w r:~w" a k1 k2 rst))
  912. (test "a:12 k1:#f k2:#f r:[7 8]" f2 12 7 8)
  913. (test "a:12 k1:#f k2:99 r:[7 8]" 'f2 (f2 12 k2: 99 7 8))
  914. (define (f3 a #!key k1 k2 #!rest rst)
  915. (format #f "a:~w k1:~w k2:~w r:~w" a k1 k2 rst))
  916. (test "a:12 k1:#f k2:#f r:(7 8)" f3 12 7 8)
  917. (test "a:12 k1:#f k2:99 r:(7 8)" 'f3
  918. (f3 12 k2: 99 7 8))
  919. (define (f4 a #!rest rst #!key k1 k2)
  920. (format #f "a:~w k1:~w k2:~w r:~w" a k1 k2 rst))
  921. (test "a:12 k1:#f k2:#f r:(7 8)" f4 12 7 8)
  922. (test "a:12 k1:#f k2:99 r:(k2: 99 7 8)" 'f4
  923. (f4 12 k2: 99 7 8))
  924. )