r6rs-enums.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ;;; r6rs-enums.test --- Test suite for R6RS (rnrs enums)
  2. ;; Copyright (C) 2010 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. (define-module (test-suite test-rnrs-enums)
  18. :use-module ((rnrs conditions) :version (6))
  19. :use-module ((rnrs enums) :version (6))
  20. :use-module ((rnrs exceptions) :version (6))
  21. :use-module (test-suite lib))
  22. (define-enumeration foo-enumeration (foo bar baz) make-foo-set)
  23. (with-test-prefix "enum-set-universe"
  24. (pass-if "universe of an enumeration is itself"
  25. (let ((et (make-enumeration '(a b c))))
  26. (eq? (enum-set-universe et) et)))
  27. (pass-if "enum-set-universe returns universe"
  28. (let* ((et (make-enumeration '(a b c)))
  29. (es ((enum-set-constructor et) '(a b))))
  30. (eq? (enum-set-universe es) et))))
  31. (with-test-prefix "enum-set-indexer"
  32. (pass-if "indexer returns index of symbol in universe"
  33. (let* ((universe (make-enumeration '(a b c)))
  34. (set ((enum-set-constructor universe) '(a c)))
  35. (indexer (enum-set-indexer set)))
  36. (and (eqv? (indexer 'a) 0) (eqv? (indexer 'c) 2))))
  37. (pass-if "indexer returns index of symbol in universe but not set"
  38. (let* ((universe (make-enumeration '(a b c)))
  39. (set ((enum-set-constructor universe) '(a c)))
  40. (indexer (enum-set-indexer set)))
  41. (eqv? (indexer 'b) 1)))
  42. (pass-if "indexer returns #f for symbol not in universe"
  43. (let* ((universe (make-enumeration '(a b c)))
  44. (set ((enum-set-constructor universe) '(a b c)))
  45. (indexer (enum-set-indexer set)))
  46. (eqv? (indexer 'd) #f))))
  47. (with-test-prefix "enum-set->list"
  48. (pass-if "enum-set->list returns members in universe order"
  49. (let* ((universe (make-enumeration '(a b c d e)))
  50. (set ((enum-set-constructor universe) '(d a e c))))
  51. (equal? (enum-set->list set) '(a c d e)))))
  52. (with-test-prefix "enum-set-member?"
  53. (pass-if "enum-set-member? is #t for set members"
  54. (let* ((universe (make-enumeration '(a b c)))
  55. (set ((enum-set-constructor universe) '(a b c))))
  56. (enum-set-member? 'a set)))
  57. (pass-if "enum-set-member? is #f for set non-members"
  58. (let* ((universe (make-enumeration '(a b c)))
  59. (set ((enum-set-constructor universe) '(a b c))))
  60. (not (enum-set-member? 'd set))))
  61. (pass-if "enum-set-member? is #f for universe but not set members"
  62. (let* ((universe (make-enumeration '(a b c d)))
  63. (set ((enum-set-constructor universe) '(a b c))))
  64. (not (enum-set-member? 'd set)))))
  65. (with-test-prefix "enum-set-subset?"
  66. (pass-if "enum-set-subset? is #t when set1 subset of set2"
  67. (let* ((universe (make-enumeration '(a b c d e)))
  68. (set1 ((enum-set-constructor universe) '(a b c)))
  69. (set2 ((enum-set-constructor universe) '(a b c d))))
  70. (enum-set-subset? set1 set2)))
  71. (pass-if "enum-set-subset? is #t when universe and set are subsets"
  72. (let* ((universe1 (make-enumeration '(a b c d)))
  73. (universe2 (make-enumeration '(a b c d e)))
  74. (set1 ((enum-set-constructor universe1) '(a b c)))
  75. (set2 ((enum-set-constructor universe2) '(a b c d))))
  76. (enum-set-subset? set1 set2)))
  77. (pass-if "enum-set-subset? is #f when set not subset"
  78. (let* ((universe (make-enumeration '(a b c d e)))
  79. (set1 ((enum-set-constructor universe) '(a b c d)))
  80. (set2 ((enum-set-constructor universe) '(a b c))))
  81. (not (enum-set-subset? set1 set2))))
  82. (pass-if "enum-set-subset? is #f when universe not subset"
  83. (let* ((universe1 (make-enumeration '(a b c d e)))
  84. (universe2 (make-enumeration '(a b c d)))
  85. (set1 ((enum-set-constructor universe1) '(a b c)))
  86. (set2 ((enum-set-constructor universe2) '(a b c d))))
  87. (not (enum-set-subset? set1 set2)))))
  88. (with-test-prefix "enum-set=?"
  89. (pass-if "enum-set=? is #t when sets are equal"
  90. (let* ((universe1 (make-enumeration '(a b c)))
  91. (universe2 (make-enumeration '(a b c)))
  92. (set1 ((enum-set-constructor universe1) '(a b c)))
  93. (set2 ((enum-set-constructor universe2) '(a b c))))
  94. (enum-set=? set1 set2)))
  95. (pass-if "enum-set=? is #f when sets are not equal"
  96. (let* ((universe (make-enumeration '(a b c d)))
  97. (set1 ((enum-set-constructor universe) '(a b)))
  98. (set2 ((enum-set-constructor universe) '(c d))))
  99. (not (enum-set=? set1 set2))))
  100. (pass-if "enum-set=? is #f when universes are not equal"
  101. (let* ((universe1 (make-enumeration '(a b c d)))
  102. (universe2 (make-enumeration '(a b c d e)))
  103. (set1 ((enum-set-constructor universe1) '(a b c d)))
  104. (set2 ((enum-set-constructor universe2) '(a b c d))))
  105. (not (enum-set=? set1 set2)))))
  106. (with-test-prefix "enum-set-union"
  107. (pass-if "&assertion raised on different universes"
  108. (guard (condition ((assertion-violation? condition) #t))
  109. (let* ((universe1 (make-enumeration '(a b c)))
  110. (universe2 (make-enumeration '(d e f)))
  111. (set1 ((enum-set-constructor universe1) '(a b c)))
  112. (set2 ((enum-set-constructor universe2) '(d e f))))
  113. (enum-set-union set1 set2)
  114. #f)))
  115. (pass-if "enum-set-union creates union on overlapping sets"
  116. (let* ((universe (make-enumeration '(a b c d e)))
  117. (set1 ((enum-set-constructor universe) '(a b c)))
  118. (set2 ((enum-set-constructor universe) '(c d e)))
  119. (union (enum-set-union set1 set2)))
  120. (equal? (enum-set->list union) '(a b c d e))))
  121. (pass-if "enum-set-union creates union on disjoint sets"
  122. (let* ((universe (make-enumeration '(a b c d e f)))
  123. (set1 ((enum-set-constructor universe) '(a b c)))
  124. (set2 ((enum-set-constructor universe) '(d e f)))
  125. (union (enum-set-union set1 set2)))
  126. (equal? (enum-set->list union) '(a b c d e f))))
  127. (pass-if "enum-set-union operates on syntactically-generated sets"
  128. (let* ((set1 (make-foo-set foo))
  129. (set2 (make-foo-set bar))
  130. (union (enum-set-union set1 set2)))
  131. (equal? (enum-set->list union) '(foo bar)))))
  132. (with-test-prefix "enum-set-intersection"
  133. (pass-if "&assertion raised on different universes"
  134. (guard (condition ((assertion-violation? condition) #t))
  135. (let* ((universe1 (make-enumeration '(a b c)))
  136. (universe2 (make-enumeration '(d e f)))
  137. (set1 ((enum-set-constructor universe1) '(a b c)))
  138. (set2 ((enum-set-constructor universe2) '(d e f))))
  139. (enum-set-intersection set1 set2)
  140. #f)))
  141. (pass-if "enum-set-intersection on overlapping sets"
  142. (let* ((universe (make-enumeration '(a b c d e)))
  143. (set1 ((enum-set-constructor universe) '(a b c)))
  144. (set2 ((enum-set-constructor universe) '(c d e)))
  145. (intersection (enum-set-intersection set1 set2)))
  146. (equal? (enum-set->list intersection) '(c))))
  147. (pass-if "enum-set-intersection on disjoint sets"
  148. (let* ((universe (make-enumeration '(a b c d e f)))
  149. (set1 ((enum-set-constructor universe) '(a b c)))
  150. (set2 ((enum-set-constructor universe) '(d e f)))
  151. (intersection (enum-set-intersection set1 set2)))
  152. (null? (enum-set->list intersection))))
  153. (pass-if "enum-set-intersection on syntactically-generated sets"
  154. (let* ((set1 (make-foo-set foo bar))
  155. (set2 (make-foo-set bar baz))
  156. (intersection (enum-set-intersection set1 set2)))
  157. (equal? (enum-set->list intersection) '(bar)))))
  158. (with-test-prefix "enum-set-difference"
  159. (pass-if "&assertion raised on different universes"
  160. (guard (condition ((assertion-violation? condition) #t))
  161. (let* ((universe1 (make-enumeration '(a b c)))
  162. (universe2 (make-enumeration '(d e f)))
  163. (set1 ((enum-set-constructor universe1) '(a b c)))
  164. (set2 ((enum-set-constructor universe2) '(d e f))))
  165. (enum-set-difference set1 set2)
  166. #f)))
  167. (pass-if "enum-set-difference with subset"
  168. (let* ((universe (make-enumeration '(a b c)))
  169. (set1 ((enum-set-constructor universe) '(a b c)))
  170. (set2 ((enum-set-constructor universe) '(a)))
  171. (difference (enum-set-difference set1 set2)))
  172. (equal? (enum-set->list difference) '(b c))))
  173. (pass-if "enum-set-difference with superset is empty"
  174. (let* ((universe (make-enumeration '(a b c d)))
  175. (set1 ((enum-set-constructor universe) '(a b c)))
  176. (set2 ((enum-set-constructor universe) '(a b c d)))
  177. (difference (enum-set-difference set1 set2)))
  178. (null? (enum-set->list difference))))
  179. (pass-if "enum-set-difference on syntactically-generated sets"
  180. (let* ((set1 (make-foo-set foo bar baz))
  181. (set2 (make-foo-set foo baz))
  182. (difference (enum-set-difference set1 set2)))
  183. (equal? (enum-set->list difference) '(bar)))))
  184. (with-test-prefix "enum-set-complement"
  185. (pass-if "complement of empty set is universe"
  186. (let* ((universe (make-enumeration '(a b c)))
  187. (set ((enum-set-constructor universe) '()))
  188. (complement (enum-set-complement set)))
  189. (equal? (enum-set->list complement) (enum-set->list universe))))
  190. (pass-if "simple complement"
  191. (let* ((universe (make-enumeration '(a b c d)))
  192. (set ((enum-set-constructor universe) '(a c)))
  193. (complement (enum-set-complement set)))
  194. (equal? (enum-set->list complement) '(b d)))))
  195. (with-test-prefix "enum-set-projection"
  196. (pass-if "projection onto subset universe"
  197. (let* ((universe1 (make-enumeration '(a b c d)))
  198. (universe2 (make-enumeration '(a b c)))
  199. (set1 ((enum-set-constructor universe1) '(a d)))
  200. (set2 ((enum-set-constructor universe2) '(b c)))
  201. (projection (enum-set-projection set1 set2)))
  202. (equal? (enum-set->list projection) '(a))))
  203. (pass-if "projection onto superset universe"
  204. (let* ((universe1 (make-enumeration '(a b c)))
  205. (universe2 (make-enumeration '(a b c d)))
  206. (set1 ((enum-set-constructor universe1) '(a c)))
  207. (set2 ((enum-set-constructor universe2) '(b d)))
  208. (projection (enum-set-projection set1 set2)))
  209. (equal? (enum-set->list projection) '(a c))))
  210. (pass-if "projection onto disjoint universe"
  211. (let* ((universe1 (make-enumeration '(a b c)))
  212. (universe2 (make-enumeration '(d e f)))
  213. (set1 ((enum-set-constructor universe1) '(a c)))
  214. (set2 ((enum-set-constructor universe2) '(d f)))
  215. (projection (enum-set-projection set1 set2)))
  216. (equal? (enum-set->list projection) '()))))
  217. (with-test-prefix "define-enumeration"
  218. (pass-if "define-enumeration creates bindings"
  219. (and (defined? 'foo-enumeration) (defined? 'make-foo-set)))
  220. (pass-if "type-name syntax raises &syntax on non-member"
  221. (guard (condition ((syntax-violation? condition) #t))
  222. (begin (eval '(foo-enumeration a) (current-module)) #f)))
  223. (pass-if "type-name evaluates to quote on member"
  224. (guard (condition ((syntax-violation? condition) #f))
  225. (eq? (eval '(foo-enumeration foo) (current-module)) 'foo)))
  226. (pass-if "constructor-syntax raises &syntax on non-members"
  227. (guard (condition ((syntax-violation? condition) #t))
  228. (begin (eval '(make-foo-set foo bar not-baz) (current-module)) #f)))
  229. (pass-if "constructor-syntax evaluates to new set"
  230. (guard (condition ((syntax-violation? condition) #f))
  231. (equal? (enum-set->list (eval '(make-foo-set foo bar)
  232. (current-module)))
  233. '(foo bar)))))