Fix bytevector-copy when applied to SRFI-4 homogeneous numeric vectors.
[bpt/guile.git] / test-suite / tests / r6rs-records-syntactic.test
1 ;;; -*- scheme -*-
2 ;;; r6rs-records-syntactic.test --- Test suite for R6RS (rnrs records syntactic)
3
4 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;;
6 ;; This library is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU Lesser General Public
8 ;; License as published by the Free Software Foundation; either
9 ;; version 3 of the License, or (at your option) any later version.
10 ;;
11 ;; This library is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; Lesser General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU Lesser General Public
17 ;; License along with this library; if not, write to the Free Software
18 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 \f
20
21 (define-module (test-suite test-rnrs-records-syntactic)
22 #:use-module ((rnrs records syntactic) #:version (6))
23 #:use-module ((rnrs records procedural) #:version (6))
24 #:use-module ((rnrs records inspection) #:version (6))
25 #:use-module ((rnrs conditions) #:version (6))
26 #:use-module ((rnrs exceptions) #:version (6))
27 #:use-module ((system base compile) #:select (compile))
28 #:use-module (test-suite lib))
29
30 (define-record-type simple-rtd)
31 (define-record-type
32 (specified-rtd specified-rtd-constructor specified-rtd-predicate))
33 (define-record-type parent-rtd (fields x y))
34 (define-record-type child-parent-rtd-rtd
35 (parent-rtd (record-type-descriptor parent-rtd)
36 (record-constructor-descriptor parent-rtd))
37 (fields z))
38 (define-record-type child-parent-rtd (parent parent-rtd) (fields z))
39 (define-record-type mutable-fields-rtd
40 (fields (mutable mutable-bar)
41 (mutable mutable-baz mutable-baz-accessor mutable-baz-mutator)))
42 (define-record-type immutable-fields-rtd
43 (fields immutable-foo
44 (immutable immutable-bar)
45 (immutable immutable-baz immutable-baz-accessor)))
46 (define-record-type protocol-rtd
47 (fields (immutable x) (immutable y))
48 (protocol (lambda (p) (lambda (x y) (p (+ x 1) (+ y 1))))))
49 (define-record-type sealed-rtd (sealed #t))
50 (define-record-type opaque-rtd (opaque #t))
51 (define-record-type nongenerative-rtd (nongenerative))
52 (define-record-type nongenerative-uid-rtd (nongenerative foo))
53
54 (with-test-prefix "simple record names"
55 (pass-if "define-record-type defines record type"
56 (defined? 'simple-rtd))
57
58 (pass-if "define-record-type defines record predicate"
59 (defined? 'simple-rtd?))
60
61 (pass-if "define-record-type defines record-constructor"
62 (defined? 'make-simple-rtd)))
63
64 (with-test-prefix "fully-specified record names"
65 (pass-if "define-record-type defines named predicate"
66 (defined? 'specified-rtd-predicate))
67
68 (pass-if "define-record-type defines named constructor"
69 (defined? 'specified-rtd-constructor)))
70
71 (pass-if "parent-rtd clause includes specified parent"
72 (eq? (record-type-parent child-parent-rtd-rtd) parent-rtd))
73
74 (pass-if "parent clause includes specified parent"
75 (eq? (record-type-parent child-parent-rtd) parent-rtd))
76
77 (pass-if "protocol clause includes specified protocol"
78 (let ((protocol-record (make-protocol-rtd 1 2)))
79 (and (eqv? (protocol-rtd-x protocol-record) 2)
80 (eqv? (protocol-rtd-y protocol-record) 3))))
81
82 (pass-if "sealed clause produces sealed type"
83 (record-type-sealed? sealed-rtd))
84
85 (pass-if "opaque clause produces opaque type"
86 (record-type-opaque? opaque-rtd))
87
88 (with-test-prefix "nongenerative"
89 (pass-if "nongenerative clause produces nongenerative type"
90 (not (record-type-generative? nongenerative-rtd)))
91
92 (pass-if "nongenerative clause preserves specified uid"
93 (and (not (record-type-generative? nongenerative-uid-rtd))
94 (eq? (record-type-uid nongenerative-uid-rtd) 'foo))))
95
96 (with-test-prefix "fields"
97 (pass-if "raw symbol produces accessor only"
98 (and (defined? 'immutable-fields-rtd-immutable-foo)
99 (not (defined? 'immutable-fields-rtd-immutable-foo-set!))))
100
101 (pass-if "(immutable x) form produces accessor only"
102 (and (defined? 'immutable-fields-rtd-immutable-bar)
103 (not (defined? 'immutable-fields-rtd-immutable-bar-set!))))
104
105 (pass-if "(immutable x y) form produces named accessor"
106 (defined? 'immutable-baz-accessor))
107
108 (pass-if "(mutable x) form produces accessor and mutator"
109 (and (defined? 'mutable-fields-rtd-mutable-bar)
110 (defined? 'mutable-fields-rtd-mutable-bar-set!)))
111
112 (pass-if "(mutable x y) form produces named accessor and mutator"
113 (and (defined? 'mutable-baz-accessor)
114 (defined? 'mutable-baz-mutator))))
115
116 (pass-if "record-type-descriptor returns rtd"
117 (eq? (record-type-descriptor simple-rtd) simple-rtd))
118
119 (pass-if "record-constructor-descriptor returns rcd"
120 (procedure? (record-constructor (record-constructor-descriptor simple-rtd))))
121
122 (with-test-prefix "record hygiene"
123 (pass-if-exception "using shadowed record keywords fails" exception:syntax-pattern-unmatched
124 (compile '(let ((fields #f))
125 (define-record-type foo (fields bar))
126 #t)
127 #:env (current-module)))
128 (pass-if "using shadowed record keywords fails 2"
129 (guard (condition ((syntax-violation? condition) #t))
130 (compile '(let ((immutable #f))
131 (define-record-type foo (fields (immutable bar)))
132 #t)
133 #:env (current-module))
134 #f))
135 (pass-if "hygiene preserved when using macros"
136 (compile '(begin
137 (define pass #t)
138 (define-syntax define-record
139 (syntax-rules ()
140 ((define-record name field)
141 (define-record-type name
142 (protocol
143 (lambda (x)
144 (lambda ()
145 ;; pass refers to pass in scope of macro not use
146 (x pass))))
147 (fields field)))))
148 (let ((pass #f))
149 (define-record foo bar)
150 (foo-bar (make-foo))))
151 #:env (current-module))))