r6rs-records-procedural.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ;;; r6rs-records-procedural.test --- Test suite for R6RS
  2. ;;; (rnrs records procedural)
  3. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  4. ;;
  5. ;; This library is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU Lesser General Public
  7. ;; License as published by the Free Software Foundation; either
  8. ;; version 3 of the License, or (at your option) any later version.
  9. ;;
  10. ;; This library is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; Lesser General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU Lesser General Public
  16. ;; License along with this library; if not, write to the Free Software
  17. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite test-rnrs-records-procedural)
  19. :use-module ((rnrs conditions) :version (6))
  20. :use-module ((rnrs exceptions) :version (6))
  21. :use-module ((rnrs records procedural) :version (6))
  22. :use-module (test-suite lib))
  23. (define :point (make-record-type-descriptor
  24. 'point #f #f #f #f '#((mutable x) (mutable y))))
  25. (define :point-cd (make-record-constructor-descriptor :point #f #f))
  26. (define :voxel (make-record-type-descriptor
  27. 'voxel :point #f #f #f '#((mutable z))))
  28. (define :voxel-cd (make-record-constructor-descriptor :voxel :point-cd #f))
  29. (with-test-prefix "make-record-type-descriptor"
  30. (pass-if "simple"
  31. (let* ((:point-cd (make-record-constructor-descriptor :point #f #f))
  32. (make-point (record-constructor :point-cd))
  33. (point? (record-predicate :point))
  34. (point-x (record-accessor :point 0))
  35. (point-y (record-accessor :point 1))
  36. (point-x-set! (record-mutator :point 0))
  37. (point-y-set! (record-mutator :point 1))
  38. (p1 (make-point 1 2)))
  39. (point? p1)
  40. (eqv? (point-x p1) 1)
  41. (eqv? (point-y p1) 2)
  42. (unspecified? (point-x-set! p1 5))
  43. (eqv? (point-x p1) 5)))
  44. (pass-if "sealed records cannot be subtyped"
  45. (let* ((:sealed-point (make-record-type-descriptor
  46. 'sealed-point #f #f #t #f '#((mutable x)
  47. (mutable y))))
  48. (success #f))
  49. (call/cc
  50. (lambda (continuation)
  51. (with-exception-handler
  52. (lambda (condition)
  53. ;; FIXME: While R6RS specifies an assertion violation, by
  54. ;; building on core Guile records we just see a Guile
  55. ;; condition, which is just &serious.
  56. (set! success (serious-condition? condition))
  57. (continuation))
  58. (lambda () (make-record-type-descriptor
  59. 'sealed-point-subtype :sealed-point #f #f #f
  60. '#((mutable z)))))))
  61. success))
  62. (pass-if "non-generative records with same uid are eq"
  63. (let* ((:rtd-1 (make-record-type-descriptor
  64. 'rtd1 #f 'my-uid #f #f '#((mutable foo) (immutable bar))))
  65. (:rtd-2 (make-record-type-descriptor
  66. 'rtd1 #f 'my-uid #f #f '#((mutable foo) (immutable bar)))))
  67. (eq? :rtd-1 :rtd-2)))
  68. (pass-if "&assertion raised on conflicting non-generative types"
  69. (let* ((:rtd-1 (make-record-type-descriptor
  70. 'rtd1 #f 'my-uid-2 #f #f '#((mutable foo) (immutable bar))))
  71. (success 0)
  72. (check-definition
  73. (lambda (thunk)
  74. (call/cc
  75. (lambda (continuation)
  76. (with-exception-handler
  77. (lambda (condition)
  78. ;; FIXME: While R6RS specifies an assertion
  79. ;; violation, by building on core Guile records we
  80. ;; just see a Guile condition, which is just
  81. ;; &serious.
  82. (if (serious-condition? condition)
  83. (set! success (+ success 1)))
  84. (continuation))
  85. thunk))))))
  86. (check-definition
  87. (lambda ()
  88. (make-record-type-descriptor
  89. 'rtd1a #f 'my-uid-2 #f #f '#((mutable foo) (immutable bar)))))
  90. (check-definition
  91. (lambda ()
  92. (make-record-type-descriptor
  93. 'rtd1 :point 'my-uid-2 #f #f '#((mutable foo) (immutable bar)))))
  94. (check-definition
  95. (lambda ()
  96. (make-record-type-descriptor
  97. 'rtd1 #f 'my-uid-2 #t #f '#((mutable foo) (immutable bar)))))
  98. (check-definition
  99. (lambda ()
  100. (make-record-type-descriptor
  101. 'rtd1 #f 'my-uid-2 #f #t '#((mutable foo) (immutable bar)))))
  102. (check-definition
  103. (lambda () (make-record-type-descriptor 'rtd1 #f 'my-uid-2 #f #f '#())))
  104. (check-definition
  105. (lambda ()
  106. (make-record-type-descriptor
  107. 'rtd1 #f 'my-uid-2 #f #f '#((mutable foo) (immutable baz)))))
  108. (check-definition
  109. (lambda ()
  110. (make-record-type-descriptor
  111. 'rtd1 #f 'my-uid-2 #f #f '#((immutable foo) (immutable bar)))))
  112. (eqv? success 7))))
  113. (with-test-prefix "make-record-constructor-descriptor"
  114. (pass-if "simple protocol"
  115. (let* ((:point-protocol (lambda (p) (lambda (x y) (p (+ x 1) (+ y 1)))))
  116. (:point-protocol-cd (make-record-constructor-descriptor
  117. :point #f :point-protocol))
  118. (make-point (record-constructor :point-protocol-cd))
  119. (point-x (record-accessor :point 0))
  120. (point-y (record-accessor :point 1))
  121. (point (make-point 1 2)))
  122. (and (eqv? (point-x point) 2)
  123. (eqv? (point-y point) 3))))
  124. (pass-if "protocol delegates to parent with protocol"
  125. (let* ((:point-protocol (lambda (p) (lambda (x y) (p (+ x 1) (+ y 1)))))
  126. (:point-protocol-cd (make-record-constructor-descriptor
  127. :point #f :point-protocol))
  128. (:voxel-protocol (lambda (n)
  129. (lambda (x y z)
  130. (let ((p (n x y))) (p (+ z 100))))))
  131. (:voxel-protocol-cd (make-record-constructor-descriptor
  132. :voxel :point-protocol-cd :voxel-protocol))
  133. (make-voxel (record-constructor :voxel-protocol-cd))
  134. (point-x (record-accessor :point 0))
  135. (point-y (record-accessor :point 1))
  136. (voxel-z (record-accessor :voxel 0))
  137. (voxel (make-voxel 1 2 3)))
  138. (and (eqv? (point-x voxel) 2)
  139. (eqv? (point-y voxel) 3)
  140. (eqv? (voxel-z voxel) 103)))))
  141. (with-test-prefix "record-type-descriptor?"
  142. (pass-if "simple"
  143. (record-type-descriptor?
  144. (make-record-type-descriptor 'test #f #f #f #f '#()))))
  145. (with-test-prefix "record-constructor"
  146. (pass-if "simple"
  147. (let* ((make-point (record-constructor :point-cd))
  148. (point? (record-predicate :point))
  149. (point-x (record-accessor :point 0))
  150. (point-y (record-accessor :point 1))
  151. (point (make-point 1 2)))
  152. (and (point? point)
  153. (eqv? (point-x point) 1)
  154. (eqv? (point-y point) 2))))
  155. (pass-if "construct record subtype"
  156. (let* ((make-voxel (record-constructor :voxel-cd))
  157. (voxel? (record-predicate :voxel))
  158. (voxel-z (record-accessor :voxel 0))
  159. (voxel (make-voxel 1 2 3)))
  160. (and (voxel? voxel)
  161. (eqv? (voxel-z voxel) 3)))))
  162. (with-test-prefix "record-predicate"
  163. (pass-if "simple"
  164. (let* ((make-point (record-constructor :point-cd))
  165. (point (make-point 1 2))
  166. (point? (record-predicate :point)))
  167. (point? point)))
  168. (pass-if "predicate returns true on subtype"
  169. (let* ((make-voxel (record-constructor :voxel-cd))
  170. (voxel (make-voxel 1 2 3))
  171. (point? (record-predicate :point)))
  172. (point? voxel)))
  173. (pass-if "predicate returns false on supertype"
  174. (let* ((make-point (record-constructor :point-cd))
  175. (point (make-point 1 2))
  176. (voxel? (record-predicate :voxel)))
  177. (not (voxel? point)))))
  178. (with-test-prefix "record-accessor"
  179. (pass-if "simple"
  180. (let* ((make-point (record-constructor :point-cd))
  181. (point (make-point 1 2))
  182. (point-x (record-accessor :point 0))
  183. (point-y (record-accessor :point 1)))
  184. (and (eqv? (point-x point) 1)
  185. (eqv? (point-y point) 2))))
  186. (pass-if "accessor for supertype applied to subtype"
  187. (let* ((make-voxel (record-constructor :voxel-cd))
  188. (voxel (make-voxel 1 2 3))
  189. (point-x (record-accessor :point 0))
  190. (point-y (record-accessor :point 1)))
  191. (and (eqv? (point-x voxel) 1)
  192. (eqv? (point-y voxel) 2)))))
  193. (with-test-prefix "record-mutator"
  194. (pass-if "simple"
  195. (let* ((make-point (record-constructor :point-cd))
  196. (point (make-point 1 2))
  197. (point-set-x! (record-mutator :point 0))
  198. (point-set-y! (record-mutator :point 1))
  199. (point-x (record-accessor :point 0))
  200. (point-y (record-accessor :point 1)))
  201. (point-set-x! point 3)
  202. (point-set-y! point 4)
  203. (and (eqv? (point-x point) 3)
  204. (eqv? (point-y point) 4))))
  205. (pass-if "&assertion raised on request for immutable field"
  206. (let* ((:immutable-point (make-record-type-descriptor
  207. 'point #f #f #f #f '#((immutable x)
  208. (immutable y))))
  209. (success #f))
  210. (call/cc
  211. (lambda (continuation)
  212. (with-exception-handler
  213. (lambda (condition)
  214. (set! success (assertion-violation? condition))
  215. (continuation))
  216. (lambda () (record-mutator :immutable-point 0)))))
  217. success))
  218. (pass-if "mutator for supertype applied to subtype"
  219. (let* ((make-voxel (record-constructor :voxel-cd))
  220. (voxel (make-voxel 1 2 3))
  221. (point-set-x! (record-mutator :point 0))
  222. (point-set-y! (record-mutator :point 1))
  223. (point-x (record-accessor :point 0))
  224. (point-y (record-accessor :point 1)))
  225. (point-set-x! voxel 3)
  226. (point-set-y! voxel 4)
  227. (and (eqv? (point-x voxel) 3)
  228. (eqv? (point-y voxel) 4)))))