Debian Codegraph @

test/type.scm

raw
1 ;; Tests for typeutil
2
3 (use gauche.test)
4 (test-start "typeutil")
5
6 (use gauche.typeutil)
7 (test-module 'gauche.typeutil)
8
9 (test-section "type constuctor memoization")
10
11 ;; This tests the constructed types from the same arguments gets eq?,
12 ;; because of the memoization.
13
14 (define-syntax t-identity
15 (syntax-rules ()
16 [(_ expect a b)
17 (test* (list 'a 'b) expect (eq? a b))]))
18
19 (t-identity #t (<?> <integer>) (<?> <integer>))
20 (t-identity #t (</> <integer> <string>) (</> <integer> <string>))
21 (t-identity #f (<?> <integer>) (<?> <int>))
22 (t-identity #t (</> <uint8> <uint16>) (</> <uint8> <uint16>))
23 (t-identity #f (</> <uint8> <uint16>) (</> <uint16> <uint8>))
24 (t-identity #t (<Assortment> 'a 'b) (<Assortment> 'a 'b))
25
26 (test-section "subtype?")
27
28 (define-syntax t-subtype
29 (syntax-rules ()
30 [(_ sub sup expect)
31 (test* (list 'subtype? 'sub 'sup) expect (subtype? sub sup))]))
32
33 (t-subtype <fixnum> <fixnum> #t)
34 (t-subtype <fixnum> <integer> #t)
35 (t-subtype <fixnum> <real> #t)
36 (t-subtype <fixnum> <number> #t)
37 (t-subtype <fixnum> <top> #t)
38 (t-subtype <bottom> <fixnum> #t)
39 (t-subtype <fixnum> <boolean> #f)
40 (t-subtype <short> <integer> #t)
41 (t-subtype <ushort> <integer> #t)
42 (t-subtype <int> <integer> #t)
43 (t-subtype <uint> <integer> #t)
44 (t-subtype <long> <integer> #t)
45 (t-subtype <ulong> <integer> #t)
46 (t-subtype <int8> <integer> #t)
47 (t-subtype <uint8> <integer> #t)
48 (t-subtype <int16> <integer> #t)
49 (t-subtype <uint16> <integer> #t)
50 (t-subtype <int32> <integer> #t)
51 (t-subtype <uint32> <integer> #t)
52 (t-subtype <int64> <integer> #t)
53 (t-subtype <uint64> <integer> #t)
54 (t-subtype <float> <integer> #f)
55 (t-subtype <float> <real> #t)
56 (t-subtype <float> <number> #t)
57 (t-subtype <double> <integer> #f)
58 (t-subtype <double> <real> #t)
59 (t-subtype <double> <number> #t)
60 (t-subtype <const-cstring> <const-cstring> #t)
61 (t-subtype <const-cstring> <string> #t)
62 (t-subtype <const-cstring> <boolean> #f)
63
64
65 (t-subtype <integer> (</> <integer> <string>) #t)
66 (t-subtype <integer> (</>) #f)
67 (t-subtype <integer> (</> <char> <string>) #f)
68 (t-subtype <integer> (</> <number> <string>) #t)
69 (t-subtype (</> <integer> <string>) <top> #t)
70 (t-subtype (</> <integer> <string>) <integer> #f)
71 (t-subtype (</> <integer> <string>) (</> <string> <integer>) #t)
72 (t-subtype (</> <integer> <string>) (</> <string> <char> <integer>) #t)
73 (t-subtype (</> <integer> <string>) (</> <char> <integer>) #f)
74 (t-subtype (</> <integer> <string>) (<?> (</> <number> <string>)) #t)
75
76 (t-subtype <integer> (<?> <integer>) #t)
77 (t-subtype <boolean> (<?> <integer>) #f)
78 (t-subtype <integer> (<?> <real>) #t)
79 (t-subtype <real> (<?> <integer>) #f)
80 (t-subtype (<?> <integer>) (<?> <real>) #t)
81 (t-subtype (<?> <integer>) <integer> #f)
82 (t-subtype (<?> <integer>) (</> (<?> <number>) (<?> <string>)) #t)
83
84 (t-subtype (<Tuple> <integer> <string>) <list> #t)
85 (t-subtype (<Tuple> <integer> <string>) (<Tuple> <integer> <string>) #t)
86 (t-subtype (<Tuple> <integer> <string>) (<Tuple> <integer> <string> <char>) #f)
87 (t-subtype (<Tuple> <integer> <string>) (<Tuple> <real> <string>) #t)
88 (t-subtype (<Tuple> <integer> <integer>) (<List> <integer>) #t)
89 (t-subtype (<Tuple> <integer> <integer>) (<List> <integer> 2) #t)
90 (t-subtype (<Tuple> <integer> <integer>) (<List> <integer> 0 2) #t)
91 (t-subtype (<Tuple> <integer> <integer>) (<List> <integer> 0 1) #f)
92 (t-subtype (<Tuple> <integer> <string>) (<List> <integer>) #f)
93 (t-subtype (<Tuple> <char> <string> *) <list> #t)
94 (t-subtype (<Tuple> <char> <string> *) (<Tuple> <char>) #f)
95 (t-subtype (<Tuple> <char> <string> *) (<Tuple> <char> <string>) #t)
96 (t-subtype (<Tuple> <char> <string> *) (<Tuple> <char> <string> <char>) #t)
97 (t-subtype (<Tuple> *) <list> #t)
98 (t-subtype (<Tuple> *) (<Tuple> <integer> *) #t)
99 (t-subtype (<Tuple> <integer>) (<Tuple> <integer> *) #t)
100 (t-subtype (<Tuple> <integer>) (<Tuple> <integer> <integer> *) #f)
101 (t-subtype (<Tuple> <integer> *) (<Tuple> <integer> <integer> *) #t)
102
103 (t-subtype (<List> <integer>) <list> #t)
104 (t-subtype (<List> <integer>) (<List> <number>) #t)
105 (t-subtype (<List> <number>) (<List> <integer>) #f)
106 (t-subtype (<List> <integer> 2 3) (<List> <integer> 0 4) #t)
107 (t-subtype (<List> <integer> 0 3) (<List> <integer> 2 3) #f)
108 (t-subtype (<List> <integer> 2 4) (<List> <integer> 2 3) #f)
109 (t-subtype (<List> <integer> #f 3) (<List> <integer> 0 4) #t)
110 (t-subtype (<List> <integer> #f 3) (<List> <integer> 0) #t)
111 (t-subtype (<List> <integer> 0) (<List> <integer> 0 3) #f)
112 (t-subtype (<List> <integer>) (<?> (<List> <number>)) #t)
113 (t-subtype (<List> <integer>) (</> (<List> <string>) (<List> <number>)) #t)
114
115 (t-subtype (<Vector> <integer>) <vector> #t)
116 (t-subtype (<Vector> <integer>) (<Vector> <number>) #t)
117 (t-subtype (<Vector> <number>) (<Vector> <integer>) #f)
118 (t-subtype (<Vector> <integer> 2 3) (<Vector> <integer> 0 4) #t)
119 (t-subtype (<Vector> <integer> 0 3) (<Vector> <integer> 2 3) #f)
120 (t-subtype (<Vector> <integer> 2 4) (<Vector> <integer> 2 3) #f)
121 (t-subtype (<Vector> <integer> #f 3) (<Vector> <integer> 0 4) #t)
122 (t-subtype (<Vector> <integer> #f 3) (<Vector> <integer> 0) #t)
123 (t-subtype (<Vector> <integer> 0) (<Vector> <integer> 0 3) #f)
124 (t-subtype (<Vector> <integer>) (<?> (<Vector> <number>)) #t)
125 (t-subtype (<Vector> <integer>) (</> (<Vector> <string>) (<Vector> <number>)) #t)
126
127 (t-subtype (<Assortment> 'a) (<Assortment> 'a 'b 'c) #t)
128 (t-subtype (<Assortment> 1 2 3) (<Assortment> 1 2 3) #t)
129 (t-subtype (<Assortment> 1 2 3) (<Assortment> 1 2) #f)
130 (t-subtype (<Assortment> 1 3) (<Assortment> 1 2) #f)
131 (t-subtype (<Assortment> 1 3) <integer> #t)
132 (t-subtype (<Assortment> 1 'a) (</> <integer> <symbol>) #t)
133 (t-subtype (<Assortment> 1 'a) (</> <integer> <string>) #f)
134
135 (test-section "built-in type constructors")
136
137 (define (validation-test type alist)
138 (dolist [p alist]
139 (test* (format "~a ~s" (class-name type) (car p))
140 (cdr p)
141 (of-type? (car p) type))))
142
143 (validation-test (</> <string> <integer>)
144 '(("abc" . #t)
145 (123 . #t)
146 (abc . #f)
147 (#f . #f)
148 (#t . #f)
149 (("abc") . #f)))
150
151 (validation-test (<Tuple> <char> <integer> <symbol>)
152 '(((#\a 1 a) . #t)
153 ((#\a 1) . #f)
154 (() . #f)
155 ((1 #\a b) . #f)
156 ((#\a 1 b x) . #f)))
157
158 (validation-test (<?> <integer>)
159 '((3 . #t)
160 (#f . #t)
161 (#t . #f)
162 (3.5 . #f)))
163
164 (validation-test (<Tuple> (<?> <char>) (<?> <string>))
165 '((3 . #f)
166 ((#\a "a") . #t)
167 ((#f "a") . #t)
168 ((#\a . "a") . #f)
169 ((#\a "a" . z) . #f)
170 ((#\a #f) . #t)
171 ((#f #f) . #t)
172 ((#f) . #f)
173 ((#\a) . #f)
174 (("a") . #f)))
175
176 (validation-test (<Tuple> <integer> <real> *)
177 '(((2 2.3) . #t)
178 ((2 2.3 3) . #t)
179 ((2.2 3) . #f)
180 ((2 2.3 . 3) . #f)))
181
182 (validation-test (<^> * -> *)
183 `((,car . #t)
184 (,cons . #t)
185 (,list . #t)
186 (1 . #f)
187 ;;(#/abc/ . #t) ; applicable objects are not supported yet
188 ))
189
190 (validation-test (<^> <top> -> *)
191 `((,car . #t)
192 (,cons . #f)
193 (,list . #f)
194 (,cons* . #t)
195 (,current-input-port . #f)
196 (,(lambda () #f) . #f)))
197
198 (validation-test (<^> -> *)
199 `((,(lambda () #f) . #t)
200 (,car . #f)
201 (,list . #t)))
202
203 (validation-test (<^> <top> <top> -> *)
204 `((,cons . #t)
205 (,car . #f)))
206
207 (validation-test (<^> <top> <top> -> *)
208 `((,(case-lambda ((a) 1) ((a b) 2)) . #t)))
209
210 (validation-test (</> (<^> <top> -> *) (<^> <top> <top> -> *))
211 `((,(case-lambda ((a) 1) ((a b) 2)) . #t)))
212
213 (validation-test (<List> <integer>)
214 '((() . #t)
215 ((1) . #t)
216 ((1 2 3 4 5 6 7) . #t)
217 ((1 . 2) . #f)
218 ((1 2 a 3 4) . #f)
219 (1 . #f)))
220
221 (validation-test (<List> <integer> 3)
222 '((() . #f)
223 ((1) . #f)
224 ((1 2 3) . #t)
225 ((1 2 3 4) . #t)
226 ((1 2 3 4 5 6 7) . #t)
227 ((1 . 2) . #f)
228 ((1 2 a 3 4) . #f)
229 (1 . #f)))
230
231 (validation-test (<List> <integer> #f 3)
232 '((() . #t)
233 ((1) . #t)
234 ((1 2 3) . #t)
235 ((1 2 3 4) . #f)
236 ((1 2 3 4 5 6 7) . #f)
237 ((1 . 2) . #f)
238 ((1 2 a 3 4) . #f)
239 (1 . #f)))
240
241 (validation-test (<List> <integer> 3 3)
242 '((() . #f)
243 ((1) . #f)
244 ((1 2 3) . #t)
245 ((1 2 3 4) . #f)
246 ((1 2 3 4 5 6 7) . #f)
247 ((1 . 2) . #f)
248 ((1 2 a 3 4) . #f)
249 (1 . #f)))
250
251 (validation-test (<Vector> <integer>)
252 '((#() . #t)
253 (#(1) . #t)
254 (#(1 2 3 4 5 6 7) . #t)
255 (#(1 2 a 3 4) . #f)
256 ((1) . #f)))
257
258 (validation-test (<Vector> <integer> 3)
259 '((#() . #f)
260 (#(1) . #f)
261 (#(1 2 3) . #t)
262 (#(1 2 3 4) . #t)
263 (#(1 2 3 4 5 6 7) . #t)
264 (#(1 2 a 3 4) . #f)
265 ((1) . #f)))
266
267 (validation-test (<Vector> <integer> #f 3)
268 '((#() . #t)
269 (#(1) . #t)
270 (#(1 2 3) . #t)
271 (#(1 2 3 4) . #f)
272 (#(1 2 3 4 5 6 7) . #f)
273 (#(1 2 a 3 4) . #f)
274 (1 . #f)))
275
276 (validation-test (<Vector> <integer> 3 3)
277 '((#() . #f)
278 (#(1) . #f)
279 (#(1 2 3) . #t)
280 (#(1 2 3 4) . #f)
281 (#(1 2 3 4 5 6 7) . #f)
282 (#(1 2 a 3 4) . #f)
283 (1 . #f)))
284
285 (test-section "procedure types")
286
287 (define-syntax proctype-test
288 (syntax-rules ()
289 [(_ proc supposed-type)
290 (test* '(procedure-type proc) supposed-type
291 (procedure-type proc))]))
292
293
294 (proctype-test cons (<^> <top> <top> -> <pair>))
295 (proctype-test car (<^> <pair> -> <top>))
296 (proctype-test list (<^> * -> <list>))
297 (proctype-test set-cdr! (<^> <pair> <top> -> <void>))
298
299 ;; This tests gf's type is recomputed after method addition
300 (define-method a-gf ((x <number>)) x)
301
302 (proctype-test a-gf (</> (<^> <number> -> *)))
303
304 (define-method a-gf ((x <string>)) x)
305
306 (proctype-test a-gf (</> (<^> <string> -> *) (<^> <number> -> *)))
307
308 (test-section "typecase")
309
310 (define (t-typecase obj)
311 (typecase obj
312 [<integer> 'int]
313 [<string> 'str]
314 [(<?> <symbol>) 'maybe-symbol]
315 [(<List> (</> <string> <symbol>)) 'string-or-symbol-list]
316 [else 'other]))
317
318 (test* "typecase" '(int str maybe-symbol maybe-symbol
319 string-or-symbol-list other)
320 (map t-typecase
321 '(10 "abc" foo #f (a "b" c) (d 3 f))))
322
323 (define (t-etypecase obj)
324 (etypecase obj
325 [<string> 'str]
326 [<symbol> 'sym]))
327
328 (test* "etypecase" 'str (t-etypecase "abc"))
329 (test* "etypecase" 'sym (t-etypecase 'abc))
330 (test* "etypecase"
331 (test-error <error> #/expecting one of types in \(<string> <symbol>\)/)
332 (t-etypecase 1))
333
334 (test-end)