blob: 392dac6b3bb9d264991c5f8fed67260e8bae8c5f (
plain) (
blame)
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
|
#| Copyright 2024 Peter McGoron
|
| Licensed under the Apache License, Version 2.0 (the "License");
|
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|#
(import (scheme base)
(mcgoron srfi 64)
(SAHP)
(srfi 64) (srfi 252))
(test-runner-factory factory)
(test-runner-current (test-runner-create))
(set-verbosity! '(fails group-stack))
(define iref (make-new-SAHP))
(set-global-SAHP! iref 'vector vector-ref)
(set-global-SAHP! iref 'bytevector bytevector-u8-ref)
(define (iref-works-with-vector v)
(let loop ((i 0))
(cond
((= i (vector-length v)) #t)
((= (vector-ref v i) (iref v i)) (loop (+ i 1)))
(else #f))))
(define (iref-works-with-bytevector v)
(let loop ((i 0))
(cond
((= i (bytevector-length v)) #t)
((= (bytevector-u8-ref v i) (iref v i)) (loop (+ i 1)))
(else #f))))
(define (override-vector-in-dynamic-scope procedure)
(lambda (v)
(define cntr 0)
(parameterize-SAHP ((iref ('vector
(lambda (v i)
(set! cntr (+ cntr 1))
(vector-ref v i)))))
(procedure v (lambda () cntr)))))
(test-group "iref"
(test-group "global scope"
(test-group "on vectors"
(test-property iref-works-with-vector
(list (vector-generator-of (exact-integer-generator)))))
(test-group "on bytevectors"
(test-property iref-works-with-bytevector
(list (bytevector-generator))))
(test-group "undefined raises error"
(test-assert
(guard (c
((SAHP-implementation-not-found-error? c) #t)
(else #f))
(iref '() 0)))))
(test-group "dynamic scope"
(test-group "override in dynamic scope"
(define (impl v cntr)
(test-assert "works" (iref-works-with-vector v))
(test-equal "counted" (vector-length v) (cntr)))
(test-property (override-vector-in-dynamic-scope impl)
(list (vector-generator-of (exact-integer-generator)))))
(test-group "override in dynamic scope does not affect other implementations"
(define (impl bc cntr)
(test-assert "works" (iref-works-with-bytevector bc))
(test-equal "did not count" 0 (cntr)))
(test-property (override-vector-in-dynamic-scope impl)
(list (bytevector-generator)))))
(test-group "local scope"
(test-group "override in local scope doesnt affect global scope"
(define (override v)
(define cntr 0)
(letrec-SAHP ((iref ('vector
(lambda (v i)
(set! cntr (+ cntr 1))
(vector-ref v i)))))
(and (iref-works-with-vector v)
(= cntr 0))))
(test-property override
(list (vector-generator-of
(exact-integer-generator)))))
(test-group "override in local scope affects lexical scope"
(define (override v)
(define cntr 0)
(letrec-SAHP ((iref ('vector
(lambda (v i)
(set! cntr (+ cntr 1))
(vector-ref v i)))))
(let loop ((i 0))
(cond
((= i (vector-length v)) (= cntr i))
((= (vector-ref v i) (iref v i)) (loop (+ i 1)))
(else #f)))))
(test-property override
(list (vector-generator-of
(exact-integer-generator)))))
(test-group "override in local scope overrides dynamic scope"
(define (override v)
(define cntr 0)
(letrec-SAHP ((iref ('vector
(lambda (v i)
(set! cntr (+ cntr 1))
(vector-ref v i)))))
(parameterize-SAHP ((iref ('vector vector-ref)))
(let loop ((i 0))
(cond
((= i (vector-length v)) (= cntr i))
((= (vector-ref v i) (iref v i)) (loop (+ i 1)))
(else #f))))))
(test-property override
(list (vector-generator-of
(exact-integer-generator)))))
(test-group "override in local scope only affects that type"
(define (override bv)
(define cntr 0)
(letrec-SAHP ((iref ('vector
(lambda (v i)
(set! cntr (+ cntr 1))
(vector-ref v i)))))
(let loop ((i 0))
(cond
((= i (bytevector-length bv)) (= cntr 0))
((= (bytevector-u8-ref bv i) (iref bv i)) (loop (+ i 1)))
(else #f)))))
(test-property override (list (bytevector-generator))))))
(test-group "subtyping"
(define sub (make-new-SAHP))
(define (test-for-all)
(test-assert "exact integer" (sub 0))
(test-assert "integer" (sub 1.0))
(test-assert "rational" (sub 1/2))
(test-assert "real" (sub +inf.0))
(test-assert "complex" (sub 1+2i))
(test-assert "not implemented"
(guard (c
(else
(SAHP-implementation-not-found-error? c)))
(sub "not a number")
#f)))
(define (test-for-some)
(test-assert "exact integer" (sub 0))
(test-assert "integer" (sub 1.0))
(test-assert "rational" (sub 1/2))
(test-assert "not for complex"
(guard (c
(else
(SAHP-implementation-not-found-error? c)))
(sub +inf.0))))
(test-group "local scope"
(test-group "subtypes flow downwards"
(letrec-SAHP ((sub ('number (lambda (x) #t))))
(test-assert "exact integer" (sub 0))
(test-assert "integer" (sub 1.0))
(test-assert "rational" (sub 1/2))
(test-assert "real" (sub +inf.0))
(test-assert "complex" (sub 1+2i))
(test-assert "not implemented"
(guard (c
(else
(SAHP-implementation-not-found-error? c)))
(sub "not a number")
#f))))
(test-group "subtypes do not flow upwards"
(letrec-SAHP ((sub ('rational (lambda (x) #t))))
(test-assert "exact integer" (sub 0))
(test-assert "integer" (sub 1.0))
(test-assert "rational" (sub 1/2))
(test-assert "not for complex"
(guard (c
(else
(SAHP-implementation-not-found-error? c)))
(sub +inf.0))))))
(test-group "dynamic scope"
(test-group "subtypes flow downwards"
(parameterize-SAHP ((sub ('number (lambda (x) #t))))
(test-for-all)))
(test-group "subtypes do not flow upwards"
(parameterize-SAHP ((sub ('rational (lambda (x) #t))))
(test-for-some)))
(test-group "local scope overrides dynamic scope subtyping"
(parameterize-SAHP ((sub ('number (lambda (x) 1))))
(letrec-SAHP ((sub ('rational (lambda (x) 2))))
(test-equal 2 (sub 0))
(test-equal 2 (sub 1/2))
(test-equal 1 (sub 1+2i)))))))
|