Commit | Line | Data |
---|---|---|
ce543a9f JG |
1 | ;;; r6rs-control.test --- Test suite for R6RS (rnrs control) |
2 | ||
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 | \f | |
19 | ||
20 | (define-module (test-suite test-rnrs-records-procedural) | |
21 | :use-module ((rnrs conditions) :version (6)) | |
22 | :use-module ((rnrs exceptions) :version (6)) | |
23 | :use-module ((rnrs records procedural) :version (6)) | |
24 | :use-module (test-suite lib)) | |
25 | ||
26 | (define :point (make-record-type-descriptor | |
27 | 'point #f #f #f #f '#((mutable x) (mutable y)))) | |
28 | (define :point-cd (make-record-constructor-descriptor :point #f #f)) | |
29 | ||
30 | (define :voxel (make-record-type-descriptor | |
31 | 'voxel :point #f #f #f '#((mutable z)))) | |
32 | (define :voxel-cd (make-record-constructor-descriptor :voxel :point-cd #f)) | |
33 | ||
34 | (with-test-prefix "make-record-type-descriptor" | |
35 | (pass-if "simple" | |
36 | (let* ((:point-cd (make-record-constructor-descriptor :point #f #f)) | |
37 | (make-point (record-constructor :point-cd)) | |
38 | (point? (record-predicate :point)) | |
39 | (point-x (record-accessor :point 0)) | |
40 | (point-y (record-accessor :point 1)) | |
41 | (point-x-set! (record-mutator :point 0)) | |
42 | (point-y-set! (record-mutator :point 1)) | |
43 | (p1 (make-point 1 2))) | |
44 | (point? p1) | |
45 | (eqv? (point-x p1) 1) | |
46 | (eqv? (point-y p1) 2) | |
47 | (unspecified? (point-x-set! p1 5)) | |
48 | (eqv? (point-x p1) 5))) | |
49 | ||
50 | (pass-if "sealed records cannot be subtyped" | |
51 | (let* ((:sealed-point (make-record-type-descriptor | |
52 | 'sealed-point #f #f #t #f '#((mutable x) | |
53 | (mutable y)))) | |
54 | (success #f)) | |
55 | (call/cc | |
56 | (lambda (continuation) | |
57 | (with-exception-handler | |
58 | (lambda (condition) | |
59 | (set! success (assertion-violation? condition)) | |
60 | (continuation)) | |
61 | (lambda () (make-record-type-descriptor | |
62 | 'sealed-point-subtype :sealed-point #f #f #f | |
63 | '#((mutable z))))))) | |
64 | success)) | |
65 | ||
66 | (pass-if "non-generative records with same uid are eq" | |
67 | (let* ((:rtd-1 (make-record-type-descriptor | |
68 | 'rtd1 #f 'my-uid #f #f '#((mutable foo) (immutable bar)))) | |
69 | (:rtd-2 (make-record-type-descriptor | |
70 | 'rtd1 #f 'my-uid #f #f '#((mutable foo) (immutable bar))))) | |
71 | (eq? :rtd-1 :rtd-2))) | |
72 | ||
73 | (pass-if "&assertion raised on conflicting non-generative types" | |
74 | (let* ((:rtd-1 (make-record-type-descriptor | |
75 | 'rtd1 #f 'my-uid-2 #f #f '#((mutable foo) (immutable bar)))) | |
76 | (success 0) | |
77 | (check-definition | |
78 | (lambda (thunk) | |
79 | (call/cc | |
80 | (lambda (continuation) | |
81 | (with-exception-handler | |
82 | (lambda (condition) | |
83 | (if (assertion-violation? condition) | |
84 | (set! success (+ success 1))) | |
85 | (continuation)) | |
86 | thunk)))))) | |
87 | (check-definition | |
88 | (lambda () | |
89 | (make-record-type-descriptor | |
90 | 'rtd1a #f 'my-uid-2 #f #f '#((mutable foo) (immutable bar))))) | |
91 | (check-definition | |
92 | (lambda () | |
93 | (make-record-type-descriptor | |
94 | 'rtd1 :point 'my-uid-2 #f #f '#((mutable foo) (immutable bar))))) | |
95 | (check-definition | |
96 | (lambda () | |
97 | (make-record-type-descriptor | |
98 | 'rtd1 #f 'my-uid-2 #t #f '#((mutable foo) (immutable bar))))) | |
99 | (check-definition | |
100 | (lambda () | |
101 | (make-record-type-descriptor | |
102 | 'rtd1 #f 'my-uid-2 #f #t '#((mutable foo) (immutable bar))))) | |
103 | (check-definition | |
104 | (lambda () (make-record-type-descriptor 'rtd1 #f 'my-uid-2 #f #f '#()))) | |
105 | (check-definition | |
106 | (lambda () | |
107 | (make-record-type-descriptor | |
108 | 'rtd1 #f 'my-uid-2 #f #f '#((mutable foo) (immutable baz))))) | |
109 | (check-definition | |
110 | (lambda () | |
111 | (make-record-type-descriptor | |
112 | 'rtd1 #f 'my-uid-2 #f #f '#((immutable foo) (immutable bar))))) | |
113 | (eqv? success 7)))) | |
114 | ||
115 | (with-test-prefix "record-type-descriptor?" | |
116 | (pass-if "simple" | |
117 | (record-type-descriptor? | |
118 | (make-record-type-descriptor 'test #f #f #f #f '#())))) | |
119 | ||
120 | (with-test-prefix "record-constructor" | |
121 | (pass-if "simple" | |
122 | (let* ((make-point (record-constructor :point-cd)) | |
123 | (point? (record-predicate :point)) | |
124 | (point-x (record-accessor :point 0)) | |
125 | (point-y (record-accessor :point 1)) | |
126 | (point (make-point 1 2))) | |
127 | (and (point? point) | |
128 | (eqv? (point-x point) 1) | |
129 | (eqv? (point-y point) 2)))) | |
130 | ||
131 | (pass-if "construct record subtype" | |
132 | (let* ((make-voxel (record-constructor :voxel-cd)) | |
133 | (voxel? (record-predicate :voxel)) | |
134 | (voxel-z (record-accessor :voxel 0)) | |
135 | (voxel (make-voxel 1 2 3))) | |
136 | (and (voxel? voxel) | |
137 | (eqv? (voxel-z voxel) 3))))) | |
138 | ||
139 | (with-test-prefix "record-predicate" | |
140 | (pass-if "simple" | |
141 | (let* ((make-point (record-constructor :point-cd)) | |
142 | (point (make-point 1 2)) | |
143 | (point? (record-predicate :point))) | |
144 | (point? point))) | |
145 | ||
146 | (pass-if "predicate returns true on subtype" | |
147 | (let* ((make-voxel (record-constructor :voxel-cd)) | |
148 | (voxel (make-voxel 1 2 3)) | |
149 | (point? (record-predicate :point))) | |
150 | (point? voxel))) | |
151 | ||
152 | (pass-if "predicate returns false on supertype" | |
153 | (let* ((make-point (record-constructor :point-cd)) | |
154 | (point (make-point 1 2)) | |
155 | (voxel? (record-predicate :voxel))) | |
156 | (not (voxel? point))))) | |
157 | ||
158 | (with-test-prefix "record-accessor" | |
159 | (pass-if "simple" | |
160 | (let* ((make-point (record-constructor :point-cd)) | |
161 | (point (make-point 1 2)) | |
162 | (point-x (record-accessor :point 0)) | |
163 | (point-y (record-accessor :point 1))) | |
164 | (and (eqv? (point-x point) 1) | |
165 | (eqv? (point-y point) 2)))) | |
166 | ||
167 | (pass-if "accessor for supertype applied to subtype" | |
168 | (let* ((make-voxel (record-constructor :voxel-cd)) | |
169 | (voxel (make-voxel 1 2 3)) | |
170 | (point-x (record-accessor :point 0)) | |
171 | (point-y (record-accessor :point 1))) | |
172 | (and (eqv? (point-x voxel) 1) | |
173 | (eqv? (point-y voxel) 2))))) | |
174 | ||
175 | (with-test-prefix "record-mutator" | |
176 | (pass-if "simple" | |
177 | (let* ((make-point (record-constructor :point-cd)) | |
178 | (point (make-point 1 2)) | |
179 | (point-set-x! (record-mutator :point 0)) | |
180 | (point-set-y! (record-mutator :point 1)) | |
181 | (point-x (record-accessor :point 0)) | |
182 | (point-y (record-accessor :point 1))) | |
183 | (point-set-x! point 3) | |
184 | (point-set-y! point 4) | |
185 | (and (eqv? (point-x point) 3) | |
186 | (eqv? (point-y point) 4)))) | |
187 | ||
188 | (pass-if "&assertion raised on request for immutable field" | |
189 | (let* ((:immutable-point (make-record-type-descriptor | |
190 | 'point #f #f #f #f '#((immutable x) | |
191 | (immutable y)))) | |
192 | (success #f)) | |
193 | (call/cc | |
194 | (lambda (continuation) | |
195 | (with-exception-handler | |
196 | (lambda (condition) | |
197 | (set! success (assertion-violation? condition)) | |
198 | (continuation)) | |
199 | (lambda () (record-mutator :immutable-point 0))))) | |
200 | success)) | |
201 | ||
202 | (pass-if "mutator for supertype applied to subtype" | |
203 | (let* ((make-voxel (record-constructor :voxel-cd)) | |
204 | (voxel (make-voxel 1 2 3)) | |
205 | (point-set-x! (record-mutator :point 0)) | |
206 | (point-set-y! (record-mutator :point 1)) | |
207 | (point-x (record-accessor :point 0)) | |
208 | (point-y (record-accessor :point 1))) | |
209 | (point-set-x! voxel 3) | |
210 | (point-set-y! voxel 4) | |
211 | (and (eqv? (point-x voxel) 3) | |
212 | (eqv? (point-y voxel) 4))))) | |
213 |