r5rs_pitfall.test 9.4 KB

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