summaryrefslogtreecommitdiff
path: root/test-suite/tests/r6rs-enums.test
blob: 4b7d1d72a34a783acfe4aa5999101869fb42ae68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
;;; r6rs-enums.test --- Test suite for R6RS (rnrs enums)

;;      Copyright (C) 2010 Free Software Foundation, Inc.
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;; 
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;; 
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


(define-module (test-suite test-rnrs-enums)
  :use-module ((rnrs conditions) :version (6))
  :use-module ((rnrs enums) :version (6))
  :use-module ((rnrs exceptions) :version (6))
  :use-module (test-suite lib))

(define-enumeration foo-enumeration (foo bar baz) make-foo-set)

(with-test-prefix "enum-set-universe"
  (pass-if "universe of an enumeration is itself"
    (let ((et (make-enumeration '(a b c))))
      (eq? (enum-set-universe et) et)))

  (pass-if "enum-set-universe returns universe"
    (let* ((et (make-enumeration '(a b c)))
	   (es ((enum-set-constructor et) '(a b))))
      (eq? (enum-set-universe es) et))))

(with-test-prefix "enum-set-indexer"
  (pass-if "indexer returns index of symbol in universe"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '(a c)))
	   (indexer (enum-set-indexer set)))
      (and (eqv? (indexer 'a) 0) (eqv? (indexer 'c) 2))))

  (pass-if "indexer returns index of symbol in universe but not set"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '(a c)))
	   (indexer (enum-set-indexer set)))
      (eqv? (indexer 'b) 1)))

  (pass-if "indexer returns #f for symbol not in universe"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '(a b c)))
	   (indexer (enum-set-indexer set)))
      (eqv? (indexer 'd) #f))))

(with-test-prefix "enum-set->list"
  (pass-if "enum-set->list returns members in universe order"
    (let* ((universe (make-enumeration '(a b c d e)))
	   (set ((enum-set-constructor universe) '(d a e c))))
      (equal? (enum-set->list set) '(a c d e)))))

(with-test-prefix "enum-set-member?"
  (pass-if "enum-set-member? is #t for set members"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '(a b c))))
      (enum-set-member? 'a set)))

  (pass-if "enum-set-member? is #f for set non-members"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '(a b c))))
      (not (enum-set-member? 'd set))))

  (pass-if "enum-set-member? is #f for universe but not set members"
    (let* ((universe (make-enumeration '(a b c d)))
	   (set ((enum-set-constructor universe) '(a b c))))
      (not (enum-set-member? 'd set)))))

(with-test-prefix "enum-set-subset?"
  (pass-if "enum-set-subset? is #t when set1 subset of set2"
    (let* ((universe (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(a b c d))))
      (enum-set-subset? set1 set2)))

  (pass-if "enum-set-subset? is #t when universe and set are subsets"
    (let* ((universe1 (make-enumeration '(a b c d)))
	   (universe2 (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe1) '(a b c)))
	   (set2 ((enum-set-constructor universe2) '(a b c d))))
      (enum-set-subset? set1 set2)))

  (pass-if "enum-set-subset? is #f when set not subset"
    (let* ((universe (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe) '(a b c d)))
	   (set2 ((enum-set-constructor universe) '(a b c))))
      (not (enum-set-subset? set1 set2))))

  (pass-if "enum-set-subset? is #f when universe not subset"
    (let* ((universe1 (make-enumeration '(a b c d e)))
	   (universe2 (make-enumeration '(a b c d)))
	   (set1 ((enum-set-constructor universe1) '(a b c)))
	   (set2 ((enum-set-constructor universe2) '(a b c d))))
      (not (enum-set-subset? set1 set2)))))

(with-test-prefix "enum-set=?"
  (pass-if "enum-set=? is #t when sets are equal"
    (let* ((universe1 (make-enumeration '(a b c)))
	   (universe2 (make-enumeration '(a b c)))
	   (set1 ((enum-set-constructor universe1) '(a b c)))
	   (set2 ((enum-set-constructor universe2) '(a b c))))
      (enum-set=? set1 set2)))

  (pass-if "enum-set=? is #f when sets are not equal"
    (let* ((universe (make-enumeration '(a b c d)))
	   (set1 ((enum-set-constructor universe) '(a b)))
	   (set2 ((enum-set-constructor universe) '(c d))))
      (not (enum-set=? set1 set2))))

  (pass-if "enum-set=? is #f when universes are not equal"
    (let* ((universe1 (make-enumeration '(a b c d)))
	   (universe2 (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe1) '(a b c d)))
	   (set2 ((enum-set-constructor universe2) '(a b c d))))
      (not (enum-set=? set1 set2)))))

(with-test-prefix "enum-set-union"
  (pass-if "&assertion raised on different universes"
    (guard (condition ((assertion-violation? condition) #t))
	   (let* ((universe1 (make-enumeration '(a b c)))
		  (universe2 (make-enumeration '(d e f)))
		  (set1 ((enum-set-constructor universe1) '(a b c)))
		  (set2 ((enum-set-constructor universe2) '(d e f))))
	     (enum-set-union set1 set2)
	     #f)))

  (pass-if "enum-set-union creates union on overlapping sets"
    (let* ((universe (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(c d e)))
	   (union (enum-set-union set1 set2)))
      (equal? (enum-set->list union) '(a b c d e))))

  (pass-if "enum-set-union creates union on disjoint sets"
    (let* ((universe (make-enumeration '(a b c d e f)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(d e f)))
	   (union (enum-set-union set1 set2)))
      (equal? (enum-set->list union) '(a b c d e f))))

  (pass-if "enum-set-union operates on syntactically-generated sets"
    (let* ((set1 (make-foo-set foo))
	   (set2 (make-foo-set bar))
	   (union (enum-set-union set1 set2)))
      (equal? (enum-set->list union) '(foo bar)))))

(with-test-prefix "enum-set-intersection"
  (pass-if "&assertion raised on different universes"
    (guard (condition ((assertion-violation? condition) #t))
	   (let* ((universe1 (make-enumeration '(a b c)))
		  (universe2 (make-enumeration '(d e f)))
		  (set1 ((enum-set-constructor universe1) '(a b c)))
		  (set2 ((enum-set-constructor universe2) '(d e f))))
	     (enum-set-intersection set1 set2)
	     #f)))

  (pass-if "enum-set-intersection on overlapping sets"
    (let* ((universe (make-enumeration '(a b c d e)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(c d e)))
	   (intersection (enum-set-intersection set1 set2)))
      (equal? (enum-set->list intersection) '(c))))

  (pass-if "enum-set-intersection on disjoint sets"
    (let* ((universe (make-enumeration '(a b c d e f)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(d e f)))
	   (intersection (enum-set-intersection set1 set2)))
      (null? (enum-set->list intersection))))

  (pass-if "enum-set-intersection on syntactically-generated sets"
    (let* ((set1 (make-foo-set foo bar))
	   (set2 (make-foo-set bar baz))
	   (intersection (enum-set-intersection set1 set2)))
      (equal? (enum-set->list intersection) '(bar)))))

(with-test-prefix "enum-set-difference"
  (pass-if "&assertion raised on different universes"
    (guard (condition ((assertion-violation? condition) #t))
	   (let* ((universe1 (make-enumeration '(a b c)))
		  (universe2 (make-enumeration '(d e f)))
		  (set1 ((enum-set-constructor universe1) '(a b c)))
		  (set2 ((enum-set-constructor universe2) '(d e f))))
	     (enum-set-difference set1 set2)
	     #f)))

  (pass-if "enum-set-difference with subset"
    (let* ((universe (make-enumeration '(a b c)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(a)))
	   (difference (enum-set-difference set1 set2)))
      (equal? (enum-set->list difference) '(b c))))

  (pass-if "enum-set-difference with superset is empty"
    (let* ((universe (make-enumeration '(a b c d)))
	   (set1 ((enum-set-constructor universe) '(a b c)))
	   (set2 ((enum-set-constructor universe) '(a b c d)))
	   (difference (enum-set-difference set1 set2)))
      (null? (enum-set->list difference))))

  (pass-if "enum-set-difference on syntactically-generated sets"
    (let* ((set1 (make-foo-set foo bar baz))
	   (set2 (make-foo-set foo baz))
	   (difference (enum-set-difference set1 set2)))
      (equal? (enum-set->list difference) '(bar)))))

(with-test-prefix "enum-set-complement"
  (pass-if "complement of empty set is universe"
    (let* ((universe (make-enumeration '(a b c)))
	   (set ((enum-set-constructor universe) '()))
	   (complement (enum-set-complement set)))
      (equal? (enum-set->list complement) (enum-set->list universe))))

  (pass-if "simple complement"
    (let* ((universe (make-enumeration '(a b c d)))
	   (set ((enum-set-constructor universe) '(a c)))
	   (complement (enum-set-complement set)))
      (equal? (enum-set->list complement) '(b d)))))

(with-test-prefix "enum-set-projection"
  (pass-if "projection onto subset universe"
    (let* ((universe1 (make-enumeration '(a b c d)))
	   (universe2 (make-enumeration '(a b c)))
	   (set1 ((enum-set-constructor universe1) '(a d)))
	   (set2 ((enum-set-constructor universe2) '(b c)))
	   (projection (enum-set-projection set1 set2)))
      (equal? (enum-set->list projection) '(a))))

  (pass-if "projection onto superset universe"
    (let* ((universe1 (make-enumeration '(a b c)))
	   (universe2 (make-enumeration '(a b c d)))
	   (set1 ((enum-set-constructor universe1) '(a c)))
	   (set2 ((enum-set-constructor universe2) '(b d)))
	   (projection (enum-set-projection set1 set2)))
      (equal? (enum-set->list projection) '(a c))))

  (pass-if "projection onto disjoint universe"
    (let* ((universe1 (make-enumeration '(a b c)))
	   (universe2 (make-enumeration '(d e f)))
	   (set1 ((enum-set-constructor universe1) '(a c)))
	   (set2 ((enum-set-constructor universe2) '(d f)))
	   (projection (enum-set-projection set1 set2)))
      (equal? (enum-set->list projection) '()))))

(with-test-prefix "define-enumeration"
  (pass-if "define-enumeration creates bindings"
    (and (defined? 'foo-enumeration) (defined? 'make-foo-set)))

  (pass-if "type-name syntax raises &syntax on non-member"
    (guard (condition ((syntax-violation? condition) #t))
	   (begin (eval '(foo-enumeration a) (current-module)) #f)))

  (pass-if "type-name evaluates to quote on member"
    (guard (condition ((syntax-violation? condition) #f))
	   (eq? (eval '(foo-enumeration foo) (current-module)) 'foo)))

  (pass-if "constructor-syntax raises &syntax on non-members"
    (guard (condition ((syntax-violation? condition) #t))
	   (begin (eval '(make-foo-set foo bar not-baz) (current-module)) #f)))

  (pass-if "constructor-syntax evaluates to new set"
    (guard (condition ((syntax-violation? condition) #f))
	   (equal? (enum-set->list (eval '(make-foo-set foo bar) 
					 (current-module))) 
		   '(foo bar)))))