r5rs_pitfall.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;;; r5rs_pitfall.test --- tests some pitfalls in R5RS -*- scheme -*-
  2. ;;;; Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;; These tests have been copied from
  18. ;; http://sisc.sourceforge.net/r5rs_pitfall.scm and the 'should-be'
  19. ;; macro has been modified to fit into our test suite machinery.
  20. (define-module (test-suite test-r5rs-pitfall)
  21. :use-syntax (ice-9 syncase)
  22. :use-module (test-suite lib))
  23. (define-syntax should-be
  24. (syntax-rules ()
  25. ((_ test-id value expression)
  26. (run-test test-id #t (lambda ()
  27. (false-if-exception
  28. (equal? expression value)))))))
  29. (define-syntax should-be-but-isnt
  30. (syntax-rules ()
  31. ((_ test-id value expression)
  32. (run-test test-id #f (lambda ()
  33. (false-if-exception
  34. (equal? expression value)))))))
  35. (define call/cc call-with-current-continuation)
  36. ;; Section 1: Proper letrec implementation
  37. ;;Credits to Al Petrofsky
  38. ;; In thread:
  39. ;; defines in letrec body
  40. ;; http://groups.google.com/groups?selm=87bsoq0wfk.fsf%40app.dial.idiom.com
  41. (should-be 1.1 0
  42. (let ((cont #f))
  43. (letrec ((x (call-with-current-continuation (lambda (c) (set! cont c) 0)))
  44. (y (call-with-current-continuation (lambda (c) (set! cont c) 0))))
  45. (if cont
  46. (let ((c cont))
  47. (set! cont #f)
  48. (set! x 1)
  49. (set! y 1)
  50. (c 0))
  51. (+ x y)))))
  52. ;;Credits to Al Petrofsky
  53. ;; In thread:
  54. ;; Widespread bug (arguably) in letrec when an initializer returns twice
  55. ;; http://groups.google.com/groups?selm=87d793aacz.fsf_-_%40app.dial.idiom.com
  56. (should-be 1.2 #t
  57. (letrec ((x (call/cc list)) (y (call/cc list)))
  58. (cond ((procedure? x) (x (pair? y)))
  59. ((procedure? y) (y (pair? x))))
  60. (let ((x (car x)) (y (car y)))
  61. (and (call/cc x) (call/cc y) (call/cc x)))))
  62. ;;Credits to Alan Bawden
  63. ;; In thread:
  64. ;; LETREC + CALL/CC = SET! even in a limited setting
  65. ;; http://groups.google.com/groups?selm=19890302162742.4.ALAN%40PIGPEN.AI.MIT.EDU
  66. (should-be 1.3 #t
  67. (letrec ((x (call-with-current-continuation
  68. (lambda (c)
  69. (list #T c)))))
  70. (if (car x)
  71. ((cadr x) (list #F (lambda () x)))
  72. (eq? x ((cadr x))))))
  73. ;; Section 2: Proper call/cc and procedure application
  74. ;;Credits to Al Petrofsky, (and a wink to Matthias Blume)
  75. ;; In thread:
  76. ;; Widespread bug in handling (call/cc (lambda (c) (0 (c 1)))) => 1
  77. ;; http://groups.google.com/groups?selm=87g00y4b6l.fsf%40radish.petrofsky.org
  78. (should-be 2.1 1
  79. (call/cc (lambda (c) (0 (c 1)))))
  80. ;; Section 3: Hygienic macros
  81. ;; Eli Barzilay
  82. ;; In thread:
  83. ;; R5RS macros...
  84. ;; http://groups.google.com/groups?selm=skitsdqjq3.fsf%40tulare.cs.cornell.edu
  85. (should-be 3.1 4
  86. (let-syntax ((foo
  87. (syntax-rules ()
  88. ((_ expr) (+ expr 1)))))
  89. (let ((+ *))
  90. (foo 3))))
  91. ;; Al Petrofsky again
  92. ;; In thread:
  93. ;; Buggy use of begin in r5rs cond and case macros.
  94. ;; http://groups.google.com/groups?selm=87bse3bznr.fsf%40radish.petrofsky.org
  95. (should-be 3.2 2
  96. (let-syntax ((foo (syntax-rules ()
  97. ((_ var) (define var 1)))))
  98. (let ((x 2))
  99. (begin (define foo +))
  100. (cond (else (foo x)))
  101. x)))
  102. ;;Al Petrofsky
  103. ;; In thread:
  104. ;; An Advanced syntax-rules Primer for the Mildly Insane
  105. ;; http://groups.google.com/groups?selm=87it8db0um.fsf@radish.petrofsky.org
  106. (should-be 3.3 1
  107. (let ((x 1))
  108. (let-syntax
  109. ((foo (syntax-rules ()
  110. ((_ y) (let-syntax
  111. ((bar (syntax-rules ()
  112. ((_) (let ((x 2)) y)))))
  113. (bar))))))
  114. (foo x))))
  115. ;; Al Petrofsky
  116. ;; Contributed directly
  117. (should-be 3.4 1
  118. (let-syntax ((x (syntax-rules ()))) 1))
  119. ;; Setion 4: No identifiers are reserved
  120. ;;(Brian M. Moore)
  121. ;; In thread:
  122. ;; shadowing syntatic keywords, bug in MIT Scheme?
  123. ;; http://groups.google.com/groups?selm=6e6n88%248qf%241%40news.cc.ukans.edu
  124. (should-be 4.1 '(x)
  125. ((lambda lambda lambda) 'x))
  126. (should-be 4.2 '(1 2 3)
  127. ((lambda (begin) (begin 1 2 3)) (lambda lambda lambda)))
  128. (should-be 4.3 #f
  129. (let ((quote -)) (eqv? '1 1)))
  130. ;; Section 5: #f/() distinctness
  131. ;; Scott Miller
  132. (should-be 5.1 #f
  133. (eq? #f '()))
  134. (should-be 5.2 #f
  135. (eqv? #f '()))
  136. (should-be 5.3 #f
  137. (equal? #f '()))
  138. ;; Section 6: string->symbol case sensitivity
  139. ;; Jens Axel S?gaard
  140. ;; In thread:
  141. ;; Symbols in DrScheme - bug?
  142. ;; http://groups.google.com/groups?selm=3be55b4f%240%24358%24edfadb0f%40dspool01.news.tele.dk
  143. (should-be 6.1 #f
  144. (eq? (string->symbol "f") (string->symbol "F")))
  145. ;; Section 7: First class continuations
  146. ;; Scott Miller
  147. ;; No newsgroup posting associated. The jist of this test and 7.2
  148. ;; is that once captured, a continuation should be unmodified by the
  149. ;; invocation of other continuations. This test determines that this is
  150. ;; the case by capturing a continuation and setting it aside in a temporary
  151. ;; variable while it invokes that and another continuation, trying to
  152. ;; side effect the first continuation. This test case was developed when
  153. ;; testing SISC 1.7's lazy CallFrame unzipping code.
  154. (define r #f)
  155. (define a #f)
  156. (define b #f)
  157. (define c #f)
  158. (define i 0)
  159. (should-be 7.1 28
  160. (let ()
  161. (set! r (+ 1 (+ 2 (+ 3 (call/cc (lambda (k) (set! a k) 4))))
  162. (+ 5 (+ 6 (call/cc (lambda (k) (set! b k) 7))))))
  163. (if (not c)
  164. (set! c a))
  165. (set! i (+ i 1))
  166. (case i
  167. ((1) (a 5))
  168. ((2) (b 8))
  169. ((3) (a 6))
  170. ((4) (c 4)))
  171. r))
  172. ;; Same test, but in reverse order
  173. (define r #f)
  174. (define a #f)
  175. (define b #f)
  176. (define c #f)
  177. (define i 0)
  178. (should-be 7.2 28
  179. (let ()
  180. (set! r (+ 1 (+ 2 (+ 3 (call/cc (lambda (k) (set! a k) 4))))
  181. (+ 5 (+ 6 (call/cc (lambda (k) (set! b k) 7))))))
  182. (if (not c)
  183. (set! c a))
  184. (set! i (+ i 1))
  185. (case i
  186. ((1) (b 8))
  187. ((2) (a 5))
  188. ((3) (b 7))
  189. ((4) (c 4)))
  190. r))
  191. ;; Credits to Matthias Radestock
  192. ;; Another test case used to test SISC's lazy CallFrame routines.
  193. (should-be 7.3 '((-1 4 5 3)
  194. (4 -1 5 3)
  195. (-1 5 4 3)
  196. (5 -1 4 3)
  197. (4 5 -1 3)
  198. (5 4 -1 3))
  199. (let ((k1 #f)
  200. (k2 #f)
  201. (k3 #f)
  202. (state 0))
  203. (define (identity x) x)
  204. (define (fn)
  205. ((identity (if (= state 0)
  206. (call/cc (lambda (k) (set! k1 k) +))
  207. +))
  208. (identity (if (= state 0)
  209. (call/cc (lambda (k) (set! k2 k) 1))
  210. 1))
  211. (identity (if (= state 0)
  212. (call/cc (lambda (k) (set! k3 k) 2))
  213. 2))))
  214. (define (check states)
  215. (set! state 0)
  216. (let* ((res '())
  217. (r (fn)))
  218. (set! res (cons r res))
  219. (if (null? states)
  220. res
  221. (begin (set! state (car states))
  222. (set! states (cdr states))
  223. (case state
  224. ((1) (k3 4))
  225. ((2) (k2 2))
  226. ((3) (k1 -)))))))
  227. (map check '((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1)))))
  228. ;; Modification of the yin-yang puzzle so that it terminates and produces
  229. ;; a value as a result. (Scott G. Miller)
  230. (should-be 7.4 '(10 9 8 7 6 5 4 3 2 1 0)
  231. (let ((x '())
  232. (y 0))
  233. (call/cc
  234. (lambda (escape)
  235. (let* ((yin ((lambda (foo)
  236. (set! x (cons y x))
  237. (if (= y 10)
  238. (escape x)
  239. (begin
  240. (set! y 0)
  241. foo)))
  242. (call/cc (lambda (bar) bar))))
  243. (yang ((lambda (foo)
  244. (set! y (+ y 1))
  245. foo)
  246. (call/cc (lambda (baz) baz)))))
  247. (yin yang))))))
  248. ;; Miscellaneous
  249. ;;Al Petrofsky
  250. ;; In thread:
  251. ;; R5RS Implementors Pitfalls
  252. ;; http://groups.google.com/groups?selm=871zemtmd4.fsf@app.dial.idiom.com
  253. (should-be 8.1 -1
  254. (let - ((n (- 1))) n))
  255. (should-be 8.2 '(1 2 3 4 1 2 3 4 5)
  256. (let ((ls (list 1 2 3 4)))
  257. (append ls ls '(5))))
  258. ;;Not really an error to fail this (Matthias Radestock)
  259. ;;If this returns (0 1 0), your map isn't call/cc safe, but is probably
  260. ;;tail-recursive. If its (0 0 0), the opposite is true.
  261. (should-be 8.3 '(0 1 0)
  262. (let ()
  263. (define executed-k #f)
  264. (define cont #f)
  265. (define res1 #f)
  266. (define res2 #f)
  267. (set! res1 (map (lambda (x)
  268. (if (= x 0)
  269. (call/cc (lambda (k) (set! cont k) 0))
  270. 0))
  271. '(1 0 2)))
  272. (if (not executed-k)
  273. (begin (set! executed-k #t)
  274. (set! res2 res1)
  275. (cont 1)))
  276. res2))